2 * This file is part of NixNote
\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.ListManager;
\r
30 public class SyncTable {
\r
32 private final ApplicationLogger logger;
\r
33 private final DatabaseConnection db;
\r
37 public SyncTable(ApplicationLogger l, DatabaseConnection d) {
\r
42 public void createTable() {
\r
43 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
44 logger.log(logger.HIGH, "Creating table Sync...");
\r
45 if (!query.exec("Create table Sync (key varchar primary key, value varchar);"))
\r
46 logger.log(logger.HIGH, "Table Sync creation FAILED!!!");
\r
47 addRecord("LastSequenceDate","0");
\r
48 addRecord("UpdateSequenceNumber", "0");
\r
51 public void dropTable() {
\r
52 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
53 query.exec("Drop table Sync");
\r
55 // Add an item to the table
\r
56 public void addRecord(String key, String value) {
\r
57 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
58 query.prepare("Insert Into Sync (key, value) values (:key, :value);");
\r
59 query.bindValue(":key", key);
\r
60 query.bindValue(":value", value);
\r
61 if (!query.exec()) {
\r
62 logger.log(logger.MEDIUM, "Add to into Sync failed.");
\r
63 logger.log(logger.MEDIUM, query.lastError());
\r
66 // Add an item to the table
\r
67 public void deleteRecord(String key) {
\r
68 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
69 query.prepare("Delete From Sync where key=:key");
\r
70 query.bindValue(":key", key);
\r
71 if (!query.exec()) {
\r
72 logger.log(logger.MEDIUM, "Delete from Sync failed.");
\r
73 logger.log(logger.MEDIUM, query.lastError());
\r
77 public String getRecord(String key) {
\r
78 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
79 query.prepare("Select value from Sync where key=:key");
\r
80 query.bindValue(":key", key);
\r
81 if (!query.exec()) {
\r
82 logger.log(logger.MEDIUM, "getRecord from sync failed.");
\r
83 logger.log(logger.MEDIUM, query.lastError());
\r
87 return (query.valueString(0));
\r
92 public void setRecord(String key, String value) {
\r
93 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
94 query.prepare("Update Sync set value=:value where key=:key");
\r
95 query.bindValue(":key", key);
\r
96 query.bindValue(":value", value);
\r
97 if (!query.exec()) {
\r
98 logger.log(logger.MEDIUM, "setRecord from sync failed.");
\r
99 logger.log(logger.MEDIUM, query.lastError());
\r
104 // Set the last sequence date
\r
105 public void setLastSequenceDate(long date) {
\r
106 logger.log(logger.LOW, "Updating Last Sequence Date: " +date);
\r
107 long old = getLastSequenceDate();
\r
108 logger.log(logger.LOW, "Old Last Sequence Date: " +old);
\r
110 logger.log(logger.LOW, "************* SEQUENCE DATE PROBLEM!!! "+(old-date));
\r
111 setRecord("LastSequenceDate", new Long(date).toString());
\r
113 // Set the last sequence date
\r
114 public void setUpdateSequenceNumber(int number) {
\r
115 logger.log(logger.LOW, "Updating Last Sequence Number: " +number);
\r
116 int old = getUpdateSequenceNumber();
\r
117 logger.log(logger.LOW, "Old Last Sequence Number: " +old);
\r
119 logger.log(logger.LOW, "************* SEQUENCE NUMBER PROBLEM!!! "+(old-number));
\r
120 setRecord("UpdateSequenceNumber", new Integer(number).toString());
\r
122 // get last sequence date
\r
123 public long getLastSequenceDate() {
\r
124 return new Long(getRecord("LastSequenceDate"));
\r
126 // Get invalid attributes for a given element
\r
127 public int getUpdateSequenceNumber() {
\r
128 return new Integer(getRecord("UpdateSequenceNumber"));
\r
130 // Get notebooks/tags to ignore
\r
131 public List<String> getIgnoreRecords(String type) {
\r
132 List<String> values = new ArrayList<String>();
\r
133 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
134 if (!query.prepare("Select value from Sync where key like :type")) {
\r
135 logger.log(logger.MEDIUM, "getIgnoreRecords from sync failed.");
\r
136 logger.log(logger.MEDIUM, query.lastError());
\r
139 query.bindValue(":type", "IGNORE" +type +"-%");
\r
141 while (query.next()) {
\r
142 values.add(query.valueString(0));
\r
146 // Expunge ignore records
\r
147 // Add an item to the table
\r