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 final ApplicationLogger logger;
47 private Connection conn;
51 public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword) {
53 dbSetup(url, userid, password, cypherPassword);
56 private void setupTables() {
57 tagTable = new TagTable(logger, this);
58 notebookTable = new NotebookTable(logger, this);
59 noteTable = new NoteTable(logger, this);
60 deletedTable = new DeletedTable(logger, this);
61 searchTable = new SavedSearchTable(logger, this);
62 watchFolderTable = new WatchFolderTable(logger, this);
63 invalidXMLTable = new InvalidXMLTable(logger, this);
64 wordsTable = new WordsTable(logger, this);
65 syncTable = new SyncTable(logger, this);
66 linkedNotebookTable = new LinkedNotebookTable(logger, this);
67 sharedNotebookTable = new SharedNotebookTable(logger, this);
71 // Compact the database
72 public void compactDatabase() {
76 // Initialize the database connection
77 public void dbSetup(String url,String userid, String userPassword, String cypherPassword) {
78 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
82 Class.forName("org.h2.Driver");
83 } catch (ClassNotFoundException e1) {
92 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
93 boolean dbExists = f.exists();
95 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
99 String passwordString = null;
100 if (cypherPassword==null || cypherPassword.trim().equals(""))
101 passwordString = userPassword;
103 passwordString = cypherPassword+" "+userPassword;
104 conn = DriverManager.getConnection(url,userid,passwordString);
105 // conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
106 } catch (SQLException e) {
111 // If it doesn't exist and we are the main thread, then we need to create stuff.
114 Global.setAutomaticLogin(false);
117 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
121 public void dbShutdown() {
122 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
125 } catch (SQLException e) {
128 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
131 public void upgradeDb(String version) {
132 if (version.equals("0.85")) {
133 executeSql("alter table note add column titleColor integer");
134 executeSql("alter table note add column thumbnail blob");
135 executeSql("alter table note add column thumbnailneeded boolean");
136 executeSql("Update note set thumbnailneeded = true;");
137 executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
138 executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
140 Global.setDatabaseVersion(version);
142 if (version.equals("0.86")) {
144 executeSql("alter table notebook add column publishingUri VarChar");
145 executeSql("alter table notebook add column publishingOrder Integer");
146 executeSql("alter table notebook add column publishingAscending VarChar");
147 executeSql("alter table notebook add column publishingPublicDescription varchar");
148 executeSql("alter table notebook add column stack varchar");
149 executeSql("alter table notebook add column icon blob");
150 executeSql("alter table tag add column icon blob");
151 executeSql("alter table SavedSearch add column icon blob");
153 executeSql("create index NOTE_THUMBNAIL_INDEX on note (thumbnailneeded, guid);");
154 executeSql("create index NOTE_EXPUNGED_INDEX on note (isExpunged, guid);");
155 executeSql("create index NOTE_DUEDATE_INDEX on note (attributeSubjectDate, guid);");
156 executeSql("create index RESOURCES_GUID_INDEX on noteresources (noteGuid, guid);");
157 executeSql("update note set thumbnailneeded=true, thumbnail=null;");
158 executeSql("update notebook set publishingUri='', " +
159 "publishingAscending='', stack='', publishingOrder=1, " +
160 "publishingPublicDescription=''");
162 sharedNotebookTable.createTable();
163 linkedNotebookTable.createTable();
166 executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
167 executeSql("Insert into Sync (key, value) values ('FullSharedNotebookSync', 'true')");
168 Global.setDatabaseVersion(version);
172 public void executeSql(String sql) {
173 NSqlQuery query = new NSqlQuery(conn);
177 public void checkDatabaseVersion() {
178 if (!Global.getDatabaseVersion().equals("0.86")) {
179 upgradeDb(Global.getDatabaseVersion());
181 if (!Global.getDatabaseVersion().equals("0.95")) {
182 upgradeDb(Global.getDatabaseVersion());
187 public void backupDatabase(int highSequence, long date) {
192 public void createTables() {
193 Global.setDatabaseVersion("0.85");
194 Global.setAutomaticLogin(false);
195 Global.saveCurrentNoteGuid("");
196 Global.saveUploadAmount(0);
198 getTagTable().createTable();
199 notebookTable.createTable();
200 noteTable.createTable();
201 deletedTable.createTable();
202 searchTable.createTable();
203 watchFolderTable.createTable();
204 invalidXMLTable.createTable();
205 wordsTable.createTable();
206 syncTable.createTable();
209 public Connection getConnection() {
213 //***************************************************************
214 //* Table get methods
215 //***************************************************************
216 public DeletedTable getDeletedTable() {
219 public TagTable getTagTable() {
222 public NoteTable getNoteTable() {
225 public NotebookTable getNotebookTable() {
226 return notebookTable;
228 public SavedSearchTable getSavedSearchTable() {
231 public WatchFolderTable getWatchFolderTable() {
232 return watchFolderTable;
234 public WordsTable getWordsTable() {
237 public InvalidXMLTable getInvalidXMLTable() {
238 return invalidXMLTable;
240 public SyncTable getSyncTable() {
243 public LinkedNotebookTable getLinkedNotebookTable() {
244 return linkedNotebookTable;
246 public SharedNotebookTable getSharedNotebookTable() {
247 return sharedNotebookTable;