OSDN Git Service

- Correct issue with importing when not a premium member.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / InkImagesTable.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 \r
21 package cx.fbn.nevernote.sql;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 \r
26 import com.trolltech.qt.core.QByteArray;\r
27 \r
28 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
29 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
30 \r
31 public class InkImagesTable {\r
32         private final ApplicationLogger                 logger;\r
33         DatabaseConnection                                              db;\r
34 \r
35         // Constructor\r
36         public InkImagesTable(ApplicationLogger l,DatabaseConnection d) {\r
37                 logger = l;\r
38                 db = d;\r
39         }\r
40         // Create the table\r
41         public void createTable() {\r
42                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
43         // Create the NoteTag table\r
44         logger.log(logger.HIGH, "Creating table InkImage...");\r
45         if (!query.exec("Create table InkImages (guid varchar, " +\r
46                         "slice integer, primary key(guid, slice), image blob)"))\r
47                 logger.log(logger.HIGH, "Table InkImage creation FAILED!!!"); \r
48         }\r
49         // Drop the table\r
50         public void dropTable() {\r
51                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
52                 query.exec("drop table InkImages");\r
53         }\r
54         // Delete an image\r
55         public void expungeImage(String guid) {\r
56                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
57                 if (!query.prepare("Delete from InkImages where guid=:guid ")) {\r
58                         logger.log(logger.EXTREME, "InkImage SQL prepare has failed.");\r
59                         logger.log(logger.MEDIUM, query.lastError());\r
60                         return;\r
61                 }\r
62                 query.bindValue(":guid", guid);\r
63                 if (!query.exec()) {\r
64                         logger.log(logger.EXTREME, "InkImage SQL delete has failed.");\r
65                         logger.log(logger.MEDIUM, query.lastError());\r
66                         return;\r
67                 }\r
68                 return;         \r
69         }\r
70         // Get a note tags by the note's Guid\r
71         public List<QByteArray> getImage(String guid) {\r
72                 List<QByteArray> data = new ArrayList<QByteArray>();\r
73                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
74                 if (!query.prepare("Select image from InkImages where guid = :guid order by slice")) {\r
75                         logger.log(logger.EXTREME, "InkImage SQL prepare has failed.");\r
76                         logger.log(logger.MEDIUM, query.lastError());\r
77                         return null;\r
78                 }\r
79                 query.bindValue(":guid", guid);\r
80                 if (!query.exec()) {\r
81                         logger.log(logger.EXTREME, "InkImage SQL exec has failed.");\r
82                         logger.log(logger.MEDIUM, query.lastError());\r
83                         return null;\r
84                 }\r
85                 while (query.next()) {\r
86                         data.add(new QByteArray(query.getBlob(0)));     \r
87                 }       \r
88                 return data;\r
89         }\r
90         // Save an ink note image\r
91         public void saveImage(String guid, int slice, QByteArray data) {\r
92                 logger.log(logger.HIGH, "Entering inkImageTable.saveImage");\r
93                 boolean check;\r
94                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
95                 check = query.prepare("Insert Into InkImages ("\r
96                                 +"guid, slice, image) Values("\r
97                                 +":guid, :slice, :data)");\r
98                 if (!check) {\r
99                         logger.log(logger.EXTREME, "InkImages SQL insert prepare has failed.");\r
100                         logger.log(logger.MEDIUM, query.lastError());\r
101                         return;\r
102                 }\r
103                 query.bindValue(":guid", guid);\r
104                 query.bindValue(":slice", slice);\r
105                 query.bindBlob(":data", data.toByteArray());                                            \r
106                 check = query.exec();\r
107                 if (!check) {\r
108                         logger.log(logger.MEDIUM, "*** InkImages Table insert failed.");                \r
109                         logger.log(logger.MEDIUM, query.lastError());\r
110                 }                       \r
111                 logger.log(logger.HIGH, "Leaving InkImages.saveImage");\r
112         }\r
113 \r
114 }\r