2 * This file is part of NeverNote
3 * Copyright 2009 Randy Baumgarte
5 * This file may be licensed under the terms of of the
6 * GNU General Public License Version 2 (the ``GPL'').
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.
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.
19 package cx.fbn.nevernote.sql;
22 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
26 import com.trolltech.qt.sql.QJdbc;
28 import cx.fbn.nevernote.Global;
29 import cx.fbn.nevernote.sql.driver.NSqlQuery;
30 import cx.fbn.nevernote.utilities.ApplicationLogger;
33 public class DatabaseConnection {
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 SyncTable syncTable;
46 private SystemIconTable systemIconTable;
47 private final ApplicationLogger logger;
48 private Connection conn;
52 public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword) {
54 dbSetup(url, userid, password, cypherPassword);
57 private void setupTables() {
58 tagTable = new TagTable(logger, this);
59 notebookTable = new NotebookTable(logger, this);
60 noteTable = new NoteTable(logger, this);
61 deletedTable = new DeletedTable(logger, this);
62 searchTable = new SavedSearchTable(logger, this);
63 watchFolderTable = new WatchFolderTable(logger, this);
64 invalidXMLTable = new InvalidXMLTable(logger, this);
65 wordsTable = new WordsTable(logger, this);
66 syncTable = new SyncTable(logger, this);
67 linkedNotebookTable = new LinkedNotebookTable(logger, this);
68 sharedNotebookTable = new SharedNotebookTable(logger, this);
69 systemIconTable = new SystemIconTable(logger, this);
73 // Compact the database
74 public void compactDatabase() {
78 // Initialize the database connection
79 public void dbSetup(String url,String userid, String userPassword, String cypherPassword) {
80 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
84 Class.forName("org.h2.Driver");
85 } catch (ClassNotFoundException e1) {
94 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
95 boolean dbExists = f.exists();
97 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
101 String passwordString = null;
102 if (cypherPassword==null || cypherPassword.trim().equals(""))
103 passwordString = userPassword;
105 passwordString = cypherPassword+" "+userPassword;
106 conn = DriverManager.getConnection(url,userid,passwordString);
107 // conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
108 } catch (SQLException e) {
113 // If it doesn't exist and we are the main thread, then we need to create stuff.
116 Global.setAutomaticLogin(false);
119 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
123 public void dbShutdown() {
124 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
127 } catch (SQLException e) {
130 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
133 public void upgradeDb(String version) {
134 if (version.equals("0.85")) {
135 executeSql("alter table note add column titleColor integer");
136 executeSql("alter table note add column thumbnail blob");
137 executeSql("alter table note add column thumbnailneeded boolean");
138 executeSql("Update note set thumbnailneeded = true;");
139 executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
140 executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
142 Global.setDatabaseVersion(version);
144 if (version.equals("0.86")) {
146 executeSql("alter table notebook add column publishingUri VarChar");
147 executeSql("alter table notebook add column publishingOrder Integer");
148 executeSql("alter table notebook add column publishingAscending Boolean");
149 executeSql("alter table notebook add column publishingPublicDescription varchar");
150 executeSql("alter table notebook add column stack varchar");
151 executeSql("alter table notebook add column icon blob");
152 executeSql("alter table tag add column icon blob");
153 executeSql("alter table SavedSearch add column icon blob");
155 executeSql("create index NOTE_THUMBNAIL_INDEX on note (thumbnailneeded, guid);");
156 executeSql("create index NOTE_EXPUNGED_INDEX on note (isExpunged, guid);");
157 executeSql("create index NOTE_DUEDATE_INDEX on note (attributeSubjectDate, guid);");
158 executeSql("create index RESOURCES_GUID_INDEX on noteresources (noteGuid, guid);");
159 executeSql("update note set thumbnailneeded=true, thumbnail=null;");
160 executeSql("update notebook set publishingUri='', " +
161 "publishingAscending=false, stack='', publishingOrder=1, " +
162 "publishingPublicDescription=''");
164 sharedNotebookTable.createTable();
165 linkedNotebookTable.createTable();
166 systemIconTable.createTable();
169 executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
170 executeSql("Insert into Sync (key, value) values ('FullSharedNotebookSync', 'true')");
171 Global.setDatabaseVersion(version);
175 public void executeSql(String sql) {
176 NSqlQuery query = new NSqlQuery(conn);
180 public void checkDatabaseVersion() {
181 if (!Global.getDatabaseVersion().equals("0.86")) {
182 upgradeDb(Global.getDatabaseVersion());
184 if (!Global.getDatabaseVersion().equals("0.95")) {
185 upgradeDb(Global.getDatabaseVersion());
190 public void backupDatabase(int highSequence, long date) {
195 public void createTables() {
196 Global.setDatabaseVersion("0.85");
197 Global.setAutomaticLogin(false);
198 Global.saveCurrentNoteGuid("");
199 Global.saveUploadAmount(0);
201 getTagTable().createTable();
202 notebookTable.createTable();
203 noteTable.createTable();
204 deletedTable.createTable();
205 searchTable.createTable();
206 watchFolderTable.createTable();
207 invalidXMLTable.createTable();
208 wordsTable.createTable();
209 syncTable.createTable();
212 public Connection getConnection() {
216 //***************************************************************
217 //* Table get methods
218 //***************************************************************
219 public DeletedTable getDeletedTable() {
222 public TagTable getTagTable() {
225 public NoteTable getNoteTable() {
228 public NotebookTable getNotebookTable() {
229 return notebookTable;
231 public SavedSearchTable getSavedSearchTable() {
234 public WatchFolderTable getWatchFolderTable() {
235 return watchFolderTable;
237 public WordsTable getWordsTable() {
240 public InvalidXMLTable getInvalidXMLTable() {
241 return invalidXMLTable;
243 public SyncTable getSyncTable() {
246 public LinkedNotebookTable getLinkedNotebookTable() {
247 return linkedNotebookTable;
249 public SharedNotebookTable getSharedNotebookTable() {
250 return sharedNotebookTable;
252 public SystemIconTable getSystemIconTable() {
253 return systemIconTable;