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 // NFC FIXME: should be parameterized with databaseName like in RDatabaseConnection?
64 File f = Global.getFileManager().getDbDirFile("NeverNote.h2.db");
65 boolean dbExists = f.exists();
67 // If it doesn't exist and we are the main thread, then we need to create stuff.
68 if (!dbExists && id == 0) {
70 Global.setAutomaticLogin(false);
73 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
77 public void dbShutdown() {
78 DatabaseRequest req = new DatabaseRequest();
79 req.type = DatabaseRequest.Shutdown;
80 Global.dbRunner.addWork(req);
83 public void upgradeDb(String version) {
84 if (version.equals("0.85")) {
85 DatabaseRequest req = new DatabaseRequest();
86 req.type = DatabaseRequest.Execute_Sql;
87 req.string1 = new String("alter table note add column titleColor integer");
88 Global.dbRunner.addWork(req);
89 Global.dbClientWait(id);
90 req.type = DatabaseRequest.Execute_Sql;
91 req.string1 = new String("update note set titlecolor=-1");
92 Global.dbRunner.addWork(req);
93 Global.dbClientWait(id);
94 req.type = DatabaseRequest.Execute_Sql;
95 req.string1 = new String("alter table note add column thumbnail blob");
96 Global.dbRunner.addWork(req);
97 Global.dbClientWait(id);
98 req.string1 = new String("alter table note add column thumbnailneeded boolean");
99 Global.dbRunner.addWork(req);
100 Global.dbClientWait(id);
101 req.string1 = new String("Update note set thumbnailneeded = true;");
102 Global.dbRunner.addWork(req);
103 Global.dbClientWait(id);
104 req.string1 = new String("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
105 Global.dbRunner.addWork(req);
106 Global.dbClientWait(id);
107 req.string1 = new String("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
108 Global.dbRunner.addWork(req);
109 Global.dbClientWait(id);
111 Global.setDatabaseVersion(version);
115 public void checkDatabaseVersion() {
116 if (!Global.getDatabaseVersion().equals("0.86")) {
117 upgradeDb(Global.getDatabaseVersion());
121 public void compactDatabase() {
122 DatabaseRequest request = new DatabaseRequest();
123 request.requestor_id = id;
124 request.type = DatabaseRequest.Compact;
125 Global.dbRunner.addWork(request);
126 Global.dbClientWait(id);
129 public void backupDatabase(int highSequence, long date) {
130 DatabaseRequest request = new DatabaseRequest();
131 request.requestor_id = id;
132 request.int1 = highSequence;
133 request.long1 = date;
134 request.type = DatabaseRequest.Backup_Database;
135 Global.dbRunner.addWork(request);
136 Global.dbClientWait(id);
140 public void createTables() {
141 Global.setDatabaseVersion("0.85");
142 // Global.setUpdateSequenceNumber(0);
143 Global.setAutomaticLogin(false);
144 Global.saveCurrentNoteGuid("");
145 Global.saveUploadAmount(0);
149 //***************************************************************
150 //* Table get methods
151 //***************************************************************
152 public DeletedTable getDeletedTable() {
155 public TagTable getTagTable() {
158 public NoteTable getNoteTable() {
161 public NotebookTable getNotebookTable() {
162 return notebookTable;
164 public SavedSearchTable getSavedSearchTable() {
167 public WatchFolderTable getWatchFolderTable() {
168 return watchFolderTable;
170 public WordsTable getWordsTable() {
173 public InvalidXMLTable getInvalidXMLTable() {
174 return invalidXMLTable;
176 public SyncTable getSyncTable() {