OSDN Git Service

Fix handling of H2 lock files
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / DatabaseConnection.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
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.
12  *
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.
17  *
18 */
19 package cx.fbn.nevernote.sql;
20
21 import java.io.File;
22
23 import cx.fbn.nevernote.Global;
24 import cx.fbn.nevernote.sql.requests.DatabaseRequest;
25
26
27 public class DatabaseConnection {
28         // Table helpers
29         private final WordsTable                                wordsTable;
30         private final TagTable                                  tagTable;
31         private final NotebookTable                             notebookTable;
32         private final NoteTable                                 noteTable;
33         private final DeletedTable                              deletedTable;
34         private final SavedSearchTable                  searchTable;
35         private final WatchFolderTable                  watchFolderTable;
36         private final InvalidXMLTable                   invalidXMLTable;
37         private final SyncTable                                 syncTable;
38         int id;
39
40         
41         public DatabaseConnection(int i) {
42                 id = i;
43                 tagTable = new TagTable(id);
44                 notebookTable = new NotebookTable(id);
45                 noteTable = new NoteTable(id);
46                 deletedTable = new DeletedTable(id);
47                 searchTable = new SavedSearchTable(id);
48                 watchFolderTable = new WatchFolderTable(id);
49                 wordsTable = new WordsTable(id);
50                 invalidXMLTable = new InvalidXMLTable(id);
51                 syncTable = new SyncTable(id);
52         }
53         
54         
55         // Initialize the database connection
56         public void dbSetup() {
57                 // NFC FIXME: should be parameterized with databaseName like in RDatabaseConnection?
58                 File f = Global.getFileManager().getDbDirFile("NeverNote.h2.db");
59                 boolean dbExists = f.exists(); 
60                 
61                 // If it doesn't exist and we are the main thread, then we need to create stuff.
62                 if (!dbExists && id  == 0)  {
63                         createTables();
64                         Global.setAutomaticLogin(false);
65                 }
66         }
67         
68         
69         public void dbShutdown() {
70                 DatabaseRequest req = new DatabaseRequest();
71                 req.type = DatabaseRequest.Shutdown;
72                 Global.dbRunner.addWork(req);
73         }
74         
75         public void upgradeDb(String version) {
76                 if (version.equals("0.85")) {
77                         DatabaseRequest req = new DatabaseRequest();
78                         req.type = DatabaseRequest.Execute_Sql;
79                         req.string1 = new String("alter table note add column titleColor integer");
80                         Global.dbRunner.addWork(req);
81                         Global.dbClientWait(id);
82                         req.type = DatabaseRequest.Execute_Sql;
83                         req.string1 = new String("update note set titlecolor=-1");
84                         Global.dbRunner.addWork(req);
85                         Global.dbClientWait(id);
86                         req.type = DatabaseRequest.Execute_Sql;
87                         req.string1 = new String("alter table note add column thumbnail blob");
88                         Global.dbRunner.addWork(req);
89                         Global.dbClientWait(id);
90                         req.string1 = new String("alter table note add column thumbnailneeded boolean");
91                         Global.dbRunner.addWork(req);
92                         Global.dbClientWait(id);
93                         req.string1 = new String("Update note set thumbnailneeded = true;");
94                         Global.dbRunner.addWork(req);
95                         Global.dbClientWait(id);
96                         req.string1 = new String("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
97                         Global.dbRunner.addWork(req);
98                         Global.dbClientWait(id);
99                         req.string1 = new String("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
100                         Global.dbRunner.addWork(req);
101                         Global.dbClientWait(id);
102                         version = "0.86";
103                         Global.setDatabaseVersion(version);
104                 } 
105         }
106         
107         public void checkDatabaseVersion() {
108                 // NFC FIXME: this needs to read the existing version number from a table in the DB
109                 if (!Global.getDatabaseVersion().equals("0.86")) {
110                         upgradeDb(Global.getDatabaseVersion());
111                 }
112         }
113         
114         public void compactDatabase() {
115                 DatabaseRequest request = new DatabaseRequest();
116                 request.requestor_id = id;
117                 request.type = DatabaseRequest.Compact;
118                 Global.dbRunner.addWork(request);
119                 Global.dbClientWait(id);
120         }
121
122         public void backupDatabase(int highSequence, long date) {
123                 DatabaseRequest request = new DatabaseRequest();
124                 request.requestor_id = id;
125                 request.int1 = highSequence;
126                 request.long1 = date;
127                 request.type = DatabaseRequest.Backup_Database;
128                 Global.dbRunner.addWork(request);
129                 Global.dbClientWait(id);
130         }
131         
132         
133         private void createTables() {
134                 Global.setDatabaseVersion("0.85");
135 //              Global.setUpdateSequenceNumber(0);
136                 Global.setAutomaticLogin(false);
137                 Global.saveCurrentNoteGuid("");
138                 Global.saveUploadAmount(0);
139                 
140         }
141         
142         //***************************************************************
143         //* Table get methods
144         //***************************************************************
145         public DeletedTable getDeletedTable() {
146                 return deletedTable;
147         }
148         public TagTable getTagTable() {
149                 return tagTable;
150         }
151         public NoteTable getNoteTable() {
152                 return noteTable;
153         }
154         public NotebookTable getNotebookTable() {
155                 return notebookTable;
156         }
157         public SavedSearchTable getSavedSearchTable() {
158                 return searchTable;
159         }
160         public WatchFolderTable getWatchFolderTable() {
161                 return watchFolderTable;
162         }
163         public WordsTable getWordsTable() {
164                 return wordsTable;
165         }
166         public InvalidXMLTable getInvalidXMLTable() {
167                 return invalidXMLTable;
168         }
169         public SyncTable getSyncTable() {
170                 return syncTable;
171         }
172 }