OSDN Git Service

Added ability to customize db cache.
[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 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25
26 import com.trolltech.qt.sql.QJdbc;
27
28 import cx.fbn.nevernote.Global;
29 import cx.fbn.nevernote.sql.driver.NSqlQuery;
30 import cx.fbn.nevernote.utilities.ApplicationLogger;
31
32
33 public class DatabaseConnection {
34         // Table helpers
35         private WordsTable                                      wordsTable;
36         private TagTable                                        tagTable;
37         private NotebookTable                           notebookTable;
38         private NoteTable                                       noteTable;
39         private DeletedTable                            deletedTable;
40         private SavedSearchTable                        searchTable;
41         private WatchFolderTable                        watchFolderTable;
42         private InvalidXMLTable                         invalidXMLTable;
43         private LinkedNotebookTable                     linkedNotebookTable;
44         private SharedNotebookTable                     sharedNotebookTable;
45         private InkImagesTable                          inkImagesTable;
46         private SyncTable                                       syncTable;
47         private SystemIconTable                         systemIconTable;
48         private final ApplicationLogger         logger;
49         private Connection                                      conn;
50         int id;
51
52         
53         public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword) {
54                 logger = l;
55                 dbSetup(url, userid, password, cypherPassword);
56         }
57         
58         private void setupTables() {
59                 tagTable = new TagTable(logger, this);
60                 notebookTable = new NotebookTable(logger, this);
61                 noteTable = new NoteTable(logger, this);
62                 deletedTable = new DeletedTable(logger, this);
63                 searchTable = new SavedSearchTable(logger, this);       
64                 watchFolderTable = new WatchFolderTable(logger, this);
65                 invalidXMLTable = new InvalidXMLTable(logger, this);
66                 wordsTable = new WordsTable(logger, this);
67                 syncTable = new SyncTable(logger, this);
68                 linkedNotebookTable = new LinkedNotebookTable(logger, this);
69                 sharedNotebookTable = new SharedNotebookTable(logger, this);
70                 systemIconTable = new SystemIconTable(logger, this);
71                 inkImagesTable = new InkImagesTable(logger, this);
72         }
73         
74         
75         // Compact the database
76         public void compactDatabase() {
77                 
78         }
79         
80         // Initialize the database connection
81         public void dbSetup(String url,String userid, String userPassword, String cypherPassword) {
82                 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
83
84                 
85                 try {
86                         Class.forName("org.h2.Driver");
87                 } catch (ClassNotFoundException e1) {
88                         e1.printStackTrace();
89                         System.exit(16);
90                 }
91                 
92                 QJdbc.initialize();
93                 
94                 setupTables();
95                 
96                 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
97                 boolean dbExists = f.exists(); 
98                 
99                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
100                 
101
102                 try {
103                         String passwordString = null;
104                         if (cypherPassword==null || cypherPassword.trim().equals(""))
105                                 passwordString = userPassword;
106                         else
107                                 passwordString = cypherPassword+" "+userPassword;
108 //                      conn = DriverManager.getConnection(url,userid,passwordString);
109 //                      conn = DriverManager.getConnection(url+";CACHE_SIZE=4096",userid,passwordString);
110                         conn = DriverManager.getConnection(url+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
111 //                      conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
112                 } catch (SQLException e) {
113                         e.printStackTrace();
114                         return;
115                 }
116                 
117                 // If it doesn't exist and we are the main thread, then we need to create stuff.
118                 if (!dbExists)  {
119                         createTables();
120                         Global.setAutomaticLogin(false);
121                 }               
122                 
123                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
124         }
125         
126         
127         public void dbShutdown() {
128                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
129                 try {
130                         conn.close();
131                 } catch (SQLException e) {
132                         e.printStackTrace();
133                 }
134                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
135         }
136         
137         public void upgradeDb(String version) {
138                 if (version.equals("0.85")) {
139                         executeSql("alter table note add column titleColor integer");
140                         executeSql("alter table note add column thumbnail blob");
141                         executeSql("alter table note add column thumbnailneeded boolean");
142                         executeSql("Update note set thumbnailneeded = true;");
143                         executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
144                         executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
145                         version = "0.86";
146                         Global.setDatabaseVersion(version);
147                 } 
148                 if (version.equals("0.86")) {
149         
150                         executeSql("alter table notebook add column publishingUri VarChar");
151                         executeSql("alter table notebook add column publishingOrder Integer");
152                         executeSql("alter table notebook add column publishingAscending Boolean");
153                         executeSql("alter table notebook add column publishingPublicDescription varchar");
154                         executeSql("alter table notebook add column stack varchar");
155                         executeSql("alter table notebook add column icon blob");
156                         executeSql("alter table notebook add column readOnly boolean");
157                         executeSql("alter table notebook add column linked boolean");
158                         
159                         executeSql("alter table tag add column realname varchar");
160                         executeSql("alter table tag add column linked boolean");
161                         executeSql("alter table tag add column icon blob");
162                         executeSql("alter table tag add column notebookguid varchar");
163                         executeSql("alter table SavedSearch add column icon blob");
164
165                         executeSql("create index NOTE_THUMBNAIL_INDEX on note (thumbnailneeded, guid);");
166                         executeSql("create index NOTE_EXPUNGED_INDEX on note (isExpunged, guid);");
167                         executeSql("create index NOTE_DUEDATE_INDEX on note (attributeSubjectDate, guid);");
168                         executeSql("create index RESOURCES_GUID_INDEX on noteresources (noteGuid, guid);");
169                         executeSql("create index TAG_NOTEBOOK_INDEX on tag (notebookGuid);");
170                         
171                         executeSql("update note set thumbnailneeded=true, thumbnail=null;");
172                         executeSql("update notebook set publishingUri='', " +
173                                         "publishingAscending=false, stack='', readonly=false, publishingOrder=1, " +
174                                         "publishingPublicDescription='', linked=false");
175                         executeSql("update tag set linked=false, realname='', notebookguid=''");
176                         
177                         sharedNotebookTable.createTable();
178                         linkedNotebookTable.createTable();
179                         systemIconTable.createTable();
180                         inkImagesTable.createTable();
181                         
182                         version = "0.95";
183                         executeSql("Insert into Sync (key, value) values ('FullNotebookSync', 'true')");
184                         executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
185                         executeSql("Insert into Sync (key, value) values ('FullSharedNotebookSync', 'true')");
186                         executeSql("Insert into Sync (key, value) values ('FullInkNoteImageSync', 'true')");
187                         executeSql("Update note set indexneeded='true'");
188                         executeSql("Update noteresources set indexneeded='true'");
189                         Global.setDatabaseVersion(version);
190                 } 
191         }
192         
193         public void executeSql(String sql) {
194                 NSqlQuery query = new NSqlQuery(conn);
195                 query.exec(sql);        
196         }
197         
198         public void checkDatabaseVersion() {
199                 if (!Global.getDatabaseVersion().equals("0.86")) {
200                         upgradeDb(Global.getDatabaseVersion());
201                 }
202                 if (!Global.getDatabaseVersion().equals("0.95")) {
203                         upgradeDb(Global.getDatabaseVersion());
204                 }
205         }
206         
207
208         public void backupDatabase(int highSequence, long date) {
209                 
210         }
211         
212         
213         public void createTables() {
214                 Global.setDatabaseVersion("0.85");
215                 Global.setAutomaticLogin(false);
216                 Global.saveCurrentNoteGuid("");
217                 Global.saveUploadAmount(0);
218                 
219                 getTagTable().createTable();
220                 notebookTable.createTable(true);
221                 noteTable.createTable();
222                 deletedTable.createTable();             
223                 searchTable.createTable();
224                 watchFolderTable.createTable();
225                 invalidXMLTable.createTable();
226                 wordsTable.createTable();
227                 syncTable.createTable();
228         }
229         
230         public Connection getConnection() {
231                 return conn;
232         }
233         
234         //***************************************************************
235         //* Table get methods
236         //***************************************************************
237         public DeletedTable getDeletedTable() {
238                 return deletedTable;
239         }
240         public TagTable getTagTable() {
241                 return tagTable;
242         }
243         public NoteTable getNoteTable() {
244                 return noteTable;
245         }
246         public NotebookTable getNotebookTable() {
247                 return notebookTable;
248         }
249         public SavedSearchTable getSavedSearchTable() {
250                 return searchTable;
251         }
252         public WatchFolderTable getWatchFolderTable() {
253                 return watchFolderTable;
254         }
255         public WordsTable getWordsTable() {
256                 return wordsTable;
257         }
258         public InvalidXMLTable getInvalidXMLTable() {
259                 return invalidXMLTable;
260         }
261         public SyncTable getSyncTable() {
262                 return syncTable;
263         }
264         public LinkedNotebookTable getLinkedNotebookTable() {
265                 return linkedNotebookTable;
266         }
267         public SharedNotebookTable getSharedNotebookTable() {
268                 return sharedNotebookTable;
269         }
270         public SystemIconTable getSystemIconTable() {
271                 return systemIconTable;
272         }
273         public InkImagesTable getInkImagesTable() {
274                 return inkImagesTable;
275         }
276
277 }