OSDN Git Service

- Gui cleanup - Rework count threads to reduce SQL overhead & improve performance.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NoteTagsTable.java
index 8cc57a8..75585ce 100644 (file)
 \r
 package cx.fbn.nevernote.sql;\r
 \r
+import java.util.ArrayList;\r
 import java.util.List;\r
 \r
-import cx.fbn.nevernote.Global;\r
-import cx.fbn.nevernote.sql.requests.NoteTagsRequest;\r
-import cx.fbn.nevernote.sql.runners.NoteTagsRecord;\r
+import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
+import cx.fbn.nevernote.utilities.ApplicationLogger;\r
 import cx.fbn.nevernote.utilities.Pair;\r
 \r
 public class NoteTagsTable {\r
-       private final int id;\r
+       private final ApplicationLogger                 logger;\r
+       DatabaseConnection                                              db;\r
+       NSqlQuery                                                               getNoteTagsQuery;\r
+\r
        \r
        // Constructor\r
-       public NoteTagsTable(int i) {\r
-               id = i;\r
+       public NoteTagsTable(ApplicationLogger l,DatabaseConnection d) {\r
+               logger = l;\r
+               db = d;\r
        }\r
        // Create the table\r
        public void createTable() {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Create_Table;\r
-               Global.dbRunner.addWork(request);\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+        // Create the NoteTag table\r
+        logger.log(logger.HIGH, "Creating table NoteTags...");\r
+        if (!query.exec("Create table NoteTags (noteGuid varchar, " +\r
+                       "tagGuid varchar, primary key(noteGuid, tagGuid))"))\r
+               logger.log(logger.HIGH, "Table NoteTags creation FAILED!!!"); \r
        }\r
        // Drop the table\r
        public void dropTable() {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Drop_Table;\r
-               Global.dbRunner.addWork(request);       }\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               query.exec("drop table NoteTags");\r
+       }\r
        // Get a note tags by the note's Guid\r
        public List<String> getNoteTags(String noteGuid) {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Get_Note_Tags;\r
-               request.string1 = new String(noteGuid);\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
-               NoteTagsRequest req = Global.dbRunner.noteTagsResponse.get(id).copy();\r
-               return req.responseStrings;\r
+               if (noteGuid == null)\r
+                       return null;\r
+               List<String> tags = new ArrayList<String>();\r
+               \r
+               if (getNoteTagsQuery == null)\r
+                       prepareGetNoteTagsQuery();\r
+               \r
+               getNoteTagsQuery.bindValue(":guid", noteGuid);\r
+               if (!getNoteTagsQuery.exec()) {\r
+                       logger.log(logger.EXTREME, "NoteTags SQL select has failed.");\r
+                       logger.log(logger.MEDIUM, getNoteTagsQuery.lastError());\r
+                       return null;\r
+               }\r
+               while (getNoteTagsQuery.next()) {\r
+                       tags.add(getNoteTagsQuery.valueString(0));\r
+               }       \r
+               return tags;\r
+       }\r
+       void prepareGetNoteTagsQuery() {\r
+               getNoteTagsQuery = new NSqlQuery(db.getConnection());\r
+               getNoteTagsQuery.prepare("Select TagGuid from NoteTags where noteGuid = :guid");\r
        }\r
        // Get a note tags by the note's Guid\r
        public List<NoteTagsRecord> getAllNoteTags() {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Get_All_Note_Tags;\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
-               NoteTagsRequest req = Global.dbRunner.noteTagsResponse.get(id).copy();\r
-               return req.responseNoteTagsRecord;\r
+               List<NoteTagsRecord> tags = new ArrayList<NoteTagsRecord>();\r
+               \r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (!query.exec("Select TagGuid, NoteGuid from NoteTags")) {\r
+                       logger.log(logger.EXTREME, "NoteTags SQL select has failed.");\r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+                       return null;\r
+               }\r
+               while (query.next()) {\r
+                       NoteTagsRecord record = new NoteTagsRecord();\r
+                       record.tagGuid = query.valueString(0);\r
+                       record.noteGuid = query.valueString(1);\r
+                       tags.add(record);\r
+               }       \r
+               return tags;\r
        }\r
        // Check if a note has a specific tag already\r
        public boolean checkNoteNoteTags(String noteGuid, String tagGuid) {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Check_Note_Note_Tags;\r
-               request.string1 = new String(noteGuid);\r
-               request.string2 = new String(tagGuid);\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
-               NoteTagsRequest req = Global.dbRunner.noteTagsResponse.get(id).copy();\r
-               return req.responseBoolean;\r
+               if (noteGuid == null || tagGuid == null)\r
+                       return false;\r
+               boolean check;\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               check = query.prepare("Select "\r
+                               +"NoteGuid, TagGuid from NoteTags where noteGuid = :noteGuid and tagGuid = :tagGuid");\r
+               if (!check)\r
+                       logger.log(logger.EXTREME, "checkNoteTags SQL prepare has failed.");\r
+               \r
+               query.bindValue(":noteGuid", noteGuid);\r
+               query.bindValue(":tagGuid", tagGuid);\r
+               query.exec();\r
+               \r
+               if (!check) {\r
+                       logger.log(logger.EXTREME, "checkNoteTags SQL select has failed.");\r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+                       return false;\r
+               }\r
+               \r
+               if (query.next()) {\r
+                       return true;\r
+               }       \r
+               return false;\r
        }\r
        // Save Note Tags\r
        public void saveNoteTag(String noteGuid, String tagGuid) {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Save_Note_Tag;\r
-               request.string1 = new String(noteGuid);\r
-               request.string2 = new String(tagGuid);\r
-               Global.dbRunner.addWork(request);\r
+               boolean check;\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+\r
+               check = query.prepare("Insert Into NoteTags (noteGuid, tagGuid) "\r
+                               +"Values("\r
+                               +":noteGuid, :tagGuid)");\r
+               if (!check)\r
+                       logger.log(logger.EXTREME, "Note SQL insert prepare has failed.");\r
+       \r
+               query.bindValue(":noteGuid", noteGuid);\r
+               query.bindValue(":tagGuid", tagGuid);\r
+                                               \r
+               check = query.exec();\r
+               if (!check) {\r
+                       logger.log(logger.MEDIUM, "NoteTags Table insert failed.");             \r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+               }\r
+               check = query.prepare("Update Note set isDirty=1 where guid=:guid");\r
+               if (!check)\r
+                       logger.log(logger.EXTREME, "RNoteTagsTable.saveNoteTag prepare has failed.");\r
+               query.bindValue(":guid", noteGuid);\r
+               if (!check) {\r
+                       logger.log(logger.MEDIUM, "RNoteTagsTable.saveNoteTag has failed to set note as dirty.");               \r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+               }\r
        }\r
        // Delete a note's tags\r
        public void deleteNoteTag(String noteGuid) {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Delete_Note_Tag;\r
-               request.string1 = new String(noteGuid);\r
-               Global.dbRunner.addWork(request);\r
+               boolean check;\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               check = query.prepare("Delete from NoteTags where noteGuid = :noteGuid");\r
+               if (!check)\r
+                       logger.log(logger.EXTREME, "Note SQL delete prepare has failed.");\r
+       \r
+               query.bindValue(":noteGuid", noteGuid);\r
+               check = query.exec();\r
+               if (!check) {\r
+                       logger.log(logger.MEDIUM, "NoteTags Table delete failed.");             \r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+               }\r
+\r
        }\r
-       // Get tag counts\r
+       // Get a note tag counts\r
        public List<Pair<String,Integer>> getTagCounts() {\r
-               NoteTagsRequest request = new NoteTagsRequest();\r
-               request.requestor_id = id;\r
-               request.type = NoteTagsRequest.Tag_Counts;\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
-               NoteTagsRequest req = Global.dbRunner.noteTagsResponse.get(id).copy();\r
-               return req.responseCounts;\r
-               \r
+               List<Pair<String,Integer>> counts = new ArrayList<Pair<String,Integer>>();              \r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (!query.exec("select tagguid, count(noteguid) from notetags group by tagguid;")) {\r
+                       logger.log(logger.EXTREME, "NoteTags SQL getTagCounts has failed.");\r
+                       logger.log(logger.MEDIUM, query.lastError());\r
+                       return null;\r
+               }\r
+               while (query.next()) {\r
+                       Pair<String,Integer> newCount = new Pair<String,Integer>();\r
+                       newCount.setFirst(query.valueString(0));\r
+                       newCount.setSecond(query.valueInteger(1));\r
+                       counts.add(newCount);\r
+               }       \r
+               return counts;\r
        }\r
 }\r