OSDN Git Service

Change generation of files/paths in db dir to use FileManager
[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 import cx.fbn.nevernote.utilities.ApplicationLogger;
26
27
28 public class DatabaseConnection {
29         // Table helpers
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;
40         int id;
41
42         
43         public DatabaseConnection(ApplicationLogger l, int i) {
44                 id = 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);
54                 logger = l;
55
56         }
57         
58         
59         // Initialize the database connection
60         public void dbSetup() {
61                 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
62
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(); 
66                 
67                 // If it doesn't exist and we are the main thread, then we need to create stuff.
68                 if (!dbExists && id  == 0)  {
69                         createTables();
70                         Global.setAutomaticLogin(false);
71                 }
72                 
73                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
74         }
75         
76         
77         public void dbShutdown() {
78                 DatabaseRequest req = new DatabaseRequest();
79                 req.type = DatabaseRequest.Shutdown;
80                 Global.dbRunner.addWork(req);
81         }
82         
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);
110                         version = "0.86";
111                         Global.setDatabaseVersion(version);
112                 } 
113         }
114         
115         public void checkDatabaseVersion() {
116                 if (!Global.getDatabaseVersion().equals("0.86")) {
117                         upgradeDb(Global.getDatabaseVersion());
118                 }
119         }
120         
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);
127         }
128
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);
137         }
138         
139         
140         public void createTables() {
141                 Global.setDatabaseVersion("0.85");
142 //              Global.setUpdateSequenceNumber(0);
143                 Global.setAutomaticLogin(false);
144                 Global.saveCurrentNoteGuid("");
145                 Global.saveUploadAmount(0);
146                 
147         }
148         
149         //***************************************************************
150         //* Table get methods
151         //***************************************************************
152         public DeletedTable getDeletedTable() {
153                 return deletedTable;
154         }
155         public TagTable getTagTable() {
156                 return tagTable;
157         }
158         public NoteTable getNoteTable() {
159                 return noteTable;
160         }
161         public NotebookTable getNotebookTable() {
162                 return notebookTable;
163         }
164         public SavedSearchTable getSavedSearchTable() {
165                 return searchTable;
166         }
167         public WatchFolderTable getWatchFolderTable() {
168                 return watchFolderTable;
169         }
170         public WordsTable getWordsTable() {
171                 return wordsTable;
172         }
173         public InvalidXMLTable getInvalidXMLTable() {
174                 return invalidXMLTable;
175         }
176         public SyncTable getSyncTable() {
177                 return syncTable;
178         }
179 }