OSDN Git Service

アイコンを変更した
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / StaredTable.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 package cx.fbn.nevernote.sql;
21
22 import cx.fbn.nevernote.sql.driver.NSqlQuery;
23 import cx.fbn.nevernote.utilities.ApplicationLogger;
24
25 public class StaredTable {
26         private final ApplicationLogger logger;
27         private final DatabaseConnection db;
28
29         // コンストラクタ
30         public StaredTable(ApplicationLogger l, DatabaseConnection d) {
31                 logger = l;
32                 db = d;
33         }
34
35         // テーブル作成
36         public void createTable() {
37                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
38                 logger.log(logger.HIGH, "StaredNotesテーブルを作成しています...");
39                 if (!query.exec("Create table StaredNotes (id integer primary key auto_increment, masterGuid varchar, staredGuid varchar)"))
40                         logger.log(logger.HIGH, "StaredNotesテーブル作成失敗!!!");
41         }
42
43         // テーブルをドロップ
44         public void dropTable() {
45                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
46                 query.exec("Drop table StaredNotes");
47         }
48
49         // StaredNotesテーブルにアイテムを1つ追加
50         public void addStaredItem(String masterGuid, String staredGuid) {
51                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
52                 query.prepare("Insert Into StaredNotes (masterGuid, staredGuid) Values(:masterGuid, :staredGuid)");
53                 query.bindValue(":masterGuid", masterGuid);
54                 query.bindValue(":staredGuid", staredGuid);
55                 if (!query.exec()) {
56                         logger.log(logger.MEDIUM, "StaredNotesテーブルへのアイテム追加に失敗");
57                         logger.log(logger.MEDIUM, query.lastError());
58                 }
59         }
60         
61         // StaredNotesテーブルからアイテムを1つ削除
62         public void removeStaredItem(String masterGuid, String staredGuid) {
63                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
64                 query.prepare("Delete from StaredNotes where (masterGuid=:masterGuid and staredGuid=:staredGuid)");
65                 query.bindValue(":masterGuid", masterGuid);
66                 query.bindValue(":staredGuid", staredGuid);
67                 if (!query.exec()) {
68                         logger.log(logger.MEDIUM, "StaredNotesテーブルからのアイテム削除に失敗");
69                         logger.log(logger.MEDIUM, query.lastError());
70                 }
71         }
72         
73         // guidを含む列をStaredNotesテーブルから削除
74         public void expungeStaredNote(String guid) {
75                 NSqlQuery query = new NSqlQuery(db.getBehaviorConnection());
76                 boolean check;
77                 
78                 query.prepare("Delete from StaredNotes where masterGuid=:masterGuid or staredGuid=:staredGuid");
79                 query.bindValue(":masterGuid", guid);
80                 query.bindValue(":staredGuid", guid);
81                 
82                 check = query.exec();
83                 if(!check){
84                         logger.log(logger.MEDIUM, "StaredNotesテーブルからguid=" + guid + "のデータ削除に失敗");
85                         logger.log(logger.MEDIUM, query.lastError());
86                 }
87         }
88         
89         // masterGuidとchildGuidをマージ
90         public void mergeHistoryGuid(String masterGuid, String childGuid) {
91                 NSqlQuery staredNotesQuery = new NSqlQuery(db.getBehaviorConnection());
92                 boolean check = false;
93                 
94                 // マージ後に重複してしまうデータを先に削除
95                 staredNotesQuery.prepare("Delete from StaredNotes where (masterGuid=:masterGuid1 and staredGuid=:staredGuid1) or (masterGuid=:masterGuid2 and staredGuid=:staredGuid2)");
96                 staredNotesQuery.bindValue(":masterGuid1", masterGuid);
97                 staredNotesQuery.bindValue(":childGuid1", childGuid);
98                 staredNotesQuery.bindValue(":masterGuid2", childGuid);
99                 staredNotesQuery.bindValue(":staredGuid2", masterGuid);
100                 check = staredNotesQuery.exec();
101                 if(!check){
102                         logger.log(logger.MEDIUM, "staredNotesテーブルの重複削除で失敗");
103                         logger.log(logger.MEDIUM, staredNotesQuery.lastError());
104                 }
105                 
106                 updateStaredNoteGuid(masterGuid, childGuid);
107         }
108         
109         // StaredNotesテーブルのGuidを更新
110         public void updateStaredNoteGuid(String newGuid, String oldGuid){
111                 NSqlQuery staredNotesQuery = new NSqlQuery(db.getBehaviorConnection());
112                 boolean check = false;
113                 
114                 staredNotesQuery.prepare("Update StaredNotes set masterGuid=:newGuid where masterGuid=:oldGuid");
115                 staredNotesQuery.bindValue(":newGuid", newGuid);
116                 staredNotesQuery.bindValue(":oldGuid", oldGuid);
117                 check = staredNotesQuery.exec();
118                 if (!check) {
119                         logger.log(logger.MEDIUM, "StaredNotesテーブルのmasterGuidのところでguid更新失敗");
120                         logger.log(logger.MEDIUM, staredNotesQuery.lastError());
121                 }
122                 staredNotesQuery.prepare("Update StaredNotes set staredGuid=:newGuid where staredGuid=:oldGuid");
123                 staredNotesQuery.bindValue(":newGuid", newGuid);
124                 staredNotesQuery.bindValue(":oldGuid", oldGuid);
125                 check = staredNotesQuery.exec();
126                 if (!check) {
127                         logger.log(logger.MEDIUM, "StaredNotesテーブルのstaredGuidのところでguid更新失敗");
128                         logger.log(logger.MEDIUM, staredNotesQuery.lastError());
129                 }
130         }
131
132         // StaredNotesテーブルに引数guidのノートが存在するか
133         public boolean existNote(String masterGuid, String staredGuid) {
134                 NSqlQuery staredNotesQuery = new NSqlQuery(db.getBehaviorConnection());
135
136                 // 2つの引数guidを含むアイテムの存在確認
137                 staredNotesQuery.prepare("Select * from StaredNotes where Exists(Select * from StaredNotes where (masterGuid=:masterGuid1 and staredGuid=:staredGuid1) or (masterGuid=:masterGuid2 and staredGuid=:staredGuid2))");
138                 staredNotesQuery.bindValue(":masterGuid1", masterGuid);
139                 staredNotesQuery.bindValue(":staredGuid1", staredGuid);
140                 staredNotesQuery.bindValue(":masterGuid2", masterGuid);
141                 staredNotesQuery.bindValue(":staredGuid2", staredGuid);
142                 
143                 if (!staredNotesQuery.exec()) {
144                         logger.log(logger.MEDIUM, "StaredNotesテーブルからmasterGuid=" + masterGuid + "かつstaredGuid=" + staredGuid + "(またはその逆)のアイテムの存在確認失敗");
145                         logger.log(logger.MEDIUM, staredNotesQuery.lastError());
146                 }
147                 
148                 if (staredNotesQuery.next()) {
149                         return true;
150                 }
151                 
152                 return false;
153         }
154         
155         // oldGuidのノートの除外ノートをnewGuidのノートの除外ノートとして複製
156         public void duplicateStaredNotes(String newGuid, String oldGuid) {
157                 NSqlQuery staredNotesQuery = new NSqlQuery(db.getBehaviorConnection());
158
159                 // masterGuid = oldGuidのスター付きノートを取得
160                 staredNotesQuery.prepare("Select staredGuid from StaredNotes where masterGuid=:oldGuid");
161                 staredNotesQuery.bindValue(":oldGuid", oldGuid);
162                 if(!staredNotesQuery.exec()){
163                         logger.log(logger.MEDIUM, "StaredNotesテーブルからmasterGuid=" + oldGuid + "のアイテム取得失敗");
164                         logger.log(logger.MEDIUM, staredNotesQuery.lastError());
165                 }
166                 // masterGuid = newGuidのスター付きノートとして複製
167                 while(staredNotesQuery.next()){
168                         String staredGuid = staredNotesQuery.valueString(0);
169                         
170                         addStaredItem(newGuid, staredGuid);
171                 }
172                 
173                 // staredGuid = oldGuidの除外ノートを取得
174                 staredNotesQuery.prepare("Select masterGuid from StaredNotes where staredGuid=:oldGuid");
175                 staredNotesQuery.bindValue(":oldGuid", oldGuid);
176                 if(!staredNotesQuery.exec()){
177                         logger.log(logger.MEDIUM, "StaredNotesテーブルからstaredGuid=" + oldGuid + "のアイテム取得失敗");
178                         logger.log(logger.MEDIUM,  staredNotesQuery.lastError());
179                 }
180                 // staredGuid = newGuidの除外ノートとして複製
181                 while(staredNotesQuery.next()){
182                         String masterGuid = staredNotesQuery.valueString(0);
183                         
184                         addStaredItem(masterGuid, newGuid);
185                 }
186         }
187 }