2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
21 package cx.fbn.nevernote.sql;
\r
23 import java.util.ArrayList;
\r
24 import java.util.List;
\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
30 public class NoteTagsTable {
\r
31 private final ApplicationLogger logger;
\r
32 DatabaseConnection db;
\r
36 public NoteTagsTable(ApplicationLogger l,DatabaseConnection d) {
\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
50 public void dropTable() {
\r
51 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
52 query.exec("drop table NoteTags");
\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
59 List<String> tags = new ArrayList<String>();
\r
61 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
62 check = query.exec("Select "
\r
63 +"TagGuid from NoteTags where noteGuid = '" +noteGuid +"'");
\r
65 logger.log(logger.EXTREME, "NoteTags SQL select has failed.");
\r
66 logger.log(logger.MEDIUM, query.lastError());
\r
69 while (query.next()) {
\r
70 tags.add(query.valueString(0));
\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
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
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
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
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
101 logger.log(logger.EXTREME, "checkNoteTags SQL prepare has failed.");
\r
103 query.bindValue(":noteGuid", noteGuid);
\r
104 query.bindValue(":tagGuid", tagGuid);
\r
108 logger.log(logger.EXTREME, "checkNoteTags SQL select has failed.");
\r
109 logger.log(logger.MEDIUM, query.lastError());
\r
113 if (query.next()) {
\r
119 public void saveNoteTag(String noteGuid, String tagGuid) {
\r
121 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
123 check = query.prepare("Insert Into NoteTags (noteGuid, tagGuid) "
\r
125 +":noteGuid, :tagGuid)");
\r
127 logger.log(logger.EXTREME, "Note SQL insert prepare has failed.");
\r
129 query.bindValue(":noteGuid", noteGuid);
\r
130 query.bindValue(":tagGuid", tagGuid);
\r
132 check = query.exec();
\r
134 logger.log(logger.MEDIUM, "NoteTags Table insert failed.");
\r
135 logger.log(logger.MEDIUM, query.lastError());
\r
137 check = query.prepare("Update Note set isDirty=1 where guid=:guid");
\r
139 logger.log(logger.EXTREME, "RNoteTagsTable.saveNoteTag prepare has failed.");
\r
140 query.bindValue(":guid", noteGuid);
\r
142 logger.log(logger.MEDIUM, "RNoteTagsTable.saveNoteTag has failed to set note as dirty.");
\r
143 logger.log(logger.MEDIUM, query.lastError());
\r
146 // Delete a note's tags
\r
147 public void deleteNoteTag(String noteGuid) {
\r
149 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
150 check = query.prepare("Delete from NoteTags where noteGuid = :noteGuid");
\r
152 logger.log(logger.EXTREME, "Note SQL delete prepare has failed.");
\r
154 query.bindValue(":noteGuid", noteGuid);
\r
155 check = query.exec();
\r
157 logger.log(logger.MEDIUM, "NoteTags Table delete failed.");
\r
158 logger.log(logger.MEDIUM, query.lastError());
\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
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