2 * This file is part of NeverNote
3 * Copyright 2009 Randy Baumgarte
5 * This file may be licensed under the terms of of the
6 * GNU General Public License Version 2 (the ``GPL'').
8 * Software distributed under the License is distributed
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10 * express or implied. See the GPL for the specific language
11 * governing rights and limitations.
13 * You should have received a copy of the GPL along with this
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
15 * or write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 package cx.fbn.nevernote.sql;
23 import cx.fbn.nevernote.Global;
24 import cx.fbn.nevernote.sql.requests.DatabaseRequest;
25 import cx.fbn.nevernote.utilities.ApplicationLogger;
28 public class DatabaseConnection {
30 private final WordsTable wordsTable;
31 private final TagTable tagTable;
32 private final NotebookTable notebookTable;
33 private final NoteTable noteTable;
34 private final DeletedTable deletedTable;
35 private final SavedSearchTable searchTable;
36 private final WatchFolderTable watchFolderTable;
37 private final InvalidXMLTable invalidXMLTable;
38 private final SyncTable syncTable;
39 private final ApplicationLogger logger;
43 public DatabaseConnection(ApplicationLogger l, int i) {
45 tagTable = new TagTable(id);
46 notebookTable = new NotebookTable(id);
47 noteTable = new NoteTable(id);
48 deletedTable = new DeletedTable(id);
49 searchTable = new SavedSearchTable(id);
50 watchFolderTable = new WatchFolderTable(id);
51 wordsTable = new WordsTable(id);
52 invalidXMLTable = new InvalidXMLTable(id);
53 syncTable = new SyncTable(id);
59 // Initialize the database connection
60 public void dbSetup() {
61 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
63 File f = new File(Global.getDirectoryPath() +File.separator +"db" +File.separator +"NeverNote.h2.db");
64 boolean dbExists = f.exists();
66 // If it doesn't exist and we are the main thread, then we need to create stuff.
67 if (!dbExists && id == 0) {
69 Global.setAutomaticLogin(false);
72 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
76 public void dbShutdown() {
77 DatabaseRequest req = new DatabaseRequest();
78 req.type = DatabaseRequest.Shutdown;
79 Global.dbRunner.addWork(req);
82 public void upgradeDb(String version) {
83 if (version.equals("0.85")) {
84 DatabaseRequest req = new DatabaseRequest();
85 req.type = DatabaseRequest.Execute_Sql;
86 req.string1 = new String("alter table note add column titleColor integer");
87 Global.dbRunner.addWork(req);
88 Global.dbClientWait(id);
89 req.type = DatabaseRequest.Execute_Sql;
90 req.string1 = new String("update note set titlecolor=-1");
91 Global.dbRunner.addWork(req);
92 Global.dbClientWait(id);
93 req.type = DatabaseRequest.Execute_Sql;
94 req.string1 = new String("alter table note add column thumbnail blob");
95 Global.dbRunner.addWork(req);
96 Global.dbClientWait(id);
97 req.string1 = new String("alter table note add column thumbnailneeded boolean");
98 Global.dbRunner.addWork(req);
99 Global.dbClientWait(id);
100 req.string1 = new String("Update note set thumbnailneeded = true;");
101 Global.dbRunner.addWork(req);
102 Global.dbClientWait(id);
103 req.string1 = new String("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
104 Global.dbRunner.addWork(req);
105 Global.dbClientWait(id);
106 req.string1 = new String("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
107 Global.dbRunner.addWork(req);
108 Global.dbClientWait(id);
110 Global.setDatabaseVersion(version);
114 public void checkDatabaseVersion() {
115 if (!Global.getDatabaseVersion().equals("0.86")) {
116 upgradeDb(Global.getDatabaseVersion());
120 public void compactDatabase() {
121 DatabaseRequest request = new DatabaseRequest();
122 request.requestor_id = id;
123 request.type = DatabaseRequest.Compact;
124 Global.dbRunner.addWork(request);
125 Global.dbClientWait(id);
128 public void backupDatabase(int highSequence, long date) {
129 DatabaseRequest request = new DatabaseRequest();
130 request.requestor_id = id;
131 request.int1 = highSequence;
132 request.long1 = date;
133 request.type = DatabaseRequest.Backup_Database;
134 Global.dbRunner.addWork(request);
135 Global.dbClientWait(id);
139 public void createTables() {
140 Global.setDatabaseVersion("0.85");
141 // Global.setUpdateSequenceNumber(0);
142 Global.setAutomaticLogin(false);
143 Global.saveCurrentNoteGuid("");
144 Global.saveUploadAmount(0);
148 //***************************************************************
149 //* Table get methods
150 //***************************************************************
151 public DeletedTable getDeletedTable() {
154 public TagTable getTagTable() {
157 public NoteTable getNoteTable() {
160 public NotebookTable getNotebookTable() {
161 return notebookTable;
163 public SavedSearchTable getSavedSearchTable() {
166 public WatchFolderTable getWatchFolderTable() {
167 return watchFolderTable;
169 public WordsTable getWordsTable() {
172 public InvalidXMLTable getInvalidXMLTable() {
173 return invalidXMLTable;
175 public SyncTable getSyncTable() {