OSDN Git Service

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