0
|
1 // Filename : db.cpp
|
13
|
2 // Last Change: 23-May-2014.
|
0
|
3 //
|
|
4
|
13
|
5 #include <wx/tokenzr.h>
|
0
|
6 #include "db.h"
|
|
7 #include "wx/wxsqlite3.h"
|
|
8
|
2
|
9 //********** HHS-DB **********//
|
13
|
10 /* 被保険者台帳を更新 */
|
|
11 void UpdateHhs( wxArrayString info )
|
|
12 {
|
|
13 long n = info.GetCount();
|
|
14
|
|
15 wxProgressDialog pd( wxT("進行状況"), wxT("処理開始..."), n, NULL, wxPD_APP_MODAL|wxPD_REMAINING_TIME|wxPD_AUTO_HIDE );
|
|
16 pd.SetSize( wxSize( 320, 140 ) );
|
|
17
|
|
18 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("hhs.db");
|
|
19 wxRemoveFile( gszFile );
|
|
20
|
|
21 wxSQLite3Database hhsdb;
|
|
22 hhsdb.Open( gszFile );
|
|
23 hhsdb.Begin();
|
|
24
|
|
25 wxString sql = wxT( "PRAGMA foregin_keys=OFF" );
|
|
26 wxSQLite3Statement stmt = hhsdb.PrepareStatement( sql );
|
|
27 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
28
|
|
29 sql = wxT( "CREATE TABLE 'hhs_master' ( hhsno text PRIMARY KEY, birth text, name text, kana text, addr text, sex text )" );
|
|
30 stmt = hhsdb.PrepareStatement( sql );
|
|
31 q = stmt.ExecuteQuery();
|
|
32
|
|
33 wxString hhsno, birth, name, kana, addr, sex;
|
|
34 for ( long i = 0; i < n; i++ ) {
|
|
35
|
|
36 info[i].Replace( wxT("\""), wxEmptyString, true );
|
|
37
|
|
38 wxStringTokenizer token( info[i], wxT(",") );
|
|
39 hhsno = token.GetNextToken();
|
|
40 birth = token.GetNextToken();
|
|
41 name = token.GetNextToken();
|
|
42 kana = token.GetNextToken();
|
|
43 addr = token.GetNextToken();
|
|
44 sex = token.GetNextToken();
|
|
45
|
|
46 stmt = hhsdb.PrepareStatement( "INSERT INTO hhs_master VALUES( ?, ?, ?, ?, ?, ? );" );
|
|
47 stmt.Bind( 1, hhsno );
|
|
48 stmt.Bind( 2, birth );
|
|
49 stmt.Bind( 3, name );
|
|
50 stmt.Bind( 4, kana );
|
|
51 stmt.Bind( 5, addr );
|
|
52 stmt.Bind( 6, sex );
|
|
53 stmt.ExecuteQuery();
|
|
54 stmt.Finalize();
|
|
55
|
|
56 if ( i % 1000 == 0 ) {
|
|
57 pd.Update( i, wxString::Format( wxT("%d / %d done."), i, n ) );
|
|
58 }
|
|
59 }
|
|
60
|
|
61 hhsdb.Commit();
|
|
62 hhsdb.Close();
|
|
63 }
|
|
64
|
2
|
65 /* 被保番で被保険者情報を取得 */
|
1
|
66 wxString GetHhsInfoByHhsNo( wxString hhsno )
|
0
|
67 {
|
1
|
68 wxString name, addr;
|
0
|
69
|
|
70 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("hhs.db");
|
|
71 wxSQLite3Database hhsdb;
|
|
72 hhsdb.Open( gszFile );
|
|
73
|
2
|
74 wxSQLite3Statement stmt = hhsdb.PrepareStatement( "SELECT name, addr FROM hhs_master WHERE hhsno = ?" );
|
0
|
75 stmt.Bind( 1, hhsno );
|
|
76 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
77 if ( !q.IsNull(0) ) {
|
|
78 while ( q.NextRow() ) {
|
|
79 name = q.GetString(0);
|
1
|
80 addr = q.GetString(1);
|
0
|
81 }
|
|
82 }
|
|
83 stmt.Finalize();
|
|
84 hhsdb.Close();
|
|
85
|
1
|
86 if ( name.IsEmpty() ) {
|
|
87 return wxEmptyString;
|
|
88 }
|
|
89 else {
|
|
90 return name + wxT("_") + addr;
|
|
91 }
|
0
|
92 }
|
1
|
93
|
9
|
94 // 被保険者番号リストから氏名を取得
|
|
95 wxArrayString GetHhsInfoByHhsNoList( wxArrayString hhsno )
|
|
96 {
|
|
97 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("hhs.db");
|
|
98 wxSQLite3Database hhsdb;
|
|
99 hhsdb.Open( gszFile );
|
|
100
|
|
101 wxString sql = wxT( "SELECT name FROM hhs_master WHERE hhsno = ?;" );
|
|
102 wxSQLite3Statement stmt;
|
|
103 wxSQLite3ResultSet q;
|
|
104
|
|
105 wxArrayString result;
|
|
106 for ( unsigned int i = 0; i < hhsno.GetCount(); i++ ) {
|
|
107 wxString str = hhsno[i];
|
|
108 str.Append( wxT("_") );
|
|
109
|
|
110 stmt = hhsdb.PrepareStatement( sql );
|
|
111 stmt.Bind( 1, hhsno[i] );
|
|
112 q = stmt.ExecuteQuery();
|
|
113 if ( !q.IsNull(0) ) {
|
|
114 while ( q.NextRow() ) {
|
|
115 str.Append( q.GetString(0) );
|
|
116 }
|
|
117 }
|
|
118
|
|
119 result.Add( str );
|
|
120 }
|
|
121 stmt.Finalize();
|
|
122 hhsdb.Close();
|
|
123
|
|
124 return result;
|
|
125 }
|
|
126
|
2
|
127 // 氏名カナで被保険者情報を検索
|
1
|
128 wxArrayString GetHhsInfoByKana( wxString kana, bool fuzzy )
|
0
|
129 {
|
|
130 wxArrayString data;
|
|
131
|
|
132 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("hhs.db");
|
|
133 wxSQLite3Database hhsdb;
|
|
134 hhsdb.Open( gszFile );
|
|
135
|
6
|
136 wxString sql = wxT( "SELECT hhsno, kana, name, birth, addr FROM hhs_master " );
|
|
137 if ( fuzzy ) {
|
|
138 kana = wxT("%") + kana + wxT("%");
|
|
139 sql += wxT( "WHERE kana LIKE ? ORDER BY kana, birth;" );
|
|
140 }
|
|
141 else {
|
|
142 sql += wxT( "WHERE kana = ? ORDER BY kana, birth;" );
|
|
143 }
|
1
|
144
|
|
145 wxSQLite3Statement stmt = hhsdb.PrepareStatement( sql );
|
0
|
146 stmt.Bind( 1, kana );
|
|
147 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
148
|
|
149 if ( !q.IsNull(0) ) {
|
|
150 wxString str;
|
|
151 while ( q.NextRow() ) {
|
|
152 str = q.GetString(0);
|
|
153 for ( int i=1; i<5; i++ ) {
|
|
154 str += "_" + q.GetString(i);
|
|
155 }
|
|
156 data.Add( str );
|
|
157 }
|
|
158 }
|
|
159 stmt.Finalize();
|
|
160 hhsdb.Close();
|
|
161
|
|
162 return data;
|
|
163 }
|
|
164
|
2
|
165 //********** CCN-DB **********//
|
|
166 /* 被保険者番号からファイルパスを取得 */
|
0
|
167 wxArrayString GetPathByHhsNo( wxString hhsno )
|
|
168 {
|
2
|
169 wxArrayString path;
|
0
|
170
|
|
171 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
172 wxSQLite3Database ccndb;
|
|
173 ccndb.Open( gszFile );
|
|
174
|
2
|
175 wxString sql = wxT( "SELECT path FROM path WHERE hhsno = ? ORDER BY path DESC;" );
|
|
176 wxSQLite3Statement stmt = ccndb.PrepareStatement( sql );
|
0
|
177 stmt.Bind( 1, hhsno );
|
|
178 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
179
|
|
180 if ( !q.IsNull(0) ) {
|
|
181 while ( q.NextRow() ) {
|
2
|
182 path.Add( q.GetString(0) );
|
0
|
183 }
|
|
184 }
|
|
185 stmt.Finalize();
|
|
186 ccndb.Close();
|
|
187
|
2
|
188 return path;
|
0
|
189 }
|
|
190
|
9
|
191 // 審査会情報のある被保険者を取得
|
|
192 wxArrayString GetJudgedHhsNo( void )
|
|
193 {
|
|
194 wxArrayString hhsno;
|
|
195
|
|
196 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
197 wxSQLite3Database ccndb;
|
|
198 ccndb.Open( gszFile );
|
|
199
|
|
200 //wxString sql = wxT( "SELECT DISTINCT hhsno FROM path;" );
|
|
201 wxString sql = wxT( "SELECT hhsno FROM path ORDER BY path DESC LIMIT 200;" );
|
|
202 wxSQLite3Statement stmt = ccndb.PrepareStatement( sql );
|
|
203 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
204
|
|
205 if ( !q.IsNull(0) ) {
|
|
206 while ( q.NextRow() ) {
|
|
207 hhsno.Add( q.GetString(0) );
|
|
208 }
|
|
209 }
|
|
210 stmt.Finalize();
|
|
211 ccndb.Close();
|
|
212
|
|
213 return hhsno;
|
|
214 }
|
|
215
|
2
|
216 /* 被保険者が審査会にかかったかどうか */
|
|
217 bool IsHhsJudged( wxString hhsno )
|
|
218 {
|
|
219 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
220 wxSQLite3Database ccndb;
|
|
221 ccndb.Open( gszFile );
|
|
222
|
|
223 wxString sql = wxT( "SELECT path FROM path WHERE hhsno = ?;" );
|
|
224 wxSQLite3Statement stmt = ccndb.PrepareStatement( sql );
|
|
225 stmt.Bind( 1, hhsno );
|
|
226 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
227
|
|
228 bool ret = true;
|
|
229 if ( q.IsNull(0) ) ret = false;
|
|
230
|
|
231 stmt.Finalize();
|
|
232 ccndb.Close();
|
|
233
|
|
234 return ret;
|
|
235 }
|
|
236
|
|
237 /* 合議体開催日を取得 */
|
0
|
238 wxArrayString GetCcnDate( void )
|
|
239 {
|
|
240 wxArrayString date_cnt;
|
|
241
|
|
242 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
243 wxSQLite3Database ccndb;
|
|
244 ccndb.Open( gszFile );
|
|
245
|
2
|
246 wxSQLite3Statement stmt = ccndb.PrepareStatement( "SELECT date, count(*) FROM ccn GROUP BY date ORDER BY date desc" );
|
0
|
247 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
248
|
|
249 wxString str;
|
|
250 if ( !q.IsNull(0) ) {
|
|
251 while ( q.NextRow() ) {
|
|
252 str = q.GetString(0) + "_" + q.GetString(1);
|
|
253 date_cnt.Add( str );
|
|
254 }
|
|
255 }
|
|
256 stmt.Finalize();
|
|
257 ccndb.Close();
|
|
258
|
|
259 return date_cnt;
|
|
260 }
|
|
261
|
2
|
262 /* 日付から審査会を取得 */
|
0
|
263 wxArrayString GetCcnByDate( wxString date )
|
|
264 {
|
2
|
265 wxArrayString data;
|
0
|
266
|
|
267 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
268 wxSQLite3Database ccndb;
|
|
269 ccndb.Open( gszFile );
|
|
270
|
2
|
271 wxSQLite3Statement stmt = ccndb.PrepareStatement( "SELECT hhsno, path, date FROM path WHERE date = ? ORDER BY path" );
|
0
|
272 stmt.Bind( 1, date );
|
|
273 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
274
|
|
275 wxString str;
|
|
276 if ( !q.IsNull(0) ) {
|
|
277 while ( q.NextRow() ) {
|
2
|
278 str = q.GetString(0) + "_" + q.GetString(1) + "_" + q.GetString(2);
|
|
279 data.Add( str );
|
0
|
280 }
|
|
281 }
|
|
282 stmt.Finalize();
|
|
283 ccndb.Close();
|
|
284
|
2
|
285 return data;
|
0
|
286 }
|
|
287
|
11
|
288 /* 範囲日時のパスを取得 */
|
|
289 wxArrayString GetPathes( wxString from, wxString to )
|
|
290 {
|
|
291 wxArrayString path;
|
|
292
|
|
293 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
294 wxSQLite3Database ccndb;
|
|
295 ccndb.Open( gszFile );
|
|
296
|
|
297 wxSQLite3Statement stmt = ccndb.PrepareStatement( "SELECT path FROM path WHERE date >= ? AND date <= ?" );
|
|
298 stmt.Bind( 1, from );
|
|
299 stmt.Bind( 2, to );
|
|
300 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
301
|
|
302 if ( !q.IsNull(0) ) {
|
|
303 while ( q.NextRow() ) {
|
|
304 path.Add( q.GetString(0) );
|
|
305 }
|
|
306 }
|
|
307 stmt.Finalize();
|
|
308 ccndb.Close();
|
|
309
|
|
310 return path;
|
|
311 }
|
|
312
|
2
|
313 /* 合議体から被保険者番号を取得 */
|
0
|
314 wxArrayString GetHhsNoByCcn( wxString ccn, wxString date )
|
|
315 {
|
|
316 wxArrayString hhsno;
|
|
317
|
|
318 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
319 wxSQLite3Database ccndb;
|
|
320 ccndb.Open( gszFile );
|
|
321
|
2
|
322 wxSQLite3Statement stmt = ccndb.PrepareStatement( "SELECT hhsno FROM ccn WHERE ccn = ? AND date = ? ORDER BY hhsno" );
|
0
|
323 stmt.Bind( 1, ccn );
|
|
324 stmt.Bind( 2, date );
|
|
325 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
326
|
|
327 if ( !q.IsNull(0) ) {
|
|
328 while ( q.NextRow() ) {
|
|
329 hhsno.Add( q.GetString(0) );
|
|
330 }
|
|
331 }
|
|
332 stmt.Finalize();
|
|
333 ccndb.Close();
|
|
334
|
|
335 return hhsno;
|
|
336 }
|
|
337
|
2
|
338 /* インデックスを更新 */
|
8
|
339 void UpdateIndex( wxString datadir, wxString date )
|
0
|
340 {
|
|
341 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
342 wxSQLite3Database ccndb;
|
|
343 ccndb.Open( gszFile );
|
|
344
|
2
|
345 wxSQLite3Statement stmt = ccndb.PrepareStatement( "DELETE FROM path WHERE date = ?;" );
|
|
346 stmt.Bind( 1, date );
|
|
347 stmt.ExecuteQuery();
|
|
348 stmt.Finalize();
|
0
|
349
|
2
|
350 wxString ccndir;
|
8
|
351 wxDir dated( datadir );
|
2
|
352 if ( !dated.IsOpened() ) {
|
|
353 return;
|
0
|
354 }
|
|
355
|
2
|
356 wxRegEx reSinsei( wxT("^00000") );
|
|
357 bool cont = dated.GetFirst( &ccndir, wxEmptyString, wxDIR_DIRS );
|
|
358
|
|
359 wxProgressDialog pd( wxT("進行状況"), wxT("処理開始..."), 240, NULL, wxPD_APP_MODAL|wxPD_REMAINING_TIME|wxPD_AUTO_HIDE );
|
|
360 pd.SetSize( wxSize( 320, 140 ) );
|
|
361 int count = 0;
|
|
362
|
|
363 while ( cont ) {
|
|
364
|
8
|
365 wxDir ccnd( datadir + wxFILE_SEP_PATH + ccndir );
|
2
|
366 if ( !ccnd.IsOpened() ) return;
|
|
367 wxString hhsdir;
|
|
368 bool c = ccnd.GetFirst( &hhsdir, wxEmptyString, wxDIR_DIRS );
|
|
369
|
|
370 while ( c ) {
|
|
371 if ( ! reSinsei.Matches( hhsdir ) ) {
|
|
372
|
8
|
373 wxString path = datadir + wxFILE_SEP_PATH + ccndir + wxFILE_SEP_PATH + hhsdir;
|
2
|
374
|
|
375 stmt = ccndb.PrepareStatement( "INSERT INTO path VALUES( ?, ?, ?, datetime( 'now', 'localtime' ) );" );
|
|
376 stmt.Bind( 1, hhsdir );
|
|
377 stmt.Bind( 2, path );
|
|
378 stmt.Bind( 3, date );
|
|
379 stmt.ExecuteQuery();
|
|
380 stmt.Finalize();
|
|
381 pd.Update( count++, hhsdir + wxT("@") + ccndir + wxT("を処理しました.") );
|
|
382 }
|
|
383 c = ccnd.GetNext( &hhsdir );
|
|
384 }
|
|
385
|
|
386 cont = dated.GetNext( &ccndir );
|
|
387 }
|
0
|
388 ccndb.Close();
|
|
389 }
|
|
390
|
4
|
391 //********** HHS-DB & CCN-DB **********//
|
7
|
392 /* DBの更新日時を取得 */
|
|
393 wxArrayString GetLastUpdate( void )
|
|
394 {
|
|
395 wxArrayString date;
|
|
396 wxString dbFile;
|
|
397 wxDateTime t;
|
|
398
|
|
399 dbFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
400 wxFileName ccn( dbFile );
|
|
401 t = ccn.GetModificationTime();
|
|
402 date.Add( t.FormatISODate() );
|
|
403
|
|
404 dbFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("hhs.db");
|
|
405 wxFileName hhs( dbFile );
|
|
406 t = hhs.GetModificationTime();
|
|
407 date.Add( t.FormatISODate() );
|
|
408
|
|
409 return date;
|
|
410 }
|
|
411
|
2
|
412 /* DB整合性チェック */
|
|
413 wxArrayString CheckDBs( void )
|
|
414 {
|
|
415 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
416 wxSQLite3Database ccndb;
|
|
417 ccndb.Open( gszFile );
|
|
418
|
|
419 wxString sql = wxT("ATTACH 'db/hhs.db' AS hhs");
|
|
420 wxSQLite3Statement stmt = ccndb.PrepareStatement( sql );
|
|
421 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
422
|
|
423 sql = wxT("SELECT hhsno FROM path EXCEPT SELECT hhsno FROM hhs.hhs_master");
|
|
424 stmt = ccndb.PrepareStatement( sql );
|
|
425 q = stmt.ExecuteQuery();
|
|
426
|
|
427 wxArrayString result;
|
|
428 while ( q.NextRow() ) {
|
|
429 result.Add( q.GetString(0) );
|
|
430 }
|
|
431
|
|
432 stmt.Finalize();
|
|
433 ccndb.Close();
|
|
434
|
|
435 return result;
|
|
436 }
|
|
437
|
4
|
438 // 被保険者番号リストから氏名と最新ファイルパスを取得
|
|
439 wxArrayString GetHhsInfoAndPathByHhsNoList( wxArrayString hhsno )
|
|
440 {
|
|
441 wxString gszFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("db") + wxFILE_SEP_PATH + wxT("ccn.db");
|
|
442 wxSQLite3Database ccndb;
|
|
443 ccndb.Open( gszFile );
|
|
444
|
|
445 wxString sql = wxT( "ATTACH 'db/hhs.db' AS hhs;" );
|
|
446 wxSQLite3Statement stmt = ccndb.PrepareStatement( sql );
|
|
447 wxSQLite3ResultSet q = stmt.ExecuteQuery();
|
|
448
|
|
449 wxArrayString result;
|
|
450 for ( int i = 0; i < hhsno.GetCount(); i++ ) {
|
|
451 wxString str = hhsno[i];
|
|
452 str.Append( wxT("_") );
|
|
453
|
|
454 sql = wxT( "SELECT name FROM hhs.hhs_master WHERE hhsno = ?;" );
|
|
455 stmt = ccndb.PrepareStatement( sql );
|
|
456 stmt.Bind( 1, hhsno[i] );
|
|
457 q = stmt.ExecuteQuery();
|
|
458 if ( !q.IsNull(0) ) {
|
|
459 while ( q.NextRow() ) {
|
|
460 str.Append( q.GetString(0) );
|
|
461 }
|
|
462 }
|
|
463 str.Append( wxT("_") );
|
|
464
|
|
465 sql = wxT( "SELECT path FROM path WHERE hhsno = ? ORDER BY path DESC LIMIT 1;" );
|
|
466 stmt = ccndb.PrepareStatement( sql );
|
|
467 stmt.Bind( 1, hhsno[i] );
|
|
468 q = stmt.ExecuteQuery();
|
|
469 if ( !q.IsNull(0) ) {
|
|
470 while ( q.NextRow() ) {
|
|
471 str.Append( q.GetString(0) );
|
|
472 }
|
|
473 }
|
|
474
|
|
475 result.Add( str );
|
|
476 }
|
|
477 stmt.Finalize();
|
|
478 ccndb.Close();
|
|
479
|
|
480 return result;
|
|
481 }
|
7
|
482
|