X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=src%2Fcx%2Ffbn%2Fnevernote%2Fsql%2FDatabaseConnection.java;h=03dd230cb3c4e71bb9e831f980b355107b3eb3b8;hb=5b36878624c21cdd8df75cc3775c818b2c5506a3;hp=d117f5ae44fb90df87c745aa76696193fba50b81;hpb=d3224704f464d7b6bc601565d276184ee8e722bc;p=neighbornote%2FNeighborNote.git diff --git a/src/cx/fbn/nevernote/sql/DatabaseConnection.java b/src/cx/fbn/nevernote/sql/DatabaseConnection.java index d117f5a..03dd230 100644 --- a/src/cx/fbn/nevernote/sql/DatabaseConnection.java +++ b/src/cx/fbn/nevernote/sql/DatabaseConnection.java @@ -1,6 +1,7 @@ /* - * This file is part of NeverNote + * This file is part of NixNote/NeighborNote * Copyright 2009 Randy Baumgarte + * Copyright 2013 Yuki Takahashi * * This file may be licensed under the terms of of the * GNU General Public License Version 2 (the ``GPL''). @@ -43,18 +44,23 @@ public class DatabaseConnection { private InkImagesTable inkImagesTable; private SyncTable syncTable; private SystemIconTable systemIconTable; + private HistoryTable historyTable; + private ExcludedTable excludedTable; + private StaredTable staredTable; + private final ApplicationLogger logger; private Connection conn; private Connection indexConn; private Connection resourceConn; + private Connection behaviorConn; + int throttle; int id; - - public DatabaseConnection(ApplicationLogger l, String url, String iurl, String rurl, String userid, String password, String cypherPassword, int throttle) { + public DatabaseConnection(ApplicationLogger l, String url, String iurl, String rurl, String burl, String userid, String password, String cypherPassword, int throttle) { logger = l; this.throttle = throttle; - dbSetup(url, iurl, rurl, userid, password, cypherPassword); + dbSetup(url, iurl, rurl, burl, userid, password, cypherPassword); } private void setupTables() { @@ -71,6 +77,10 @@ public class DatabaseConnection { sharedNotebookTable = new SharedNotebookTable(logger, this); systemIconTable = new SystemIconTable(logger, this); inkImagesTable = new InkImagesTable(logger, this); + historyTable = new HistoryTable(logger, this); + excludedTable = new ExcludedTable(logger, this); + staredTable = new StaredTable(logger, this); + } @@ -80,7 +90,7 @@ public class DatabaseConnection { } // Initialize the database connection - public void dbSetup(String url,String indexUrl, String resourceUrl, String userid, String userPassword, String cypherPassword) { + public void dbSetup(String url,String indexUrl, String resourceUrl, String behaviorUrl, String userid, String userPassword, String cypherPassword) { logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id); @@ -101,6 +111,8 @@ public class DatabaseConnection { boolean indexDbExists = f.exists(); f = Global.getFileManager().getDbDirFile(Global.resourceDatabaseName + ".h2.db"); boolean resourceDbExists = f.exists(); + f = Global.getFileManager().getDbDirFile(Global.behaviorDatabaseName + ".h2.db"); + boolean behaviorDbExists = f.exists(); logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup"); @@ -121,6 +133,8 @@ public class DatabaseConnection { } indexConn = DriverManager.getConnection(indexUrl,userid,passwordString); resourceConn = DriverManager.getConnection(resourceUrl,userid,passwordString); + behaviorConn = DriverManager.getConnection(behaviorUrl, userid, passwordString); + // conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString); } catch (SQLException e) { e.printStackTrace(); @@ -151,14 +165,25 @@ public class DatabaseConnection { executeSql("Update note set indexneeded='true'"); } - // Drop the note resource table & re-connect it in case it was recently encrypted. -/* NSqlQuery query = new NSqlQuery(conn); - query.exec("drop table NoteResources;"); - String linkcmd = "create linked table NoteResources " - +"('org.h2.Driver', '"+url+"', '"+userid+"', '"+passwordString+ "', 'NoteResources')"; - query.exec(linkcmd); - System.err.println(query.lastError()); -*/ + // 操作履歴テーブルと除外ノートテーブルとスター付きノートテーブルを作る + if (!behaviorDbExists) { + createHistoryTables(); + createExcludedTables(); + createStaredTables(); + } + + // If we encrypted/decrypted it the last time, we need to reconnect the tables. +// if (Global.relinkTables) { +// NSqlQuery query = new NSqlQuery(conn); +// query.exec("Drop table NoteResources;"); +// String linkcmd = "create linked table NoteResources " +// +"('org.h2.Driver', '"+url+"', '"+userid+"', '"+passwordString+ "', 'NoteResources')"; +// System.out.println(linkcmd); +// query.exec(linkcmd); +// System.err.println(query.lastError()); +// Global.relinkTables = false; +// } + logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id); } @@ -235,8 +260,72 @@ public class DatabaseConnection { executeSql("alter table note add column ORIGINAL_GUID VarChar"); executeSql("create index NOTE_ORIGINAL_GUID_INDEX on note (original_guid, guid);"); } + if (!dbTableColumnExists("NOTEBOOK", "NARROW_SORT_ORDER")) { + executeSql("alter table notebook add column NARROW_SORT_ORDER integer"); + executeSql("update notebook set NARROW_SORT_ORDER = -1"); + executeSql("alter table notebook add column WIDE_SORT_ORDER integer"); + executeSql("update notebook set WIDE_SORT_ORDER = -1"); + + executeSql("alter table notebook add column WIDE_SORT_COLUMN integer"); + executeSql("update notebook set WIDE_SORT_COLUMN = -1"); + + executeSql("alter table notebook add column NARROW_SORT_COLUMN integer"); + executeSql("update notebook set NARROW_SORT_COLUMN = -1"); + } + if (!dbTableColumnExists("NOTE", "PINNED")) { + executeSql("alter table note add column pinned integer"); + executeSql("update note set pinned = 0"); + } + if (!dbTableColumnExists("NOTE", "ATTRIBUTECONTENTCLASS")) { + executeSql("alter table note add column attributeContentClass VarChar"); + executeSql("update note set attributeContentClass = ''"); + } + + // Evernoteサムネイルカラムを追加 + if (!dbTableColumnExists("NOTE", "ENTHUMBNAIL")) { + executeSql("alter table note add column enThumbNail Blob"); + } + + // Apache Luceneを使った日本語検索のためのプレーンテキストノートコンテンツカラムを準備 + if (!dbTableColumnExists("NOTE", "CONTENTTEXT")) { + executeSql("alter table note add column contentText VarChar"); + executeSql("update note set contentText = ''"); + NSqlQuery query = new NSqlQuery(conn); + query.exec("Select guid, content from Note where contentText = ''"); + while (query.next()) { + String guid = query.valueString(0); + String content = query.valueString(1); + String contentText = Global.extractPlainText(content); + NSqlQuery query2 = new NSqlQuery(conn); + query2.prepare("update note set contentText=:contentText where guid=:guid"); + query2.bindValue(":contentText", contentText); + query2.bindValue(":guid", guid); + query2.exec(); + } + + // Apache Luceneを使った全文検索のための準備 + query.exec("CREATE ALIAS IF NOT EXISTS FTL_INIT FOR \"org.h2.fulltext.FullTextLuceneEx.init\""); + query.exec("CALL FTL_INIT()"); + + Global.rebuildFullTextNoteTarget(this); + } + + // Apache Luceneを使った日本語検索のためのプレーンテキストノートリソースカラムを準備 + NSqlQuery rQuery = new NSqlQuery(resourceConn); + rQuery.exec("select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='NOTERESOURCES' and COLUMN_NAME='RESOURCETEXT'"); + if (!rQuery.next()) { + rQuery.exec("alter table noteResources add column resourceText VarChar"); + rQuery.exec("update noteResources set resourceText = ''"); + + // Apache Luceneを使った全文検索のための準備 + rQuery.exec("CREATE ALIAS IF NOT EXISTS FTL_INIT FOR \"org.h2.fulltext.FullTextLuceneEx.init\""); + rQuery.exec("CALL FTL_INIT()"); + + Global.rebuildFullTextResourceTarget(this); + } + // 注意:ここから先でnoteテーブルとnoteResourcesテーブルの構造を変更するな。全文検索ができなくなる。 } public void executeSql(String sql) { @@ -286,6 +375,18 @@ public class DatabaseConnection { noteTable.noteResourceTable.createTable(); } + public void createHistoryTables() { + historyTable.createTable(); + } + + public void createExcludedTables() { + excludedTable.createTable(); + } + + public void createStaredTables() { + staredTable.createTable(); + } + public Connection getConnection() { return conn; } @@ -296,6 +397,10 @@ public class DatabaseConnection { return resourceConn; } + public Connection getBehaviorConnection() { + return behaviorConn; + } + //*************************************************************** //* Table get methods //*************************************************************** @@ -338,6 +443,18 @@ public class DatabaseConnection { public InkImagesTable getInkImagesTable() { return inkImagesTable; } + + public HistoryTable getHistoryTable() { + return historyTable; + } + + public ExcludedTable getExcludedTable() { + return excludedTable; + } + + public StaredTable getStaredTable() { + return staredTable; + } //**************************************************************** //* Begin/End transactions