OSDN Git Service

NeverNote 0.88.
[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                 File f = new File(Global.getDirectoryPath() +File.separator +"db" +File.separator +"NeverNote.h2.db");
64                 boolean dbExists = f.exists(); 
65                 
66                 // If it doesn't exist and we are the main thread, then we need to create stuff.
67                 if (!dbExists && id  == 0)  {
68                         createTables();
69                         Global.setAutomaticLogin(false);
70                 }
71                 
72                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
73         }
74         
75         
76         public void dbShutdown() {
77                 DatabaseRequest req = new DatabaseRequest();
78                 req.type = DatabaseRequest.Shutdown;
79                 Global.dbRunner.addWork(req);
80         }
81         
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);
109                         version = "0.86";
110                         Global.setDatabaseVersion(version);
111                 } 
112         }
113         
114         public void checkDatabaseVersion() {
115                 if (!Global.getDatabaseVersion().equals("0.86")) {
116                         upgradeDb(Global.getDatabaseVersion());
117                 }
118         }
119         
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);
126         }
127
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);
136         }
137         
138         
139         public void createTables() {
140                 Global.setDatabaseVersion("0.85");
141 //              Global.setUpdateSequenceNumber(0);
142                 Global.setAutomaticLogin(false);
143                 Global.saveCurrentNoteGuid("");
144                 Global.saveUploadAmount(0);
145                 
146         }
147         
148         //***************************************************************
149         //* Table get methods
150         //***************************************************************
151         public DeletedTable getDeletedTable() {
152                 return deletedTable;
153         }
154         public TagTable getTagTable() {
155                 return tagTable;
156         }
157         public NoteTable getNoteTable() {
158                 return noteTable;
159         }
160         public NotebookTable getNotebookTable() {
161                 return notebookTable;
162         }
163         public SavedSearchTable getSavedSearchTable() {
164                 return searchTable;
165         }
166         public WatchFolderTable getWatchFolderTable() {
167                 return watchFolderTable;
168         }
169         public WordsTable getWordsTable() {
170                 return wordsTable;
171         }
172         public InvalidXMLTable getInvalidXMLTable() {
173                 return invalidXMLTable;
174         }
175         public SyncTable getSyncTable() {
176                 return syncTable;
177         }
178 }