OSDN Git Service

Correct java cast exceptions when doing a getUserInformation.
[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 cx.fbn.nevernote.Global;
27 import cx.fbn.nevernote.sql.driver.NSqlQuery;
28 import cx.fbn.nevernote.utilities.ApplicationLogger;
29
30
31 public class DatabaseConnection {
32         // Table helpers
33         private WordsTable                                      wordsTable;
34         private TagTable                                        tagTable;
35         private NotebookTable                           notebookTable;
36         private NoteTable                                       noteTable;
37         private DeletedTable                            deletedTable;
38         private SavedSearchTable                        searchTable;
39         private WatchFolderTable                        watchFolderTable;
40         private InvalidXMLTable                         invalidXMLTable;
41         private LinkedNotebookTable                     linkedNotebookTable;
42         private SharedNotebookTable                     sharedNotebookTable;
43         private InkImagesTable                          inkImagesTable;
44         private SyncTable                                       syncTable;
45         private SystemIconTable                         systemIconTable;
46         private final ApplicationLogger         logger;
47         private Connection                                      conn;
48         private Connection                                      indexConn;
49         private Connection                                      resourceConn;
50         int throttle;
51         int id;
52
53         
54         public DatabaseConnection(ApplicationLogger l, String url, String iurl, String rurl, String userid, String password, String cypherPassword, int throttle) {
55                 logger = l;
56                 this.throttle = throttle;
57                 dbSetup(url, iurl, rurl, userid, password, cypherPassword);
58         }
59         
60         private void setupTables() {
61                 tagTable = new TagTable(logger, this);
62                 notebookTable = new NotebookTable(logger, this);
63                 noteTable = new NoteTable(logger, this);
64                 deletedTable = new DeletedTable(logger, this);
65                 searchTable = new SavedSearchTable(logger, this);       
66                 watchFolderTable = new WatchFolderTable(logger, this);
67                 invalidXMLTable = new InvalidXMLTable(logger, this);
68                 wordsTable = new WordsTable(logger, this);
69                 syncTable = new SyncTable(logger, this);
70                 linkedNotebookTable = new LinkedNotebookTable(logger, this);
71                 sharedNotebookTable = new SharedNotebookTable(logger, this);
72                 systemIconTable = new SystemIconTable(logger, this);
73                 inkImagesTable = new InkImagesTable(logger, this);
74         }
75         
76         
77         // Compact the database
78         public void compactDatabase() {
79                 
80         }
81         
82         // Initialize the database connection
83         public void dbSetup(String url,String indexUrl, String resourceUrl, String userid, String userPassword, String cypherPassword) {
84                 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
85
86                 
87                 try {
88                         Class.forName("org.h2.Driver");
89                 } catch (ClassNotFoundException e1) {
90                         e1.printStackTrace();
91                         System.exit(16);
92                 }
93                 
94 //              QJdbc.initialize();
95                 
96                 setupTables();
97                 
98                 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
99                 boolean dbExists = f.exists(); 
100                 f = Global.getFileManager().getDbDirFile(Global.indexDatabaseName + ".h2.db");
101                 boolean indexDbExists = f.exists(); 
102                 f = Global.getFileManager().getDbDirFile(Global.resourceDatabaseName + ".h2.db");
103                 boolean resourceDbExists = f.exists();
104                 
105                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
106                 
107                 String passwordString = null;
108                 try {
109                         
110                         if (cypherPassword==null || cypherPassword.trim().equals(""))
111                                 passwordString = userPassword;
112                         else
113                                 passwordString = cypherPassword+" "+userPassword;
114 //                      conn = DriverManager.getConnection(url,userid,passwordString);
115 //                      conn = DriverManager.getConnection(url,userid,passwordString);
116 //                      conn = DriverManager.getConnection(url+";CACHE_SIZE=4096",userid,passwordString);
117                         if (throttle == 0) {
118                                 conn = DriverManager.getConnection(url+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
119                         } else {
120                                 conn = DriverManager.getConnection(url+";THROTTLE=" +new Integer(throttle).toString()+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
121                         }
122                         indexConn = DriverManager.getConnection(indexUrl,userid,passwordString);
123                         resourceConn = DriverManager.getConnection(resourceUrl,userid,passwordString);
124 //                      conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
125                 } catch (SQLException e) {
126                         e.printStackTrace();
127                         return;
128                 }
129                 
130                 // If it doesn't exist and we are the main thread, then we need to create stuff.
131                 if (!dbExists)  {
132                         createTables();
133                         Global.setAutomaticLogin(false);
134                 }               
135                 if (!resourceDbExists) {
136                         createResourceTables();
137                         if (dbTableExists("NoteResources")) {
138                                 // Begin migration of database
139                                 NSqlQuery query = new NSqlQuery(resourceConn);
140                                 String linkcmd = "create linked table oldnoteresources "+
141                                                 "('org.h2.Driver', '"+url+"', '"+userid+"', '"+passwordString+"', 'NoteResources')";
142                                 query.exec(linkcmd);
143                                 query.exec("insert into noteresources (select * from oldnoteresources)");
144                                 query.exec("Drop table oldnoteresources;");
145                                 query.exec("Update noteresources set indexneeded='true'");
146                                 
147                         }
148                 }
149                 if (!indexDbExists)  {
150                         createIndexTables();
151                         executeSql("Update note set indexneeded='true'");
152                 }
153                 
154                 // Drop the note resource table & re-connect it in case it was recently encrypted.
155 /*              NSqlQuery query = new NSqlQuery(conn);
156                 query.exec("drop table NoteResources;");
157                 String linkcmd = "create linked table NoteResources "
158                         +"('org.h2.Driver', '"+url+"', '"+userid+"', '"+passwordString+ "', 'NoteResources')";
159                 query.exec(linkcmd);
160                 System.err.println(query.lastError());
161 */              
162                 
163                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
164         }
165         
166         
167         public void dbShutdown() {
168                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
169                 try {
170                         conn.close();
171                 } catch (SQLException e) {
172                         e.printStackTrace();
173                 }
174                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
175         }
176         
177         public void upgradeDb(String version) {
178                 if (version.equals("0.85")) {
179                         executeSql("alter table note add column titleColor integer");
180                         executeSql("alter table note add column thumbnail blob");
181                         executeSql("alter table note add column thumbnailneeded boolean");
182                         executeSql("Update note set thumbnailneeded = true;");
183                         executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
184                         executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
185                         version = "0.86";
186                         Global.setDatabaseVersion(version);
187                 } 
188                 if (version.equals("0.86")) {
189         
190                         executeSql("alter table notebook add column publishingUri VarChar");
191                         executeSql("alter table notebook add column publishingOrder Integer");
192                         executeSql("alter table notebook add column publishingAscending Boolean");
193                         executeSql("alter table notebook add column publishingPublicDescription varchar");
194                         executeSql("alter table notebook add column stack varchar");
195                         executeSql("alter table notebook add column icon blob");
196                         executeSql("alter table notebook add column readOnly boolean");
197                         executeSql("alter table notebook add column linked boolean");
198                         
199                         executeSql("alter table tag add column realname varchar");
200                         executeSql("alter table tag add column linked boolean");
201                         executeSql("alter table tag add column icon blob");
202                         executeSql("alter table tag add column notebookguid varchar");
203                         executeSql("alter table SavedSearch add column icon blob");
204
205                         executeSql("create index NOTE_THUMBNAIL_INDEX on note (thumbnailneeded, guid);");
206                         executeSql("create index NOTE_EXPUNGED_INDEX on note (isExpunged, guid);");
207                         executeSql("create index NOTE_DUEDATE_INDEX on note (attributeSubjectDate, guid);");
208                         executeSql("create index TAG_NOTEBOOK_INDEX on tag (notebookGuid);");
209                         
210                         executeSql("update note set thumbnailneeded=true, thumbnail=null;");
211                         executeSql("update notebook set publishingUri='', " +
212                                         "publishingAscending=false, stack='', readonly=false, publishingOrder=1, " +
213                                         "publishingPublicDescription='', linked=false");
214                         executeSql("update tag set linked=false, realname='', notebookguid=''");
215                         
216                         sharedNotebookTable.createTable();
217                         linkedNotebookTable.createTable();
218                         systemIconTable.createTable();
219                         inkImagesTable.createTable();
220                         
221                         version = "0.95";
222                         executeSql("Insert into Sync (key, value) values ('FullNotebookSync', 'true')");
223                         executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
224                         executeSql("Insert into Sync (key, value) values ('FullSharedNotebookSync', 'true')");
225                         executeSql("Insert into Sync (key, value) values ('FullInkNoteImageSync', 'true')");
226                         Global.setDatabaseVersion(version);
227                 } 
228                 if (version.equals("0.95")) {
229                         if (dbTableExists("words"))
230                                 executeSql("Drop table words;");
231                         if (dbTableExists("NoteResources"))
232                                 executeSql("Drop table NoteResources;");
233                 }
234                 if (!dbTableColumnExists("NOTE", "ORIGINAL_GUID")) {
235                         executeSql("alter table note add column ORIGINAL_GUID VarChar");
236                         executeSql("create index NOTE_ORIGINAL_GUID_INDEX on note (original_guid, guid);");
237                 }
238
239                 
240         }
241         
242         public void executeSql(String sql) {
243                 NSqlQuery query = new NSqlQuery(conn);
244                 query.exec(sql);        
245         }
246         
247         public void checkDatabaseVersion() {
248                 if (!Global.getDatabaseVersion().equals("0.86")) {
249                         upgradeDb(Global.getDatabaseVersion());
250                 }
251                 if (!Global.getDatabaseVersion().equals("0.95")) {
252                         upgradeDb(Global.getDatabaseVersion());
253                 }
254                 if (!Global.getDatabaseVersion().equals("0.97")) {
255                         upgradeDb(Global.getDatabaseVersion());
256                 }
257         }
258         
259
260         public void backupDatabase(int highSequence, long date) {
261                 
262         }
263         
264         
265         public void createTables() {
266                 Global.setDatabaseVersion("0.85");
267                 Global.setAutomaticLogin(false);
268                 Global.saveCurrentNoteGuid("");
269                 Global.saveUploadAmount(0);
270                 
271                 getTagTable().createTable();
272                 notebookTable.createTable(true);
273                 noteTable.createTable();
274                 deletedTable.createTable();             
275                 searchTable.createTable();
276                 watchFolderTable.createTable();
277                 invalidXMLTable.createTable();
278                 syncTable.createTable();
279         }
280         
281         public void createIndexTables() {
282                 wordsTable.createTable();
283         }
284         
285         public void createResourceTables() {
286                 noteTable.noteResourceTable.createTable();
287         }
288         
289         public Connection getConnection() {
290                 return conn;
291         }
292         public Connection getIndexConnection() {
293                 return  indexConn;
294         }
295         public Connection getResourceConnection() {
296                 return resourceConn;
297         }
298         
299         //***************************************************************
300         //* Table get methods
301         //***************************************************************
302         public DeletedTable getDeletedTable() {
303                 return deletedTable;
304         }
305         public TagTable getTagTable() {
306                 return tagTable;
307         }
308         public NoteTable getNoteTable() {
309                 return noteTable;
310         }
311         public NotebookTable getNotebookTable() {
312                 return notebookTable;
313         }
314         public SavedSearchTable getSavedSearchTable() {
315                 return searchTable;
316         }
317         public WatchFolderTable getWatchFolderTable() {
318                 return watchFolderTable;
319         }
320         public WordsTable getWordsTable() {
321                 return wordsTable;
322         }
323         public InvalidXMLTable getInvalidXMLTable() {
324                 return invalidXMLTable;
325         }
326         public SyncTable getSyncTable() {
327                 return syncTable;
328         }
329         public LinkedNotebookTable getLinkedNotebookTable() {
330                 return linkedNotebookTable;
331         }
332         public SharedNotebookTable getSharedNotebookTable() {
333                 return sharedNotebookTable;
334         }
335         public SystemIconTable getSystemIconTable() {
336                 return systemIconTable;
337         }
338         public InkImagesTable getInkImagesTable() {
339                 return inkImagesTable;
340         }
341
342         //****************************************************************
343         //* Begin/End transactions
344         //****************************************************************
345         public void beginTransaction() {
346                 commitTransaction();
347         NSqlQuery query = new NSqlQuery(getConnection());                                                       
348                 if (!query.exec("Begin Transaction"))
349                         logger.log(logger.EXTREME, "Begin transaction has failed: " +query.lastError());
350
351         }
352         public void commitTransaction() {
353         NSqlQuery query = new NSqlQuery(getConnection());
354                                                         
355                 if (!query.exec("Commit"))
356                         logger.log(logger.EXTREME, "Transaction commit has failed: " +query.lastError());
357         }
358
359         //****************************************************************
360         //* Check if a table exists
361         //****************************************************************
362         public boolean dbTableExists(String name) {
363         NSqlQuery query = new NSqlQuery(getConnection());
364         query.prepare("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME=:name");
365         query.bindValue(":name", name.toUpperCase());
366         query.exec();
367         if (query.next())
368                 return true;
369         else
370                 return false;
371         }
372         
373         //****************************************************************
374         //* Check if a row in a table exists
375         //****************************************************************
376         public boolean dbTableColumnExists(String tableName, String columnName) {
377         NSqlQuery query = new NSqlQuery(getConnection());
378         query.prepare("select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=:name and COLUMN_NAME=:column");
379         query.bindValue(":name", tableName.toUpperCase());
380         query.bindValue(":column", columnName);
381         query.exec();
382         if (query.next())
383                 return true;
384         else
385                 return false;
386         }
387 }