2 * This file is part of NeighborNote
3 * Copyright 2013 Yuki Takahashi
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.
21 package cx.fbn.nevernote.sql;
23 import cx.fbn.nevernote.sql.driver.NSqlQuery;
24 import cx.fbn.nevernote.utilities.ApplicationLogger;
26 public class ExcludedTable {
27 private final ApplicationLogger logger;
28 private final DatabaseConnection db;
31 public ExcludedTable(ApplicationLogger l, DatabaseConnection d) {
37 public void createTable() {
38 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
39 logger.log(logger.HIGH, "ExcludedNotesテーブルを作成しています...");
40 if (!query.exec("Create table ExcludedNotes (id integer primary key auto_increment, guid1 varchar, guid2 varchar)"))
41 logger.log(logger.HIGH, "ExcludedNotesテーブル作成失敗!!!");
45 public void dropTable() {
46 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
47 query.exec("Drop table ExcludedNotes");
50 // ExcludedNotesテーブルにアイテムを1つ追加
51 public void addExclusion(String guid1, String guid2) {
52 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
53 query.prepare("Insert Into ExcludedNotes (guid1, guid2) Values(:guid1, :guid2)");
54 query.bindValue(":guid1", guid1);
55 query.bindValue(":guid2", guid2);
57 logger.log(logger.MEDIUM, "ExcludedNotesテーブルへのアイテム追加に失敗");
58 logger.log(logger.MEDIUM, query.lastError());
62 // masterGuidとchildGuidをマージ
63 public void mergeHistoryGuid(String masterGuid, String childGuid) {
64 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
65 boolean check = false;
67 // マージ後に重複してしまうデータを先に削除
68 excludedNotesQuery.prepare("Delete from ExcludedNotes where (guid1=:oldGuid1 and guid2=:newGuid1) or (guid1=:newGuid2 and guid2=:oldGuid2)");
69 excludedNotesQuery.bindValue(":oldGuid1", masterGuid);
70 excludedNotesQuery.bindValue(":newGuid1", childGuid);
71 excludedNotesQuery.bindValue(":oldGuid2", masterGuid);
72 excludedNotesQuery.bindValue(":newGuid2", childGuid);
73 check = excludedNotesQuery.exec();
75 logger.log(logger.MEDIUM, "excludedNotesテーブルの重複削除で失敗");
76 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
79 updateExcludedNoteGuid(masterGuid, childGuid);
82 // ExcludedNotesテーブルのGuidを更新
83 public void updateExcludedNoteGuid(String newGuid, String oldGuid){
84 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
85 boolean check = false;
87 excludedNotesQuery.prepare("Update ExcludedNotes set guid1=:newGuid where guid1=:oldGuid");
88 excludedNotesQuery.bindValue(":newGuid", newGuid);
89 excludedNotesQuery.bindValue(":oldGuid", oldGuid);
90 check = excludedNotesQuery.exec();
92 logger.log(logger.MEDIUM, "ExcludedNotesテーブルのguid1のところでguid更新失敗");
93 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
95 excludedNotesQuery.prepare("Update ExcludedNotes set guid2=:newGuid where guid2=:oldGuid");
96 excludedNotesQuery.bindValue(":newGuid", newGuid);
97 excludedNotesQuery.bindValue(":oldGuid", oldGuid);
98 check = excludedNotesQuery.exec();
100 logger.log(logger.MEDIUM, "ExcludedNotesテーブルのguid2のところでguid更新失敗");
101 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
105 // ExcludedNotesテーブルに引数guidのノートが存在するか
106 public boolean existNote(String guid1, String guid2) {
107 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
109 // 2つの引数guidを含むアイテムの存在確認
110 excludedNotesQuery.prepare("Select * from ExcludedNotes where Exists(Select * from ExcludedNotes where (guid1=:guid1_1 and guid2=:guid2_1) or (guid1=:guid2_2 and guid2=:guid1_2))");
111 excludedNotesQuery.bindValue(":guid1_1", guid1);
112 excludedNotesQuery.bindValue(":guid2_1", guid2);
113 excludedNotesQuery.bindValue(":guid1_2", guid1);
114 excludedNotesQuery.bindValue(":guid2_2", guid2);
116 if (!excludedNotesQuery.exec()) {
117 logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid1=" + guid1 + "かつguid2=" + guid2 + "(またはその逆)のアイテムの存在確認失敗");
118 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
121 if (excludedNotesQuery.next()) {
128 // oldGuidのノートの除外ノートをnewGuidのノートの除外ノートとして複製
129 public void duplicateExcludedNotes(String newGuid, String oldGuid) {
130 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
132 // guid1 = oldGuidの除外ノートを取得
133 excludedNotesQuery.prepare("Select guid2 from ExcludedNotes where guid1=:oldGuid");
134 excludedNotesQuery.bindValue(":oldGuid", oldGuid);
135 if(!excludedNotesQuery.exec()){
136 logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid1=" + oldGuid + "のアイテム取得失敗");
137 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
139 // guid1 = newGuidの除外ノートとして複製
140 while(excludedNotesQuery.next()){
141 String guid2 = excludedNotesQuery.valueString(0);
143 addExclusion(newGuid, guid2);
146 // guid2 = oldGuidの除外ノートを取得
147 excludedNotesQuery.prepare("Select guid1 from ExcludedNotes where guid2=:oldGuid");
148 excludedNotesQuery.bindValue(":oldGuid", oldGuid);
149 if(!excludedNotesQuery.exec()){
150 logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid2=" + oldGuid + "のアイテム取得失敗");
151 logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
153 // guid2 = newGuidの除外ノートとして複製
154 while(excludedNotesQuery.next()){
155 String guid1 = excludedNotesQuery.valueString(0);
157 addExclusion(guid1, newGuid);
161 // guidを含む列をExcludedNotesテーブルから削除
162 public void expungeExcludedNote(String guid) {
163 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
166 query.prepare("Delete from ExcludedNotes where guid1=:guid1 or guid2=:guid2");
167 query.bindValue(":guid1", guid);
168 query.bindValue(":guid2", guid);
170 check = query.exec();
172 logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid=" + guid + "のデータ削除に失敗");
173 logger.log(logger.MEDIUM, query.lastError());