OSDN Git Service

2d9594b42bc12079156733f610218208cc21d8cd
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / ExcludedTable.java
1 /*
2  * This file is part of NeighborNote
3  * Copyright 2013 Yuki Takahashi
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
20 // ICHANGED
21 package cx.fbn.nevernote.sql;
22
23 import cx.fbn.nevernote.sql.driver.NSqlQuery;
24 import cx.fbn.nevernote.utilities.ApplicationLogger;
25
26 public class ExcludedTable {
27         private final ApplicationLogger logger;
28         private final DatabaseConnection db;
29
30         // コンストラクタ
31         public ExcludedTable(ApplicationLogger l, DatabaseConnection d) {
32                 logger = l;
33                 db = d;
34         }
35
36         // テーブル作成
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テーブル作成失敗!!!");
42         }
43
44         // テーブルをドロップ
45         public void dropTable() {
46                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
47                 query.exec("Drop table ExcludedNotes");
48         }
49
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);
56                 if (!query.exec()) {
57                         logger.log(logger.MEDIUM, "ExcludedNotesテーブルへのアイテム追加に失敗");
58                         logger.log(logger.MEDIUM, query.lastError());
59                 }
60         }
61         
62         // masterGuidとchildGuidをマージ
63         public void mergeHistoryGuid(String masterGuid, String childGuid) {
64                 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
65                 boolean check = false;
66                 
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();
74                 if(!check){
75                         logger.log(logger.MEDIUM, "excludedNotesテーブルの重複削除で失敗");
76                         logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
77                 }
78                 
79                 updateExcludedNoteGuid(masterGuid, childGuid);
80         }
81         
82         // ExcludedNotesテーブルのGuidを更新
83         public void updateExcludedNoteGuid(String newGuid, String oldGuid){
84                 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
85                 boolean check = false;
86                 
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();
91                 if (!check) {
92                         logger.log(logger.MEDIUM, "ExcludedNotesテーブルのguid1のところでguid更新失敗");
93                         logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
94                 }
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();
99                 if (!check) {
100                         logger.log(logger.MEDIUM, "ExcludedNotesテーブルのguid2のところでguid更新失敗");
101                         logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
102                 }
103         }
104
105         // ExcludedNotesテーブルに引数guidのノートが存在するか
106         public boolean existNote(String guid1, String guid2) {
107                 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
108
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);
115                 
116                 if (!excludedNotesQuery.exec()) {
117                         logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid1=" + guid1 + "かつguid2=" + guid2 + "(またはその逆)のアイテムの存在確認失敗");
118                         logger.log(logger.MEDIUM, excludedNotesQuery.lastError());
119                 }
120                 
121                 if (excludedNotesQuery.next()) {
122                         return true;
123                 }
124                 
125                 return false;
126         }
127         
128         // oldGuidのノートの除外ノートをnewGuidのノートの除外ノートとして複製
129         public void duplicateExcludedNotes(String newGuid, String oldGuid) {
130                 NSqlQuery excludedNotesQuery = new NSqlQuery(db.getBehaviorConnection());
131
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());
138                 }
139                 // guid1 = newGuidの除外ノートとして複製
140                 while(excludedNotesQuery.next()){
141                         String guid2 = excludedNotesQuery.valueString(0);
142                         
143                         addExclusion(newGuid, guid2);
144                 }
145                 
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());
152                 }
153                 // guid2 = newGuidの除外ノートとして複製
154                 while(excludedNotesQuery.next()){
155                         String guid1 = excludedNotesQuery.valueString(0);
156                         
157                         addExclusion(guid1, newGuid);
158                 }
159         }
160
161         // guidを含む列をExcludedNotesテーブルから削除
162         public void expungeExcludedNote(String guid) {
163                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
164                 boolean check;
165                 
166                 query.prepare("Delete from ExcludedNotes where guid1=:guid1 or guid2=:guid2");
167                 query.bindValue(":guid1", guid);
168                 query.bindValue(":guid2", guid);
169                 
170                 check = query.exec();
171                 if(!check){
172                         logger.log(logger.MEDIUM, "ExcludedNotesテーブルからguid=" + guid + "のデータ削除に失敗");
173                         logger.log(logger.MEDIUM, query.lastError());
174                 }
175         }
176 }