Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[d2857c]: System.Data.SQLite / SQLiteConnection.cs Maximize Restore History

Download this file

SQLiteConnection.cs    2542 lines (2284 with data), 120.3 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Transactions;
namespace System.Data.SQLite
{
/// <summary>
/// SQLite implentation of DbConnection.
/// </summary>
/// <remarks>
/// The <see cref="ConnectionString">ConnectionString</see> property of the SQLiteConnection class can contain the following parameter(s), delimited with a semi-colon:
/// <list type="table">
/// <listheader>
/// <term>Parameter</term>
/// <term>Values</term>
/// <term>Required</term>
/// <term>Default</term>
/// </listheader>
/// <item>
/// <description>Data Source</description>
/// <description>{filename}</description>
/// <description>Y</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Version</description>
/// <description>3</description>
/// <description>N</description>
/// <description>3</description>
/// </item>
/// <item>
/// <description>UseUTF16Encoding</description>
/// <description><b>True</b><br/><b>False</b></description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>DateTimeFormat</description>
/// <description><b>Ticks</b> - Use DateTime.Ticks<br/><b>ISO8601</b> - Use ISO8601 DateTime format</description>
/// <description>N</description>
/// <description>ISO8601</description>
/// </item>
/// <item>
/// <description>BinaryGUID</description>
/// <description><b>True</b> - Store GUID columns in binary form<br/><b>False</b> - Store GUID columns as text</description>
/// <description>N</description>
/// <description>True</description>
/// </item>
/// <item>
/// <description>Cache Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>2000</description>
/// </item>
/// <item>
/// <description>Synchronous</description>
/// <description><b>Normal</b> - Normal file flushing behavior<br/><b>Full</b> - Full flushing after all writes<br/><b>Off</b> - Underlying OS flushes I/O's</description>
/// <description>N</description>
/// <description>Normal</description>
/// </item>
/// <item>
/// <description>Page Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>1024</description>
/// </item>
/// <item>
/// <description>Password</description>
/// <description>{password}</description>
/// <description>N</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Enlist</description>
/// <description><b>Y</b> - Automatically enlist in distributed transactions<br/><b>N</b> - No automatic enlistment</description>
/// <description>N</description>
/// <description>Y</description>
/// </item>
/// <item>
/// <description>Pooling</description>
/// <description><b>True</b> - Use connection pooling<br/><b>False</b> - Do not use connection pooling</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>FailIfMissing</description>
/// <description><b>True</b> - Don't create the database if it does not exist, throw an error instead<br/><b>False</b> - Automatically create the database if it does not exist</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Max Page Count</description>
/// <description>{size in pages} - Limits the maximum number of pages (limits the size) of the database</description>
/// <description>N</description>
/// <description>0</description>
/// </item>
/// <item>
/// <description>Legacy Format</description>
/// <description><b>True</b> - Use the more compatible legacy 3.x database format<br/><b>False</b> - Use the newer 3.3x database format which compresses numbers more effectively</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Default Timeout</description>
/// <description>{time in seconds}<br/>The default command timeout</description>
/// <description>N</description>
/// <description>30</description>
/// </item>
/// <item>
/// <description>Journal Mode</description>
/// <description><b>Delete</b> - Delete the journal file after a commit<br/><b>Persist</b> - Zero out and leave the journal file on disk after a commit<br/><b>Off</b> - Disable the rollback journal entirely</description>
/// <description>N</description>
/// <description>Delete</description>
/// </item>
/// <item>
/// <description>Read Only</description>
/// <description><b>True</b> - Open the database for read only access<br/><b>False</b> - Open the database for normal read/write access</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Max Pool Size</description>
/// <description>The maximum number of connections for the given connection string that can be in the connection pool</description>
/// <description>N</description>
/// <description>100</description>
/// </item>
/// <item>
/// <description>Default IsolationLevel</description>
/// <description>The default transaciton isolation level</description>
/// <description>N</description>
/// <description>Serializable</description>
/// </item>
/// <item>
/// <description>Foreign Keys</description>
/// <description><b>True</b> - Enables foreign key enforcement<br/><b>False</b> - Disables foreign key enforcement</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// </list>
/// </remarks>
public sealed partial class SQLiteConnection : DbConnection, ICloneable
{
private const string _dataDirectory = "|DataDirectory|";
private const string _masterdb = "sqlite_master";
private const string _tempmasterdb = "sqlite_temp_master";
/// <summary>
/// State of the current connection
/// </summary>
private ConnectionState _connectionState;
/// <summary>
/// The connection string
/// </summary>
private string _connectionString;
/// <summary>
/// Nesting level of the transactions open on the connection
/// </summary>
internal int _transactionLevel;
/// <summary>
/// The default isolation level for new transactions
/// </summary>
private IsolationLevel _defaultIsolation;
#if !PLATFORM_COMPACTFRAMEWORK
/// <summary>
/// Whether or not the connection is enlisted in a distrubuted transaction
/// </summary>
internal SQLiteEnlistment _enlistment;
#endif
/// <summary>
/// The base SQLite object to interop with
/// </summary>
internal SQLiteBase _sql;
/// <summary>
/// The database filename minus path and extension
/// </summary>
private string _dataSource;
/// <summary>
/// Temporary password storage, emptied after the database has been opened
/// </summary>
private byte[] _password;
/// <summary>
/// Default command timeout
/// </summary>
private int _defaultTimeout = 30;
internal bool _binaryGuid;
internal long _version;
private event SQLiteUpdateEventHandler _updateHandler;
private event SQLiteCommitHandler _commitHandler;
private event EventHandler _rollbackHandler;
private SQLiteUpdateCallback _updateCallback;
private SQLiteCommitCallback _commitCallback;
private SQLiteRollbackCallback _rollbackCallback;
/// <summary>
/// This event is raised whenever the database is opened or closed.
/// </summary>
public override event StateChangeEventHandler StateChange;
///<overloads>
/// Constructs a new SQLiteConnection object
/// </overloads>
/// <summary>
/// Default constructor
/// </summary>
public SQLiteConnection()
: this("")
{
}
/// <summary>
/// Initializes the connection with the specified connection string
/// </summary>
/// <param name="connectionString">The connection string to use on the connection</param>
public SQLiteConnection(string connectionString)
{
_connectionState = ConnectionState.Closed;
_connectionString = "";
//_commandList = new List<WeakReference>();
if (connectionString != null)
ConnectionString = connectionString;
}
/// <summary>
/// Clones the settings and connection string from an existing connection. If the existing connection is already open, this
/// function will open its own connection, enumerate any attached databases of the original connection, and automatically
/// attach to them.
/// </summary>
/// <param name="connection"></param>
public SQLiteConnection(SQLiteConnection connection)
: this(connection.ConnectionString)
{
string str;
if (connection.State == ConnectionState.Open)
{
Open();
// Reattach all attached databases from the existing connection
using (DataTable tbl = connection.GetSchema("Catalogs"))
{
foreach (DataRow row in tbl.Rows)
{
str = row[0].ToString();
if (String.Compare(str, "main", StringComparison.OrdinalIgnoreCase) != 0
&& String.Compare(str, "temp", StringComparison.OrdinalIgnoreCase) != 0)
{
using (SQLiteCommand cmd = CreateCommand())
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture,
"ATTACH DATABASE '{0}' AS [{1}]", row[1], row[0]);
cmd.ExecuteNonQuery();
}
}
}
}
}
}
#if PLATFORM_COMPACTFRAMEWORK
/// <summary>
/// Obsolete
/// </summary>
public override int ConnectionTimeout
{
get
{
return 30;
}
}
#endif
/// <summary>
/// Creates a clone of the connection. All attached databases and user-defined functions are cloned. If the existing connection is open, the cloned connection
/// will also be opened.
/// </summary>
/// <returns></returns>
public object Clone()
{
return new SQLiteConnection(this);
}
/// <summary>
/// Disposes of the SQLiteConnection, closing it if it is active.
/// </summary>
/// <param name="disposing">True if the connection is being explicitly closed.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
Close();
}
/// <summary>
/// Creates a database file. This just creates a zero-byte file which SQLite
/// will turn into a database when the file is opened properly.
/// </summary>
/// <param name="databaseFileName">The file to create</param>
public static void CreateFile(string databaseFileName)
{
FileStream fs = File.Create(databaseFileName);
fs.Close();
}
/// <summary>
/// Raises the state change event when the state of the connection changes
/// </summary>
/// <param name="newState">The new state. If it is different from the previous state, an event is raised.</param>
internal void OnStateChange(ConnectionState newState)
{
ConnectionState oldState = _connectionState;
_connectionState = newState;
if (StateChange != null && oldState != newState)
{
var e = new StateChangeEventArgs(oldState, newState);
StateChange(this, e);
}
}
/// <summary>
/// OBSOLETE. Creates a new SQLiteTransaction if one isn't already active on the connection.
/// </summary>
/// <param name="isolationLevel">This parameter is ignored.</param>
/// <param name="deferredLock">When TRUE, SQLite defers obtaining a write lock until a write operation is requested.
/// When FALSE, a writelock is obtained immediately. The default is TRUE, but in a multi-threaded multi-writer
/// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
/// <returns>Returns a SQLiteTransaction object.</returns>
[Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
public SQLiteTransaction BeginTransaction(IsolationLevel isolationLevel, bool deferredLock)
{
return
(SQLiteTransaction)
BeginDbTransaction(deferredLock == false ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted);
}
/// <summary>
/// OBSOLETE. Creates a new SQLiteTransaction if one isn't already active on the connection.
/// </summary>
/// <param name="deferredLock">When TRUE, SQLite defers obtaining a write lock until a write operation is requested.
/// When FALSE, a writelock is obtained immediately. The default is false, but in a multi-threaded multi-writer
/// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
/// <returns>Returns a SQLiteTransaction object.</returns>
[Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
public SQLiteTransaction BeginTransaction(bool deferredLock)
{
return
(SQLiteTransaction)
BeginDbTransaction(deferredLock == false ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted);
}
/// <summary>
/// Creates a new SQLiteTransaction if one isn't already active on the connection.
/// </summary>
/// <param name="isolationLevel">Supported isolation levels are Serializable, ReadCommitted and Unspecified.</param>
/// <remarks>
/// Unspecified will use the default isolation level specified in the connection string. If no isolation level is specified in the
/// connection string, Serializable is used.
/// Serializable transactions are the default. In this mode, the engine gets an immediate lock on the database, and no other threads
/// may begin a transaction. Other threads may read from the database, but not write.
/// With a ReadCommitted isolation level, locks are deferred and elevated as needed. It is possible for multiple threads to start
/// a transaction in ReadCommitted mode, but if a thread attempts to commit a transaction while another thread
/// has a ReadCommitted lock, it may timeout or cause a deadlock on both threads until both threads' CommandTimeout's are reached.
/// </remarks>
/// <returns>Returns a SQLiteTransaction object.</returns>
public new SQLiteTransaction BeginTransaction(IsolationLevel isolationLevel)
{
return (SQLiteTransaction) BeginDbTransaction(isolationLevel);
}
/// <summary>
/// Creates a new SQLiteTransaction if one isn't already active on the connection.
/// </summary>
/// <returns>Returns a SQLiteTransaction object.</returns>
public new SQLiteTransaction BeginTransaction()
{
return (SQLiteTransaction) BeginDbTransaction(_defaultIsolation);
}
/// <summary>
/// Forwards to the local BeginTransaction() function
/// </summary>
/// <param name="isolationLevel">Supported isolation levels are Unspecified, Serializable, and ReadCommitted</param>
/// <returns></returns>
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
{
if (_connectionState != ConnectionState.Open)
throw new InvalidOperationException();
if (isolationLevel == IsolationLevel.Unspecified) isolationLevel = _defaultIsolation;
if (isolationLevel != IsolationLevel.Serializable && isolationLevel != IsolationLevel.ReadCommitted)
throw new ArgumentException("isolationLevel");
return new SQLiteTransaction(this, isolationLevel != IsolationLevel.Serializable);
}
/// <summary>
/// Not implemented
/// </summary>
/// <param name="databaseName"></param>
public override void ChangeDatabase(string databaseName)
{
throw new NotImplementedException();
}
/// <summary>
/// When the database connection is closed, all commands linked to this connection are automatically reset.
/// </summary>
public override void Close()
{
if (_sql != null)
{
#if !PLATFORM_COMPACTFRAMEWORK
if (_enlistment != null)
{
// If the connection is enlisted in a transaction scope and the scope is still active,
// we cannot truly shut down this connection until the scope has completed. Therefore make a
// hidden connection temporarily to hold open the connection until the scope has completed.
var cnn = new SQLiteConnection();
cnn._sql = _sql;
cnn._transactionLevel = _transactionLevel;
cnn._enlistment = _enlistment;
cnn._connectionState = _connectionState;
cnn._version = _version;
cnn._enlistment._transaction._cnn = cnn;
cnn._enlistment._disposeConnection = true;
_sql = null;
_enlistment = null;
}
#endif
if (_sql != null)
{
_sql.Close();
}
_sql = null;
_transactionLevel = 0;
}
OnStateChange(ConnectionState.Closed);
}
/// <summary>
/// Clears the connection pool associated with the connection. Any other active connections using the same database file
/// will be discarded instead of returned to the pool when they are closed.
/// </summary>
/// <param name="connection"></param>
public static void ClearPool(SQLiteConnection connection)
{
if (connection._sql == null) return;
connection._sql.ClearPool();
}
/// <summary>
/// Clears all connection pools. Any active connections will be discarded instead of sent to the pool when they are closed.
/// </summary>
public static void ClearAllPools()
{
SQLiteConnectionPool.ClearAllPools();
}
/// <summary>
/// The connection string containing the parameters for the connection
/// </summary>
/// <remarks>
/// <list type="table">
/// <listheader>
/// <term>Parameter</term>
/// <term>Values</term>
/// <term>Required</term>
/// <term>Default</term>
/// </listheader>
/// <item>
/// <description>Data Source</description>
/// <description>{filename}</description>
/// <description>Y</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Version</description>
/// <description>3</description>
/// <description>N</description>
/// <description>3</description>
/// </item>
/// <item>
/// <description>UseUTF16Encoding</description>
/// <description><b>True</b><br/><b>False</b></description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>DateTimeFormat</description>
/// <description><b>Ticks</b> - Use DateTime.Ticks<br/><b>ISO8601</b> - Use ISO8601 DateTime format<br/><b>JulianDay</b> - Use JulianDay format</description>
/// <description>N</description>
/// <description>ISO8601</description>
/// </item>
/// <item>
/// <description>BinaryGUID</description>
/// <description><b>Yes/On/1</b> - Store GUID columns in binary form<br/><b>No/Off/0</b> - Store GUID columns as text</description>
/// <description>N</description>
/// <description>On</description>
/// </item>
/// <item>
/// <description>Cache Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>2000</description>
/// </item>
/// <item>
/// <description>Synchronous</description>
/// <description><b>Normal</b> - Normal file flushing behavior<br/><b>Full</b> - Full flushing after all writes<br/><b>Off</b> - Underlying OS flushes I/O's</description>
/// <description>N</description>
/// <description>Normal</description>
/// </item>
/// <item>
/// <description>Page Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>1024</description>
/// </item>
/// <item>
/// <description>Password</description>
/// <description>{password}</description>
/// <description>N</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Enlist</description>
/// <description><B>Y</B> - Automatically enlist in distributed transactions<br/><b>N</b> - No automatic enlistment</description>
/// <description>N</description>
/// <description>Y</description>
/// </item>
/// <item>
/// <description>Pooling</description>
/// <description><b>True</b> - Use connection pooling<br/><b>False</b> - Do not use connection pooling</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>FailIfMissing</description>
/// <description><b>True</b> - Don't create the database if it does not exist, throw an error instead<br/><b>False</b> - Automatically create the database if it does not exist</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Max Page Count</description>
/// <description>{size in pages} - Limits the maximum number of pages (limits the size) of the database</description>
/// <description>N</description>
/// <description>0</description>
/// </item>
/// <item>
/// <description>Legacy Format</description>
/// <description><b>True</b> - Use the more compatible legacy 3.x database format<br/><b>False</b> - Use the newer 3.3x database format which compresses numbers more effectively</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Default Timeout</description>
/// <description>{time in seconds}<br/>The default command timeout</description>
/// <description>N</description>
/// <description>30</description>
/// </item>
/// <item>
/// <description>Journal Mode</description>
/// <description><b>Delete</b> - Delete the journal file after a commit<br/><b>Persist</b> - Zero out and leave the journal file on disk after a commit<br/><b>Off</b> - Disable the rollback journal entirely</description>
/// <description>N</description>
/// <description>Delete</description>
/// </item>
/// <item>
/// <description>Read Only</description>
/// <description><b>True</b> - Open the database for read only access<br/><b>False</b> - Open the database for normal read/write access</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>Max Pool Size</description>
/// <description>The maximum number of connections for the given connection string that can be in the connection pool</description>
/// <description>N</description>
/// <description>100</description>
/// </item>
/// <item>
/// <description>Default IsolationLevel</description>
/// <description>The default transaciton isolation level</description>
/// <description>N</description>
/// <description>Serializable</description>
/// </item>
/// <item>
/// <description>Foreign Keys</description>
/// <description><b>True</b> - Enables foreign key enforcement<br/><b>False</b> - Disables foreign key enforcement</description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// </list>
/// </remarks>
#if !PLATFORM_COMPACTFRAMEWORK
[RefreshProperties(RefreshProperties.All), DefaultValue("")]
[Editor(
"SQLite.Designer.SQLiteConnectionStringEditor, SQLite.Designer, Version=1.0.37.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
,
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
)]
#endif
public override string ConnectionString
{
get { return _connectionString; }
set
{
if (value == null)
throw new ArgumentNullException();
else if (_connectionState != ConnectionState.Closed)
throw new InvalidOperationException();
_connectionString = value;
}
}
/// <summary>
/// Create a new SQLiteCommand and associate it with this connection.
/// </summary>
/// <returns>Returns an instantiated SQLiteCommand object already assigned to this connection.</returns>
public new SQLiteCommand CreateCommand()
{
return new SQLiteCommand(this);
}
/// <summary>
/// Forwards to the local CreateCommand() function
/// </summary>
/// <returns></returns>
protected override DbCommand CreateDbCommand()
{
return CreateCommand();
}
/// <summary>
/// Returns the filename without extension or path
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public override string DataSource
{
get { return _dataSource; }
}
/// <summary>
/// Returns an empty string
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public override string Database
{
get { return "main"; }
}
internal static string MapUriPath(string path)
{
if (path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
return path.Substring(7);
else if (path.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
return path.Substring(5);
else if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
return path;
else
throw new InvalidOperationException("Invalid connection string: invalid URI");
}
/// <summary>
/// Parses the connection string into component parts
/// </summary>
/// <param name="connectionString">The connection string to parse</param>
/// <returns>An array of key-value pairs representing each parameter of the connection string</returns>
internal static SortedList<string, string> ParseConnectionString(string connectionString)
{
string s = connectionString;
int n;
var ls = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase);
// First split into semi-colon delimited values. The Split() function of SQLiteBase accounts for and properly
// skips semi-colons in quoted strings
string[] arParts = SQLiteConvert.Split(s, ';');
string[] arPiece;
int x = arParts.Length;
// For each semi-colon piece, split into key and value pairs by the presence of the = sign
for (n = 0; n < x; n++)
{
arPiece = SQLiteConvert.Split(arParts[n], '=');
if (arPiece.Length == 2)
{
ls.Add(arPiece[0], arPiece[1]);
}
else
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
"Invalid ConnectionString format for parameter \"{0}\"",
(arPiece.Length > 0) ? arPiece[0] : "null"));
}
return ls;
}
#if !PLATFORM_COMPACTFRAMEWORK
/// <summary>
/// Manual distributed transaction enlistment support
/// </summary>
/// <param name="transaction">The distributed transaction to enlist in</param>
public override void EnlistTransaction(Transaction transaction)
{
if (_transactionLevel > 0 && transaction != null)
throw new ArgumentException("Unable to enlist in transaction, a local transaction already exists");
if (_enlistment != null && transaction == _enlistment._scope)
return;
else if (_enlistment != null)
throw new ArgumentException("Already enlisted in a transaction");
_enlistment = new SQLiteEnlistment(this, transaction);
}
#endif
/// <summary>
/// Looks for a key in the array of key/values of the parameter string. If not found, return the specified default value
/// </summary>
/// <param name="items">The list to look in</param>
/// <param name="key">The key to find</param>
/// <param name="defValue">The default value to return if the key is not found</param>
/// <returns>The value corresponding to the specified key, or the default value if not found.</returns>
internal static string FindKey(SortedList<string, string> items, string key, string defValue)
{
string ret;
if (items.TryGetValue(key, out ret)) return ret;
return defValue;
}
/// <summary>
/// Opens the connection using the parameters found in the <see cref="ConnectionString">ConnectionString</see>
/// </summary>
public override void Open()
{
if (_connectionState != ConnectionState.Closed)
throw new InvalidOperationException();
Close();
SortedList<string, string> opts = ParseConnectionString(_connectionString);
string fileName;
if (Convert.ToInt32(FindKey(opts, "Version", "3"), CultureInfo.InvariantCulture) != 3)
throw new NotSupportedException("Only SQLite Version 3 is supported at this time");
fileName = FindKey(opts, "Data Source", "");
if (String.IsNullOrEmpty(fileName))
{
fileName = FindKey(opts, "Uri", "");
if (String.IsNullOrEmpty(fileName))
throw new ArgumentException(
"Data Source cannot be empty. Use :memory: to open an in-memory database");
else
fileName = MapUriPath(fileName);
}
if (String.Compare(fileName, ":MEMORY:", StringComparison.OrdinalIgnoreCase) == 0)
fileName = ":memory:";
else
{
#if PLATFORM_COMPACTFRAMEWORK
if (fileName.StartsWith(".\\"))
fileName = Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase) + fileName.Substring(1);
#endif
fileName = ExpandFileName(fileName);
}
try
{
bool usePooling = SQLiteConvert.ToBoolean(FindKey(opts, "Pooling", Boolean.FalseString));
bool bUTF16 = SQLiteConvert.ToBoolean(FindKey(opts, "UseUTF16Encoding", Boolean.FalseString));
int maxPoolSize = Convert.ToInt32(FindKey(opts, "Max Pool Size", "100"), CultureInfo.InvariantCulture);
_defaultTimeout = Convert.ToInt32(FindKey(opts, "Default Timeout", "30"), CultureInfo.CurrentCulture);
_defaultIsolation =
(IsolationLevel)
Enum.Parse(typeof (IsolationLevel), FindKey(opts, "Default IsolationLevel", "Serializable"), true);
if (_defaultIsolation != IsolationLevel.Serializable &&
_defaultIsolation != IsolationLevel.ReadCommitted)
throw new NotSupportedException("Invalid Default IsolationLevel specified");
var dateFormat =
(SQLiteDateFormats)
Enum.Parse(typeof (SQLiteDateFormats), FindKey(opts, "DateTimeFormat", "ISO8601"), true);
//string temp = FindKey(opts, "DateTimeFormat", "ISO8601");
//if (String.Compare(temp, "ticks", StringComparison.OrdinalIgnoreCase) == 0) dateFormat = SQLiteDateFormats.Ticks;
//else if (String.Compare(temp, "julianday", StringComparison.OrdinalIgnoreCase) == 0) dateFormat = SQLiteDateFormats.JulianDay;
if (bUTF16)
// SQLite automatically sets the encoding of the database to UTF16 if called from sqlite3_open16()
_sql = new SQLite3_UTF16(dateFormat);
else
_sql = new SQLite3(dateFormat);
SQLiteOpenFlagsEnum flags = SQLiteOpenFlagsEnum.None;
if (SQLiteConvert.ToBoolean(FindKey(opts, "FailIfMissing", Boolean.FalseString)) == false)
flags |= SQLiteOpenFlagsEnum.Create;
if (SQLiteConvert.ToBoolean(FindKey(opts, "Read Only", Boolean.FalseString)))
flags |= SQLiteOpenFlagsEnum.ReadOnly;
else
flags |= SQLiteOpenFlagsEnum.ReadWrite;
_sql.Open(fileName, flags, maxPoolSize, usePooling);
_binaryGuid = SQLiteConvert.ToBoolean(FindKey(opts, "BinaryGUID", Boolean.TrueString));
string password = FindKey(opts, "Password", null);
if (String.IsNullOrEmpty(password) == false)
_sql.SetPassword(Encoding.UTF8.GetBytes(password));
else if (_password != null)
_sql.SetPassword(_password);
_password = null;
_dataSource = Path.GetFileNameWithoutExtension(fileName);
_version++;
ConnectionState oldstate = _connectionState;
_connectionState = ConnectionState.Open;
try
{
using (SQLiteCommand cmd = CreateCommand())
{
string defValue;
if (fileName != ":memory:")
{
defValue = FindKey(opts, "Page Size", "1024");
if (Convert.ToInt32(defValue, CultureInfo.InvariantCulture) != 1024)
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA page_size={0}",
defValue);
cmd.ExecuteNonQuery();
}
}
defValue = FindKey(opts, "Max Page Count", "0");
if (Convert.ToInt32(defValue, CultureInfo.InvariantCulture) != 0)
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA max_page_count={0}",
defValue);
cmd.ExecuteNonQuery();
}
defValue = FindKey(opts, "Legacy Format", Boolean.FalseString);
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA legacy_file_format={0}",
SQLiteConvert.ToBoolean(defValue) ? "ON" : "OFF");
cmd.ExecuteNonQuery();
defValue = FindKey(opts, "Synchronous", "Normal");
if (String.Compare(defValue, "Full", StringComparison.OrdinalIgnoreCase) != 0)
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA synchronous={0}",
defValue);
cmd.ExecuteNonQuery();
}
defValue = FindKey(opts, "Cache Size", "2000");
if (Convert.ToInt32(defValue, CultureInfo.InvariantCulture) != 2000)
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA cache_size={0}",
defValue);
cmd.ExecuteNonQuery();
}
defValue = FindKey(opts, "Journal Mode", "Delete");
if (String.Compare(defValue, "Delete", StringComparison.OrdinalIgnoreCase) != 0)
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA journal_mode={0}",
defValue);
cmd.ExecuteNonQuery();
}
defValue = FindKey(opts, "Foreign Keys", Boolean.FalseString);
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "PRAGMA foreign_keys={0}",
SQLiteConvert.ToBoolean(defValue) ? "ON" : "OFF");
cmd.ExecuteNonQuery();
}
if (_commitHandler != null)
_sql.SetCommitHook(_commitCallback);
if (_updateHandler != null)
_sql.SetUpdateHook(_updateCallback);
if (_rollbackHandler != null)
_sql.SetRollbackHook(_rollbackCallback);
#if !PLATFORM_COMPACTFRAMEWORK
if (Transaction.Current != null &&
SQLiteConvert.ToBoolean(FindKey(opts, "Enlist", Boolean.TrueString)))
EnlistTransaction(Transaction.Current);
#endif
_connectionState = oldstate;
OnStateChange(ConnectionState.Open);
}
catch
{
_connectionState = oldstate;
throw;
}
}
catch (SQLiteException)
{
Close();
throw;
}
}
/// <summary>
/// Gets/sets the default command timeout for newly-created commands. This is especially useful for
/// commands used internally such as inside a SQLiteTransaction, where setting the timeout is not possible.
/// This can also be set in the ConnectionString with "Default Timeout"
/// </summary>
public int DefaultTimeout
{
get { return _defaultTimeout; }
set { _defaultTimeout = value; }
}
/// <summary>
/// Returns the version of the underlying SQLite database engine
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public override string ServerVersion
{
get
{
return SQLiteVersion;
//if (_connectionState != ConnectionState.Open)
// throw new InvalidOperationException();
//return _sql.Version;
}
}
/// <summary>
/// Returns the version of the underlying SQLite database engine
/// </summary>
public static string SQLiteVersion
{
get { return SQLite3.SQLiteVersion; }
}
/// <summary>
/// Returns the state of the connection.
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public override ConnectionState State
{
get { return _connectionState; }
}
/// <summary>
/// Change the password (or assign a password) to an open database.
/// </summary>
/// <remarks>
/// No readers or writers may be active for this process. The database must already be open
/// and if it already was password protected, the existing password must already have been supplied.
/// </remarks>
/// <param name="newPassword">The new password to assign to the database</param>
public void ChangePassword(string newPassword)
{
ChangePassword(String.IsNullOrEmpty(newPassword) ? null : Encoding.UTF8.GetBytes(newPassword));
}
/// <summary>
/// Change the password (or assign a password) to an open database.
/// </summary>
/// <remarks>
/// No readers or writers may be active for this process. The database must already be open
/// and if it already was password protected, the existing password must already have been supplied.
/// </remarks>
/// <param name="newPassword">The new password to assign to the database</param>
public void ChangePassword(byte[] newPassword)
{
if (_connectionState != ConnectionState.Open)
throw new InvalidOperationException("Database must be opened before changing the password.");
_sql.ChangePassword(newPassword);
}
/// <summary>
/// Sets the password for a password-protected database. A password-protected database is
/// unusable for any operation until the password has been set.
/// </summary>
/// <param name="databasePassword">The password for the database</param>
public void SetPassword(string databasePassword)
{
SetPassword(String.IsNullOrEmpty(databasePassword) ? null : Encoding.UTF8.GetBytes(databasePassword));
}
/// <summary>
/// Sets the password for a password-protected database. A password-protected database is
/// unusable for any operation until the password has been set.
/// </summary>
/// <param name="databasePassword">The password for the database</param>
public void SetPassword(byte[] databasePassword)
{
if (_connectionState != ConnectionState.Closed)
throw new InvalidOperationException("Password can only be set before the database is opened.");
if (databasePassword != null)
if (databasePassword.Length == 0) databasePassword = null;
_password = databasePassword;
}
/// <summary>
/// Expand the filename of the data source, resolving the |DataDirectory| macro as appropriate.
/// </summary>
/// <param name="sourceFile">The database filename to expand</param>
/// <returns>The expanded path and filename of the filename</returns>
private string ExpandFileName(string sourceFile)
{
if (String.IsNullOrEmpty(sourceFile)) return sourceFile;
if (sourceFile.StartsWith(_dataDirectory, StringComparison.OrdinalIgnoreCase))
{
string dataDirectory;
#if PLATFORM_COMPACTFRAMEWORK
dataDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase);
#else
dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
if (String.IsNullOrEmpty(dataDirectory))
dataDirectory = AppDomain.CurrentDomain.BaseDirectory;
#endif
if (sourceFile.Length > _dataDirectory.Length)
{
if (sourceFile[_dataDirectory.Length] == Path.DirectorySeparatorChar ||
sourceFile[_dataDirectory.Length] == Path.AltDirectorySeparatorChar)
sourceFile = sourceFile.Remove(_dataDirectory.Length, 1);
}
sourceFile = Path.Combine(dataDirectory, sourceFile.Substring(_dataDirectory.Length));
}
#if !PLATFORM_COMPACTFRAMEWORK
sourceFile = Path.GetFullPath(sourceFile);
#endif
return sourceFile;
}
///<overloads>
/// The following commands are used to extract schema information out of the database. Valid schema types are:
/// <list type="bullet">
/// <item>
/// <description>MetaDataCollections</description>
/// </item>
/// <item>
/// <description>DataSourceInformation</description>
/// </item>
/// <item>
/// <description>Catalogs</description>
/// </item>
/// <item>
/// <description>Columns</description>
/// </item>
/// <item>
/// <description>ForeignKeys</description>
/// </item>
/// <item>
/// <description>Indexes</description>
/// </item>
/// <item>
/// <description>IndexColumns</description>
/// </item>
/// <item>
/// <description>Tables</description>
/// </item>
/// <item>
/// <description>Views</description>
/// </item>
/// <item>
/// <description>ViewColumns</description>
/// </item>
/// </list>
/// </overloads>
/// <summary>
/// Returns the MetaDataCollections schema
/// </summary>
/// <returns>A DataTable of the MetaDataCollections schema</returns>
public override DataTable GetSchema()
{
return GetSchema("MetaDataCollections", null);
}
/// <summary>
/// Returns schema information of the specified collection
/// </summary>
/// <param name="collectionName">The schema collection to retrieve</param>
/// <returns>A DataTable of the specified collection</returns>
public override DataTable GetSchema(string collectionName)
{
return GetSchema(collectionName, new string[0]);
}
/// <summary>
/// Retrieves schema information using the specified constraint(s) for the specified collection
/// </summary>
/// <param name="collectionName">The collection to retrieve</param>
/// <param name="restrictionValues">The restrictions to impose</param>
/// <returns>A DataTable of the specified collection</returns>
public override DataTable GetSchema(string collectionName, string[] restrictionValues)
{
if (_connectionState != ConnectionState.Open)
throw new InvalidOperationException();
var parms = new string[5];
if (restrictionValues == null) restrictionValues = new string[0];
restrictionValues.CopyTo(parms, 0);
switch (collectionName.ToUpper(CultureInfo.InvariantCulture))
{
case "METADATACOLLECTIONS":
return Schema_MetaDataCollections();
case "DATASOURCEINFORMATION":
return Schema_DataSourceInformation();
case "DATATYPES":
return Schema_DataTypes();
case "COLUMNS":
case "TABLECOLUMNS":
return Schema_Columns(parms[0], parms[2], parms[3]);
case "INDEXES":
return Schema_Indexes(parms[0], parms[2], parms[3]);
case "TRIGGERS":
return Schema_Triggers(parms[0], parms[2], parms[3]);
case "INDEXCOLUMNS":
return Schema_IndexColumns(parms[0], parms[2], parms[3], parms[4]);
case "TABLES":
return Schema_Tables(parms[0], parms[2], parms[3]);
case "VIEWS":
return Schema_Views(parms[0], parms[2]);
case "VIEWCOLUMNS":
return Schema_ViewColumns(parms[0], parms[2], parms[3]);
case "FOREIGNKEYS":
return Schema_ForeignKeys(parms[0], parms[2], parms[3]);
case "CATALOGS":
return Schema_Catalogs(parms[0]);
case "RESERVEDWORDS":
return Schema_ReservedWords();
}
throw new NotSupportedException();
}
private static DataTable Schema_ReservedWords()
{
var tbl = new DataTable("ReservedWords");
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("ReservedWord", typeof (string));
tbl.Columns.Add("MaximumVersion", typeof (string));
tbl.Columns.Add("MinimumVersion", typeof (string));
tbl.BeginLoadData();
DataRow row;
foreach (string word in SR.Keywords.Split(new[] {','}))
{
row = tbl.NewRow();
row[0] = word;
tbl.Rows.Add(row);
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Builds a MetaDataCollections schema datatable
/// </summary>
/// <returns>DataTable</returns>
private static DataTable Schema_MetaDataCollections()
{
var tbl = new DataTable("MetaDataCollections");
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("CollectionName", typeof (string));
tbl.Columns.Add("NumberOfRestrictions", typeof (int));
tbl.Columns.Add("NumberOfIdentifierParts", typeof (int));
tbl.BeginLoadData();
var reader = new StringReader(SR.MetaDataCollections);
tbl.ReadXml(reader);
reader.Close();
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Builds a DataSourceInformation datatable
/// </summary>
/// <returns>DataTable</returns>
private DataTable Schema_DataSourceInformation()
{
var tbl = new DataTable("DataSourceInformation");
DataRow row;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add(DbMetaDataColumnNames.CompositeIdentifierSeparatorPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.DataSourceProductName, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.DataSourceProductVersion, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.DataSourceProductVersionNormalized, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.GroupByBehavior, typeof (int));
tbl.Columns.Add(DbMetaDataColumnNames.IdentifierPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.IdentifierCase, typeof (int));
tbl.Columns.Add(DbMetaDataColumnNames.OrderByColumnsInSelect, typeof (bool));
tbl.Columns.Add(DbMetaDataColumnNames.ParameterMarkerFormat, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.ParameterMarkerPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.ParameterNameMaxLength, typeof (int));
tbl.Columns.Add(DbMetaDataColumnNames.ParameterNamePattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.QuotedIdentifierPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.QuotedIdentifierCase, typeof (int));
tbl.Columns.Add(DbMetaDataColumnNames.StatementSeparatorPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.StringLiteralPattern, typeof (string));
tbl.Columns.Add(DbMetaDataColumnNames.SupportedJoinOperators, typeof (int));
tbl.BeginLoadData();
row = tbl.NewRow();
row.ItemArray = new object[]
{
null,
"SQLite",
_sql.Version,
_sql.Version,
3,
@"(^\[\p{Lo}\p{Lu}\p{Ll}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Nd}@$#_]*$)|(^\[[^\]\0]|\]\]+\]$)|(^\""[^\""\0]|\""\""+\""$)"
,
1,
false,
"{0}",
@"@[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)"
,
255,
@"^[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)"
,
@"(([^\[]|\]\])*)",
1,
";",
@"'(([^']|'')*)'",
15
};
tbl.Rows.Add(row);
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Build a Columns schema
/// </summary>
/// <param name="strCatalog">The catalog (attached database) to query, can be null</param>
/// <param name="strTable">The table to retrieve schema information for, must not be null</param>
/// <param name="strColumn">The column to retrieve schema information for, can be null</param>
/// <returns>DataTable</returns>
private DataTable Schema_Columns(string strCatalog, string strTable, string strColumn)
{
var tbl = new DataTable("Columns");
DataRow row;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("COLUMN_NAME", typeof (string));
tbl.Columns.Add("COLUMN_GUID", typeof (Guid));
tbl.Columns.Add("COLUMN_PROPID", typeof (long));
tbl.Columns.Add("ORDINAL_POSITION", typeof (int));
tbl.Columns.Add("COLUMN_HASDEFAULT", typeof (bool));
tbl.Columns.Add("COLUMN_DEFAULT", typeof (string));
tbl.Columns.Add("COLUMN_FLAGS", typeof (long));
tbl.Columns.Add("IS_NULLABLE", typeof (bool));
tbl.Columns.Add("DATA_TYPE", typeof (string));
tbl.Columns.Add("TYPE_GUID", typeof (Guid));
tbl.Columns.Add("CHARACTER_MAXIMUM_LENGTH", typeof (int));
tbl.Columns.Add("CHARACTER_OCTET_LENGTH", typeof (int));
tbl.Columns.Add("NUMERIC_PRECISION", typeof (int));
tbl.Columns.Add("NUMERIC_SCALE", typeof (int));
tbl.Columns.Add("DATETIME_PRECISION", typeof (long));
tbl.Columns.Add("CHARACTER_SET_CATALOG", typeof (string));
tbl.Columns.Add("CHARACTER_SET_SCHEMA", typeof (string));
tbl.Columns.Add("CHARACTER_SET_NAME", typeof (string));
tbl.Columns.Add("COLLATION_CATALOG", typeof (string));
tbl.Columns.Add("COLLATION_SCHEMA", typeof (string));
tbl.Columns.Add("COLLATION_NAME", typeof (string));
tbl.Columns.Add("DOMAIN_CATALOG", typeof (string));
tbl.Columns.Add("DOMAIN_NAME", typeof (string));
tbl.Columns.Add("DESCRIPTION", typeof (string));
tbl.Columns.Add("PRIMARY_KEY", typeof (bool));
tbl.Columns.Add("EDM_TYPE", typeof (string));
tbl.Columns.Add("AUTOINCREMENT", typeof (bool));
tbl.Columns.Add("UNIQUE", typeof (bool));
tbl.BeginLoadData();
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
using (
var cmdTables =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table' OR [type] LIKE 'view'",
strCatalog, master), this))
using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
{
while (rdTables.Read())
{
if (String.IsNullOrEmpty(strTable) ||
String.Compare(strTable, rdTables.GetString(2), StringComparison.OrdinalIgnoreCase) == 0)
{
try
{
using (
var cmd =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}]",
strCatalog, rdTables.GetString(2)), this))
using (SQLiteDataReader rd = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
using (DataTable tblSchema = rd.GetSchemaTable(true, true))
{
foreach (DataRow schemaRow in tblSchema.Rows)
{
if (
String.Compare(schemaRow[SchemaTableColumn.ColumnName].ToString(), strColumn,
StringComparison.OrdinalIgnoreCase) == 0
|| strColumn == null)
{
row = tbl.NewRow();
row["NUMERIC_PRECISION"] = schemaRow[SchemaTableColumn.NumericPrecision];
row["NUMERIC_SCALE"] = schemaRow[SchemaTableColumn.NumericScale];
row["TABLE_NAME"] = rdTables.GetString(2);
row["COLUMN_NAME"] = schemaRow[SchemaTableColumn.ColumnName];
row["TABLE_CATALOG"] = strCatalog;
row["ORDINAL_POSITION"] = schemaRow[SchemaTableColumn.ColumnOrdinal];
row["COLUMN_HASDEFAULT"] = (schemaRow[SchemaTableOptionalColumn.DefaultValue] !=
DBNull.Value);
row["COLUMN_DEFAULT"] = schemaRow[SchemaTableOptionalColumn.DefaultValue];
row["IS_NULLABLE"] = schemaRow[SchemaTableColumn.AllowDBNull];
row["DATA_TYPE"] =
schemaRow["DataTypeName"].ToString().ToLower(CultureInfo.InvariantCulture);
row["EDM_TYPE"] =
SQLiteConvert.DbTypeToTypeName(
(DbType) schemaRow[SchemaTableColumn.ProviderType]).ToLower(
CultureInfo.InvariantCulture);
row["CHARACTER_MAXIMUM_LENGTH"] = schemaRow[SchemaTableColumn.ColumnSize];
row["TABLE_SCHEMA"] = schemaRow[SchemaTableColumn.BaseSchemaName];
row["PRIMARY_KEY"] = schemaRow[SchemaTableColumn.IsKey];
row["AUTOINCREMENT"] = schemaRow[SchemaTableOptionalColumn.IsAutoIncrement];
row["COLLATION_NAME"] = schemaRow["CollationType"];
row["UNIQUE"] = schemaRow[SchemaTableColumn.IsUnique];
tbl.Rows.Add(row);
}
}
}
}
catch (SQLiteException)
{
}
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Returns index information for the given database and catalog
/// </summary>
/// <param name="strCatalog">The catalog (attached database) to query, can be null</param>
/// <param name="strIndex">The name of the index to retrieve information for, can be null</param>
/// <param name="strTable">The table to retrieve index information for, can be null</param>
/// <returns>DataTable</returns>
private DataTable Schema_Indexes(string strCatalog, string strTable, string strIndex)
{
var tbl = new DataTable("Indexes");
DataRow row;
var primaryKeys = new List<int>();
bool maybeRowId;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("INDEX_CATALOG", typeof (string));
tbl.Columns.Add("INDEX_SCHEMA", typeof (string));
tbl.Columns.Add("INDEX_NAME", typeof (string));
tbl.Columns.Add("PRIMARY_KEY", typeof (bool));
tbl.Columns.Add("UNIQUE", typeof (bool));
tbl.Columns.Add("CLUSTERED", typeof (bool));
tbl.Columns.Add("TYPE", typeof (int));
tbl.Columns.Add("FILL_FACTOR", typeof (int));
tbl.Columns.Add("INITIAL_SIZE", typeof (int));
tbl.Columns.Add("NULLS", typeof (int));
tbl.Columns.Add("SORT_BOOKMARKS", typeof (bool));
tbl.Columns.Add("AUTO_UPDATE", typeof (bool));
tbl.Columns.Add("NULL_COLLATION", typeof (int));
tbl.Columns.Add("ORDINAL_POSITION", typeof (int));
tbl.Columns.Add("COLUMN_NAME", typeof (string));
tbl.Columns.Add("COLUMN_GUID", typeof (Guid));
tbl.Columns.Add("COLUMN_PROPID", typeof (long));
tbl.Columns.Add("COLLATION", typeof (short));
tbl.Columns.Add("CARDINALITY", typeof (Decimal));
tbl.Columns.Add("PAGES", typeof (int));
tbl.Columns.Add("FILTER_CONDITION", typeof (string));
tbl.Columns.Add("INTEGRATED", typeof (bool));
tbl.Columns.Add("INDEX_DEFINITION", typeof (string));
tbl.BeginLoadData();
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
using (
var cmdTables =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
{
while (rdTables.Read())
{
maybeRowId = false;
primaryKeys.Clear();
if (String.IsNullOrEmpty(strTable) ||
String.Compare(rdTables.GetString(2), strTable, StringComparison.OrdinalIgnoreCase) == 0)
{
// First, look for any rowid indexes -- which sqlite defines are INTEGER PRIMARY KEY columns.
// Such indexes are not listed in the indexes list but count as indexes just the same.
try
{
using (
var cmdTable =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].table_info([{1}])",
strCatalog, rdTables.GetString(2)), this))
using (SQLiteDataReader rdTable = cmdTable.ExecuteReader())
{
while (rdTable.Read())
{
if (rdTable.GetInt32(5) == 1)
{
primaryKeys.Add(rdTable.GetInt32(0));
// If the primary key is of type INTEGER, then its a rowid and we need to make a fake index entry for it.
if (
String.Compare(rdTable.GetString(2), "INTEGER",
StringComparison.OrdinalIgnoreCase) == 0)
maybeRowId = true;
}
}
}
}
catch (SQLiteException)
{
}
if (primaryKeys.Count == 1 && maybeRowId)
{
row = tbl.NewRow();
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rdTables.GetString(2);
row["INDEX_CATALOG"] = strCatalog;
row["PRIMARY_KEY"] = true;
row["INDEX_NAME"] = String.Format(CultureInfo.InvariantCulture, "{1}_PK_{0}",
rdTables.GetString(2), master);
row["UNIQUE"] = true;
if (
String.Compare((string) row["INDEX_NAME"], strIndex, StringComparison.OrdinalIgnoreCase) ==
0
|| strIndex == null)
{
tbl.Rows.Add(row);
}
primaryKeys.Clear();
}
// Now fetch all the rest of the indexes.
try
{
using (
var cmd =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].index_list([{1}])",
strCatalog, rdTables.GetString(2)), this))
using (SQLiteDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
if (String.Compare(rd.GetString(1), strIndex, StringComparison.OrdinalIgnoreCase) ==
0
|| strIndex == null)
{
row = tbl.NewRow();
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rdTables.GetString(2);
row["INDEX_CATALOG"] = strCatalog;
row["INDEX_NAME"] = rd.GetString(1);
row["UNIQUE"] = rd.GetBoolean(2);
row["PRIMARY_KEY"] = false;
// get the index definition
using (
var cmdIndexes =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{2}] WHERE [type] LIKE 'index' AND [name] LIKE '{1}'",
strCatalog, rd.GetString(1).Replace("'", "''"), master),
this))
using (SQLiteDataReader rdIndexes = cmdIndexes.ExecuteReader())
{
while (rdIndexes.Read())
{
if (rdIndexes.IsDBNull(4) == false)
row["INDEX_DEFINITION"] = rdIndexes.GetString(4);
break;
}
}
// Now for the really hard work. Figure out which index is the primary key index.
// The only way to figure it out is to check if the index was an autoindex and if we have a non-rowid
// primary key, and all the columns in the given index match the primary key columns
if (primaryKeys.Count > 0 &&
rd.GetString(1).StartsWith("sqlite_autoindex_" + rdTables.GetString(2),
StringComparison.InvariantCultureIgnoreCase))
{
using (
var cmdDetails =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"PRAGMA [{0}].index_info([{1}])", strCatalog,
rd.GetString(1)), this))
using (SQLiteDataReader rdDetails = cmdDetails.ExecuteReader())
{
int nMatches = 0;
while (rdDetails.Read())
{
if (primaryKeys.Contains(rdDetails.GetInt32(1)) == false)
{
nMatches = 0;
break;
}
nMatches++;
}
if (nMatches == primaryKeys.Count)
{
row["PRIMARY_KEY"] = true;
primaryKeys.Clear();
}
}
}
tbl.Rows.Add(row);
}
}
}
}
catch (SQLiteException)
{
}
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
private DataTable Schema_Triggers(string catalog, string table, string triggerName)
{
var tbl = new DataTable("Triggers");
DataRow row;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("TRIGGER_NAME", typeof (string));
tbl.Columns.Add("TRIGGER_DEFINITION", typeof (string));
tbl.BeginLoadData();
if (String.IsNullOrEmpty(table)) table = null;
if (String.IsNullOrEmpty(catalog)) catalog = "main";
string master = (String.Compare(catalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
using (
var cmd =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [{0}].[{1}] WHERE [type] LIKE 'trigger'",
catalog, master), this))
using (SQLiteDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
if (String.Compare(rd.GetString(1), triggerName, StringComparison.OrdinalIgnoreCase) == 0
|| triggerName == null)
{
if (table == null ||
String.Compare(table, rd.GetString(2), StringComparison.OrdinalIgnoreCase) == 0)
{
row = tbl.NewRow();
row["TABLE_CATALOG"] = catalog;
row["TABLE_NAME"] = rd.GetString(2);
row["TRIGGER_NAME"] = rd.GetString(1);
row["TRIGGER_DEFINITION"] = rd.GetString(4);
tbl.Rows.Add(row);
}
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Retrieves table schema information for the database and catalog
/// </summary>
/// <param name="strCatalog">The catalog (attached database) to retrieve tables on</param>
/// <param name="strTable">The table to retrieve, can be null</param>
/// <param name="strType">The table type, can be null</param>
/// <returns>DataTable</returns>
private DataTable Schema_Tables(string strCatalog, string strTable, string strType)
{
var tbl = new DataTable("Tables");
DataRow row;
string strItem;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("TABLE_TYPE", typeof (string));
tbl.Columns.Add("TABLE_ID", typeof (long));
tbl.Columns.Add("TABLE_ROOTPAGE", typeof (int));
tbl.Columns.Add("TABLE_DEFINITION", typeof (string));
tbl.BeginLoadData();
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
using (
var cmd =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [{0}].[{1}] WHERE [type] LIKE 'table'",
strCatalog, master), this))
using (SQLiteDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
strItem = rd.GetString(0);
if (String.Compare(rd.GetString(2), 0, "SQLITE_", 0, 7, StringComparison.OrdinalIgnoreCase) == 0)
strItem = "SYSTEM_TABLE";
if (String.Compare(strType, strItem, StringComparison.OrdinalIgnoreCase) == 0
|| strType == null)
{
if (String.Compare(rd.GetString(2), strTable, StringComparison.OrdinalIgnoreCase) == 0
|| strTable == null)
{
row = tbl.NewRow();
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rd.GetString(2);
row["TABLE_TYPE"] = strItem;
row["TABLE_ID"] = rd.GetInt64(5);
row["TABLE_ROOTPAGE"] = rd.GetInt32(3);
row["TABLE_DEFINITION"] = rd.GetString(4);
tbl.Rows.Add(row);
}
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Retrieves view schema information for the database
/// </summary>
/// <param name="strCatalog">The catalog (attached database) to retrieve views on</param>
/// <param name="strView">The view name, can be null</param>
/// <returns>DataTable</returns>
private DataTable Schema_Views(string strCatalog, string strView)
{
var tbl = new DataTable("Views");
DataRow row;
string strItem;
int nPos;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("VIEW_DEFINITION", typeof (string));
tbl.Columns.Add("CHECK_OPTION", typeof (bool));
tbl.Columns.Add("IS_UPDATABLE", typeof (bool));
tbl.Columns.Add("DESCRIPTION", typeof (string));
tbl.Columns.Add("DATE_CREATED", typeof (DateTime));
tbl.Columns.Add("DATE_MODIFIED", typeof (DateTime));
tbl.BeginLoadData();
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
using (
var cmd =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'view'",
strCatalog, master), this))
using (SQLiteDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
if (String.Compare(rd.GetString(1), strView, StringComparison.OrdinalIgnoreCase) == 0
|| String.IsNullOrEmpty(strView))
{
strItem = rd.GetString(4).Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' ');
nPos = CultureInfo.InvariantCulture.CompareInfo.IndexOf(strItem, " AS ",
CompareOptions.IgnoreCase);
if (nPos > -1)
{
strItem = strItem.Substring(nPos + 4).Trim();
row = tbl.NewRow();
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rd.GetString(2);
row["IS_UPDATABLE"] = false;
row["VIEW_DEFINITION"] = strItem;
tbl.Rows.Add(row);
}
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Retrieves catalog (attached databases) schema information for the database
/// </summary>
/// <param name="strCatalog">The catalog to retrieve, can be null</param>
/// <returns>DataTable</returns>
private DataTable Schema_Catalogs(string strCatalog)
{
var tbl = new DataTable("Catalogs");
DataRow row;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("CATALOG_NAME", typeof (string));
tbl.Columns.Add("DESCRIPTION", typeof (string));
tbl.Columns.Add("ID", typeof (long));
tbl.BeginLoadData();
using (var cmd = new SQLiteCommand("PRAGMA database_list", this))
using (SQLiteDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
if (String.Compare(rd.GetString(1), strCatalog, StringComparison.OrdinalIgnoreCase) == 0
|| strCatalog == null)
{
row = tbl.NewRow();
row["CATALOG_NAME"] = rd.GetString(1);
row["DESCRIPTION"] = rd.GetString(2);
row["ID"] = rd.GetInt64(0);
tbl.Rows.Add(row);
}
}
}
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
private DataTable Schema_DataTypes()
{
var tbl = new DataTable("DataTypes");
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("TypeName", typeof (String));
tbl.Columns.Add("ProviderDbType", typeof (int));
tbl.Columns.Add("ColumnSize", typeof (long));
tbl.Columns.Add("CreateFormat", typeof (String));
tbl.Columns.Add("CreateParameters", typeof (String));
tbl.Columns.Add("DataType", typeof (String));
tbl.Columns.Add("IsAutoIncrementable", typeof (bool));
tbl.Columns.Add("IsBestMatch", typeof (bool));
tbl.Columns.Add("IsCaseSensitive", typeof (bool));
tbl.Columns.Add("IsFixedLength", typeof (bool));
tbl.Columns.Add("IsFixedPrecisionScale", typeof (bool));
tbl.Columns.Add("IsLong", typeof (bool));
tbl.Columns.Add("IsNullable", typeof (bool));
tbl.Columns.Add("IsSearchable", typeof (bool));
tbl.Columns.Add("IsSearchableWithLike", typeof (bool));
tbl.Columns.Add("IsLiteralSupported", typeof (bool));
tbl.Columns.Add("LiteralPrefix", typeof (String));
tbl.Columns.Add("LiteralSuffix", typeof (String));
tbl.Columns.Add("IsUnsigned", typeof (bool));
tbl.Columns.Add("MaximumScale", typeof (short));
tbl.Columns.Add("MinimumScale", typeof (short));
tbl.Columns.Add("IsConcurrencyType", typeof (bool));
tbl.BeginLoadData();
var reader = new StringReader(SR.DataTypes);
tbl.ReadXml(reader);
reader.Close();
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Returns the base column information for indexes in a database
/// </summary>
/// <param name="strCatalog">The catalog to retrieve indexes for (can be null)</param>
/// <param name="strTable">The table to restrict index information by (can be null)</param>
/// <param name="strIndex">The index to restrict index information by (can be null)</param>
/// <param name="strColumn">The source column to restrict index information by (can be null)</param>
/// <returns>A DataTable containing the results</returns>
private DataTable Schema_IndexColumns(string strCatalog, string strTable, string strIndex, string strColumn)
{
var tbl = new DataTable("IndexColumns");
DataRow row;
var primaryKeys = new List<KeyValuePair<int, string>>();
bool maybeRowId;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("CONSTRAINT_CATALOG", typeof (string));
tbl.Columns.Add("CONSTRAINT_SCHEMA", typeof (string));
tbl.Columns.Add("CONSTRAINT_NAME", typeof (string));
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("COLUMN_NAME", typeof (string));
tbl.Columns.Add("ORDINAL_POSITION", typeof (int));
tbl.Columns.Add("INDEX_NAME", typeof (string));
tbl.Columns.Add("COLLATION_NAME", typeof (string));
tbl.Columns.Add("SORT_MODE", typeof (string));
tbl.Columns.Add("CONFLICT_OPTION", typeof (int));
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
tbl.BeginLoadData();
using (
var cmdTables =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
{
while (rdTables.Read())
{
maybeRowId = false;
primaryKeys.Clear();
if (String.IsNullOrEmpty(strTable) ||
String.Compare(rdTables.GetString(2), strTable, StringComparison.OrdinalIgnoreCase) == 0)
{
try
{
using (
var cmdTable =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].table_info([{1}])",
strCatalog, rdTables.GetString(2)), this))
using (SQLiteDataReader rdTable = cmdTable.ExecuteReader())
{
while (rdTable.Read())
{
if (rdTable.GetInt32(5) == 1) // is a primary key
{
primaryKeys.Add(new KeyValuePair<int, string>(rdTable.GetInt32(0),
rdTable.GetString(1)));
// Is an integer -- could be a rowid if no other primary keys exist in the table
if (
String.Compare(rdTable.GetString(2), "INTEGER",
StringComparison.OrdinalIgnoreCase) == 0)
maybeRowId = true;
}
}
}
}
catch (SQLiteException)
{
}
// This is a rowid row
if (primaryKeys.Count == 1 && maybeRowId)
{
row = tbl.NewRow();
row["CONSTRAINT_CATALOG"] = strCatalog;
row["CONSTRAINT_NAME"] = String.Format(CultureInfo.InvariantCulture, "{1}_PK_{0}",
rdTables.GetString(2), master);
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rdTables.GetString(2);
row["COLUMN_NAME"] = primaryKeys[0].Value;
row["INDEX_NAME"] = row["CONSTRAINT_NAME"];
row["ORDINAL_POSITION"] = 0; // primaryKeys[0].Key;
row["COLLATION_NAME"] = "BINARY";
row["SORT_MODE"] = "ASC";
row["CONFLICT_OPTION"] = 2;
if (String.IsNullOrEmpty(strIndex) ||
String.Compare(strIndex, (string) row["INDEX_NAME"], StringComparison.OrdinalIgnoreCase) ==
0)
tbl.Rows.Add(row);
}
using (
var cmdIndexes =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{2}] WHERE [type] LIKE 'index' AND [tbl_name] LIKE '{1}'",
strCatalog, rdTables.GetString(2).Replace("'", "''"), master), this))
using (SQLiteDataReader rdIndexes = cmdIndexes.ExecuteReader())
{
while (rdIndexes.Read())
{
int ordinal = 0;
if (String.IsNullOrEmpty(strIndex) ||
String.Compare(strIndex, rdIndexes.GetString(1), StringComparison.OrdinalIgnoreCase) ==
0)
{
try
{
using (
var cmdIndex =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"PRAGMA [{0}].index_info([{1}])", strCatalog,
rdIndexes.GetString(1)), this))
using (SQLiteDataReader rdIndex = cmdIndex.ExecuteReader())
{
while (rdIndex.Read())
{
row = tbl.NewRow();
row["CONSTRAINT_CATALOG"] = strCatalog;
row["CONSTRAINT_NAME"] = rdIndexes.GetString(1);
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = rdIndexes.GetString(2);
row["COLUMN_NAME"] = rdIndex.GetString(2);
row["INDEX_NAME"] = rdIndexes.GetString(1);
row["ORDINAL_POSITION"] = ordinal; // rdIndex.GetInt32(1);
string collationSequence;
int sortMode;
int onError;
_sql.GetIndexColumnExtendedInfo(strCatalog, rdIndexes.GetString(1),
rdIndex.GetString(2), out sortMode,
out onError, out collationSequence);
if (String.IsNullOrEmpty(collationSequence) == false)
row["COLLATION_NAME"] = collationSequence;
row["SORT_MODE"] = (sortMode == 0) ? "ASC" : "DESC";
row["CONFLICT_OPTION"] = onError;
ordinal++;
if (String.IsNullOrEmpty(strColumn) ||
String.Compare(strColumn, row["COLUMN_NAME"].ToString(),
StringComparison.OrdinalIgnoreCase) == 0)
tbl.Rows.Add(row);
}
}
}
catch (SQLiteException)
{
}
}
}
}
}
}
}
tbl.EndLoadData();
tbl.AcceptChanges();
return tbl;
}
/// <summary>
/// Returns detailed column information for a specified view
/// </summary>
/// <param name="strCatalog">The catalog to retrieve columns for (can be null)</param>
/// <param name="strView">The view to restrict column information by (can be null)</param>
/// <param name="strColumn">The source column to restrict column information by (can be null)</param>
/// <returns>A DataTable containing the results</returns>
private DataTable Schema_ViewColumns(string strCatalog, string strView, string strColumn)
{
var tbl = new DataTable("ViewColumns");
DataRow row;
string strSql;
int n;
DataRow schemaRow;
DataRow viewRow;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("VIEW_CATALOG", typeof (string));
tbl.Columns.Add("VIEW_SCHEMA", typeof (string));
tbl.Columns.Add("VIEW_NAME", typeof (string));
tbl.Columns.Add("VIEW_COLUMN_NAME", typeof (String));
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("COLUMN_NAME", typeof (string));
tbl.Columns.Add("ORDINAL_POSITION", typeof (int));
tbl.Columns.Add("COLUMN_HASDEFAULT", typeof (bool));
tbl.Columns.Add("COLUMN_DEFAULT", typeof (string));
tbl.Columns.Add("COLUMN_FLAGS", typeof (long));
tbl.Columns.Add("IS_NULLABLE", typeof (bool));
tbl.Columns.Add("DATA_TYPE", typeof (string));
tbl.Columns.Add("CHARACTER_MAXIMUM_LENGTH", typeof (int));
tbl.Columns.Add("NUMERIC_PRECISION", typeof (int));
tbl.Columns.Add("NUMERIC_SCALE", typeof (int));
tbl.Columns.Add("DATETIME_PRECISION", typeof (long));
tbl.Columns.Add("CHARACTER_SET_CATALOG", typeof (string));
tbl.Columns.Add("CHARACTER_SET_SCHEMA", typeof (string));
tbl.Columns.Add("CHARACTER_SET_NAME", typeof (string));
tbl.Columns.Add("COLLATION_CATALOG", typeof (string));
tbl.Columns.Add("COLLATION_SCHEMA", typeof (string));
tbl.Columns.Add("COLLATION_NAME", typeof (string));
tbl.Columns.Add("PRIMARY_KEY", typeof (bool));
tbl.Columns.Add("EDM_TYPE", typeof (string));
tbl.Columns.Add("AUTOINCREMENT", typeof (bool));
tbl.Columns.Add("UNIQUE", typeof (bool));
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
tbl.BeginLoadData();
using (
var cmdViews =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'view'",
strCatalog, master), this))
using (SQLiteDataReader rdViews = cmdViews.ExecuteReader())
{
while (rdViews.Read())
{
if (String.IsNullOrEmpty(strView) ||
String.Compare(strView, rdViews.GetString(2), StringComparison.OrdinalIgnoreCase) == 0)
{
using (
var cmdViewSelect =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}]", strCatalog,
rdViews.GetString(2)), this))
{
strSql = rdViews.GetString(4).Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' ');
n = CultureInfo.InvariantCulture.CompareInfo.IndexOf(strSql, " AS ",
CompareOptions.IgnoreCase);
if (n < 0)
continue;
strSql = strSql.Substring(n + 4);
using (var cmd = new SQLiteCommand(strSql, this))
using (
SQLiteDataReader rdViewSelect = cmdViewSelect.ExecuteReader(CommandBehavior.SchemaOnly))
using (SQLiteDataReader rd = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
using (DataTable tblSchemaView = rdViewSelect.GetSchemaTable(false, false))
using (DataTable tblSchema = rd.GetSchemaTable(false, false))
{
for (n = 0; n < tblSchema.Rows.Count; n++)
{
viewRow = tblSchemaView.Rows[n];
schemaRow = tblSchema.Rows[n];
if (
String.Compare(viewRow[SchemaTableColumn.ColumnName].ToString(), strColumn,
StringComparison.OrdinalIgnoreCase) == 0
|| strColumn == null)
{
row = tbl.NewRow();
row["VIEW_CATALOG"] = strCatalog;
row["VIEW_NAME"] = rdViews.GetString(2);
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_SCHEMA"] = schemaRow[SchemaTableColumn.BaseSchemaName];
row["TABLE_NAME"] = schemaRow[SchemaTableColumn.BaseTableName];
row["COLUMN_NAME"] = schemaRow[SchemaTableColumn.BaseColumnName];
row["VIEW_COLUMN_NAME"] = viewRow[SchemaTableColumn.ColumnName];
row["COLUMN_HASDEFAULT"] = (viewRow[SchemaTableOptionalColumn.DefaultValue] !=
DBNull.Value);
row["COLUMN_DEFAULT"] = viewRow[SchemaTableOptionalColumn.DefaultValue];
row["ORDINAL_POSITION"] = viewRow[SchemaTableColumn.ColumnOrdinal];
row["IS_NULLABLE"] = viewRow[SchemaTableColumn.AllowDBNull];
row["DATA_TYPE"] = viewRow["DataTypeName"];
// SQLiteConvert.DbTypeToType((DbType)viewRow[SchemaTableColumn.ProviderType]).ToString();
row["EDM_TYPE"] =
SQLiteConvert.DbTypeToTypeName(
(DbType) viewRow[SchemaTableColumn.ProviderType]).ToLower(
CultureInfo.InvariantCulture);
row["CHARACTER_MAXIMUM_LENGTH"] = viewRow[SchemaTableColumn.ColumnSize];
row["TABLE_SCHEMA"] = viewRow[SchemaTableColumn.BaseSchemaName];
row["PRIMARY_KEY"] = viewRow[SchemaTableColumn.IsKey];
row["AUTOINCREMENT"] = viewRow[SchemaTableOptionalColumn.IsAutoIncrement];
row["COLLATION_NAME"] = viewRow["CollationType"];
row["UNIQUE"] = viewRow[SchemaTableColumn.IsUnique];
tbl.Rows.Add(row);
}
}
}
}
}
}
}
tbl.EndLoadData();
tbl.AcceptChanges();
return tbl;
}
/// <summary>
/// Retrieves foreign key information from the specified set of filters
/// </summary>
/// <param name="strCatalog">An optional catalog to restrict results on</param>
/// <param name="strTable">An optional table to restrict results on</param>
/// <param name="strKeyName">An optional foreign key name to restrict results on</param>
/// <returns>A DataTable with the results of the query</returns>
private DataTable Schema_ForeignKeys(string strCatalog, string strTable, string strKeyName)
{
var tbl = new DataTable("ForeignKeys");
DataRow row;
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add("CONSTRAINT_CATALOG", typeof (string));
tbl.Columns.Add("CONSTRAINT_SCHEMA", typeof (string));
tbl.Columns.Add("CONSTRAINT_NAME", typeof (string));
tbl.Columns.Add("TABLE_CATALOG", typeof (string));
tbl.Columns.Add("TABLE_SCHEMA", typeof (string));
tbl.Columns.Add("TABLE_NAME", typeof (string));
tbl.Columns.Add("CONSTRAINT_TYPE", typeof (string));
tbl.Columns.Add("IS_DEFERRABLE", typeof (bool));
tbl.Columns.Add("INITIALLY_DEFERRED", typeof (bool));
tbl.Columns.Add("FKEY_FROM_COLUMN", typeof (string));
tbl.Columns.Add("FKEY_FROM_ORDINAL_POSITION", typeof (int));
tbl.Columns.Add("FKEY_TO_CATALOG", typeof (string));
tbl.Columns.Add("FKEY_TO_SCHEMA", typeof (string));
tbl.Columns.Add("FKEY_TO_TABLE", typeof (string));
tbl.Columns.Add("FKEY_TO_COLUMN", typeof (string));
if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
string master = (String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0)
? _tempmasterdb
: _masterdb;
tbl.BeginLoadData();
using (
var cmdTables =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
{
while (rdTables.Read())
{
if (String.IsNullOrEmpty(strTable) ||
String.Compare(strTable, rdTables.GetString(2), StringComparison.OrdinalIgnoreCase) == 0)
{
try
{
using (var builder = new SQLiteCommandBuilder())
//using (SQLiteCommand cmdTable = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}]", strCatalog, rdTables.GetString(2)), this))
//using (SQLiteDataReader rdTable = cmdTable.ExecuteReader(CommandBehavior.SchemaOnly))
using (
var cmdKey =
new SQLiteCommand(
String.Format(CultureInfo.InvariantCulture,
"PRAGMA [{0}].foreign_key_list([{1}])", strCatalog,
rdTables.GetString(2)), this))
using (SQLiteDataReader rdKey = cmdKey.ExecuteReader())
{
while (rdKey.Read())
{
row = tbl.NewRow();
row["CONSTRAINT_CATALOG"] = strCatalog;
row["CONSTRAINT_NAME"] = String.Format(CultureInfo.InvariantCulture, "FK_{0}_{1}",
rdTables[2], rdKey.GetInt32(0));
row["TABLE_CATALOG"] = strCatalog;
row["TABLE_NAME"] = builder.UnquoteIdentifier(rdTables.GetString(2));
row["CONSTRAINT_TYPE"] = "FOREIGN KEY";
row["IS_DEFERRABLE"] = false;
row["INITIALLY_DEFERRED"] = false;
row["FKEY_FROM_COLUMN"] = builder.UnquoteIdentifier(rdKey[3].ToString());
row["FKEY_TO_CATALOG"] = strCatalog;
row["FKEY_TO_TABLE"] = builder.UnquoteIdentifier(rdKey[2].ToString());
row["FKEY_TO_COLUMN"] = builder.UnquoteIdentifier(rdKey[4].ToString());
row["FKEY_FROM_ORDINAL_POSITION"] = rdKey[1];
if (String.IsNullOrEmpty(strKeyName) ||
String.Compare(strKeyName, row["CONSTRAINT_NAME"].ToString(),
StringComparison.OrdinalIgnoreCase) == 0)
tbl.Rows.Add(row);
}
}
}
catch (SQLiteException)
{
}
}
}
}
tbl.EndLoadData();
tbl.AcceptChanges();
return tbl;
}
/// <summary>
/// This event is raised whenever SQLite makes an update/delete/insert into the database on
/// this connection. It only applies to the given connection.
/// </summary>
public event SQLiteUpdateEventHandler Update
{
add
{
if (_updateHandler == null)
{
_updateCallback = new SQLiteUpdateCallback(UpdateCallback);
if (_sql != null) _sql.SetUpdateHook(_updateCallback);
}
_updateHandler += value;
}
remove
{
_updateHandler -= value;
if (_updateHandler == null)
{
if (_sql != null) _sql.SetUpdateHook(null);
_updateCallback = null;
}
}
}
private void UpdateCallback(IntPtr puser, int type, IntPtr database, IntPtr table, Int64 rowid)
{
_updateHandler(this, new UpdateEventArgs(
SQLiteConvert.UTF8ToString(database, -1),
SQLiteConvert.UTF8ToString(table, -1),
(UpdateEventType) type,
rowid));
}
/// <summary>
/// This event is raised whenever SQLite is committing a transaction.
/// Return non-zero to trigger a rollback
/// </summary>
public event SQLiteCommitHandler Commit
{
add
{
if (_commitHandler == null)
{
_commitCallback = new SQLiteCommitCallback(CommitCallback);
if (_sql != null) _sql.SetCommitHook(_commitCallback);
}
_commitHandler += value;
}
remove
{
_commitHandler -= value;
if (_commitHandler == null)
{
if (_sql != null) _sql.SetCommitHook(null);
_commitCallback = null;
}
}
}
/// <summary>
/// This event is raised whenever SQLite is committing a transaction.
/// Return non-zero to trigger a rollback
/// </summary>
public event EventHandler RollBack
{
add
{
if (_rollbackHandler == null)
{
_rollbackCallback = new SQLiteRollbackCallback(RollbackCallback);
if (_sql != null) _sql.SetRollbackHook(_rollbackCallback);
}
_rollbackHandler += value;
}
remove
{
_rollbackHandler -= value;
if (_rollbackHandler == null)
{
if (_sql != null) _sql.SetRollbackHook(null);
_rollbackCallback = null;
}
}
}
private int CommitCallback(IntPtr parg)
{
var e = new CommitEventArgs();
_commitHandler(this, e);
return e.AbortTransaction ? 1 : 0;
}
private void RollbackCallback(IntPtr parg)
{
_rollbackHandler(this, EventArgs.Empty);
}
}
/// <summary>
/// The I/O file cache flushing behavior for the connection
/// </summary>
public enum SynchronizationModes
{
/// <summary>
/// Normal file flushing at critical sections of the code
/// </summary>
Normal = 0,
/// <summary>
/// Full file flushing after every write operation
/// </summary>
Full = 1,
/// <summary>
/// Use the default operating system's file flushing, SQLite does not explicitly flush the file buffers after writing
/// </summary>
Off = 2,
}
#if !PLATFORM_COMPACTFRAMEWORK
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
internal delegate void SQLiteUpdateCallback(IntPtr puser, int type, IntPtr database, IntPtr table, Int64 rowid);
#if !PLATFORM_COMPACTFRAMEWORK
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
internal delegate int SQLiteCommitCallback(IntPtr puser);
#if !PLATFORM_COMPACTFRAMEWORK
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
internal delegate void SQLiteRollbackCallback(IntPtr puser);
/// <summary>
/// Raised when a transaction is about to be committed. To roll back a transaction, set the
/// rollbackTrans boolean value to true.
/// </summary>
/// <param name="sender">The connection committing the transaction</param>
/// <param name="e">Event arguments on the transaction</param>
public delegate void SQLiteCommitHandler(object sender, CommitEventArgs e);
/// <summary>
/// Raised when data is inserted, updated and deleted on a given connection
/// </summary>
/// <param name="sender">The connection committing the transaction</param>
/// <param name="e">The event parameters which triggered the event</param>
public delegate void SQLiteUpdateEventHandler(object sender, UpdateEventArgs e);
/// <summary>
/// Whenever an update event is triggered on a connection, this enum will indicate
/// exactly what type of operation is being performed.
/// </summary>
public enum UpdateEventType
{
/// <summary>
/// A row is being deleted from the given database and table
/// </summary>
Delete = 9,
/// <summary>
/// A row is being inserted into the table.
/// </summary>
Insert = 18,
/// <summary>
/// A row is being updated in the table.
/// </summary>
Update = 23,
}
/// <summary>
/// Passed during an Update callback, these event arguments detail the type of update operation being performed
/// on the given connection.
/// </summary>
public class UpdateEventArgs : EventArgs
{
/// <summary>
/// The name of the database being updated (usually "main" but can be any attached or temporary database)
/// </summary>
public readonly string Database;
/// <summary>
/// The type of update being performed (insert/update/delete)
/// </summary>
public readonly UpdateEventType Event;
/// <summary>
/// The RowId affected by this update.
/// </summary>
public readonly Int64 RowId;
/// <summary>
/// The name of the table being updated
/// </summary>
public readonly string Table;
internal UpdateEventArgs(string database, string table, UpdateEventType eventType, Int64 rowid)
{
Database = database;
Table = table;
Event = eventType;
RowId = rowid;
}
}
/// <summary>
/// Event arguments raised when a transaction is being committed
/// </summary>
public class CommitEventArgs : EventArgs
{
/// <summary>
/// Set to true to abort the transaction and trigger a rollback
/// </summary>
public bool AbortTransaction;
internal CommitEventArgs()
{
}
}
}