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 cx.fbn.nevernote.sql.driver.NSqlQuery;
\r
24 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
26 public class WordsTable {
\r
27 private final ApplicationLogger logger;
\r
28 private final DatabaseConnection db;
\r
32 public WordsTable(ApplicationLogger l, DatabaseConnection d) {
\r
37 public void createTable() {
\r
38 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
39 logger.log(logger.HIGH, "Creating table WORDS ...");
\r
40 if (!query.exec("create table words (word varchar, guid varchar, source varchar, weight int, primary key (word, guid, source));")) {
\r
41 logger.log(logger.HIGH, "Table WORDS creation FAILED!!!");
\r
42 logger.log(logger.HIGH, query.lastError());
\r
46 public void dropTable() {
\r
47 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
48 query.exec("drop table words");
\r
50 // Count unindexed notes
\r
51 public int getWordCount() {
\r
52 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
53 query.exec("select count(*) from words");
\r
55 int returnValue = new Integer(query.valueString(0));
\r
59 // Clear out the word index table
\r
60 public void clearWordIndex() {
\r
61 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
62 logger.log(logger.HIGH, "DELETE FROM WORDS");
\r
64 boolean check = query.exec("DELETE FROM WORDS");
\r
66 logger.log(logger.HIGH, "Table WORDS clear has FAILED!!!");
\r
69 //********************************************************************************
\r
70 //********************************************************************************
\r
71 //* Support adding & deleting index words
\r
72 //********************************************************************************
\r
73 //********************************************************************************
\r
74 public void expungeFromWordIndex(String guid, String type) {
\r
75 NSqlQuery deleteWords = new NSqlQuery(db.getConnection());
\r
76 if (!deleteWords.prepare("delete from words where guid=:guid and source=:source")) {
\r
77 logger.log(logger.EXTREME, "Note SQL select prepare deleteWords has failed.");
\r
78 logger.log(logger.MEDIUM, deleteWords.lastError());
\r
81 deleteWords.bindValue(":guid", guid);
\r
82 deleteWords.bindValue(":source", type);
\r
87 public synchronized void addWordToNoteIndex(String guid, String word, String type, Integer weight) {
\r
88 NSqlQuery findWords = new NSqlQuery(db.getConnection());
\r
89 if (!findWords.prepare("Select weight from words where guid=:guid and source=:type and word=:word")) {
\r
90 logger.log(logger.MEDIUM, "Prepare failed in addWordToNoteIndex()");
\r
91 logger.log(logger.MEDIUM, findWords.lastError());
\r
94 findWords.bindValue(":guid", guid);
\r
95 findWords.bindValue(":type", type);
\r
96 findWords.bindValue(":word", word);
\r
98 boolean addNeeded = true;
\r
100 // If we have a match, find out which has the heigher weight & update accordingly
\r
101 if (findWords.next()) {
\r
102 int recordWeight = new Integer(findWords.valueString(0));
\r
104 if (recordWeight < weight) {
\r
105 NSqlQuery updateWord = new NSqlQuery(db.getConnection());
\r
106 if (!updateWord.prepare("Update words set weight=:weight where guid=:guid and source=:type and word=:word")) {
\r
107 logger.log(logger.MEDIUM, "Prepare failed for find words in addWordToNoteIndex()");
\r
108 logger.log(logger.MEDIUM, findWords.lastError());
\r
111 updateWord.bindValue(":weight", weight);
\r
112 updateWord.bindValue(":guid", guid);
\r
113 updateWord.bindValue(":type", type);
\r
114 updateWord.bindValue(":word",word);
\r
123 NSqlQuery insertWords = new NSqlQuery(db.getConnection());
\r
124 if (!insertWords.prepare("Insert Into Words (word, guid, weight, source)"
\r
125 +" Values(:word, :guid, :weight, :type )")) {
\r
126 logger.log(logger.EXTREME, "Note SQL select prepare checkWords has failed.");
\r
127 logger.log(logger.MEDIUM, insertWords.lastError());
\r
129 insertWords.bindValue(":word", word);
\r
130 insertWords.bindValue(":guid", guid);
\r
131 insertWords.bindValue(":weight", weight);
\r
132 insertWords.bindValue(":type", type);
\r
133 insertWords.exec();
\r