OSDN Git Service

2b71bb48e4beed3c377d343e6b391dc73173370f
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NoteTagsTable.java
1 /*\r
2  * This file is part of NeverNote \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 cx.fbn.nevernote.sql.driver.NSqlQuery;\r
27 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
28 import cx.fbn.nevernote.utilities.Pair;\r
29 \r
30 public class NoteTagsTable {\r
31         private final ApplicationLogger                 logger;\r
32         DatabaseConnection                                              db;\r
33 \r
34         \r
35         // Constructor\r
36         public NoteTagsTable(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 NoteTags...");\r
45         if (!query.exec("Create table NoteTags (noteGuid varchar, " +\r
46                         "tagGuid varchar, primary key(noteGuid, tagGuid))"))\r
47                 logger.log(logger.HIGH, "Table NoteTags 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 NoteTags");\r
53         }\r
54         // Get a note tags by the note's Guid\r
55         public List<String> getNoteTags(String noteGuid) {\r
56                 if (noteGuid == null)\r
57                         return null;\r
58                 boolean check;\r
59                 List<String> tags = new ArrayList<String>();\r
60                 \r
61                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
62                 check = query.exec("Select "\r
63                                 +"TagGuid from NoteTags where noteGuid = '" +noteGuid +"'");\r
64                 if (!check) {\r
65                         logger.log(logger.EXTREME, "NoteTags SQL select has failed.");\r
66                         logger.log(logger.MEDIUM, query.lastError());\r
67                         return null;\r
68                 }\r
69                 while (query.next()) {\r
70                         tags.add(query.valueString(0));\r
71                 }       \r
72                 return tags;\r
73         }\r
74         // Get a note tags by the note's Guid\r
75         public List<NoteTagsRecord> getAllNoteTags() {\r
76                 List<NoteTagsRecord> tags = new ArrayList<NoteTagsRecord>();\r
77                 \r
78                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
79                 if (!query.exec("Select TagGuid, NoteGuid from NoteTags")) {\r
80                         logger.log(logger.EXTREME, "NoteTags SQL select has failed.");\r
81                         logger.log(logger.MEDIUM, query.lastError());\r
82                         return null;\r
83                 }\r
84                 while (query.next()) {\r
85                         NoteTagsRecord record = new NoteTagsRecord();\r
86                         record.tagGuid = query.valueString(0);\r
87                         record.noteGuid = query.valueString(1);\r
88                         tags.add(record);\r
89                 }       \r
90                 return tags;\r
91         }\r
92         // Check if a note has a specific tag already\r
93         public boolean checkNoteNoteTags(String noteGuid, String tagGuid) {\r
94                 if (noteGuid == null || tagGuid == null)\r
95                         return false;\r
96                 boolean check;\r
97                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
98                 check = query.prepare("Select "\r
99                                 +"NoteGuid, TagGuid from NoteTags where noteGuid = :noteGuid and tagGuid = :tagGuid");\r
100                 if (!check)\r
101                         logger.log(logger.EXTREME, "checkNoteTags SQL prepare has failed.");\r
102                 \r
103                 query.bindValue(":noteGuid", noteGuid);\r
104                 query.bindValue(":tagGuid", tagGuid);\r
105                 query.exec();\r
106                 \r
107                 if (!check) {\r
108                         logger.log(logger.EXTREME, "checkNoteTags SQL select has failed.");\r
109                         logger.log(logger.MEDIUM, query.lastError());\r
110                         return false;\r
111                 }\r
112                 \r
113                 if (query.next()) {\r
114                         return true;\r
115                 }       \r
116                 return false;\r
117         }\r
118         // Save Note Tags\r
119         public void saveNoteTag(String noteGuid, String tagGuid) {\r
120                 boolean check;\r
121                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
122 \r
123                 check = query.prepare("Insert Into NoteTags (noteGuid, tagGuid) "\r
124                                 +"Values("\r
125                                 +":noteGuid, :tagGuid)");\r
126                 if (!check)\r
127                         logger.log(logger.EXTREME, "Note SQL insert prepare has failed.");\r
128         \r
129                 query.bindValue(":noteGuid", noteGuid);\r
130                 query.bindValue(":tagGuid", tagGuid);\r
131                                                 \r
132                 check = query.exec();\r
133                 if (!check) {\r
134                         logger.log(logger.MEDIUM, "NoteTags Table insert failed.");             \r
135                         logger.log(logger.MEDIUM, query.lastError());\r
136                 }\r
137                 check = query.prepare("Update Note set isDirty=1 where guid=:guid");\r
138                 if (!check)\r
139                         logger.log(logger.EXTREME, "RNoteTagsTable.saveNoteTag prepare has failed.");\r
140                 query.bindValue(":guid", noteGuid);\r
141                 if (!check) {\r
142                         logger.log(logger.MEDIUM, "RNoteTagsTable.saveNoteTag has failed to set note as dirty.");               \r
143                         logger.log(logger.MEDIUM, query.lastError());\r
144                 }\r
145         }\r
146         // Delete a note's tags\r
147         public void deleteNoteTag(String noteGuid) {\r
148                 boolean check;\r
149                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
150                 check = query.prepare("Delete from NoteTags where noteGuid = :noteGuid");\r
151                 if (!check)\r
152                         logger.log(logger.EXTREME, "Note SQL delete prepare has failed.");\r
153         \r
154                 query.bindValue(":noteGuid", noteGuid);\r
155                 check = query.exec();\r
156                 if (!check) {\r
157                         logger.log(logger.MEDIUM, "NoteTags Table delete failed.");             \r
158                         logger.log(logger.MEDIUM, query.lastError());\r
159                 }\r
160 \r
161         }\r
162         // Get a note tag counts\r
163         public List<Pair<String,Integer>> getTagCounts() {\r
164                 List<Pair<String,Integer>> counts = new ArrayList<Pair<String,Integer>>();              \r
165                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
166                 if (!query.exec("select tagguid, count(noteguid) from notetags group by tagguid;")) {\r
167                         logger.log(logger.EXTREME, "NoteTags SQL getTagCounts has failed.");\r
168                         logger.log(logger.MEDIUM, query.lastError());\r
169                         return null;\r
170                 }\r
171                 while (query.next()) {\r
172                         Pair<String,Integer> newCount = new Pair<String,Integer>();\r
173                         newCount.setFirst(query.valueString(0));\r
174                         newCount.setSecond(query.valueInteger(1));\r
175                         counts.add(newCount);\r
176                 }       \r
177                 return counts;\r
178         }\r
179 }\r