OSDN Git Service

There are multiple changes with this commit.
[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                 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
87                 boolean dbExists = f.exists(); 
88                 
89                 // If it doesn't exist and we are the main thread, then we need to create stuff.
90                 if (!dbExists)  {
91                         createTables();
92                         Global.setAutomaticLogin(false);
93                 }
94                 setupTables();
95                 
96                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
97                 
98
99                 try {
100                         String passwordString = null;
101                         if (cypherPassword==null || cypherPassword.trim().equals(""))
102                                 passwordString = userPassword;
103                         else
104                                 passwordString = cypherPassword+" "+userPassword;
105                         conn = DriverManager.getConnection(url,userid,passwordString);
106 //                      conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
107                 } catch (SQLException e) {
108                         e.printStackTrace();
109                         return;
110                 }
111                 
112                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbSetup");
113                 
114                 
115                 
116                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
117         }
118         
119         
120         public void dbShutdown() {
121                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
122                 try {
123                         conn.close();
124                 } catch (SQLException e) {
125                         e.printStackTrace();
126                 }
127                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
128         }
129         
130         public void upgradeDb(String version) {
131                 if (version.equals("0.85")) {
132                         executeSql("alter table note add column titleColor integer");
133                         executeSql("alter table note add column thumbnail blob");
134                         executeSql("alter table note add column thumbnailneeded boolean");
135                         executeSql("Update note set thumbnailneeded = true;");
136                         executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
137                         executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
138                         version = "0.86";
139                         Global.setDatabaseVersion(version);
140                 } 
141         }
142         
143         public void executeSql(String sql) {
144                 NSqlQuery query = new NSqlQuery(conn);
145                 query.exec(sql);        
146         }
147         
148         public void checkDatabaseVersion() {
149                 if (!Global.getDatabaseVersion().equals("0.86")) {
150                         upgradeDb(Global.getDatabaseVersion());
151                 }
152         }
153         
154
155         public void backupDatabase(int highSequence, long date) {
156                 
157         }
158         
159         
160         public void createTables() {
161                 Global.setDatabaseVersion("0.85");
162                 Global.setAutomaticLogin(false);
163                 Global.saveCurrentNoteGuid("");
164                 Global.saveUploadAmount(0);
165                 
166                 getTagTable().createTable();
167                 notebookTable.createTable();
168                 noteTable.createTable();
169                 deletedTable.createTable();             
170                 searchTable.createTable();
171                 watchFolderTable.createTable();
172                 invalidXMLTable.createTable();
173                 wordsTable.createTable();
174                 syncTable.createTable();
175                 
176         }
177         
178         public Connection getConnection() {
179                 return conn;
180         }
181         
182         //***************************************************************
183         //* Table get methods
184         //***************************************************************
185         public DeletedTable getDeletedTable() {
186                 return deletedTable;
187         }
188         public TagTable getTagTable() {
189                 return tagTable;
190         }
191         public NoteTable getNoteTable() {
192                 return noteTable;
193         }
194         public NotebookTable getNotebookTable() {
195                 return notebookTable;
196         }
197         public SavedSearchTable getSavedSearchTable() {
198                 return searchTable;
199         }
200         public WatchFolderTable getWatchFolderTable() {
201                 return watchFolderTable;
202         }
203         public WordsTable getWordsTable() {
204                 return wordsTable;
205         }
206         public InvalidXMLTable getInvalidXMLTable() {
207                 return invalidXMLTable;
208         }
209         public SyncTable getSyncTable() {
210                 return syncTable;
211         }
212 }