OSDN Git Service

Corrected problem where a null pointer exception would happen if the database did...
[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 SyncTable                                       syncTable;
44         private final ApplicationLogger         logger;
45         private Connection                                      conn;
46         int id;
47
48         
49         public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword) {
50                 logger = l;
51                 dbSetup(url, userid, password, cypherPassword);
52         }
53         
54         private void setupTables() {
55                 tagTable = new TagTable(logger, this);
56                 notebookTable = new NotebookTable(logger, this);
57                 noteTable = new NoteTable(logger, this);
58                 deletedTable = new DeletedTable(logger, this);
59                 searchTable = new SavedSearchTable(logger, this);       
60                 watchFolderTable = new WatchFolderTable(logger, this);
61                 invalidXMLTable = new InvalidXMLTable(logger, this);
62                 wordsTable = new WordsTable(logger, this);
63                 syncTable = new SyncTable(logger, this);
64         }
65         
66         
67         // Compact the database
68         public void compactDatabase() {
69                 
70         }
71         
72         // Initialize the database connection
73         public void dbSetup(String url,String userid, String userPassword, String cypherPassword) {
74                 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
75
76                 
77                 try {
78                         Class.forName("org.h2.Driver");
79                 } catch (ClassNotFoundException e1) {
80                         e1.printStackTrace();
81                         System.exit(16);
82                 }
83                 
84                 QJdbc.initialize();
85                 
86                 setupTables();
87                 
88                 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
89                 boolean dbExists = f.exists(); 
90                 
91                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
92                 
93
94                 try {
95                         String passwordString = null;
96                         if (cypherPassword==null || cypherPassword.trim().equals(""))
97                                 passwordString = userPassword;
98                         else
99                                 passwordString = cypherPassword+" "+userPassword;
100                         conn = DriverManager.getConnection(url,userid,passwordString);
101 //                      conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
102                 } catch (SQLException e) {
103                         e.printStackTrace();
104                         return;
105                 }
106                 
107                 // If it doesn't exist and we are the main thread, then we need to create stuff.
108                 if (!dbExists)  {
109                         createTables();
110                         Global.setAutomaticLogin(false);
111                 }               
112                 
113                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
114         }
115         
116         
117         public void dbShutdown() {
118                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
119                 try {
120                         conn.close();
121                 } catch (SQLException e) {
122                         e.printStackTrace();
123                 }
124                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
125         }
126         
127         public void upgradeDb(String version) {
128                 if (version.equals("0.85")) {
129                         executeSql("alter table note add column titleColor integer");
130                         executeSql("alter table note add column thumbnail blob");
131                         executeSql("alter table note add column thumbnailneeded boolean");
132                         executeSql("Update note set thumbnailneeded = true;");
133                         executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
134                         executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
135                         version = "0.86";
136                         Global.setDatabaseVersion(version);
137                 } 
138         }
139         
140         public void executeSql(String sql) {
141                 NSqlQuery query = new NSqlQuery(conn);
142                 query.exec(sql);        
143         }
144         
145         public void checkDatabaseVersion() {
146                 if (!Global.getDatabaseVersion().equals("0.86")) {
147                         upgradeDb(Global.getDatabaseVersion());
148                 }
149         }
150         
151
152         public void backupDatabase(int highSequence, long date) {
153                 
154         }
155         
156         
157         public void createTables() {
158                 Global.setDatabaseVersion("0.85");
159                 Global.setAutomaticLogin(false);
160                 Global.saveCurrentNoteGuid("");
161                 Global.saveUploadAmount(0);
162                 
163                 getTagTable().createTable();
164                 notebookTable.createTable();
165                 noteTable.createTable();
166                 deletedTable.createTable();             
167                 searchTable.createTable();
168                 watchFolderTable.createTable();
169                 invalidXMLTable.createTable();
170                 wordsTable.createTable();
171                 syncTable.createTable();
172                 
173         }
174         
175         public Connection getConnection() {
176                 return conn;
177         }
178         
179         //***************************************************************
180         //* Table get methods
181         //***************************************************************
182         public DeletedTable getDeletedTable() {
183                 return deletedTable;
184         }
185         public TagTable getTagTable() {
186                 return tagTable;
187         }
188         public NoteTable getNoteTable() {
189                 return noteTable;
190         }
191         public NotebookTable getNotebookTable() {
192                 return notebookTable;
193         }
194         public SavedSearchTable getSavedSearchTable() {
195                 return searchTable;
196         }
197         public WatchFolderTable getWatchFolderTable() {
198                 return watchFolderTable;
199         }
200         public WordsTable getWordsTable() {
201                 return wordsTable;
202         }
203         public InvalidXMLTable getInvalidXMLTable() {
204                 return invalidXMLTable;
205         }
206         public SyncTable getSyncTable() {
207                 return syncTable;
208         }
209 }