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
33 NSqlQuery getNoteTagsQuery;
\r
37 public NoteTagsTable(ApplicationLogger l,DatabaseConnection d) {
\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
51 public void dropTable() {
\r
52 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
53 query.exec("drop table NoteTags");
\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
59 List<String> tags = new ArrayList<String>();
\r
61 if (getNoteTagsQuery == null)
\r
62 prepareGetNoteTagsQuery();
\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
70 while (getNoteTagsQuery.next()) {
\r
71 tags.add(getNoteTagsQuery.valueString(0));
\r
75 void prepareGetNoteTagsQuery() {
\r
76 getNoteTagsQuery = new NSqlQuery(db.getConnection());
\r
77 getNoteTagsQuery.prepare("Select TagGuid from NoteTags where noteGuid = :guid");
\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
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
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
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
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
106 logger.log(logger.EXTREME, "checkNoteTags SQL prepare has failed.");
\r
108 query.bindValue(":noteGuid", noteGuid);
\r
109 query.bindValue(":tagGuid", tagGuid);
\r
113 logger.log(logger.EXTREME, "checkNoteTags SQL select has failed.");
\r
114 logger.log(logger.MEDIUM, query.lastError());
\r
118 if (query.next()) {
\r
124 public void saveNoteTag(String noteGuid, String tagGuid) {
\r
126 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
128 check = query.prepare("Insert Into NoteTags (noteGuid, tagGuid) "
\r
130 +":noteGuid, :tagGuid)");
\r
132 logger.log(logger.EXTREME, "Note SQL insert prepare has failed.");
\r
134 query.bindValue(":noteGuid", noteGuid);
\r
135 query.bindValue(":tagGuid", tagGuid);
\r
137 check = query.exec();
\r
139 logger.log(logger.MEDIUM, "NoteTags Table insert failed.");
\r
140 logger.log(logger.MEDIUM, query.lastError());
\r
142 check = query.prepare("Update Note set isDirty=1 where guid=:guid");
\r
144 logger.log(logger.EXTREME, "RNoteTagsTable.saveNoteTag prepare has failed.");
\r
145 query.bindValue(":guid", noteGuid);
\r
147 logger.log(logger.MEDIUM, "RNoteTagsTable.saveNoteTag has failed to set note as dirty.");
\r
148 logger.log(logger.MEDIUM, query.lastError());
\r
151 // Delete a note's tags
\r
152 public void deleteNoteTag(String noteGuid) {
\r
154 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
155 check = query.prepare("Delete from NoteTags where noteGuid = :noteGuid");
\r
157 logger.log(logger.EXTREME, "Note SQL delete prepare has failed.");
\r
159 query.bindValue(":noteGuid", noteGuid);
\r
160 check = query.exec();
\r
162 logger.log(logger.MEDIUM, "NoteTags Table delete failed.");
\r
163 logger.log(logger.MEDIUM, query.lastError());
\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
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