-
Notifications
You must be signed in to change notification settings - Fork 3
/
SQLite806x.cs
1817 lines (1511 loc) · 66.9 KB
/
SQLite806x.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SQLite;
using System.Reflection;
using System.Windows.Forms;
namespace SQLite806x
{
public class SQLite806xDB
{
private string dbFilePath = string.Empty;
private string dbDataSource = string.Empty;
private string dbCurrentVersion806x = string.Empty;
private string dbCurrentVersionSAD806x = string.Empty;
private string dbCurrentVersionSAD = string.Empty;
private string dbCurrentVersionTunerPro = string.Empty;
private string dbVersion806x = string.Empty;
private string dbVersionSAD806x = string.Empty;
private string dbVersionSAD = string.Empty;
private string dbVersionTunerPro = string.Empty;
private bool validDB = false;
public bool ValidDB { get { return validDB; } }
private List<T_SQLiteTable> sqlTables = null;
private List<T_SQLiteTable> sqlMissingTables = null;
public List<T_SQLiteTable> MissingTables { get { return sqlMissingTables; } }
private List<F_SQLiteField> sqlMissingFields = null;
public List<F_SQLiteField> MissingFields { get { return sqlMissingFields; } }
private List<F_SQLiteField> sqlDifferentFields = null;
public List<F_SQLiteField> DifferentFields { get { return sqlDifferentFields; } }
public string SyncType
{
get { return this.GetType().Name; }
}
public string SyncId
{
get
{
string sId = string.Empty;
try
{
if (validDB)
{
FileInfo fiFI = new FileInfo(dbFilePath);
if (fiFI.Exists) sId = fiFI.CreationTimeUtc.ToString("yyyyMMdd.HHmmss.") + fiFI.Name;
fiFI = null;
}
}
catch { }
return sId;
}
}
public SQLite806xDB(string filePath)
{
dbFilePath = filePath;
dbDataSource = "Data Source=" + filePath + ";";
initCurrentVersionSchema();
if (!File.Exists(dbFilePath))
{
createSchema();
updateDBVersionRows();
}
sqlTables = validatedSchema();
}
public void initCurrentVersionSchema()
{
sqlTables = new List<T_SQLiteTable>();
dbCurrentVersion806x = SQLite806xTools806x.getDBVersion();
SQLite806xTools806x.initSchema(ref sqlTables);
dbCurrentVersionSAD806x = SQLite806xToolsSAD806x.getDBVersion();
SQLite806xToolsSAD806x.initSchema(ref sqlTables);
dbCurrentVersionSAD = SQLite806xToolsSAD.getDBVersion();
SQLite806xToolsSAD.initSchema(ref sqlTables);
dbCurrentVersionTunerPro = SQLite806xToolsTunerPro.getDBVersion();
SQLite806xToolsTunerPro.initSchema(ref sqlTables);
}
public void createSchema()
{
validDB = false;
try { SQLiteConnection.CreateFile(dbFilePath); }
catch { return; }
List<T_SQLiteTable> nonCreatedTables = createSchemaTables(ref sqlTables);
if (nonCreatedTables != null)
{
if (nonCreatedTables.Count > 0)
{
try { File.Delete(dbFilePath); }
catch { }
return;
}
}
validDB = true;
}
public List<T_SQLiteTable> createSchemaTables(ref List<T_SQLiteTable> lstTables)
{
List<T_SQLiteTable> nonCreatedTables = new List<T_SQLiteTable>();
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource + "Version=3;"))
{
sCon.Open();
// Schema creation
foreach (T_SQLiteTable tTable in lstTables)
{
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
List<string> fFields = new List<string>();
foreach (F_SQLiteField fField in tTable.Fields)
{
if (fField.isRowId) continue;
fFields.Add(string.Format("{0} {1}", fField.Name, fField.SchemaType));
}
string sqlQuery = string.Format("CREATE TABLE \"{0}\" ({1});", tTable.Name, string.Join(", ", fFields.ToArray()));
sCmd.CommandText = sqlQuery;
try
{
sCmd.ExecuteNonQuery();
}
catch
{
nonCreatedTables.Add(tTable);
}
}
}
sCon.Close();
}
return nonCreatedTables;
}
public List<F_SQLiteField> createSchemaFields(ref List<F_SQLiteField> lstFields)
{
List<F_SQLiteField> nonCreatedFields = new List<F_SQLiteField>();
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource + "Version=3;"))
{
sCon.Open();
// Schema creation
foreach (F_SQLiteField fField in lstFields)
{
if (fField.isRowId) continue;
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
string sFieldDef = string.Format("{0} {1}", fField.Name, fField.SchemaType);
string sqlQuery = string.Format("ALTER TABLE [{0}] ADD COLUMN {1};", fField.TableName, sFieldDef);
sCmd.CommandText = sqlQuery;
try
{
sCmd.ExecuteNonQuery();
}
catch
{
nonCreatedFields.Add(fField);
}
}
}
sCon.Close();
}
return nonCreatedFields;
}
public List<F_SQLiteField> updateSchemaFields(ref List<F_SQLiteField> lstFields)
{
List<F_SQLiteField> nonUpdatedFields = new List<F_SQLiteField>();
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource + "Version=3;"))
{
sCon.Open();
// Schema creation
foreach (F_SQLiteField fField in lstFields)
{
if (fField.isRowId) continue;
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
string sqlQuery = string.Format("ALTER TABLE [{0}] RENAME COLUMN {1} TO {2};", fField.TableName, fField.Name, fField.Name + DateTime.Now.ToString("_yyyyMMdd_HHmmss"));
sCmd.CommandText = sqlQuery;
try
{
sCmd.ExecuteNonQuery();
}
catch
{
nonUpdatedFields.Add(fField);
continue;
}
}
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
string sFieldDef = string.Format("{0} {1}", fField.Name, fField.SchemaType);
string sqlQuery = string.Format("ALTER TABLE [{0}] ADD COLUMN {1};", fField.TableName, sFieldDef);
sCmd.CommandText = sqlQuery;
try
{
sCmd.ExecuteNonQuery();
}
catch
{
nonUpdatedFields.Add(fField);
}
}
}
sCon.Close();
}
return nonUpdatedFields;
}
public void updateDBVersionRows()
{
// DB Information mngt
List<R_806x_DB_Information> lst806xDBInfo = new List<R_806x_DB_Information>();
SQLite806xTools806x.addDBVersionRow(this, ref lst806xDBInfo);
Truncate<R_806x_DB_Information>();
Write<R_806x_DB_Information>(ref lst806xDBInfo);
lst806xDBInfo = null;
List<R_SAD806x_DB_Information> lstSAD806xDBInfo = new List<R_SAD806x_DB_Information>();
SQLite806xToolsSAD806x.addDBVersionRow(this, ref lstSAD806xDBInfo);
Truncate<R_SAD806x_DB_Information>();
Write<R_SAD806x_DB_Information>(ref lstSAD806xDBInfo);
lstSAD806xDBInfo = null;
List<R_SAD_DB_Information> lstSADDBInfo = new List<R_SAD_DB_Information>();
SQLite806xToolsSAD.addDBVersionRow(this, ref lstSADDBInfo);
Truncate<R_SAD_DB_Information>();
Write<R_SAD_DB_Information>(ref lstSADDBInfo);
lstSADDBInfo = null;
List<R_TunerPro_DB_Information> lstTPDBInfo = new List<R_TunerPro_DB_Information>();
SQLite806xToolsTunerPro.addDBVersionRow(this, ref lstTPDBInfo);
Truncate<R_TunerPro_DB_Information>();
Write<R_TunerPro_DB_Information>(ref lstTPDBInfo);
lstTPDBInfo = null;
}
public List<T_SQLiteTable> validatedSchema()
{
if (!File.Exists(dbFilePath))
{
validDB = false;
return null;
}
List<T_SQLiteTable> sqlExistingTables = new List<T_SQLiteTable>();
validDB = true;
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
try
{
sCon.Open();
}
catch
{
validDB = false;
return null;
}
try
{
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table';";
using (SQLiteDataReader sReader = sCmd.ExecuteReader())
{
if (sReader.HasRows)
{
while (sReader.Read())
{
string tableName = sReader.GetString(0);
string @namespace = "SQLite806x";
string @rowClass = "R_" + tableName;
Type tableClassType = Type.GetType(string.Format("{0}.{1}", @namespace, @rowClass));
if (tableClassType == null) continue;
sqlExistingTables.Add(new T_SQLiteTable(tableName, tableClassType, string.Empty, string.Empty, false, false, false));
}
}
else
{
validDB = false;
}
}
}
}
catch
{
validDB = false;
}
try
{
foreach (T_SQLiteTable sqlTable in sqlExistingTables)
{
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "PRAGMA table_info([" + sqlTable.Name + "]);";
using (SQLiteDataReader sReader = sCmd.ExecuteReader())
{
string @namespace = this.GetType().Namespace;
string @rowClass = "R_" + sqlTable.Name;
Type tableClassType = Type.GetType(string.Format("{0}.{1}", @namespace, @rowClass));
if (tableClassType == null) continue;
if (!sReader.HasRows) continue;
//RowId forced
F_SQLiteField rIdField = new F_SQLiteField(sqlTable.Name, "RowId", "INT");
rIdField.isRowId = true;
rIdField.ReadOnly = true;
if (tableClassType.GetProperty(rIdField.Name) != null) sqlTable.Fields.Add(rIdField);
while (sReader.Read())
{
F_SQLiteField fField = new F_SQLiteField(sqlTable.Name, sReader.GetString(1), sReader.GetString(2));
if (tableClassType.GetProperty(fField.Name) == null) continue;
sqlTable.Fields.Add(fField);
}
}
}
}
}
catch
{
validDB = false;
}
try { sCon.Close(); }
catch { }
}
if (!validDB) return null;
// Version checking
// 806x_DB_Information
validateSubSchema<R_806x_DB_Information>(ref dbVersion806x, ref dbCurrentVersion806x, ref sqlExistingTables);
// SAD806x_DB_Information
validateSubSchema<R_SAD806x_DB_Information>(ref dbVersionSAD806x, ref dbCurrentVersionSAD806x, ref sqlExistingTables);
// SAD_DB_Information
validateSubSchema<R_SAD_DB_Information>(ref dbVersionSAD, ref dbCurrentVersionSAD, ref sqlExistingTables);
// TunerPro_DB_Information
validateSubSchema<R_TunerPro_DB_Information>(ref dbVersionTunerPro, ref dbCurrentVersionTunerPro, ref sqlExistingTables);
if (!validDB) return null;
return sqlExistingTables;
}
private void validateSubSchema<T>(ref string dbVersion, ref string dbCurrentVersion, ref List<T_SQLiteTable> sqlExistingTables)
{
dbVersion = string.Empty;
if (!validDB) return;
if (!File.Exists(dbFilePath))
{
validDB = false;
return;
}
string dbiTableName = SQLite806xTools.TableNameFromType(typeof(T));
if (dbiTableName == string.Empty)
{
validDB = false;
return;
}
if (!dbiTableName.EndsWith("_DB_Information"))
{
validDB = false;
return;
}
string tableNamePrefix = dbiTableName.Substring(0, dbiTableName.Length - "DB_Information".Length);
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
try { sCon.Open(); }
catch
{
validDB = false;
return;
}
if (validDB)
{
int dbiCount = 0;
// DB_Information
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "SELECT Version FROM [" + dbiTableName + "];";
try
{
using (SQLiteDataReader sReader = sCmd.ExecuteReader())
{
dbiCount = 0;
if (sReader.HasRows)
{
while (sReader.Read())
{
dbiCount++;
if (dbiCount > 1) break;
dbVersion = sReader.GetString(0);
}
}
if (dbiCount != 1) dbVersion = string.Empty;
}
}
catch { }
}
}
try { sCon.Close(); }
catch { validDB = false; }
}
// No readable version, it is not a valid DB
if (dbVersion == string.Empty)
{
validDB = false;
return;
}
// Version is the current one or an old one, it has to be checked
List<T_SQLiteTable> missingTables = new List<T_SQLiteTable>();
List<F_SQLiteField> missingFields = new List<F_SQLiteField>();
List<F_SQLiteField> differentFields = new List<F_SQLiteField>();
foreach (T_SQLiteTable expectedTable in sqlTables)
{
// Working only on specific shema part
if (!expectedTable.Name.StartsWith(tableNamePrefix)) continue;
missingTables.Add(expectedTable);
foreach (T_SQLiteTable existingTable in sqlExistingTables)
{
if (existingTable.Name != expectedTable.Name) continue;
missingTables.Remove(expectedTable);
existingTable.Label = expectedTable.Label;
existingTable.Description = expectedTable.Description;
existingTable.ReadOnly = expectedTable.ReadOnly;
existingTable.Visible = expectedTable.Visible;
foreach (F_SQLiteField expectedField in expectedTable.Fields)
{
missingFields.Add(expectedField);
foreach (F_SQLiteField existingField in existingTable.Fields)
{
if (existingField.Name != expectedField.Name) continue;
missingFields.Remove(expectedField);
existingField.isRowId = expectedField.isRowId;
existingField.isAttachment = expectedField.isAttachment;
existingField.ReadOnly = expectedField.ReadOnly;
if (existingField.EDbType != expectedField.EDbType || existingField.MaxLength != expectedField.MaxLength)
{
differentFields.Add(expectedField);
}
}
}
}
}
if (dbVersion == dbCurrentVersion806x)
// Version is the current one, no difference should exist
{
// Trying to correct
if (missingTables.Count != 0) missingTables = createSchemaTables(ref missingTables);
if (missingTables.Count != 0)
{
if (sqlMissingTables == null) sqlMissingTables = new List<T_SQLiteTable>();
sqlMissingTables.AddRange(missingTables);
validDB = false;
}
if (missingFields.Count != 0) missingFields = createSchemaFields(ref missingFields);
if (missingFields.Count != 0)
{
if (sqlMissingFields == null) sqlMissingFields = new List<F_SQLiteField>();
sqlMissingFields.AddRange(missingFields);
validDB = false;
}
if (differentFields.Count != 0) differentFields = updateSchemaFields(ref differentFields);
if (differentFields.Count != 0)
{
if (sqlDifferentFields == null) sqlDifferentFields = new List<F_SQLiteField>();
sqlDifferentFields.AddRange(differentFields);
validDB = false; // Could be corrected, but more complex
}
if (!validDB) return;
}
else
// Version is different, it has to be checked and upgraded
{
// To be managed
}
}
private T_SQLiteTable findTable<T>()
{
if (typeof(T).Namespace != this.GetType().Namespace) return null;
if (!typeof(T).Name.StartsWith("R_")) return null;
if (!validDB) return null;
string tableName = typeof(T).Name.Substring(2);
foreach (T_SQLiteTable sTable in sqlTables) if (sTable.Name == tableName) return sTable;
return null;
}
public T newRow<T>() where T : new()
{
T tRow = default(T);
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return tRow;
tRow = new T();
foreach (F_SQLiteField tField in sqlTable.Fields)
{
F_SQLiteField fField = tField.clone();
if (fField.isRowId) fField.Value = -1;
typeof(T).GetProperty(fField.Name).SetValue(tRow, fField, null);
}
return tRow;
}
public long Count<T>() where T : new()
{
return Count<T>(string.Empty);
}
public long Count<T>(string whereClause) where T : new()
{
long lResult = -1;
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return lResult;
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
sCon.Open();
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
string fullWhereClause = string.Empty;
if (whereClause != string.Empty && whereClause != null)
{
if (fullWhereClause == string.Empty) fullWhereClause = " WHERE " + whereClause;
else fullWhereClause += " AND (" + whereClause + ")";
}
sCmd.CommandText = string.Format("SELECT COUNT(1) FROM [{0}]{1};", sqlTable.Name, fullWhereClause);
try { lResult = (long)sCmd.ExecuteScalar(); }
catch { }
}
sCon.Close();
}
return lResult;
}
public Dictionary<int, string[]> ReadLabelDescription<T>() where T : new()
{
MethodInfo mGetLabel = typeof(T).GetMethod("getRecordLabel");
if (mGetLabel == null) return null;
MethodInfo mGetDescription = typeof(T).GetMethod("getRecordDescription");
if (mGetDescription == null) return null;
List<T> rResult = Read<T>(-1, null, string.Empty, string.Empty);
if (rResult == null) return null;
Dictionary<int, string[]> lResult = new Dictionary<int, string[]>();
int rowId = 0;
PropertyInfo pRowId = typeof(T).GetProperty("RowId");
foreach (T tObject in rResult)
{
if (pRowId == null) rowId++;
else
{
F_SQLiteField fRowId = (F_SQLiteField)pRowId.GetValue(tObject, null);
rowId = fRowId.Value == null ? rowId + 1 : Convert.ToInt32(fRowId.Value);
}
lResult.Add(rowId, new string[] { (string)mGetLabel.Invoke(tObject, null), (string)mGetDescription.Invoke(tObject, null) });
}
return lResult;
}
public List<T> Read<T>() where T : new()
{
return Read<T>(-1, null, string.Empty, string.Empty);
}
public List<T> Read<T>(int rowId, List<string> ignoredFields, string whereClause, string orderByClause) where T : new()
{
List<T> lResult = new List<T>();
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return lResult;
Type rType = typeof(T);
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
sCon.Open();
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
List<string> lstFields = new List<string>() {"RowId", "*"};
if (ignoredFields != null)
{
if (ignoredFields.Count > 0)
{
lstFields = new List<string>() {"RowId"};
foreach (F_SQLiteField fField in sqlTable.Fields)
{
if (fField.isRowId) continue;
if (ignoredFields.Contains(fField.Name)) continue;
lstFields.Add(fField.Name);
}
}
}
string fullWhereClause = string.Empty;
if (rowId >= 0) fullWhereClause = " WHERE RowId = " + rowId.ToString();
if (whereClause != string.Empty && whereClause != null)
{
if (fullWhereClause == string.Empty) fullWhereClause = " WHERE " + whereClause;
else fullWhereClause += " AND (" + whereClause + ")";
}
if (orderByClause != string.Empty && orderByClause != null) orderByClause = " ORDER BY " + orderByClause;
sCmd.CommandText = string.Format("SELECT {0} FROM [{1}]{2}{3};", string.Join(", ", lstFields.ToArray()), sqlTable.Name, fullWhereClause, orderByClause);
lstFields = null;
using (SQLiteDataReader sReader = sCmd.ExecuteReader())
{
if (!sReader.HasRows) return lResult;
List<F_SQLiteField> queryFields = null;
while (sReader.Read())
{
if (queryFields == null)
{
queryFields = new List<F_SQLiteField>();
for (int iField = 0; iField < sReader.FieldCount; iField++)
{
F_SQLiteField tField = sqlTable.getField(sReader.GetName(iField));
if (tField == null)
{
queryFields.Add(null);
continue;
}
PropertyInfo pInfo = rType.GetProperty(tField.Name);
if (rType == null)
{
queryFields.Add(null);
continue;
}
queryFields.Add(tField);
}
}
T tRow = new T();
for (int iField = 0; iField < sReader.FieldCount; iField++)
{
F_SQLiteField tField = queryFields[iField];
if (tField == null) continue;
F_SQLiteField fField = tField.clone();
fField.Value = sReader.GetValue(iField);
rType.GetProperty(fField.Name).SetValue(tRow, fField, null);
}
lResult.Add(tRow);
}
}
}
sCon.Close();
}
return lResult;
}
public bool Truncate<T>()
{
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return false;
Type rType = typeof(T);
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
sCon.Open();
using (SQLiteTransaction tTran = sCon.BeginTransaction())
{
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "DELETE FROM [" + sqlTable.Name + "];";
sCmd.ExecuteNonQuery();
}
tTran.Commit();
}
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "VACUUM;";
sCmd.ExecuteNonQuery();
}
sCon.Close();
}
return true;
}
public bool Write<T>(ref List<T> rList)
{
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return false;
Type rType = typeof(T);
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
sCon.Open();
string updateCmd = "UPDATE [" + sqlTable.Name + "] SET ";
string insertCmd = "INSERT INTO [" + sqlTable.Name + "] (";
string lastInsertRowIdCmd = "SELECT LAST_INSERT_ROWID();";
foreach (F_SQLiteField tField in sqlTable.Fields)
{
if (tField.isRowId) continue;
updateCmd += tField.Name + " = :p" + tField.Name.ToUpper() + ", ";
insertCmd += tField.Name + ", ";
}
updateCmd = updateCmd.Substring(0, updateCmd.Length - 2);
updateCmd += " WHERE RowId = :pROWID;";
insertCmd = insertCmd.Substring(0, insertCmd.Length - 2);
insertCmd += ") VALUES (";
foreach (F_SQLiteField tField in sqlTable.Fields)
{
if (tField.isRowId) continue;
insertCmd += ":p" + tField.Name.ToUpper() + ", ";
}
insertCmd = insertCmd.Substring(0, insertCmd.Length - 2);
insertCmd += ");";
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
using (SQLiteTransaction tTran = sCon.BeginTransaction())
{
foreach (T rRow in rList)
{
if (rRow == null) continue;
F_SQLiteField rowRowIdField = (F_SQLiteField)rType.GetProperty("RowId").GetValue(rRow, null);
bool insertMode = false;
if (rowRowIdField.Value == null) insertMode = true;
else if (Convert.ToInt64(rowRowIdField.Value) <= 0) insertMode = true;
if (insertMode) sCmd.CommandText = insertCmd;
else sCmd.CommandText = updateCmd;
foreach (F_SQLiteField tField in sqlTable.Fields)
{
if (insertMode && tField.Name.ToLower() == rowRowIdField.Name.ToLower()) continue;
F_SQLiteField fField = (F_SQLiteField)rType.GetProperty(tField.Name).GetValue(rRow, null);
sCmd.Parameters.Add("p" + tField.Name.ToUpper(), tField.EDbType).Value = fField.Value;
}
sCmd.ExecuteNonQuery();
if (insertMode)
{
sCmd.CommandText = lastInsertRowIdCmd;
rowRowIdField.Value = sCmd.ExecuteScalar();
}
}
tTran.Commit();
}
}
sCon.Close();
}
return true;
}
public bool Delete<T>(List<T> rList)
{
T_SQLiteTable sqlTable = findTable<T>();
if (sqlTable == null) return false;
Type rType = typeof(T);
bool bResult = true;
using (SQLiteConnection sCon = new SQLiteConnection(dbDataSource))
{
try { sCon.Open(); }
catch { return false; }
using (SQLiteTransaction tTran = sCon.BeginTransaction())
{
foreach (T rRow in rList)
{
if (((F_SQLiteField)rType.GetProperty("RowId").GetValue(rRow, null)).Value == null) continue;
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "DELETE FROM [" + sqlTable.Name + "] WHERE RowId = " + Convert.ToInt32(((F_SQLiteField)rType.GetProperty("RowId").GetValue(rRow, null)).Value).ToString();
try { sCmd.ExecuteNonQuery(); }
catch { bResult = false; }
}
}
try { tTran.Commit(); }
catch { bResult = false; }
}
if (bResult && rList.Count > 1)
{
using (SQLiteCommand sCmd = new SQLiteCommand(sCon))
{
sCmd.CommandText = "VACUUM;";
try { sCmd.ExecuteNonQuery(); }
catch { bResult = false; }
}
}
try { sCon.Close(); }
catch { }
}
return bResult;
}
}
public class T_SQLiteTable
{
private string name = string.Empty;
public string Name { get { return name; } }
private Type type = null;
public Type Type { get { return type; } }
private bool readOnly = false;
public bool ReadOnly
{
get { return readOnly; }
set { readOnly = value; }
}
private bool visible = false;
public bool Visible
{
get { return visible; }
set { visible = value; }
}
private string label = string.Empty;
public string Label
{
get { return label; }
set { label = value; }
}
private string description = string.Empty;
public string Description
{
get { return description; }
set { description = value; }
}
private List<F_SQLiteField> fields = null;
public List<F_SQLiteField> Fields { get { return fields; } }
public T_SQLiteTable(string tName, Type tType, string sLabel, string sDescription, bool bReadOnly, bool bVisible, bool bInitFields)
{
name = tName;
type = tType;
label = sLabel;
description = sDescription;
readOnly = bReadOnly;
visible = bVisible;
fields = new List<F_SQLiteField>();
if (!bInitFields) return;
MethodInfo mMethod = type.GetMethod("initFields");
if (mMethod == null) return;
object rObject = Activator.CreateInstance(type);
mMethod.Invoke(rObject, null);
foreach (PropertyInfo pInfo in type.GetProperties())
{
F_SQLiteField fField = (F_SQLiteField)pInfo.GetValue(rObject, null);
if (fField == null) continue;
fields.Add(fField);
}
rObject = null;
}
public F_SQLiteField getField(string fieldName)
{
foreach (F_SQLiteField fField in fields) if (fField.Name.ToLower() == fieldName.ToLower()) return fField;
return null;
}
public string toString()
{
return Name;
}
}
public class F_SQLiteField
{
private string tableName = string.Empty;
public string TableName
{
get { return tableName; }
set { tableName = value; }
}
private string name = string.Empty;
public string Name { get { return name; } }
public bool isRowId { get; set; }
public bool isAttachment { get; set; }
public bool ReadOnly { get; set; }
private string schemaType = string.Empty;
public string SchemaType
{