unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 75d0cbbb367813c1c1ece2b2a145e8d2d5a05384 230164 bytes (raw)
name: lisp/progmodes/sql.el 	 # note: path name is non-authoritative(*)

   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
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
 
;;; sql.el --- specialized comint.el for SQL interpreters  -*- lexical-binding: t -*-

;; Copyright (C) 1998-2023 Free Software Foundation, Inc.

;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Michael Mauger <michael@mauger.com>
;; Version: 3.6
;; Keywords: comm languages processes

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Please send bug reports and bug fixes to the mailing list at
;; bug-gnu-emacs@gnu.org.
;; See also the general help list at
;; https://lists.gnu.org/mailman/listinfo/help-gnu-emacs
;; I monitor this list actively.  If you send an e-mail
;; to Alex Schroeder it usually makes it to me when Alex has a chance
;; to forward them along (Thanks, Alex).

;; This file provides a sql-mode and a sql-interactive-mode.  The
;; original goals were two simple modes providing syntactic
;; highlighting.  The interactive mode had to provide a command-line
;; history; the other mode had to provide "send region/buffer to SQL
;; interpreter" functions.  "simple" in this context means easy to
;; use, easy to maintain and little or no bells and whistles.  This
;; has changed somewhat as experience with the mode has accumulated.

;; Support for different flavors of SQL and command interpreters was
;; available in early versions of sql.el.  This support has been
;; extended and formalized in later versions.  Part of the impetus for
;; the improved support of SQL flavors was borne out of the current
;; maintainers consulting experience.  In the past twenty years, I
;; have used Oracle, Sybase, Informix, MySQL, Postgres, and SQLServer.
;; On some assignments, I have used two or more of these concurrently.

;; If anybody feels like extending this sql mode, take a look at the
;; above mentioned modes and write a sqlx-mode on top of this one.  If
;; this proves to be difficult, please suggest changes that will
;; facilitate your plans.  Facilities have been provided to add
;; products and product-specific configuration.

;; sql-interactive-mode is used to interact with a SQL interpreter
;; process in a SQLi buffer (usually called `*SQL*').  The SQLi buffer
;; is created by calling a SQL interpreter-specific entry function or
;; sql-product-interactive.  Do *not* call sql-interactive-mode by
;; itself.

;; The list of currently supported interpreters and the corresponding
;; entry function used to create the SQLi buffers is shown with
;; `sql-help' (M-x sql-help).

;; Since sql-interactive-mode is built on top of the general
;; command-interpreter-in-a-buffer mode (comint mode), it shares a
;; common base functionality, and a common set of bindings, with all
;; modes derived from comint mode.  This makes these modes easier to
;; use.

;; sql-mode can be used to keep editing SQL statements.  The SQL
;; statements can be sent to the SQL process in the SQLi buffer.

;; For documentation on the functionality provided by comint mode, and
;; the hooks available for customizing it, see the file `comint.el'.

;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
;; `imenu-add-menubar-index'.

;;; Bugs:

;; sql-ms now uses osql instead of isql.  Osql flushes its error
;; stream more frequently than isql so that error messages are
;; available.  There is no prompt and some output still is buffered.
;; This improves the interaction under Emacs but it still is somewhat
;; awkward.

;; Quoted identifiers are not supported for highlighting.  Most
;; databases support the use of double quoted strings in place of
;; identifiers; ms (Microsoft SQLServer) also supports identifiers
;; enclosed within brackets [].

;;; Product Support:

;; To add support for additional SQL products the following steps
;; must be followed ("xyz" is the name of the product in the examples
;; below):

;; 1) Add the product to the list of known products.

;;     (sql-add-product 'xyz "XyzDB"
;;     	                '(:free-software t))

;; 2) Define font lock settings.  All ANSI keywords will be
;;    highlighted automatically, so only product specific keywords
;;    need to be defined here.

;;     (defvar my-sql-mode-xyz-font-lock-keywords
;;       '(("\\b\\(red\\|orange\\|yellow\\)\\b"
;;          . font-lock-keyword-face))
;;       "XyzDB SQL keywords used by font-lock.")

;;     (sql-set-product-feature 'xyz
;;                              :font-lock
;;                              'my-sql-mode-xyz-font-lock-keywords)

;; 3) Define any special syntax characters including comments and
;;    identifier characters.

;;     (sql-set-product-feature 'xyz
;;                              :syntax-alist ((?# . "_")))

;; 4) Define the interactive command interpreter for the database
;;    product.

;;     (defcustom my-sql-xyz-program "ixyz"
;;       "Command to start ixyz by XyzDB."
;;       :type 'file
;;       :group 'SQL)
;;
;;     (sql-set-product-feature 'xyz
;;                              :sqli-program 'my-sql-xyz-program)
;;     (sql-set-product-feature 'xyz
;;                              :prompt-regexp "^xyzdb> ")
;;     (sql-set-product-feature 'xyz
;;                              :prompt-length 7)

;; 5) Define login parameters and command line formatting.

;;     (defcustom my-sql-xyz-login-params '(user password server database)
;;       "Login parameters to needed to connect to XyzDB."
;;       :type 'sql-login-params
;;       :group 'SQL)
;;
;;     (sql-set-product-feature 'xyz
;;                              :sqli-login 'my-sql-xyz-login-params)

;;     (defcustom my-sql-xyz-options '("-X" "-Y" "-Z")
;;       "List of additional options for `sql-xyz-program'."
;;       :type '(repeat string)
;;       :group 'SQL)
;;
;;     (sql-set-product-feature 'xyz
;;                              :sqli-options 'my-sql-xyz-options))

;;     (defun my-sql-comint-xyz (product options &optional buf-name)
;;       "Connect ti XyzDB in a comint buffer."
;;
;;         ;; Do something with `sql-user', `sql-password',
;;         ;; `sql-database', and `sql-server'.
;;         (let ((params
;;                (append
;;           (if (not (string= "" sql-user))
;;                     (list "-U" sql-user))
;;                 (if (not (string= "" sql-password))
;;                     (list "-P" sql-password))
;;                 (if (not (string= "" sql-database))
;;                     (list "-D" sql-database))
;;                 (if (not (string= "" sql-server))
;;                     (list "-S" sql-server))
;;                 options)))
;;           (sql-comint product params buf-name)))
;;
;;     (sql-set-product-feature 'xyz
;;                              :sqli-comint-func 'my-sql-comint-xyz)

;; 6) Define a convenience function to invoke the SQL interpreter.

;;     (defun my-sql-xyz (&optional buffer)
;;       "Run ixyz by XyzDB as an inferior process."
;;       (interactive "P")
;;       (sql-product-interactive 'xyz buffer))

;;; To Do:

;; Improve keyword highlighting for individual products.  I have tried
;; to update those database that I use.  Feel free to send me updates,
;; or direct me to the reference manuals for your favorite database.

;; When there are no keywords defined, the ANSI keywords are
;; highlighted.  ANSI keywords are highlighted even if the keyword is
;; not used for your current product.  This should help identify
;; portability concerns.

;; Add different highlighting levels.

;; Add support for listing available tables or the columns in a table.

;;; Thanks to all the people who helped me out:

;; Alex Schroeder <alex@gnu.org> -- the original author
;; Kai Blauberg <kai.blauberg@metla.fi>
;; <ibalaban@dalet.com>
;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
;; nino <nino@inform.dk>
;; Berend de Boer <berend@pobox.com>
;; Adam Jenkins <adam@thejenkins.org>
;; Michael Mauger <michael@mauger.com> -- improved product support
;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
;; Harald Maier <maierh@myself.com> -- sql-send-string
;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections;
;;      code polish; on-going guidance and mentorship
;; Paul Sleigh <bat@flurf.net> -- MySQL keyword enhancement
;; Andrew Schein <andrew@andrewschein.com> -- sql-port bug
;; Ian Bjorhovde <idbjorh@dataproxy.com> -- db2 escape newlines
;;      incorrectly enabled by default
;; Roman Scherer <roman.scherer@nugg.ad> -- Connection documentation
;; Mark Wilkinson <wilkinsonmr@gmail.com> -- file-local variables ignored
;; Simen Heggestøyl <simenheg@gmail.com> -- Postgres database completion
;; Robert Cochran <robert-emacs@cochranmail.com> -- MariaDB support
;; Alex Harsanyi <alexharsanyi@gmail.com> -- sql-indent package and support
;; Roy Mathew <rmathew8@gmail.com> -- bug in `sql-send-string'
;;

\f

;;; Code:

(require 'cl-lib)
(require 'comint)
(require 'thingatpt)
(require 'view)
(eval-when-compile (require 'subr-x))   ; string-empty-p

(defvar font-lock-keyword-face)
(defvar font-lock-set-defaults)
(defvar font-lock-string-face)

;;; Allow customization

(defgroup SQL nil
  "Running a SQL interpreter from within Emacs buffers."
  :version "20.4"
  :group 'languages
  :group 'processes)

;; These five variables will be used as defaults, if set.

(defcustom sql-user ""
  "Default username."
  :type 'string
  :safe 'stringp)

(defcustom sql-password ""
  "Default password.
If you customize this, the value will be stored in your init
file.  Since that is a plaintext file, this could be dangerous."
  :type 'string
  :risky t)

(defcustom sql-database ""
  "Default database."
  :type 'string
  :safe 'stringp)

(defcustom sql-server ""
  "Default server or host."
  :type 'string
  :safe 'stringp)

(defcustom sql-port 0
  "Default port for connecting to a MySQL or Postgres server."
  :version "24.1"
  :type 'natnum
  :safe 'natnump)

(defcustom sql-default-directory nil
  "Default directory for SQL processes."
  :version "25.1"
  :type '(choice (const nil) string)
  :safe 'stringp)

;; Login parameter type

;; This seems too prescriptive.  It probably fails to match some of
;; the possible combinations.  It would probably be better to just use
;; plist for most of it.
(define-widget 'sql-login-params 'lazy
  "Widget definition of the login parameters list"
  :tag "Login Parameters"
  :type '(set :tag "Login Parameters"
              (choice :tag "user"
                      :value user
                      (const user)
                      (list :tag "Specify a default"
                            (const user)
                            (list :tag "Default"
                                  :inline t (const :default) string)))
              (const password)
              (choice :tag "server"
                      :value server
                      (const server)
                      (list :tag "Specify a default"
                            (const server)
                            (list :tag "Default"
                                  :inline t (const :default) string))
                      (list :tag "file"
                            (const :format "" server)
                            (const :format "" :file)
                            regexp)
                      (list :tag "completion"
                            (const :format "" server)
                            (const :format "" :completion)
                            (const :format "" :must-match)
                            (restricted-sexp
                             :match-alternatives (listp stringp))))
              (choice :tag "database"
                      :value database
                      (const database)
                      (list :tag "Specify a default"
                            (const database)
                            (list :tag "Default"
                                  :inline t (const :default) string))
                      (list :tag "file"
                            (const :format "" database)
                            (const :format "" :file)
                            (choice (const nil) regexp)
                            (const :format "" :must-match)
                            (symbol :tag ":must-match"))
                      (list :tag "completion"
                            (const :format "" database)
                            (const :format "" :default)
                            (string :tag ":default")
                            (const :format "" :completion)
                            (sexp :tag ":completion")
                            (const :format "" :must-match)
                            (symbol :tag ":must-match")))
              (const port)))

;; SQL Product support

(defvar sql-interactive-product nil
  "Product under `sql-interactive-mode'.")

(defvar sql-connection nil
  "Connection name if interactive session started by `sql-connect'.")

(defvar sql-product-alist
  '((ansi
     :name "ANSI"
     :font-lock sql-mode-ansi-font-lock-keywords
     :statement sql-ansi-statement-starters)

    (db2
     :name "DB2"
     :font-lock sql-mode-db2-font-lock-keywords
     :sqli-program sql-db2-program
     :sqli-options sql-db2-options
     :sqli-login sql-db2-login-params
     :sqli-comint-func sql-comint-db2
     :prompt-regexp "^db2 => "
     :prompt-length 7
     :prompt-cont-regexp "^db2 (cont\\.) => "
     :input-filter sql-escape-newlines-filter)

    (informix
     :name "Informix"
     :font-lock sql-mode-informix-font-lock-keywords
     :sqli-program sql-informix-program
     :sqli-options sql-informix-options
     :sqli-login sql-informix-login-params
     :sqli-comint-func sql-comint-informix
     :prompt-regexp "^> "
     :prompt-length 2
     :syntax-alist ((?{ . "<") (?} . ">")))

    (ingres
     :name "Ingres"
     :font-lock sql-mode-ingres-font-lock-keywords
     :sqli-program sql-ingres-program
     :sqli-options sql-ingres-options
     :sqli-login sql-ingres-login-params
     :sqli-comint-func sql-comint-ingres
     :prompt-regexp "^\\* "
     :prompt-length 2
     :prompt-cont-regexp "^\\* ")

    (interbase
     :name "Interbase"
     :font-lock sql-mode-interbase-font-lock-keywords
     :sqli-program sql-interbase-program
     :sqli-options sql-interbase-options
     :sqli-login sql-interbase-login-params
     :sqli-comint-func sql-comint-interbase
     :prompt-regexp "^SQL> "
     :prompt-length 5)

    (linter
     :name "Linter"
     :font-lock sql-mode-linter-font-lock-keywords
     :sqli-program sql-linter-program
     :sqli-options sql-linter-options
     :sqli-login sql-linter-login-params
     :sqli-comint-func sql-comint-linter
     :prompt-regexp "^SQL>"
     :prompt-length 4)

    (mariadb
     :name "MariaDB"
     :free-software t
     :font-lock sql-mode-mariadb-font-lock-keywords
     :sqli-program sql-mariadb-program
     :sqli-options sql-mariadb-options
     :sqli-login sql-mariadb-login-params
     :sqli-comint-func sql-comint-mariadb
     :list-all "SHOW TABLES;"
     :list-table "DESCRIBE %s;"
     :prompt-regexp "^MariaDB \\[.*]> "
     :prompt-cont-regexp "^    [\"'`-]> "
     :syntax-alist ((?# . "< b"))
     :input-filter sql-remove-tabs-filter)

    (ms
     :name "Microsoft"
     :font-lock sql-mode-ms-font-lock-keywords
     :sqli-program sql-ms-program
     :sqli-options sql-ms-options
     :sqli-login sql-ms-login-params
     :sqli-comint-func sql-comint-ms
     :prompt-regexp "^[0-9]*>"
     :prompt-cont-regexp "^[0-9]*>"
     :prompt-length 5
     :syntax-alist ((?@ . "_"))
     :terminator ("^go" . "go"))

    (mysql
     :name "MySQL"
     :free-software t
     :font-lock sql-mode-mysql-font-lock-keywords
     :sqli-program sql-mysql-program
     :sqli-options sql-mysql-options
     :sqli-login sql-mysql-login-params
     :sqli-comint-func sql-comint-mysql
     :list-all "SHOW TABLES;"
     :list-table "DESCRIBE %s;"
     :prompt-regexp "^mysql> "
     :prompt-length 6
     :prompt-cont-regexp "^    -> "
     :syntax-alist ((?# . "< b") (?\\ . "\\"))
     :input-filter sql-remove-tabs-filter)

    (oracle
     :name "Oracle"
     :font-lock sql-mode-oracle-font-lock-keywords
     :sqli-program sql-oracle-program
     :sqli-options sql-oracle-options
     :sqli-login sql-oracle-login-params
     :sqli-comint-func sql-comint-oracle
     :list-all sql-oracle-list-all
     :list-table sql-oracle-list-table
     :completion-object sql-oracle-completion-object
     :prompt-regexp "^SQL> "
     :prompt-length 5
     :prompt-cont-regexp "^\\(?:[ ][ ][1-9]\\|[ ][1-9][0-9]\\|[1-9][0-9]\\{2\\}\\)[ ]\\{2\\}"
     :statement sql-oracle-statement-starters
     :syntax-alist ((?$ . "_") (?# . "_"))
     :terminator ("\\(^/\\|;\\)" . "/")
     :input-filter sql-placeholders-filter)

    (postgres
     :name "Postgres"
     :free-software t
     :font-lock sql-mode-postgres-font-lock-keywords
     :sqli-program sql-postgres-program
     :sqli-options sql-postgres-options
     :sqli-login sql-postgres-login-params
     :sqli-comint-func sql-comint-postgres
     :list-all ("\\d+" . "\\dS+")
     :list-table ("\\d+ %s" . "\\dS+ %s")
     :completion-object sql-postgres-completion-object
     :prompt-regexp "^[-[:alnum:]_]*[-=][#>] "
     :prompt-length 5
     :prompt-cont-regexp "^[-[:alnum:]_]*[-'(][#>] "
     :statement sql-postgres-statement-starters
     :input-filter sql-remove-tabs-filter
     :terminator ("\\(^\\s-*\\\\g\\|;\\)" . "\\g"))

    (solid
     :name "Solid"
     :font-lock sql-mode-solid-font-lock-keywords
     :sqli-program sql-solid-program
     :sqli-options sql-solid-options
     :sqli-login sql-solid-login-params
     :sqli-comint-func sql-comint-solid
     :prompt-regexp "^"
     :prompt-length 0)

    (sqlite
     :name "SQLite"
     :free-software t
     :font-lock sql-mode-sqlite-font-lock-keywords
     :sqli-program sql-sqlite-program
     :sqli-options sql-sqlite-options
     :sqli-login sql-sqlite-login-params
     :sqli-comint-func sql-comint-sqlite
     :list-all ".tables"
     :list-table ".schema %s"
     :completion-object sql-sqlite-completion-object
     :prompt-regexp "^sqlite> "
     :prompt-length 8
     :prompt-cont-regexp "^   \\.\\.\\.> ")

    (sybase
     :name "Sybase"
     :font-lock sql-mode-sybase-font-lock-keywords
     :sqli-program sql-sybase-program
     :sqli-options sql-sybase-options
     :sqli-login sql-sybase-login-params
     :sqli-comint-func sql-comint-sybase
     :prompt-regexp "^SQL> "
     :prompt-length 5
     :syntax-alist ((?@ . "_"))
     :terminator ("^go" . "go"))

    (vertica
     :name "Vertica"
     :sqli-program sql-vertica-program
     :sqli-options sql-vertica-options
     :sqli-login sql-vertica-login-params
     :sqli-comint-func sql-comint-vertica
     :list-all ("\\d" . "\\dS")
     :list-table "\\d %s"
     :prompt-regexp "^[[:alnum:]_]*=[#>] "
     :prompt-length 5
     :prompt-cont-regexp "^[[:alnum:]_]*[-(][#>] ")
    )
  "An alist of product specific configuration settings.

Without an entry in this list a product will not be properly
highlighted and will not support `sql-interactive-mode'.

Each element in the list is in the following format:

 (PRODUCT FEATURE VALUE ...)

where PRODUCT is the appropriate value of `sql-product'.  The
product name is then followed by FEATURE-VALUE pairs.  If a
FEATURE is not specified, its VALUE is treated as nil.  FEATURE
may be any one of the following:

 :name                  string containing the displayable name of
                        the product.

 :free-software         is the product Free (as in Freedom) software?

 :font-lock             name of the variable containing the product
                        specific font lock highlighting patterns.

 :sqli-program          name of the variable containing the product
                        specific interactive program name.

 :sqli-options          name of the variable containing the list
                        of product specific options.

 :sqli-login            name of the variable containing the list of
                        login parameters (i.e., user, password,
                        database and server) needed to connect to
                        the database.

 :sqli-comint-func      function of two arguments, PRODUCT
                        and OPTIONS, that will open a comint buffer
                        and connect to the database.  PRODUCT is the
                        first argument to be passed to `sql-comint',
                        and OPTIONS should be included in its second
                        argument.  The function should use the values
                        of `sql-user', `sql-password', `sql-database',
                        `sql-server' and `sql-port' to .  Do product
                        specific configuration of comint in this
                        function.  See `sql-comint-oracle' for an
                        example of such a function.

 :list-all              Command string or function which produces
                        a listing of all objects in the database.
                        If it's a cons cell, then the car
                        produces the standard list of objects and
                        the cdr produces an enhanced list of
                        objects.  What \"enhanced\" means is
                        dependent on the SQL product and may not
                        exist.  In general though, the
                        \"enhanced\" list should include visible
                        objects from other schemas.

 :list-table            Command string or function which produces
                        a detailed listing of a specific database
                        table.  If its a cons cell, then the car
                        produces the standard list and the cdr
                        produces an enhanced list.

 :completion-object     A function that returns a list of
                        objects.  Called with a single
                        parameter--if nil then list objects
                        accessible in the current schema, if
                        not-nil it is the name of a schema whose
                        objects should be listed.

 :completion-column     A function that returns a list of
                        columns.  Called with a single
                        parameter--if nil then list objects
                        accessible in the current schema, if
                        not-nil it is the name of a schema whose
                        objects should be listed.

 :prompt-regexp         regular expression string that matches
                        the prompt issued by the product
                        interpreter.

 :prompt-length         length of the prompt on the line.

 :prompt-cont-regexp    regular expression string that matches
                        the continuation prompt issued by the
                        product interpreter.

 :input-filter          function which can filter strings sent to
                        the command interpreter.  It is also used
                        by the `sql-send-string',
                        `sql-send-region', `sql-send-paragraph'
                        and `sql-send-buffer' functions.  The
                        function is passed the string sent to the
                        command interpreter and must return the
                        filtered string.  May also be a list of
                        such functions.

 :statement             name of a variable containing a regexp that
                        matches the beginning of SQL statements.

 :terminator            the terminator to be sent after a
                        `sql-send-string', `sql-send-region',
                        `sql-send-paragraph' and
                        `sql-send-buffer' command.  May be the
                        literal string or a cons of a regexp to
                        match an existing terminator in the
                        string and the terminator to be used if
                        its absent.  By default \";\".

 :syntax-alist          alist of syntax table entries to enable
                        special character treatment by font-lock
                        and imenu.

Other features can be stored but they will be ignored.  However,
you can develop new functionality which is product independent by
using `sql-get-product-feature' to lookup the product specific
settings.")

(defvar sql-indirect-features
  '(:font-lock :sqli-program :sqli-options :sqli-login :statement))

(defcustom sql-connection-alist nil
  "An alist of connection parameters for interacting with a SQL product.
Each element of the alist is as follows:

  (CONNECTION \(SQL-VARIABLE VALUE) ...)

Where CONNECTION is a case-insensitive string identifying the
connection, SQL-VARIABLE is the symbol name of a SQL mode
variable, and VALUE is the value to be assigned to the variable.
The most common SQL-VARIABLE settings associated with a
connection are: `sql-product', `sql-user', `sql-password',
`sql-port', `sql-server', and `sql-database'.

If a SQL-VARIABLE is part of the connection, it will not be
prompted for during login.  The command `sql-connect' starts a
predefined SQLi session using the parameters from this list.
Connections defined here appear in the submenu SQL->Start...  for
making new SQLi sessions."
  :type `(alist :key-type (string :tag "Connection")
                :value-type
                (set
                 (group (const :tag "Product"  sql-product)
                        (choice
                         ,@(mapcar
                            (lambda (prod-info)
                              `(const :tag
                                      ,(or (plist-get (cdr prod-info) :name)
                                           (capitalize
                                            (symbol-name (car prod-info))))
                                      (quote ,(car prod-info))))
                            sql-product-alist)))
                 (group (const :tag "Username" sql-user)     string)
                 (group (const :tag "Password" sql-password) string)
                 (group (const :tag "Server"   sql-server)   string)
                 (group (const :tag "Database" sql-database) string)
                 (group (const :tag "Port"     sql-port)     integer)
                 (repeat :inline t
                         (list :tab "Other"
                               (symbol :tag " Variable Symbol")
                               ;; FIXME: Why "Value *Expression*"?
                               (sexp   :tag "Value Expression")))))
  :version "24.1")

(defun sql-add-connection (connection params)
  "Add a new connection to `sql-connection-alist'.

If CONNECTION already exists, it is replaced with PARAMS."
  (setq sql-connection-alist
        (assoc-delete-all connection sql-connection-alist))
  (push
   (cons connection params)
   sql-connection-alist))

(defvaralias 'sql-dialect 'sql-product)
(defcustom sql-product 'ansi
  "Select the SQL database product used.
This allows highlighting buffers properly when you open them."
  :type `(choice
          ,@(mapcar (lambda (prod-info)
                      `(const :tag
                              ,(or (plist-get (cdr prod-info) :name)
                                   (capitalize (symbol-name (car prod-info))))
                              ,(car prod-info)))
                    sql-product-alist))
  :safe 'symbolp)

;; SQL indent support

(defcustom sql-use-indent-support t
  "If non-nil then use the SQL indent support features of sql-indent.
The `sql-indent' package in ELPA provides indentation support for
SQL statements with easy customizations to support varied layout
requirements.

The package must be available to be loaded and activated."
  :link '(url-link "https://elpa.gnu.org/packages/sql-indent.html")
  :type 'boolean
  :version "27.1")

(defun sql-indent-enable ()
  "Enable `sqlind-minor-mode' if available and requested."
  (when (fboundp 'sqlind-minor-mode)
    (sqlind-minor-mode (if sql-use-indent-support +1 -1))))

;; Secure Password wallet

(require 'auth-source)

(defun sql-auth-source-search-wallet (wallet product user server database port)
    "Read auth source WALLET to locate the USER secret.
Sets `auth-sources' to WALLET and uses `auth-source-search' to locate the entry.
The DATABASE and SERVER are concatenated with a slash between them as the
host key."
    (let* ((auth-sources wallet)
           host
           secret h-secret sd-secret)

      ;; product
      (setq product (symbol-name product))

      ;; user
      (setq user (unless (string-empty-p user) user))

      ;; port
      (setq port
            (when (and port (numberp port) (not (zerop port)))
              (number-to-string port)))

      ;; server
      (setq server (unless (string-empty-p server) server))

      ;; database
      (setq database (unless (string-empty-p database) database))

      ;; host
      (setq host (if server
                     (if database
                         (concat server "/" database)
                       server)
                   database))

      ;; Perform search
      (dolist (s (auth-source-search :max 1000))
        (when (and
               ;; Is PRODUCT specified, in the entry, and they are equal
               (if product
                   (if (plist-member s :product)
                       (equal (plist-get s :product) product)
                     t)
                 t)
               ;; Is USER specified, in the entry, and they are equal
               (if user
                   (if (plist-member s :user)
                       (equal (plist-get s :user) user)
                     t)
                 t)
               ;; Is PORT specified, in the entry, and they are equal
               (if port
                   (if (plist-member s :port)
                       (equal (plist-get s :port) port)
                     t)
                 t))
          ;; Is HOST specified, in the entry, and they are equal
          ;; then the H-SECRET list
          (if (and host
                   (plist-member s :host)
                   (equal (plist-get s :host) host))
              (push s h-secret)
            ;; Are SERVER and DATABASE specified, present, and equal
            ;; then the SD-SECRET list
            (if (and server
                     (plist-member s :server)
                     database
                     (plist-member s :database)
                     (equal (plist-get s :server) server)
                     (equal (plist-get s :database) database))
                (push s sd-secret)
              ;; Is SERVER specified, in the entry, and they are equal
              ;; then the base SECRET list
              (if (and server
                       (plist-member s :server)
                       (equal (plist-get s :server) server))
                  (push s secret)
                ;; Is DATABASE specified, in the entry, and they are equal
                ;; then the base SECRET list
                (if (and database
                         (plist-member s :database)
                         (equal (plist-get s :database) database))
                    (push s secret)))))))
      (setq secret (or h-secret sd-secret secret))

      ;; If we found a single secret, return the password
      (when (= 1 (length secret))
        (setq secret (car secret))
        (if (plist-member secret :secret)
            (plist-get secret :secret)
          nil))))

(defcustom sql-password-wallet
  (let (wallet w)
    (dolist (ext '(".json.gpg" ".gpg" ".json" "") wallet)
      (unless wallet
        (setq w (locate-user-emacs-file (concat "sql-wallet" ext)
                                        (concat ".sql-wallet" ext)))
        (when (file-exists-p w)
          (setq wallet (list w))))))
  "Identification of the password wallet.
See `sql-password-search-wallet-function' to understand how this value
is used to locate the password wallet."
  :type (plist-get (symbol-plist 'auth-sources) 'custom-type)
  :version "27.1")

(defvar sql-password-search-wallet-function #'sql-auth-source-search-wallet
  "Function to handle the lookup of the database password.
The specified function will be called as:
  (wallet-func WALLET PRODUCT USER SERVER DATABASE PORT)

It is expected to return either a string containing the password,
a function returning the password, or nil.  If you want to support
another format of password file, then implement a different
search wallet function and identify the location of the password
store with `sql-password-wallet'.")

;; misc customization of sql.el behavior

(defcustom sql-electric-stuff nil
  "Treat some input as electric.
If set to the symbol `semicolon', then hitting `;' will send current
input in the SQLi buffer to the process.
If set to the symbol `go', then hitting `go' on a line by itself will
send current input in the SQLi buffer to the process.
If set to nil, then you must use \\[comint-send-input] in order to send
current input in the SQLi buffer to the process."
  :type '(choice (const :tag "Nothing" nil)
		 (const :tag "The semicolon `;'" semicolon)
		 (const :tag "The string `go' by itself" go))
  :version "20.8")

(defcustom sql-send-terminator nil
  "When non-nil, add a terminator to text sent to the SQL interpreter.

When text is sent to the SQL interpreter (via `sql-send-string',
`sql-send-region', `sql-send-paragraph' or `sql-send-buffer'), a
command terminator can be automatically sent as well.  The
terminator is not sent, if the string sent already ends with the
terminator.

If this value is t, then the default command terminator for the
SQL interpreter is sent.  If this value is a string, then the
string is sent.

If the value is a cons cell of the form (PAT . TERM), then PAT is
a regexp used to match the terminator in the string and TERM is
the terminator to be sent.  This form is useful if the SQL
interpreter has more than one way of submitting a SQL command.
The PAT regexp can match any of them, and TERM is the way we do
it automatically."

  :type '(choice (const  :tag "No Terminator" nil)
		 (const  :tag "Default Terminator" t)
		 (string :tag "Terminator String")
		 (cons   :tag "Terminator Pattern and String"
			 (regexp :tag "Terminator Pattern")
			 (string :tag "Terminator String")))
  :version "22.2")

(defvar sql-contains-names nil
  "When non-nil, the current buffer contains database names.

Globally should be set to nil; it will be non-nil in `sql-mode',
`sql-interactive-mode' and list all buffers.")

(defvar sql-login-delay 7.5 ;; Secs
  "Maximum number of seconds you are willing to wait for a login connection.")

(defvaralias 'sql-pop-to-buffer-after-send-region 'sql-display-sqli-buffer-function)

(defcustom sql-display-sqli-buffer-function #'display-buffer
  "Function to be called to display a SQLi buffer after `sql-send-*'.

When set to a function, it will be called to display the buffer.
When set to t, the default function `pop-to-buffer' will be
called.  If not set, no attempt will be made to display the
buffer."

  :type '(choice (const :tag "Default" t)
                 (const :tag "No display" nil)
		 (function :tag "Display Buffer function"))
  :version "27.1")

;; imenu support for sql-mode.

(defvar sql-imenu-generic-expression
  ;; Items are in reverse order because they are rendered in reverse.
  '(("Rules/Defaults" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:rule\\|default\\)\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\s-+\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Sequences" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*sequence\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Triggers" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*trigger\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Functions" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?function\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Procedures" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?proc\\(?:edure\\)?\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Packages" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*package\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Types" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*type\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Indexes" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*index\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
    ("Tables/Views" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:table\\|view\\)\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1))
  "Define interesting points in the SQL buffer for `imenu'.

This is used to set `imenu-generic-expression' when SQL mode is
entered.  Subsequent changes to `sql-imenu-generic-expression' will
not affect existing SQL buffers because `imenu-generic-expression' is
a local variable.")

;; history file

(defcustom sql-input-ring-file-name nil
  "If non-nil, name of the file to read/write input history.

You have to set this variable if you want the history of your commands
saved from one Emacs session to the next.  If this variable is set,
exiting the SQL interpreter in an SQLi buffer will write the input
history to the specified file.  Starting a new process in a SQLi buffer
will read the input history from the specified file.

This is used to initialize `comint-input-ring-file-name'.

Note that the size of the input history is determined by the variable
`comint-input-ring-size'."
  :type '(choice (const :tag "none" nil)
		 (file)))

(defcustom sql-input-ring-separator "\n--\n"
  "Separator between commands in the history file.

If set to \"\\n\", each line in the history file will be interpreted as
one command.  Multi-line commands are split into several commands when
the input ring is initialized from a history file.

This variable used to initialize `comint-input-ring-separator'."
  :type 'string)

;; The usual hooks

(defcustom sql-interactive-mode-hook '(sql-indent-enable)
  "Hook for customizing `sql-interactive-mode'."
  :type 'hook
  :version "27.1")

(defcustom sql-mode-hook '(sql-indent-enable)
  "Hook for customizing `sql-mode'."
  :type 'hook
  :version "27.1")

(defcustom sql-set-sqli-hook '()
  "Hook for reacting to changes of `sql-buffer'.

This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
is changed."
  :type 'hook)

(defcustom sql-login-hook '()
  "Hook for interacting with a buffer in `sql-interactive-mode'.

This hook is invoked in a buffer once it is ready to accept input
for the first time."
  :version "24.1"
  :type 'hook)

;; Customization for Oracle

(defcustom sql-oracle-program "sqlplus"
  "Command to start sqlplus by Oracle.

Starts `sql-interactive-mode' after doing some setup.

On Windows, \"sqlplus\" usually starts the sqlplus \"GUI\".  In order
to start the sqlplus console, use \"plus33\" or something similar.
You will find the file in your Orant\\bin directory."
  :type 'file)

(defcustom sql-oracle-options '("-L")
  "List of additional options for `sql-oracle-program'."
  :type '(repeat string)
  :version "24.4")

(defcustom sql-oracle-login-params '(user password database)
  "List of login parameters needed to connect to Oracle."
  :type 'sql-login-params
  :version "24.1")

(defcustom sql-oracle-scan-on t
  "Non-nil if placeholders should be replaced in Oracle SQLi.

When non-nil, Emacs will scan text sent to sqlplus and prompt
for replacement text for & placeholders as sqlplus does.  This
is needed on Windows where SQL*Plus output is buffered and the
prompts are not shown until after the text is entered.

You need to issue the following command in SQL*Plus to be safe:

    SET DEFINE OFF

In older versions of SQL*Plus, this was the SET SCAN OFF command."
  :version "24.1"
  :type 'boolean)

(defcustom sql-db2-escape-newlines nil
  "Non-nil if newlines should be escaped by a backslash in DB2 SQLi.

When non-nil, Emacs will automatically insert a space and
backslash prior to every newline in multi-line SQL statements as
they are submitted to an interactive DB2 session."
  :version "24.3"
  :type 'boolean)

;; Customization for SQLite

(defcustom sql-sqlite-program (or (executable-find "sqlite3")
                                  (executable-find "sqlite")
                                  "sqlite")
  "Command to start SQLite.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-sqlite-options nil
  "List of additional options for `sql-sqlite-program'."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-sqlite-login-params '((database :file nil
                                               :must-match confirm))
  "List of login parameters needed to connect to SQLite."
  :type 'sql-login-params
  :version "26.1")

;; Customization for MariaDB

;; MariaDB is a drop-in replacement for MySQL, so just make the
;; MariaDB variables aliases of the MySQL ones.

(defvaralias 'sql-mariadb-program 'sql-mysql-program)
(defvaralias 'sql-mariadb-options 'sql-mysql-options)
(defvaralias 'sql-mariadb-login-params 'sql-mysql-login-params)

;; Customization for MySQL

(defcustom sql-mysql-program "mysql"
  "Command to start mysql by Oracle.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-mysql-options nil
  "List of additional options for `sql-mysql-program'.
The following list of options is reported to make things work
on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-mysql-login-params '(user password database server)
  "List of login parameters needed to connect to MySQL."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Solid

(defcustom sql-solid-program "solsql"
  "Command to start SOLID SQL Editor.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-solid-login-params '(user password server)
  "List of login parameters needed to connect to Solid."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Sybase

(defcustom sql-sybase-program "isql"
  "Command to start isql by Sybase.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-sybase-options nil
  "List of additional options for `sql-sybase-program'.
Some versions of isql might require the -n option in order to work."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-sybase-login-params '(server user password database)
  "List of login parameters needed to connect to Sybase."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Informix

(defcustom sql-informix-program "dbaccess"
  "Command to start dbaccess by Informix.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-informix-login-params '(database)
  "List of login parameters needed to connect to Informix."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Ingres

(defcustom sql-ingres-program "sql"
  "Command to start sql by Ingres.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-ingres-login-params '(database)
  "List of login parameters needed to connect to Ingres."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Microsoft

;; Microsoft documentation seems to indicate that ISQL and OSQL are
;; going away and being replaced by SQLCMD.  If anyone has experience
;; using SQLCMD, modified product configuration and feedback on its
;; use would be greatly appreciated.

(defcustom sql-ms-program "osql"
  "Command to start osql by Microsoft.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-ms-options '("-w" "300" "-n")
  ;; -w is the linesize
  "List of additional options for `sql-ms-program'."
  :type '(repeat string)
  :version "22.1")

(defcustom sql-ms-login-params '(user password server database)
  "List of login parameters needed to connect to Microsoft."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Postgres

(defcustom sql-postgres-program "psql"
  "Command to start psql by Postgres.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-postgres-options '("-P" "pager=off")
  "List of additional options for `sql-postgres-program'.
The default setting includes the -P option which breaks older versions
of the psql client (such as version 6.5.3).  The -P option is equivalent
to the --pset option.  If you want the psql to prompt you for a user
name, add the string \"-u\" to the list of options.  If you want to
provide a user name on the command line (newer versions such as 7.1),
add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-postgres-login-params
  `((user :default ,(user-login-name))
    (database :default ,(user-login-name)
              :completion ,(completion-table-dynamic
                            (lambda (_) (sql-postgres-list-databases)))
              :must-match confirm)
    server)
  "List of login parameters needed to connect to Postgres."
  :type 'sql-login-params
  :version "26.1")

(defun sql-postgres-list-databases ()
  "Return a list of available PostgreSQL databases."
  (when (executable-find sql-postgres-program)
    (let ((res '()))
      (ignore-errors
        (dolist (row (process-lines sql-postgres-program
                                    "--list"
                                    "--no-psqlrc"
                                    "--tuples-only"))
          (when (string-match "^ \\([^ |]+\\) +|.*" row)
            (push (match-string 1 row) res))))
      (nreverse res))))

;; Customization for Interbase

(defcustom sql-interbase-program "isql"
  "Command to start isql by Interbase.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-interbase-options nil
  "List of additional options for `sql-interbase-program'."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-interbase-login-params '(user password database)
  "List of login parameters needed to connect to Interbase."
  :type 'sql-login-params
  :version "24.1")

;; Customization for DB2

(defcustom sql-db2-program "db2"
  "Command to start db2 by IBM.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-db2-options nil
  "List of additional options for `sql-db2-program'."
  :type '(repeat string)
  :version "20.8")

(defcustom sql-db2-login-params nil
  "List of login parameters needed to connect to DB2."
  :type 'sql-login-params
  :version "24.1")

;; Customization for Linter

(defcustom sql-linter-program "inl"
  "Command to start inl by RELEX.

Starts `sql-interactive-mode' after doing some setup."
  :type 'file)

(defcustom sql-linter-options nil
  "List of additional options for `sql-linter-program'."
  :type '(repeat string)
  :version "21.3")

(defcustom sql-linter-login-params '(user password database server)
  "Login parameters to needed to connect to Linter."
  :type 'sql-login-params
  :version "24.1")

\f

;;; Variables which do not need customization

(defvar sql-user-history nil
  "History of usernames used.")

(defvar sql-database-history nil
  "History of databases used.")

(defvar sql-server-history nil
  "History of servers used.")

;; Passwords are not kept in a history.

(defvar sql-product-history nil
  "History of products used.")

(defvar sql-connection-history nil
  "History of connections used.")

(defvar sql-buffer nil
  "Current SQLi buffer.

The global value of `sql-buffer' is the name of the latest SQLi buffer
created.  Any SQL buffer created will make a local copy of this value.
See `sql-interactive-mode' for more on multiple sessions.  If you want
to change the SQLi buffer a SQL mode sends its SQL strings to, change
the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")

(defvar sql-prompt-regexp nil
  "Prompt used to initialize `comint-prompt-regexp'.

You can change `sql-prompt-regexp' on `sql-interactive-mode-hook'.")

(defvar sql-prompt-length 0
  "Prompt used to set `left-margin' in `sql-interactive-mode'.

You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")

(defvar sql-prompt-cont-regexp nil
  "Prompt pattern of statement continuation prompts.")

(defvar sql-alternate-buffer-name nil
  "Buffer-local string used to possibly rename the SQLi buffer.

Used by `sql-rename-buffer'.")

(defun sql-buffer-live-p (buffer &optional product connection)
  "Return non-nil if the process associated with buffer is live.

BUFFER can be a buffer object or a buffer name.  The buffer must
be a live buffer, have a running process attached to it, be in
`sql-interactive-mode', and, if PRODUCT or CONNECTION are
specified, it's `sql-product' or `sql-connection' must match."

  (when buffer
    (setq buffer (get-buffer buffer))
    (and buffer
         (buffer-live-p buffer)
         (comint-check-proc buffer)
         (with-current-buffer buffer
           (and (derived-mode-p 'sql-interactive-mode)
                (or (not product)
                    (eq product sql-product))
                (or (not connection)
                    (and (stringp connection)
                         (string= connection sql-connection))))))))

(defun sql-is-sqli-buffer-p (buffer)
  "Return non-nil if buffer is a SQLi buffer."
  (when buffer
    (setq buffer (get-buffer buffer))
    (and buffer
         (buffer-live-p buffer)
         (with-current-buffer buffer
           (derived-mode-p 'sql-interactive-mode)))))

;; Keymap for sql-interactive-mode.

(defvar-keymap sql-interactive-mode-map
  :doc "Mode map used for `sql-interactive-mode'.
Based on `comint-mode-map'."
  :parent comint-mode-map
  "C-j"       #'sql-accumulate-and-indent
  "C-c C-w"   #'sql-copy-column
  "O"         #'sql-magic-go
  "o"         #'sql-magic-go
  ";"         #'sql-magic-semicolon
  "C-c C-l a" #'sql-list-all
  "C-c C-l t" #'sql-list-table)

;; Keymap for sql-mode.

(defvar-keymap sql-mode-map
  :doc "Mode map used for `sql-mode'."
  "C-c C-c"   #'sql-send-paragraph
  "C-c C-r"   #'sql-send-region
  "C-c C-s"   #'sql-send-string
  "C-c C-b"   #'sql-send-buffer
  "C-c C-n"   #'sql-send-line-and-next
  "C-c C-i"   #'sql-product-interactive
  "C-c C-z"   #'sql-show-sqli-buffer
  "C-c C-l a" #'sql-list-all
  "C-c C-l t" #'sql-list-table
  "<remap> <beginning-of-defun>" #'sql-beginning-of-statement
  "<remap> <end-of-defun>"       #'sql-end-of-statement)

;; easy menu for sql-mode.

(easy-menu-define
 sql-mode-menu sql-mode-map
 "Menu for `sql-mode'."
 `("SQL"
   ["Send Paragraph" sql-send-paragraph (sql-buffer-live-p sql-buffer)]
   ["Send Region" sql-send-region (and mark-active
				       (sql-buffer-live-p sql-buffer))]
   ["Send Buffer" sql-send-buffer (sql-buffer-live-p sql-buffer)]
   ["Send String" sql-send-string (sql-buffer-live-p sql-buffer)]
   "--"
   ["List all objects" sql-list-all (and (sql-buffer-live-p sql-buffer)
                                         (sql-get-product-feature sql-product :list-all))]
   ["List table details" sql-list-table (and (sql-buffer-live-p sql-buffer)
                                             (sql-get-product-feature sql-product :list-table))]
   "--"
   ["Start SQLi session" sql-product-interactive
    :visible (not sql-connection-alist)
    :enable (sql-get-product-feature sql-product :sqli-comint-func)]
   ("Start..."
    :visible sql-connection-alist
    :filter sql-connection-menu-filter
    "--"
    ["New SQLi Session" sql-product-interactive (sql-get-product-feature sql-product :sqli-comint-func)])
   ["--"
    :visible sql-connection-alist]
   ["Show SQLi buffer" sql-show-sqli-buffer t]
   ["Set SQLi buffer" sql-set-sqli-buffer t]
   ["Pop to SQLi buffer after send"
    sql-toggle-pop-to-buffer-after-send-region
    :style toggle
    :selected sql-pop-to-buffer-after-send-region]
   ["--" nil nil]
   ("Product"
    ,@(mapcar (lambda (prod-info)
                (let* ((prod (pop prod-info))
                       (name (or (plist-get prod-info :name)
                                 (capitalize (symbol-name prod))))
                       (cmd (intern (format "sql-highlight-%s-keywords" prod))))
                  (fset cmd `(lambda () ,(format "Highlight %s SQL keywords." name)
                               (interactive)
                               (sql-set-product ',prod)))
                  (vector name cmd
                          :style 'radio
                          :selected `(eq sql-product ',prod))))
              sql-product-alist))))

;; easy menu for sql-interactive-mode.

(easy-menu-define
 sql-interactive-mode-menu sql-interactive-mode-map
 "Menu for `sql-interactive-mode'."
 '("SQL"
   ["Rename Buffer" sql-rename-buffer t]
   ["Save Connection" sql-save-connection (not sql-connection)]
   "--"
   ["List all objects" sql-list-all (sql-get-product-feature sql-product :list-all)]
   ["List table details" sql-list-table (sql-get-product-feature sql-product :list-table)]))

;; Abbreviations -- if you want more of them, define them in your init
;; file.  Abbrevs have to be enabled in your init file, too.

(define-abbrev-table 'sql-mode-abbrev-table
  '(("ins" "insert" nil nil t)
    ("upd" "update" nil nil t)
    ("del" "delete" nil nil t)
    ("sel" "select" nil nil t)
    ("proc" "procedure" nil nil t)
    ("func" "function" nil nil t)
    ("cr" "create" nil nil t))
  "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")

;; Syntax Table

(defvar sql-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
    (modify-syntax-entry ?/ ". 14" table)
    (modify-syntax-entry ?* ". 23" table)
    ;; double-dash starts comments
    (modify-syntax-entry ?- ". 12b" table)
    ;; newline and formfeed end comments
    (modify-syntax-entry ?\n "> b" table)
    (modify-syntax-entry ?\f "> b" table)
    ;; single quotes (') delimit strings
    (modify-syntax-entry ?' "\"" table)
    ;; double quotes (") don't delimit strings
    (modify-syntax-entry ?\" "." table)
    ;; Make these all punctuation
    (mapc (lambda (c) (modify-syntax-entry c "." table))
          (string-to-list "!#$%&+,.:;<=>?@\\|"))
    table)
  "Syntax table used in `sql-mode' and `sql-interactive-mode'.")

;; Motion Function Keywords

(defvar sql-ansi-statement-starters
  (regexp-opt '("create" "alter" "drop"
                "select" "insert" "update" "delete" "merge"
                "grant" "revoke"))
  "Regexp of keywords that start SQL commands.

All products share this list; products should define a regexp to
identify additional keywords in a variable defined by
the :statement feature.")

(defvar sql-oracle-statement-starters
  (regexp-opt '("declare" "begin" "with"))
  "Additional statement-starting keywords in Oracle.")

(defvar sql-postgres-statement-starters
  (regexp-opt '("with"))
  "Additional statement-starting keywords in Postgres.")

;; Font lock support

(defvar sql-mode-font-lock-object-name
  (eval-when-compile
    (list (concat "^\\s-*\\(?:create\\|drop\\|alter\\)\\s-+" ;; lead off with CREATE, DROP or ALTER
		  "\\(?:\\w+\\s-+\\)*"  ;; optional intervening keywords
		  "\\(?:table\\|view\\|\\(?:package\\|type\\)\\(?:\\s-+body\\)?\\|proc\\(?:edure\\)?"
		  "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
                  "\\(?:if\\s-+not\\s-+exists\\s-+\\)?" ;; IF NOT EXISTS
		  "\\(\\w+\\(?:\\s-*[.]\\s-*\\w+\\)*\\)")
	  1 'font-lock-function-name-face))

  "Pattern to match the names of top-level objects.

The pattern matches the name in a CREATE, DROP or ALTER
statement.  The format of variable should be a valid
`font-lock-keywords' entry.")

;; While there are international and American standards for SQL, they
;; are not followed closely, and most vendors offer significant
;; capabilities beyond those defined in the standard specifications.

;; SQL mode provides support for highlighting based on the product.  In
;; addition to highlighting the product keywords, any ANSI keywords not
;; used by the product are also highlighted.  This will help identify
;; keywords that could be restricted in future versions of the product
;; or might be a problem if ported to another product.

;; To reduce the complexity and size of the regular expressions
;; generated to match keywords, ANSI keywords are filtered out of
;; product keywords if they are equivalent.  To do this, we define a
;; function `sql-font-lock-keywords-builder' that removes any keywords
;; that are matched by the ANSI patterns and results in the same face
;; being applied.  For this to work properly, we must play some games
;; with the execution and compile time behavior.  This code is a
;; little tricky but works properly.

;; When defining the keywords for individual products you should
;; include all of the keywords that you want matched.  The filtering
;; against the ANSI keywords will be automatic if you use the
;; `sql-font-lock-keywords-builder' function and follow the
;; implementation pattern used for the other products in this file.

(defvar sql-mode-ansi-font-lock-keywords)

(eval-and-compile
  (defun sql-font-lock-keywords-builder (face boundaries &rest keywords)
    "Generation of regexp matching any one of KEYWORDS."

    (let ((bdy (or boundaries '("\\b" . "\\b")))
	  kwd)

      ;; Remove keywords that are defined in ANSI
      (setq kwd keywords)
      ;; (dolist (k keywords)
      ;;   (catch 'next
      ;;     (dolist (a sql-mode-ansi-font-lock-keywords)
      ;;       (when (and (eq face (cdr a))
      ;;   	       (eq (string-match (car a) k 0) 0)
      ;;   	       (eq (match-end 0) (length k)))
      ;;         (setq kwd (delq k kwd))
      ;;         (throw 'next nil)))))

      ;; Create a properly formed font-lock-keywords item
      (cons (concat (car bdy)
		    (regexp-opt kwd t)
		    (cdr bdy))
	    face)))

  (defun sql-regexp-abbrev (keyword)
    (let ((brk   (string-search "~" keyword))
          (len   (length keyword))
          (sep   "\\(?:")
          re i)
      (if (not brk)
          keyword
        (setq re  (substring keyword 0 brk)
              i   (+ 2 brk)
              brk (1+ brk))
        (while (<= i len)
          (setq re  (concat re sep (substring keyword brk i))
                sep "\\|"
                i   (1+ i)))
        (concat re "\\)?"))))

  (defun sql-regexp-abbrev-list (&rest keyw-list)
    (let ((re nil)
          (sep "\\<\\(?:"))
      (while keyw-list
        (setq re (concat re sep (sql-regexp-abbrev (car keyw-list)))
              sep "\\|"
              keyw-list (cdr keyw-list)))
      (concat re "\\)\\>"))))

(eval-when-compile
  (setq sql-mode-ansi-font-lock-keywords
	(list
	 ;; ANSI Non Reserved keywords
	 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"ada" "asensitive" "assignment" "asymmetric" "atomic" "between"
"bitvar" "called" "catalog_name" "chain" "character_set_catalog"
"character_set_name" "character_set_schema" "checked" "class_origin"
"cobol" "collation_catalog" "collation_name" "collation_schema"
"column_name" "command_function" "command_function_code" "committed"
"condition_number" "connection_name" "constraint_catalog"
"constraint_name" "constraint_schema" "contains" "cursor_name"
"datetime_interval_code" "datetime_interval_precision" "defined"
"definer" "dispatch" "dynamic_function" "dynamic_function_code"
"existing" "exists" "final" "fortran" "generated" "granted"
"hierarchy" "hold" "implementation" "infix" "insensitive" "instance"
"instantiable" "invoker" "key_member" "key_type" "length" "m"
"message_length" "message_octet_length" "message_text" "method" "more"
"mumps" "name" "nullable" "number" "options" "overlaps" "overriding"
"parameter_mode" "parameter_name" "parameter_ordinal_position"
"parameter_specific_catalog" "parameter_specific_name"
"parameter_specific_schema" "pascal" "pli" "position" "repeatable"
"returned_length" "returned_octet_length" "returned_sqlstate"
"routine_catalog" "routine_name" "routine_schema" "row_count" "scale"
"schema_name" "security" "self" "sensitive" "serializable"
"server_name" "similar" "simple" "source" "specific_name" "style"
"subclass_origin" "sublist" "symmetric" "system" "table_name"
"transaction_active" "transactions_committed"
"transactions_rolled_back" "transform" "transforms" "trigger_catalog"
"trigger_name" "trigger_schema" "type" "uncommitted" "unnamed"
"user_defined_type_catalog" "user_defined_type_name"
"user_defined_type_schema"
)

	 ;; ANSI Reserved keywords
	 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
"allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
"authorization" "before" "begin" "both" "breadth" "by" "call"
"cascade" "cascaded" "case" "catalog" "check" "class" "close"
"collate" "collation" "column" "commit" "completion" "connect"
"connection" "constraint" "constraints" "constructor" "continue"
"corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
"data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
"delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
"destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
"distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
"escape" "every" "except" "exception" "exec" "execute" "external"
"false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
"function" "general" "get" "global" "go" "goto" "grant" "group"
"grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
"indicator" "initialize" "initially" "inner" "inout" "input" "insert"
"intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
"last" "lateral" "leading" "left" "less" "level" "like" "limit"
"local" "locator" "map" "match" "minute" "modifies" "modify" "module"
"month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
"off" "old" "on" "only" "open" "operation" "option" "or" "order"
"ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
"partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
"primary" "prior" "privileges" "procedure" "public" "read" "reads"
"recursive" "references" "referencing" "relative" "restrict" "result"
"return" "returns" "revoke" "right" "role" "rollback" "rollup"
"routine" "rows" "savepoint" "schema" "scroll" "search" "second"
"section" "select" "sequence" "session" "set" "sets" "size" "some"
"space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
"sqlwarning" "start" "state" "statement" "static" "structure" "table"
"temporary" "terminate" "than" "then" "timezone_hour"
"timezone_minute" "to" "trailing" "transaction" "translation"
"trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
"usage" "using" "value" "values" "variable" "view" "when" "whenever"
"where" "with" "without" "work" "write" "year"
)

	 ;; ANSI Functions
	 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"abs" "avg" "bit_length" "cardinality" "cast" "char_length"
"character_length" "coalesce" "convert" "count" "current_date"
"current_path" "current_role" "current_time" "current_timestamp"
"current_user" "extract" "localtime" "localtimestamp" "lower" "max"
"min" "mod" "nullif" "octet_length" "overlay" "placing" "session_user"
"substring" "sum" "system_user" "translate" "treat" "trim" "upper"
"user"
)

	 ;; ANSI Data Types
	 (sql-font-lock-keywords-builder 'font-lock-type-face nil
"array" "binary" "bit" "blob" "boolean" "char" "character" "clob"
"date" "dec" "decimal" "double" "float" "int" "integer" "interval"
"large" "national" "nchar" "nclob" "numeric" "object" "precision"
"real" "ref" "row" "scope" "smallint" "time" "timestamp" "varchar"
"varying" "zone"
))))

(defvar sql-mode-ansi-font-lock-keywords
  (eval-when-compile sql-mode-ansi-font-lock-keywords)
  "ANSI SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-ansi-font-lock-keywords'.  You may want
to add functions and PL/SQL keywords.")

(defun sql--oracle-show-reserved-words ()
  ;; This function is for use by the maintainer of SQL.EL only.
  (if (or (and (not (derived-mode-p 'sql-mode))
               (not (derived-mode-p 'sql-interactive-mode)))
          (not sql-buffer)
          (not (eq sql-product 'oracle)))
      (user-error "Not an Oracle buffer")

    (let ((b "*RESERVED WORDS*"))
      (sql-execute sql-buffer b
                   (concat "SELECT "
                           "  keyword "
                           ", reserved AS \"Res\" "
                           ", res_type AS \"Type\" "
                           ", res_attr AS \"Attr\" "
                           ", res_semi AS \"Semi\" "
                           ", duplicate AS \"Dup\" "
                           "FROM V$RESERVED_WORDS "
                           "WHERE length > 1 "
                           "AND SUBSTR(keyword, 1, 1) BETWEEN 'A' AND 'Z' "
                           "ORDER BY 2 DESC, 3 DESC, 4 DESC, 5 DESC, 6 DESC, 1;")
                   nil nil)
      (with-current-buffer b
        (setq-local sql-product 'oracle)
        (sql-product-font-lock t nil)
        (font-lock-mode +1)))))

(defvar sql-mode-oracle-font-lock-keywords
  (eval-when-compile
    (list
     ;; Oracle SQL*Plus Commands
     ;;   Only recognized in they start in column 1 and the
     ;;   abbreviation is followed by a space or the end of line.
     (list (concat "^" (sql-regexp-abbrev "rem~ark") "\\(?:\\s-.*\\)?$")
           0 'font-lock-comment-face t)

     (list
      (concat
       "^\\(?:"
       (sql-regexp-abbrev-list
        "[@]\\{1,2\\}" "acc~ept" "a~ppend" "archive" "attribute"
        "bre~ak" "bti~tle" "c~hange" "cl~ear" "col~umn" "conn~ect"
        "copy" "def~ine" "del" "desc~ribe" "disc~onnect" "ed~it"
        "exec~ute" "exit" "get" "help" "ho~st" "[$]" "i~nput" "l~ist"
        "passw~ord" "pau~se" "pri~nt" "pro~mpt" "quit" "recover"
        "repf~ooter" "reph~eader" "r~un" "sav~e" "sho~w" "shutdown"
        "spo~ol" "sta~rt" "startup" "store" "tim~ing" "tti~tle"
        "undef~ine" "var~iable" "whenever")
       "\\|"
       (concat "\\(?:"
               (sql-regexp-abbrev "comp~ute")
               "\\s-+"
               (sql-regexp-abbrev-list
                "avg" "cou~nt" "min~imum" "max~imum" "num~ber" "sum"
                "std" "var~iance")
               "\\)")
       "\\|"
       (concat "\\(?:set\\s-+"
               (sql-regexp-abbrev-list
                "appi~nfo" "array~size" "auto~commit" "autop~rint"
                "autorecovery" "autot~race" "blo~ckterminator"
                "cmds~ep" "colsep" "com~patibility" "con~cat"
                "copyc~ommit" "copytypecheck" "def~ine" "describe"
                "echo" "editf~ile" "emb~edded" "esc~ape" "feed~back"
                "flagger" "flu~sh" "hea~ding" "heads~ep" "instance"
                "lin~esize" "lobof~fset" "long" "longc~hunksize"
                "mark~up" "newp~age" "null" "numf~ormat" "num~width"
                "pages~ize" "pau~se" "recsep" "recsepchar"
                "scan" "serverout~put" "shift~inout" "show~mode"
                "sqlbl~anklines" "sqlc~ase" "sqlco~ntinue"
                "sqln~umber" "sqlpluscompat~ibility" "sqlpre~fix"
                "sqlp~rompt" "sqlt~erminator" "suf~fix" "tab"
                "term~out" "ti~me" "timi~ng" "trim~out" "trims~pool"
                "und~erline" "ver~ify" "wra~p")
               "\\)")

       "\\)\\(?:\\s-.*\\)?\\(?:[-]\n.*\\)*$")
      0 'font-lock-doc-face t)
     '("&?&\\(?:\\sw\\|\\s_\\)+[.]?" 0 font-lock-preprocessor-face t)

     ;; Oracle PL/SQL Attributes (Declare these first to match %TYPE correctly)
     (sql-font-lock-keywords-builder 'font-lock-builtin-face '("%" . "\\b")
"bulk_exceptions" "bulk_rowcount" "found" "isopen" "notfound"
"rowcount" "rowtype" "type"
)
     ;; Oracle Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"abs" "acos" "add_months" "appendchildxml" "ascii" "asciistr" "asin"
"atan" "atan2" "avg" "bfilename" "bin_to_num" "bitand" "cardinality"
"cast" "ceil" "chartorowid" "chr" "cluster_id" "cluster_probability"
"cluster_set" "coalesce" "collect" "compose" "concat" "convert" "corr"
"connect_by_root" "connect_by_iscycle" "connect_by_isleaf"
"corr_k" "corr_s" "cos" "cosh" "count" "covar_pop" "covar_samp"
"cube_table" "cume_dist" "current_date" "current_timestamp" "cv"
"dataobj_to_partition" "dbtimezone" "decode" "decompose" "deletexml"
"dense_rank" "depth" "deref" "dump" "empty_blob" "empty_clob"
"existsnode" "exp" "extract" "extractvalue" "feature_id" "feature_set"
"feature_value" "first" "first_value" "floor" "from_tz" "greatest"
"grouping" "grouping_id" "group_id" "hextoraw" "initcap"
"insertchildxml" "insertchildxmlafter" "insertchildxmlbefore"
"insertxmlafter" "insertxmlbefore" "instr" "instr2" "instr4" "instrb"
"instrc" "iteration_number" "lag" "last" "last_day" "last_value"
"lead" "least" "length" "length2" "length4" "lengthb" "lengthc"
"listagg" "ln" "lnnvl" "localtimestamp" "log" "lower" "lpad" "ltrim"
"make_ref" "max" "median" "min" "mod" "months_between" "nanvl" "nchr"
"new_time" "next_day" "nlssort" "nls_charset_decl_len"
"nls_charset_id" "nls_charset_name" "nls_initcap" "nls_lower"
"nls_upper" "nth_value" "ntile" "nullif" "numtodsinterval"
"numtoyminterval" "nvl" "nvl2" "ora_dst_affected" "ora_dst_convert"
"ora_dst_error" "ora_hash" "path" "percentile_cont" "percentile_disc"
"percent_rank" "power" "powermultiset" "powermultiset_by_cardinality"
"prediction" "prediction_bounds" "prediction_cost"
"prediction_details" "prediction_probability" "prediction_set"
"presentnnv" "presentv" "previous" "rank" "ratio_to_report" "rawtohex"
"rawtonhex" "ref" "reftohex" "regexp_count" "regexp_instr" "regexp_like"
"regexp_replace" "regexp_substr" "regr_avgx" "regr_avgy" "regr_count"
"regr_intercept" "regr_r2" "regr_slope" "regr_sxx" "regr_sxy"
"regr_syy" "remainder" "replace" "round" "rowidtochar" "rowidtonchar"
"row_number" "rpad" "rtrim" "scn_to_timestamp" "sessiontimezone" "set"
"sign" "sin" "sinh" "soundex" "sqrt" "stats_binomial_test"
"stats_crosstab" "stats_f_test" "stats_ks_test" "stats_mode"
"stats_mw_test" "stats_one_way_anova" "stats_t_test_indep"
"stats_t_test_indepu" "stats_t_test_one" "stats_t_test_paired"
"stats_wsr_test" "stddev" "stddev_pop" "stddev_samp" "substr"
"substr2" "substr4" "substrb" "substrc" "sum" "sysdate" "systimestamp"
"sys_connect_by_path" "sys_context" "sys_dburigen" "sys_extract_utc"
"sys_guid" "sys_typeid" "sys_xmlagg" "sys_xmlgen" "tan" "tanh"
"timestamp_to_scn" "to_binary_double" "to_binary_float" "to_blob"
"to_char" "to_clob" "to_date" "to_dsinterval" "to_lob" "to_multi_byte"
"to_nchar" "to_nclob" "to_number" "to_single_byte" "to_timestamp"
"to_timestamp_tz" "to_yminterval" "translate" "treat" "trim" "trunc"
"tz_offset" "uid" "unistr" "updatexml" "upper" "user" "userenv"
"value" "variance" "var_pop" "var_samp" "vsize" "width_bucket"
"xmlagg" "xmlcast" "xmlcdata" "xmlcolattval" "xmlcomment" "xmlconcat"
"xmldiff" "xmlelement" "xmlexists" "xmlforest" "xmlisvalid" "xmlparse"
"xmlpatch" "xmlpi" "xmlquery" "xmlroot" "xmlsequence" "xmlserialize"
"xmltable" "xmltransform"
)

     ;; See the table V$RESERVED_WORDS
     ;; Oracle Keywords
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"abort" "access" "accessed" "account" "activate" "add" "admin"
"advise" "after" "agent" "aggregate" "all" "allocate" "allow" "alter"
"always" "analyze" "ancillary" "and" "any" "apply" "archive"
"archivelog" "array" "as" "asc" "associate" "at" "attribute"
"attributes" "audit" "authenticated" "authid" "authorization" "auto"
"autoallocate" "automatic" "availability" "backup" "before" "begin"
"behalf" "between" "binding" "bitmap" "block" "blocksize" "body"
"both" "buffer_pool" "build" "by"  "cache" "call" "cancel"
"cascade" "case" "category" "certificate" "chained" "change" "check"
"checkpoint" "child" "chunk" "class" "clear" "clone" "close" "cluster"
"column" "column_value" "columns" "comment" "commit" "committed"
"compatibility" "compile" "complete" "composite_limit" "compress"
"compute" "connect" "connect_time" "consider" "consistent"
"constraint" "constraints" "constructor" "contents" "context"
"continue" "controlfile" "corruption" "cost" "cpu_per_call"
"cpu_per_session" "create" "cross" "cube" "current" "currval" "cycle"
"dangling" "data" "database" "datafile" "datafiles" "day" "ddl"
"deallocate" "debug" "default" "deferrable" "deferred" "definer"
"delay" "delete" "demand" "desc" "determines" "deterministic"
"dictionary" "dimension" "directory" "disable" "disassociate"
"disconnect" "distinct" "distinguished" "distributed" "dml" "drop"
"each" "element" "else" "enable" "end" "equals_path" "escape"
"estimate" "except" "exceptions" "exchange" "excluding" "exists"
"expire" "explain" "extent" "external" "externally"
"failed_login_attempts" "fast" "file" "final" "finish" "flush" "for"
"force" "foreign" "freelist" "freelists" "freepools" "fresh" "from"
"full" "function" "functions" "generated" "global" "global_name"
"globally" "grant" "group" "grouping" "groups" "guard" "hash"
"hashkeys" "having" "heap" "hierarchy" "id" "identified" "identifier"
"idle_time" "immediate" "in" "including" "increment" "index" "indexed"
"indexes" "indextype" "indextypes" "indicator" "initial" "initialized"
"initially" "initrans" "inner" "insert" "instance" "instantiable"
"instead" "intersect" "into" "invalidate" "is" "isolation" "java"
"join"  "keep" "key" "kill" "language" "left" "less" "level"
"levels" "library" "like" "like2" "like4" "likec" "limit" "link"
"list" "lob" "local" "location" "locator" "lock" "log" "logfile"
"logging" "logical" "logical_reads_per_call"
"logical_reads_per_session"  "managed" "management" "manual" "map"
"mapping" "master" "matched" "materialized" "maxdatafiles"
"maxextents" "maximize" "maxinstances" "maxlogfiles" "maxloghistory"
"maxlogmembers" "maxsize" "maxtrans" "maxvalue" "member" "memory"
"merge" "migrate" "minextents" "minimize" "minimum" "minus" "minvalue"
"mode" "modify" "monitoring" "month" "mount" "move" "movement" "name"
"named" "natural" "nested" "never" "new" "next" "nextval" "no"
"noarchivelog" "noaudit" "nocache" "nocompress" "nocopy" "nocycle"
"nodelay" "noforce" "nologging" "nomapping" "nomaxvalue" "nominimize"
"nominvalue" "nomonitoring" "none" "noorder" "noparallel" "norely"
"noresetlogs" "noreverse" "normal" "norowdependencies" "nosort"
"noswitch" "not" "nothing" "notimeout" "novalidate" "nowait" "null"
"nulls" "object" "of" "off" "offline" "oidindex" "old" "on" "online"
"only" "open" "operator" "optimal" "option" "or" "order"
"organization" "out" "outer" "outline" "over" "overflow" "overriding"
"package" "packages" "parallel" "parallel_enable" "parameters"
"parent" "partition" "partitions" "password" "password_grace_time"
"password_life_time" "password_lock_time" "password_reuse_max"
"password_reuse_time" "password_verify_function" "pctfree"
"pctincrease" "pctthreshold" "pctused" "pctversion" "percent"
"performance" "permanent" "pfile" "physical" "pipelined" "pivot" "plan"
"post_transaction" "pragma" "prebuilt" "preserve" "primary" "private"
"private_sga" "privileges" "procedure" "profile" "protection" "public"
"purge" "query" "quiesce" "quota" "range" "read" "reads" "rebuild"
"records_per_block" "recover" "recovery" "recycle" "reduced" "ref"
"references" "referencing" "refresh" "register" "reject" "relational"
"rely" "rename" "reset" "resetlogs" "resize" "resolve" "resolver"
"resource" "restrict" "restrict_references" "restricted" "result"
"resumable" "resume" "retention" "return" "returning" "reuse"
"reverse" "revoke" "rewrite" "right" "rnds" "rnps" "role" "roles"
"rollback" "rollup" "row" "rowdependencies" "rownum" "rows" "sample"
"savepoint" "scan" "schema" "scn" "scope" "segment" "select"
"selectivity" "self" "sequence" "serializable" "session"
"sessions_per_user" "set" "sets" "settings" "shared" "shared_pool"
"shrink" "shutdown" "siblings" "sid" "single" "size" "skip" "some"
"sort" "source" "space" "specification" "spfile" "split" "standby"
"start" "statement_id" "static" "statistics" "stop" "storage" "store"
"structure" "subpartition" "subpartitions" "substitutable"
"successful" "supplemental" "suspend" "switch" "switchover" "synonym"
"sys" "system" "table" "tables" "tablespace" "tempfile" "template"
"temporary" "test" "than" "then" "thread" "through" "time_zone"
"timeout" "to" "trace" "transaction" "trigger" "triggers" "truncate"
"trust" "type" "types" "unarchived" "under" "under_path" "undo"
"uniform" "union" "unique" "unlimited" "unlock" "unpivot" "unquiesce"
"unrecoverable" "until" "unusable" "unused" "update" "upgrade" "usage"
"use" "using" "validate" "validation" "value" "values" "variable"
"varray" "version" "view" "wait" "when" "whenever" "where" "with"
"without" "wnds" "wnps" "work" "write" "xmldata" "xmlschema" "xmltype"
)

     ;; Oracle Data Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"bfile" "binary_double" "binary_float" "blob" "byte" "char" "charbyte"
"clob" "date" "day" "float" "interval" "local" "long" "longraw"
"minute" "month" "nchar" "nclob" "number" "nvarchar2" "raw" "rowid" "second"
"time" "timestamp" "urowid" "varchar2" "with" "year" "zone"
)

     ;; Oracle PL/SQL Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"delete" "trim" "extend" "exists" "first" "last" "count" "limit"
"prior" "next" "sqlcode" "sqlerrm"
)

     ;; Oracle PL/SQL Reserved words
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"all" "alter" "and" "any" "as" "asc" "at" "begin" "between" "by"
"case" "check" "clusters" "cluster" "colauth" "columns" "compress"
"connect" "crash" "create" "cursor" "declare" "default" "desc"
"distinct" "drop" "else" "end" "exception" "exclusive" "fetch" "for"
"from" "function" "goto" "grant" "group" "having" "identified" "if"
"in" "index" "indexes" "insert" "intersect" "into" "is" "like" "lock"
"minus" "mode" "nocompress" "not" "nowait" "null" "of" "on" "option"
"or" "order" "overlaps" "procedure" "public" "resource" "revoke"
"select" "share" "size" "sql" "start" "subtype" "tabauth" "table"
"then" "to" "type" "union" "unique" "update" "values" "view" "views"
"when" "where" "with"

"true" "false"
"raise_application_error"
)

     ;; Oracle PL/SQL Keywords
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"a" "add" "agent" "aggregate" "array" "attribute" "authid" "avg"
"bfile_base" "binary" "blob_base" "block" "body" "both" "bound" "bulk"
"byte" "c" "call" "calling" "cascade" "char" "char_base" "character"
"charset" "charsetform" "charsetid" "clob_base" "close" "collect"
"comment" "commit" "committed" "compiled" "constant" "constructor"
"context" "continue" "convert" "count" "current" "customdatum"
"dangling" "data" "date" "date_base" "day" "define" "delete"
"deterministic" "double" "duration" "element" "elsif" "empty" "escape"
"except" "exceptions" "execute" "exists" "exit" "external" "final"
"fixed" "float" "forall" "force" "general" "hash" "heap" "hidden"
"hour" "immediate" "including" "indicator" "indices" "infinite"
"instantiable" "int" "interface" "interval" "invalidate" "isolation"
"java" "language" "large" "leading" "length" "level" "library" "like2"
"like4" "likec" "limit" "limited" "local" "long" "loop" "map" "max"
"maxlen" "member" "merge" "min" "minute" "mod" "modify" "month"
"multiset" "name" "nan" "national" "native" "nchar" "new" "nocopy"
"number_base" "object" "ocicoll" "ocidate" "ocidatetime" "ociduration"
"ociinterval" "ociloblocator" "ocinumber" "ociraw" "ociref"
"ocirefcursor" "ocirowid" "ocistring" "ocitype" "old" "only" "opaque"
"open" "operator" "oracle" "oradata" "organization" "orlany" "orlvary"
"others" "out" "overriding" "package" "parallel_enable" "parameter"
"parameters" "parent" "partition" "pascal" "pipe" "pipelined" "pragma"
"precision" "prior" "private" "raise" "range" "raw" "read" "record"
"ref" "reference" "relies_on" "rem" "remainder" "rename" "result"
"result_cache" "return" "returning" "reverse" "rollback" "row"
"sample" "save" "savepoint" "sb1" "sb2" "sb4" "second" "segment"
"self" "separate" "sequence" "serializable" "set" "short" "size_t"
"some" "sparse" "sqlcode" "sqldata" "sqlname" "sqlstate" "standard"
"static" "stddev" "stored" "string" "struct" "style" "submultiset"
"subpartition" "substitutable" "sum" "synonym" "tdo" "the" "time"
"timestamp" "timezone_abbr" "timezone_hour" "timezone_minute"
"timezone_region" "trailing" "transaction" "transactional" "trusted"
"ub1" "ub2" "ub4" "under" "unsigned" "untrusted" "use" "using"
"valist" "value" "variable" "variance" "varray" "varying" "void"
"while" "work" "wrapped" "write" "year" "zone"
;; Pragma
"autonomous_transaction" "exception_init" "inline"
"restrict_references" "serially_reusable"
)

     ;; Oracle PL/SQL Data Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"\"BINARY LARGE OBJECT\"" "\"CHAR LARGE OBJECT\"" "\"CHAR VARYING\""
"\"CHARACTER LARGE OBJECT\"" "\"CHARACTER VARYING\""
"\"DOUBLE PRECISION\"" "\"INTERVAL DAY TO SECOND\""
"\"INTERVAL YEAR TO MONTH\"" "\"LONG RAW\"" "\"NATIONAL CHAR\""
"\"NATIONAL CHARACTER LARGE OBJECT\"" "\"NATIONAL CHARACTER\""
"\"NCHAR LARGE OBJECT\"" "\"NCHAR\"" "\"NCLOB\"" "\"NVARCHAR2\""
"\"TIME WITH TIME ZONE\"" "\"TIMESTAMP WITH LOCAL TIME ZONE\""
"\"TIMESTAMP WITH TIME ZONE\""
"bfile" "bfile_base" "binary_double" "binary_float" "binary_integer"
"blob" "blob_base" "boolean" "char" "character" "char_base" "clob"
"clob_base" "cursor" "date" "day" "dec" "decimal"
"dsinterval_unconstrained" "float" "int" "integer" "interval" "local"
"long" "mlslabel" "month" "natural" "naturaln" "nchar_cs" "number"
"number_base" "numeric" "pls_integer" "positive" "positiven" "raw"
"real" "ref" "rowid" "second" "signtype" "simple_double"
"simple_float" "simple_integer" "smallint" "string" "time" "timestamp"
"timestamp_ltz_unconstrained" "timestamp_tz_unconstrained"
"timestamp_unconstrained" "time_tz_unconstrained" "time_unconstrained"
"to" "urowid" "varchar" "varchar2" "with" "year"
"yminterval_unconstrained" "zone"
)

     ;; Oracle PL/SQL Exceptions
     (sql-font-lock-keywords-builder 'font-lock-warning-face nil
"access_into_null" "case_not_found" "collection_is_null"
"cursor_already_open" "dup_val_on_index" "invalid_cursor"
"invalid_number" "login_denied" "no_data_found" "no_data_needed"
"not_logged_on" "program_error" "rowtype_mismatch" "self_is_null"
"storage_error" "subscript_beyond_count" "subscript_outside_limit"
"sys_invalid_rowid" "timeout_on_resource" "too_many_rows"
"value_error" "zero_divide"
)))

  "Oracle SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-oracle-font-lock-keywords'.  You may want
to add functions and PL/SQL keywords.")

(defvar sql-mode-postgres-font-lock-keywords
  (eval-when-compile
    (list
     ;; Postgres psql commands
     '("^\\s-*\\\\.*$" . font-lock-doc-face)

     ;; Postgres unreserved words but may have meaning
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil "a"
"abs" "absent" "according" "ada" "alias" "allocate" "are" "array_agg"
"asensitive" "atomic" "attribute" "attributes" "avg" "base64"
"bernoulli" "bit_length" "bitvar" "blob" "blocked" "bom" "breadth" "c"
"call" "cardinality" "catalog_name" "ceil" "ceiling" "char_length"
"character_length" "character_set_catalog" "character_set_name"
"character_set_schema" "characters" "checked" "class_origin" "clob"
"cobol" "collation" "collation_catalog" "collation_name"
"collation_schema" "collect" "column_name" "columns"
"command_function" "command_function_code" "completion" "condition"
"condition_number" "connect" "connection_name" "constraint_catalog"
"constraint_name" "constraint_schema" "constructor" "contains"
"control" "convert" "corr" "corresponding" "count" "covar_pop"
"covar_samp" "cube" "cume_dist" "current_default_transform_group"
"current_path" "current_transform_group_for_type" "cursor_name"
"datalink" "datetime_interval_code" "datetime_interval_precision" "db"
"defined" "degree" "dense_rank" "depth" "deref" "derived" "describe"
"descriptor" "destroy" "destructor" "deterministic" "diagnostics"
"disconnect" "dispatch" "dlnewcopy" "dlpreviouscopy" "dlurlcomplete"
"dlurlcompleteonly" "dlurlcompletewrite" "dlurlpath" "dlurlpathonly"
"dlurlpathwrite" "dlurlscheme" "dlurlserver" "dlvalue" "dynamic"
"dynamic_function" "dynamic_function_code" "element" "empty"
"end-exec" "equals" "every" "exception" "exec" "existing" "exp" "file"
"filter" "final" "first_value" "flag" "floor" "fortran" "found" "free"
"fs" "fusion" "g" "general" "generated" "get" "go" "goto" "grouping"
"hex" "hierarchy" "host" "id" "ignore" "implementation" "import"
"indent" "indicator" "infix" "initialize" "instance" "instantiable"
"integrity" "intersection" "iterate" "k" "key_member" "key_type" "lag"
"last_value" "lateral" "lead" "length" "less" "library" "like_regex"
"link" "ln" "locator" "lower" "m" "map" "matched" "max"
"max_cardinality" "member" "merge" "message_length"
"message_octet_length" "message_text" "method" "min" "mod" "modifies"
"modify" "module" "more" "multiset" "mumps" "namespace" "nclob"
"nesting" "new" "nfc" "nfd" "nfkc" "nfkd" "nil" "normalize"
"normalized" "nth_value" "ntile" "nullable" "number"
"occurrences_regex" "octet_length" "octets" "old" "open" "operation"
"ordering" "ordinality" "others" "output" "overriding" "p" "pad"
"parameter" "parameter_mode" "parameter_name"
"parameter_ordinal_position" "parameter_specific_catalog"
"parameter_specific_name" "parameter_specific_schema" "parameters"
"pascal" "passing" "passthrough" "percent_rank" "percentile_cont"
"percentile_disc" "permission" "pli" "position_regex" "postfix"
"power" "prefix" "preorder" "public" "rank" "reads" "recovery" "ref"
"referencing" "regr_avgx" "regr_avgy" "regr_count" "regr_intercept"
"regr_r2" "regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "requiring"
"respect" "restore" "result" "return" "returned_cardinality"
"returned_length" "returned_octet_length" "returned_sqlstate" "rollup"
"routine" "routine_catalog" "routine_name" "routine_schema"
"row_count" "row_number" "scale" "schema_name" "scope" "scope_catalog"
"scope_name" "scope_schema" "section" "selective" "self" "sensitive"
"server_name" "sets" "size" "source" "space" "specific"
"specific_name" "specifictype" "sql" "sqlcode" "sqlerror"
"sqlexception" "sqlstate" "sqlwarning" "sqrt" "state" "static"
"stddev_pop" "stddev_samp" "structure" "style" "subclass_origin"
"sublist" "submultiset" "substring_regex" "sum" "system_user" "t"
"table_name" "tablesample" "terminate" "than" "ties" "timezone_hour"
"timezone_minute" "token" "top_level_count" "transaction_active"
"transactions_committed" "transactions_rolled_back" "transform"
"transforms" "translate" "translate_regex" "translation"
"trigger_catalog" "trigger_name" "trigger_schema" "trim_array"
"uescape" "under" "unlink" "unnamed" "unnest" "untyped" "upper" "uri"
"usage" "user_defined_type_catalog" "user_defined_type_code"
"user_defined_type_name" "user_defined_type_schema" "var_pop"
"var_samp" "varbinary" "variable" "whenever" "width_bucket" "within"
"xmlagg" "xmlbinary" "xmlcast" "xmlcomment" "xmldeclaration"
"xmldocument" "xmlexists" "xmliterate" "xmlnamespaces" "xmlquery"
"xmlschema" "xmltable" "xmltext" "xmlvalidate"
)

     ;; Postgres non-reserved words
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"abort" "absolute" "access" "action" "add" "admin" "after" "aggregate"
"also" "alter" "always" "assertion" "assignment" "at" "attribute" "backward"
"before" "begin" "between" "by" "cache" "called" "cascade" "cascaded"
"catalog" "chain" "characteristics" "checkpoint" "class" "close"
"cluster" "coalesce" "comment" "comments" "commit" "committed"
"configuration" "connection" "constraints" "content" "continue"
"conversion" "copy" "cost" "createdb" "createrole" "createuser" "csv"
"current" "cursor" "cycle" "data" "database" "day" "deallocate" "dec"
"declare" "defaults" "deferred" "definer" "delete" "delimiter"
"delimiters" "dictionary" "disable" "discard" "document" "domain"
"drop" "each" "enable" "encoding" "encrypted" "enum" "escape"
"exclude" "excluding" "exclusive" "execute" "exists" "explain"
"extension" "external" "extract" "family" "first" "float" "following" "force"
"forward" "function" "functions" "global" "granted" "greatest"
"handler" "header" "hold" "hour" "identity" "if" "immediate"
"immutable" "implicit" "including" "increment" "index" "indexes"
"inherit" "inherits" "inline" "inout" "input" "insensitive" "insert"
"instead" "invoker" "isolation" "key" "label" "language" "large" "last"
"lc_collate" "lc_ctype" "leakproof" "least" "level" "listen" "load" "local"
"location" "lock" "login" "mapping" "match" "maxvalue" "minute"
"minvalue" "mode" "month" "move" "names" "national" "nchar"
"next" "no" "nocreatedb" "nocreaterole" "nocreateuser" "noinherit"
"nologin" "none"  "noreplication" "nosuperuser" "nothing" "notify" "nowait" "nullif"
"nulls" "object" "of" "off" "oids" "operator" "option" "options" "out"
"overlay" "owned" "owner" "parser" "partial" "partition" "passing" "password"
"plans" "position" "preceding" "precision" "prepare" "prepared" "preserve" "prior"
"privileges" "procedural" "procedure" "quote" "range" "read"
"reassign" "recheck" "recursive" "ref" "reindex" "relative" "release"
"rename" "repeatable" "replace" "replica" "replication" "reset" "restart" "restrict"
"returns" "revoke" "role" "rollback" "row" "rows" "rule" "savepoint"
"schema" "scroll" "search" "second" "security" "sequence"
"serializable" "server" "session" "set" "setof" "share" "show"
"simple" "snapshot" "stable" "standalone" "start" "statement" "statistics"
"stdin" "stdout" "storage" "strict" "strip" "substring" "superuser"
"sysid" "system" "tables" "tablespace" "temp" "template" "temporary"
"transaction" "treat" "trim" "truncate" "trusted" "type" "types"
"unbounded" "uncommitted" "unencrypted" "unlisten" "unlogged" "until"
"update" "vacuum" "valid" "validate" "validator" "value" "values" "varying" "version"
"view" "volatile" "whitespace" "without" "work" "wrapper" "write"
"xmlattributes" "xmlconcat" "xmlelement" "xmlexists" "xmlforest" "xmlparse"
"xmlpi" "xmlroot" "xmlserialize" "year" "yes" "zone"
)

     ;; Postgres Reserved
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"all" "analyse" "analyze" "and" "array" "asc" "as" "asymmetric"
"authorization" "binary" "both" "case" "cast" "check" "collate"
"column" "concurrently" "constraint" "create" "cross"
"current_catalog" "current_date" "current_role" "current_schema"
"current_time" "current_timestamp" "current_user" "default"
"deferrable" "desc" "distinct" "do" "else" "end" "except" "false"
"fetch" "foreign" "for" "freeze" "from" "full" "grant" "group"
"having" "ilike" "initially" "inner" "in" "intersect" "into" "isnull"
"is" "join" "leading" "left" "like" "limit" "localtime"
"localtimestamp" "natural" "notnull" "not" "null" "offset"
"only" "on" "order" "or" "outer" "overlaps" "over" "placing" "primary"
"references" "returning" "right" "select" "session_user" "similar"
"some" "symmetric" "table" "then" "to" "trailing" "true" "union"
"unique" "user" "using" "variadic" "verbose" "when" "where" "window"
"with"
)

     ;; Postgres PL/pgSQL
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"assign" "if" "case" "loop" "while" "for" "foreach" "exit" "elsif" "return"
"raise" "execsql" "dynexecute" "perform" "getdiag" "open" "fetch" "move" "close"
)

     ;; Postgres Data Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
 "bigint" "bigserial" "bigserial" "bit" "bool" "boolean" "box" "bytea" "char"
"character" "cidr" "circle" "date" "daterange" "decimal" "double" "float4"
"float8" "inet" "int" "int2" "int4" "int4range" "int8" "int8range" "integer"
"interval" "jsonb" "jsonpath" "line" "lseg" "macaddr" "macaddr8" "money"
"name" "numeric" "numrange" "oid" "path" "point" "polygon" "precision" "real"
"regclass" "regcollation" "regconfig" "regdictionary" "regnamespace "
"regoper" "regoperator" "regproc" "regprocedure" "regrole" "regtype"
"sequences" "serial" "serial4" "serial8" "smallint" "smallserial" "text"
"time" "timestamp" "timestamptz" "timetz" "tsquery" "tsrange" "tstzrange"
"tsvector" "txid_snapshot" "unknown" "uuid" "varbit" "varchar" "varying"
"without" "xml" "zone"
)))

  "Postgres SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-postgres-font-lock-keywords'.")

(defvar sql-mode-linter-font-lock-keywords
  (eval-when-compile
    (list
     ;; Linter Keywords
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
"committed" "count" "countblob" "cross" "current" "data" "database"
"datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
"denied" "description" "device" "difference" "directory" "error"
"escape" "euc" "exclusive" "external" "extfile" "false" "file"
"filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
"full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
"indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
"internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
"linter_file_device" "linter_file_size" "linter_name_length" "ln"
"local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
"minvalue" "module" "names" "national" "natural" "new" "new_table"
"no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
"only" "operation" "optimistic" "option" "page" "partially" "password"
"phrase" "plan" "precision" "primary" "priority" "privileges"
"proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
"read" "record" "records" "references" "remote" "rename" "replication"
"restart" "rewrite" "root" "row" "rule" "savepoint" "security"
"sensitive" "sequence" "serializable" "server" "since" "size" "some"
"startup" "statement" "station" "success" "sys_guid" "tables" "test"
"timeout" "trace" "transaction" "translation" "trigger"
"trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
"unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
"wait" "windows_code" "workspace" "write" "xml"
)

     ;; Linter Reserved
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"access" "action" "add" "address" "after" "all" "alter" "always" "and"
"any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
"aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
"blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
"clear" "close" "column" "comment" "commit" "connect" "contains"
"correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
"drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
"execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
"grant" "group" "having" "identified" "in" "index" "inner" "insert"
"instead" "intersect" "into" "is" "isolation" "join" "left" "level"
"like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
"or" "order" "outer" "owner" "press" "prior" "procedure" "public"
"purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
"rollback" "rownum" "select" "session" "set" "share" "shutdown"
"start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
"to" "union" "unique" "unlock" "until" "update" "using" "values"
"view" "when" "where" "with" "without"
)

     ;; Linter Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
"exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
"getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
"lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
"octet_length" "power" "rand" "rawtohex" "repeat_string"
"right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
"sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
"to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
"substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
"instr" "least" "multime" "replace" "width"
)

     ;; Linter Data Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"bigint" "bitmap" "blob" "boolean" "char" "character" "date"
"datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
"number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
"cursor" "long"
)))

  "Linter SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.")

(defvar sql-mode-ms-font-lock-keywords
  (eval-when-compile
    (list
     ;; MS isql/osql Commands
     (cons
      (concat
       "^\\(?:\\(?:set\\s-+\\(?:"
       (regexp-opt '(
"datefirst" "dateformat" "deadlock_priority" "lock_timeout"
"concat_null_yields_null" "cursor_close_on_commit"
"disable_def_cnst_chk" "fips_flagger" "identity_insert" "language"
"offsets" "quoted_identifier" "arithabort" "arithignore" "fmtonly"
"nocount" "noexec" "numeric_roundabort" "parseonly"
"query_governor_cost_limit" "rowcount" "textsize" "ansi_defaults"
"ansi_null_dflt_off" "ansi_null_dflt_on" "ansi_nulls" "ansi_padding"
"ansi_warnings" "forceplan" "showplan_all" "showplan_text"
"statistics" "implicit_transactions" "remote_proc_transactions"
"transaction" "xact_abort"
)
                   t)
       "\\)\\)\\|go\\s-*\\|use\\s-+\\|setuser\\s-+\\|dbcc\\s-+\\).*$")
      'font-lock-doc-face)

     ;; MS Reserved
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"absolute" "add" "all" "alter" "and" "any" "as" "asc" "authorization"
"avg" "backup" "begin" "between" "break" "browse" "bulk" "by"
"cascade" "case" "check" "checkpoint" "close" "clustered" "coalesce"
"column" "commit" "committed" "compute" "confirm" "constraint"
"contains" "containstable" "continue" "controlrow" "convert" "count"
"create" "cross" "current" "current_date" "current_time"
"current_timestamp" "current_user" "database" "deallocate" "declare"
"default" "delete" "deny" "desc" "disk" "distinct" "distributed"
"double" "drop" "dummy" "dump" "else" "end" "errlvl" "errorexit"
"escape" "except" "exec" "execute" "exists" "exit" "fetch" "file"
"fillfactor" "first" "floppy" "for" "foreign" "freetext"
"freetexttable" "from" "full" "goto" "grant" "group" "having"
"holdlock" "identity" "identity_insert" "identitycol" "if" "in"
"index" "inner" "insert" "intersect" "into" "is" "isolation" "join"
"key" "kill" "last" "left" "level" "like" "lineno" "load" "max" "min"
"mirrorexit" "national" "next" "nocheck" "nolock" "nonclustered" "not"
"null" "nullif" "of" "off" "offsets" "on" "once" "only" "open"
"opendatasource" "openquery" "openrowset" "option" "or" "order"
"outer" "output" "over" "paglock" "percent" "perm" "permanent" "pipe"
"plan" "precision" "prepare" "primary" "print" "prior" "privileges"
"proc" "procedure" "processexit" "public" "raiserror" "read"
"readcommitted" "readpast" "readtext" "readuncommitted" "reconfigure"
"references" "relative" "repeatable" "repeatableread" "replication"
"restore" "restrict" "return" "revoke" "right" "rollback" "rowcount"
"rowguidcol" "rowlock" "rule" "save" "schema" "select" "serializable"
"session_user" "set" "shutdown" "some" "statistics" "sum"
"system_user" "table" "tablock" "tablockx" "tape" "temp" "temporary"
"textsize" "then" "to" "top" "tran" "transaction" "trigger" "truncate"
"tsequal" "uncommitted" "union" "unique" "update" "updatetext"
"updlock" "use" "user" "values" "view" "waitfor" "when" "where"
"while" "with" "work" "writetext" "collate" "function" "openxml"
"returns"
)

     ;; MS Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"@@connections" "@@cpu_busy" "@@cursor_rows" "@@datefirst" "@@dbts"
"@@error" "@@fetch_status" "@@identity" "@@idle" "@@io_busy"
"@@langid" "@@language" "@@lock_timeout" "@@max_connections"
"@@max_precision" "@@nestlevel" "@@options" "@@pack_received"
"@@pack_sent" "@@packet_errors" "@@procid" "@@remserver" "@@rowcount"
"@@servername" "@@servicename" "@@spid" "@@textsize" "@@timeticks"
"@@total_errors" "@@total_read" "@@total_write" "@@trancount"
"@@version" "abs" "acos" "and" "app_name" "ascii" "asin" "atan" "atn2"
"avg" "case" "cast" "ceiling" "char" "charindex" "coalesce"
"col_length" "col_name" "columnproperty" "containstable" "convert"
"cos" "cot" "count" "current_timestamp" "current_user" "cursor_status"
"databaseproperty" "datalength" "dateadd" "datediff" "datename"
"datepart" "day" "db_id" "db_name" "degrees" "difference" "exp"
"file_id" "file_name" "filegroup_id" "filegroup_name"
"filegroupproperty" "fileproperty" "floor" "formatmessage"
"freetexttable" "fulltextcatalogproperty" "fulltextserviceproperty"
"getansinull" "getdate" "grouping" "host_id" "host_name" "ident_incr"
"ident_seed" "identity" "index_col" "indexproperty" "is_member"
"is_srvrolemember" "isdate" "isnull" "isnumeric" "left" "len" "log"
"log10" "lower" "ltrim" "max" "min" "month" "nchar" "newid" "nullif"
"object_id" "object_name" "objectproperty" "openquery" "openrowset"
"parsename" "patindex" "patindex" "permissions" "pi" "power"
"quotename" "radians" "rand" "replace" "replicate" "reverse" "right"
"round" "rtrim" "session_user" "sign" "sin" "soundex" "space" "sqrt"
"square" "stats_date" "stdev" "stdevp" "str" "stuff" "substring" "sum"
"suser_id" "suser_name" "suser_sid" "suser_sname" "system_user" "tan"
"textptr" "textvalid" "typeproperty" "unicode" "upper" "user"
"user_id" "user_name" "var" "varp" "year"
)

     ;; MS Variables
     '("\\b@[a-zA-Z0-9_]*\\b" . font-lock-variable-name-face)

     ;; MS Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"binary" "bit" "char" "character" "cursor" "datetime" "dec" "decimal"
"double" "float" "image" "int" "integer" "money" "national" "nchar"
"ntext" "numeric" "numeric" "nvarchar" "precision" "real"
"smalldatetime" "smallint" "smallmoney" "text" "timestamp" "tinyint"
"uniqueidentifier" "varbinary" "varchar" "varying"
)))

  "Microsoft SQLServer SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-ms-font-lock-keywords'.")

(defvar sql-mode-sybase-font-lock-keywords nil
  "Sybase SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-sybase-font-lock-keywords'.")

(defvar sql-mode-informix-font-lock-keywords nil
  "Informix SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-informix-font-lock-keywords'.")

(defvar sql-mode-interbase-font-lock-keywords nil
  "Interbase SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-interbase-font-lock-keywords'.")

(defvar sql-mode-ingres-font-lock-keywords nil
  "Ingres SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-interbase-font-lock-keywords'.")

(defvar sql-mode-solid-font-lock-keywords nil
  "Solid SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-solid-font-lock-keywords'.")

(defvaralias 'sql-mode-mariadb-font-lock-keywords 'sql-mode-mysql-font-lock-keywords
  "MariaDB is SQL compatible with MySQL.")

(defvar sql-mode-mysql-font-lock-keywords
  (eval-when-compile
    (list
     ;; MySQL Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
"acos" "adddate" "addtime" "aes_decrypt" "aes_encrypt" "area"
"asbinary" "ascii" "asin" "astext" "aswkb" "aswkt" "atan" "atan2"
"avg" "bdmpolyfromtext" "bdmpolyfromwkb" "bdpolyfromtext"
"bdpolyfromwkb" "benchmark" "bin" "binlog_gtid_pos" "bit_and"
"bit_count" "bit_length" "bit_or" "bit_xor" "both" "boundary" "buffer"
"cast" "ceil" "ceiling" "centroid" "character_length" "char_length"
"charset" "coalesce" "coercibility" "column_add" "column_check"
"column_create" "column_delete" "column_exists" "column_get"
"column_json" "column_list" "compress" "concat" "concat_ws"
"connection_id" "conv" "convert" "convert_tz" "convexhull" "cos" "cot"
"count" "crc32" "crosses" "cume_dist" "cume_dist" "curdate"
"current_date" "current_time" "current_timestamp" "curtime" "date_add"
"datediff" "date_format" "date_sub" "dayname" "dayofmonth" "dayofweek"
"dayofyear" "decode" "decode_histogram" "degrees" "dense_rank"
"dense_rank" "des_decrypt" "des_encrypt" "dimension" "disjoint" "div"
"elt" "encode" "encrypt" "endpoint" "envelope" "exp" "export_set"
"exteriorring" "extractvalue" "field" "find_in_set" "floor" "format"
"found_rows" "from" "from_base64" "from_days" "from_unixtime"
"geomcollfromtext" "geomcollfromwkb" "geometrycollectionfromtext"
"geometrycollectionfromwkb" "geometryfromtext" "geometryfromwkb"
"geometryn" "geometrytype" "geomfromtext" "geomfromwkb" "get_format"
"get_lock" "glength" "greatest" "group_concat" "hex" "ifnull"
"inet6_aton" "inet6_ntoa" "inet_aton" "inet_ntoa" "instr"
"interiorringn" "intersects" "interval" "isclosed" "isempty"
"is_free_lock" "is_ipv4" "is_ipv4_compat" "is_ipv4_mapped" "is_ipv6"
"isnull" "isring" "issimple" "is_used_lock" "json_array"
"json_array_append" "json_array_insert" "json_compact" "json_contains"
"json_contains_path" "json_depth" "json_detailed" "json_exists"
"json_extract" "json_insert" "json_keys" "json_length" "json_loose"
"json_merge" "json_object" "json_query" "json_quote" "json_remove"
"json_replace" "json_search" "json_set" "json_type" "json_unquote"
"json_valid" "json_value" "lag" "last_day" "last_insert_id" "lastval"
"last_value" "last_value" "lcase" "lead" "leading" "least" "length"
"linefromtext" "linefromwkb" "linestringfromtext" "linestringfromwkb"
"ln" "load_file" "locate" "log" "log10" "log2" "lower" "lpad" "ltrim"
"makedate" "make_set" "maketime" "master_gtid_wait" "master_pos_wait"
"max" "mbrcontains" "mbrdisjoint" "mbrequal" "mbrintersects"
"mbroverlaps" "mbrtouches" "mbrwithin" "md5" "median"
"mid" "min" "mlinefromtext" "mlinefromwkb" "monthname"
"mpointfromtext" "mpointfromwkb" "mpolyfromtext" "mpolyfromwkb"
"multilinestringfromtext" "multilinestringfromwkb"
"multipointfromtext" "multipointfromwkb" "multipolygonfromtext"
"multipolygonfromwkb" "name_const" "nextval" "now" "nth_value" "ntile"
"ntile" "nullif" "numgeometries" "numinteriorrings" "numpoints" "oct"
"octet_length" "old_password" "ord" "percentile_cont"
"percentile_disc" "percent_rank" "percent_rank" "period_add"
"period_diff" "pi" "pointfromtext" "pointfromwkb" "pointn"
"pointonsurface" "polyfromtext" "polyfromwkb" "polygonfromtext"
"polygonfromwkb" "position" "pow" "power" "quote" "radians"
"rand" "rank" "rank" "regexp" "regexp_instr" "regexp_replace"
"regexp_substr" "release_lock" "repeat" "replace" "reverse" "rlike"
"row_number" "row_number" "rpad" "rtrim" "sec_to_time" "setval" "sha"
"sha1" "sha2" "sign" "sin" "sleep" "soundex" "space"
"spider_bg_direct_sql" "spider_copy_tables" "spider_direct_sql"
"spider_flush_table_mon_cache" "sqrt" "srid" "st_area" "startpoint"
"st_asbinary" "st_astext" "st_aswkb" "st_aswkt" "st_boundary"
"st_buffer" "st_centroid" "st_contains" "st_convexhull" "st_crosses"
"std" "stddev" "stddev_pop" "stddev_samp" "st_difference"
"st_dimension" "st_disjoint" "st_distance" "st_endpoint" "st_envelope"
"st_equals" "st_exteriorring" "st_geomcollfromtext"
"st_geomcollfromwkb" "st_geometrycollectionfromtext"
"st_geometrycollectionfromwkb" "st_geometryfromtext"
"st_geometryfromwkb" "st_geometryn" "st_geometrytype"
"st_geomfromtext" "st_geomfromwkb" "st_interiorringn"
"st_intersection" "st_intersects" "st_isclosed" "st_isempty"
"st_isring" "st_issimple" "st_length" "st_linefromtext"
"st_linefromwkb" "st_linestringfromtext" "st_linestringfromwkb"
"st_numgeometries" "st_numinteriorrings" "st_numpoints" "st_overlaps"
"st_pointfromtext" "st_pointfromwkb" "st_pointn" "st_pointonsurface"
"st_polyfromtext" "st_polyfromwkb" "st_polygonfromtext"
"st_polygonfromwkb" "strcmp" "st_relate" "str_to_date" "st_srid"
"st_startpoint" "st_symdifference" "st_touches" "st_union" "st_within"
"st_x" "st_y" "subdate" "substr" "substring" "substring_index"
"subtime" "sum" "sysdate" "tan" "timediff" "time_format"
"timestampadd" "timestampdiff" "time_to_sec" "to_base64" "to_days"
"to_seconds" "touches" "trailing" "trim" "ucase" "uncompress"
"uncompressed_length" "unhex" "unix_timestamp" "updatexml" "upper"
"user" "utc_date" "utc_time" "utc_timestamp" "uuid" "uuid_short"
"variance" "var_pop" "var_samp" "version" "weekday"
"weekofyear" "weight_string" "within"
)

     ;; MySQL Keywords
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"accessible" "action" "add" "after" "against" "all" "alter" "analyze"
"and" "as" "asc" "auto_increment" "avg_row_length" "bdb" "between"
"body" "by" "cascade" "case" "change" "character" "check" "checksum"
"close" "collate" "collation" "column" "columns" "comment" "committed"
"concurrent" "condition" "constraint" "create" "cross" "data"
"database" "databases" "default" "delayed" "delay_key_write" "delete"
"desc" "directory" "disable" "distinct" "distinctrow" "do" "drop"
"dual" "dumpfile" "duplicate" "else" "elseif" "elsif" "enable"
"enclosed" "end" "escaped" "exists" "exit" "explain" "fields" "first"
"for" "force" "foreign" "from" "full" "fulltext" "global" "group"
"handler" "having" "heap" "high_priority" "history" "if" "ignore"
"ignore_server_ids" "in" "index" "infile" "inner" "insert"
"insert_method" "into" "is" "isam" "isolation" "join" "key" "keys"
"kill" "last" "leave" "left" "level" "like" "limit" "linear" "lines"
"load" "local" "lock" "long" "loop" "low_priority"
"master_heartbeat_period" "master_ssl_verify_server_cert" "match"
"max_rows" "maxvalue" "merge" "min_rows" "mode" "modify" "mrg_myisam"
"myisam" "natural" "next" "no" "not" "no_write_to_binlog" "null"
"offset" "oj" "on" "open" "optimize" "optionally" "or" "order" "outer"
"outfile" "over" "package" "pack_keys" "partial" "partition"
"password" "period" "prev" "primary" "procedure" "purge" "quick"
"raid0" "raid_type" "raise" "range" "read" "read_write" "references"
"release" "rename" "repeatable" "require" "resignal" "restrict"
"returning" "right" "rollback" "rollup" "row_format" "rowtype"
"savepoint" "schemas" "select" "separator" "serializable" "session"
"set" "share" "show" "signal" "slow" "spatial" "sql_big_result"
"sql_buffer_result" "sql_cache" "sql_calc_found_rows" "sql_no_cache"
"sql_small_result" "ssl" "starting" "straight_join" "striped"
"system_time" "table" "tables" "temporary" "terminated" "then" "to"
"transaction" "truncate" "type" "uncommitted" "undo" "union" "unique"
"unlock" "update" "use" "using" "values" "versioning" "when" "where"
"while" "window" "with" "write" "xor"
)

     ;; MySQL Data Types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"bigint" "binary" "bit" "blob" "bool" "boolean" "byte" "char" "curve"
"date" "datetime" "day" "day_hour" "day_microsecond" "day_minute"
"day_second" "dec" "decimal" "double" "enum" "fixed" "float" "float4"
"float8" "geometry" "geometrycollection" "hour" "hour_microsecond"
"hour_minute" "hour_second" "int" "int1" "int2" "int3" "int4" "int8"
"integer" "json" "line" "linearring" "linestring" "longblob"
"longtext" "mediumblob" "mediumint" "mediumtext" "microsecond"
"middleint" "minute" "minute_microsecond" "minute_second" "month"
"multicurve" "multilinestring" "multipoint" "multipolygon"
"multisurface" "national" "numeric" "point" "polygon" "precision"
"quarter" "real" "second" "second_microsecond" "signed" "smallint"
"surface" "text" "time" "timestamp" "tinyblob" "tinyint" "tinytext"
"unsigned" "varbinary" "varchar" "varcharacter" "week" "year" "year2"
"year4" "year_month" "zerofill"
)))

  "MySQL SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-mysql-font-lock-keywords'.")

(defvar sql-mode-sqlite-font-lock-keywords
  (eval-when-compile
    (list
     ;; SQLite commands
     '("^[.].*$" . font-lock-doc-face)

     ;; SQLite Keyword
     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
"abort" "action" "add" "after" "all" "alter" "analyze" "and" "as"
"asc" "attach" "autoincrement" "before" "begin" "between" "by"
"cascade" "case" "cast" "check" "collate" "column" "commit" "conflict"
"constraint" "create" "cross" "database" "default" "deferrable"
"deferred" "delete" "desc" "detach" "distinct" "drop" "each" "else"
"end" "escape" "except" "exclusive" "exists" "explain" "fail" "for"
"foreign" "from" "full" "glob" "group" "having" "if" "ignore"
"immediate" "in" "index" "indexed" "initially" "inner" "insert"
"instead" "intersect" "into" "is" "isnull" "join" "key" "left" "like"
"limit" "match" "natural" "no" "not" "notnull" "null" "of" "offset"
"on" "or" "order" "outer" "plan" "pragma" "primary" "query" "raise"
"references" "regexp" "reindex" "release" "rename" "replace"
"restrict" "right" "rollback" "row" "savepoint" "select" "set" "table"
"temp" "temporary" "then" "to" "transaction" "trigger" "union"
"unique" "update" "using" "vacuum" "values" "view" "virtual" "when"
"where"
)
     ;; SQLite Data types
     (sql-font-lock-keywords-builder 'font-lock-type-face nil
"int" "integer" "tinyint" "smallint" "mediumint" "bigint" "unsigned"
"big" "int2" "int8" "character" "varchar" "varying" "nchar" "native"
"nvarchar" "text" "clob" "blob" "real" "double" "precision" "float"
"numeric" "number" "decimal" "boolean" "date" "datetime"
)
     ;; SQLite Functions
     (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
;; Core functions
"abs" "changes" "coalesce" "glob" "ifnull" "hex" "last_insert_rowid"
"length" "like" "load_extension" "lower" "ltrim" "max" "min" "nullif"
"quote" "random" "randomblob" "replace" "round" "rtrim" "soundex"
"sqlite_compileoption_get" "sqlite_compileoption_used"
"sqlite_source_id" "sqlite_version" "substr" "total_changes" "trim"
"typeof" "upper" "zeroblob"
;; Date/time functions
"time" "julianday" "strftime"
"current_date" "current_time" "current_timestamp"
;; Aggregate functions
"avg" "count" "group_concat" "max" "min" "sum" "total"
)))

  "SQLite SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-sqlite-font-lock-keywords'.")

(defvar sql-mode-db2-font-lock-keywords nil
  "DB2 SQL keywords used by font-lock.

This variable is used by `sql-mode' and `sql-interactive-mode'.  The
regular expressions are created during compilation by calling the
function `regexp-opt'.  Therefore, take a look at the source before
you define your own `sql-mode-db2-font-lock-keywords'.")

(defvar sql-mode-font-lock-keywords nil
  "SQL keywords used by font-lock.

Setting this variable directly no longer has any affect.  Use
`sql-product' and `sql-add-product-keywords' to control the
highlighting rules in SQL mode.")

\f

;;; SQL Product support functions

(defun sql-read-product (prompt &optional initial)
  "Read a valid SQL product."
  (let ((init (or (and initial (symbol-name initial)) "ansi")))
    (intern (completing-read
             prompt
             (mapcar (lambda (info) (symbol-name (car info)))
                     sql-product-alist)
             nil 'require-match
             init 'sql-product-history init))))

(defun sql-add-product (product display &rest plist)
  "Add support for a database product in `sql-mode'.

Add PRODUCT to `sql-product-alist' which enables `sql-mode' to
properly support syntax highlighting and interactive interaction.
DISPLAY is the name of the SQL product that will appear in the
menu bar and in messages.  PLIST initializes the product
configuration."

  ;; Don't do anything if the product is already supported
  (if (assoc product sql-product-alist)
      (user-error "Product `%s' is already defined" product)

    ;; Add product to the alist
    (add-to-list 'sql-product-alist `(,product :name ,display . ,plist))
    ;; Add a menu item to the SQL->Product menu
    (easy-menu-add-item sql-mode-menu '("Product")
			;; Each product is represented by a radio
			;; button with it's display name.
			`[,display
			  (sql-set-product ',product)
			 :style radio
			 :selected (eq sql-product ',product)]
			;; Maintain the product list in
			;; (case-insensitive) alphabetic order of the
			;; display names.  Loop thru each keymap item
			;; looking for an item whose display name is
			;; after this product's name.
			(let ((next-item)
			      (down-display (downcase display)))
                          (map-keymap (lambda (k b)
                                          (when (and (not next-item)
                                                     (string-lessp down-display
                                                                   (downcase (cadr b))))
                                            (setq next-item k)))
				      (easy-menu-get-map sql-mode-menu '("Product")))
			  next-item))
    product))

(defun sql-del-product (product)
  "Remove support for PRODUCT in `sql-mode'."

  ;; Remove the menu item based on the display name
  (easy-menu-remove-item sql-mode-menu '("Product") (sql-get-product-feature product :name))
  ;; Remove the product alist item
  (setq sql-product-alist (assq-delete-all product sql-product-alist))
  nil)

(defun sql-set-product-feature (product feature newvalue)
  "Set FEATURE of database PRODUCT to NEWVALUE.

The PRODUCT must be a symbol which identifies the database
product.  The product must have already exist on the product
list.  See `sql-add-product' to add new products.  The FEATURE
argument must be a plist keyword accepted by
`sql-product-alist'."

  (let* ((p (assoc product sql-product-alist))  ;; (PRODUCT :f v ...)
         (v (plist-member (cdr p) feature)))    ;; (:FEATURE value ...) or null

    (if p
        (if (member feature sql-indirect-features) ; is indirect
            (if v
                (if (car (cdr v))
                    (if (symbolp (car (cdr v)))
                        ;; Indirect reference
                        (set (car (cdr v)) newvalue)
                      ;; indirect is not a symbol
                      (error "The value of `%s' for `%s' is not a symbol" feature product))
                  ;; keyword present, set the indirect variable name
                  (if (symbolp newvalue)
                      (if (cdr v)
                          (setf (car (cdr v)) newvalue)
                        (setf (cdr v) (list newvalue)))
                    (error "The indirect variable of `%s' for `%s' must be a symbol" feature product)))
              ;; not present; insert list
              (setq v (list feature newvalue))
              (setf (cdr (cdr v)) (cdr p))
              (setf (cdr p) v))
          ;; Not an indirect feature
          (if v
              (if (cdr v)
                  (setf (car (cdr v)) newvalue)
                (setf (cdr v) (list newvalue)))
            ;; no value; insert into the list
            (setq v (list feature newvalue))
            (setf (cdr (cdr v)) (cdr p))
            (setf (cdr p) v)))
      (error "`%s' is not a known product; use `sql-add-product' to add it first" product))))

(defun sql-get-product-feature (product feature &optional fallback not-indirect)
  "Lookup FEATURE associated with a SQL PRODUCT.

If the FEATURE is nil for PRODUCT, and FALLBACK is specified,
then the FEATURE associated with the FALLBACK product is
returned.

If the FEATURE is in the list `sql-indirect-features', and the
NOT-INDIRECT parameter is not set, then the value of the symbol
stored in the connect alist is returned.

See `sql-product-alist' for a list of products and supported features."
  (let* ((p (assoc product sql-product-alist))
         (v (plist-get (cdr p) feature)))

    (if p
        ;; If no value and fallback, lookup feature for fallback
        (if (and (not v)
                 fallback
                 (not (eq product fallback)))
            (sql-get-product-feature fallback feature)

          (if (and
               (member feature sql-indirect-features)
               (not not-indirect)
               (symbolp v))
              (symbol-value v)
            v))
      (error "`%s' is not a known product; use `sql-add-product' to add it first" product)
      nil)))

(defun sql-product-font-lock (keywords-only imenu)
  "Configure font-lock and imenu with product-specific settings.

The KEYWORDS-ONLY flag is passed to font-lock to specify whether
only keywords should be highlighted and syntactic highlighting
skipped.  The IMENU flag indicates whether `imenu' should also be
configured."

  (let
      ;; Get the product-specific syntax-alist.
      ((syntax-alist (sql-product-font-lock-syntax-alist)))

    ;; Get the product-specific keywords.
    (setq-local sql-mode-font-lock-keywords
         (append
          (unless (eq sql-product 'ansi)
            (sql-get-product-feature sql-product :font-lock))
          ;; Always highlight ANSI keywords
          (sql-get-product-feature 'ansi :font-lock)
          ;; Fontify object names in CREATE, DROP and ALTER DDL
          ;; statements
          (list sql-mode-font-lock-object-name)))

    ;; Setup font-lock.  Force re-parsing of `font-lock-defaults'.
    (kill-local-variable 'font-lock-set-defaults)
    (setq-local font-lock-defaults
         (list 'sql-mode-font-lock-keywords
               keywords-only t syntax-alist))

    ;; Force font lock to reinitialize if it is already on
    ;; Otherwise, we can wait until it can be started.
    (when font-lock-mode
      (font-lock-mode-internal nil)
      (font-lock-mode-internal t))

    ;; Setup imenu; it needs the same syntax-alist.
    (when imenu
      (setq imenu-syntax-alist syntax-alist))))

;;;###autoload
(defun sql-add-product-keywords (product keywords &optional append)
  "Add highlighting KEYWORDS for SQL PRODUCT.

PRODUCT should be a symbol, the name of a SQL product, such as
`oracle'.  KEYWORDS should be a list; see the variable
`font-lock-keywords'.  By default they are added at the beginning
of the current highlighting list.  If optional argument APPEND is
`set', they are used to replace the current highlighting list.
If APPEND is any other non-nil value, they are added at the end
of the current highlighting list.

For example:

 (sql-add-product-keywords \\='ms
  \\='((\"\\\\b\\\\w+_t\\\\b\" . font-lock-type-face)))

adds a fontification pattern to fontify identifiers ending in
`_t' as data types."

  (let* ((sql-indirect-features nil)
         (font-lock-var (sql-get-product-feature product :font-lock))
         (old-val))

    (setq old-val (symbol-value font-lock-var))
    (set font-lock-var
	 (if (eq append 'set)
	     keywords
	   (if append
	       (append old-val keywords)
	     (append keywords old-val))))))

(defun sql-for-each-login (login-params body)
  "Iterate through login parameters and return a list of results."
  (delq nil
        (mapcar
         (lambda (param)
             (let ((token (or (car-safe param) param))
                   (plist (cdr-safe param)))
               (funcall body token plist)))
         login-params)))

\f

;;; Functions to switch highlighting

(defun sql-product-syntax-table ()
  (let ((table (copy-syntax-table sql-mode-syntax-table)))
    (mapc (lambda (entry)
              (modify-syntax-entry (car entry) (cdr entry) table))
          (sql-get-product-feature sql-product :syntax-alist))
    table))

(defun sql-product-font-lock-syntax-alist ()
  (append
   ;; Change all symbol character to word characters
   (mapcar
    (lambda (entry) (if (string= (substring (cdr entry) 0 1) "_")
                          (cons (car entry)
                                (concat "w" (substring (cdr entry) 1)))
                        entry))
    (sql-get-product-feature sql-product :syntax-alist))
   '((?_ . "w"))))

(defun sql-highlight-product ()
  "Turn on the font highlighting for the SQL product selected."
  (when (derived-mode-p 'sql-mode)
    ;; Enhance the syntax table for the product
    (set-syntax-table (sql-product-syntax-table))

    ;; Setup font-lock
    (sql-product-font-lock nil t)

    ;; Set the mode name to include the product.
    (setq mode-name (concat "SQL[" (or (sql-get-product-feature sql-product :name)
				       (symbol-name sql-product)) "]"))))

(defun sql-set-product (product)
  "Set `sql-product' to PRODUCT and enable appropriate highlighting."
  (interactive
   (list (sql-read-product "SQL product: ")))
  (if (stringp product) (setq product (intern product)))
  (when (not (assoc product sql-product-alist))
    (user-error "SQL product %s is not supported; treated as ANSI" product)
    (setq product 'ansi))

  ;; Save product setting and fontify.
  (setq sql-product product)
  (sql-highlight-product))
(defalias 'sql-set-dialect 'sql-set-product)
\f
(defun sql-buffer-hidden-p (buf)
  "Is the buffer hidden?"
  (string-prefix-p " "
                   (cond
                    ((stringp buf)
                     (when (get-buffer buf)
                       buf))
                    ((bufferp buf)
                     (buffer-name buf))
                    (t nil))))

(defun sql-display-buffer (buf)
  "Display a SQLi buffer based on `sql-display-sqli-buffer-function'.

If BUF is hidden or `sql-display-sqli-buffer-function' is nil,
then the buffer will not be displayed.  Otherwise the BUF is
displayed."
  (unless (sql-buffer-hidden-p buf)
    (cond
     ((eq sql-display-sqli-buffer-function t)
      (pop-to-buffer buf))
     ((not sql-display-sqli-buffer-function)
      nil)
     ((functionp sql-display-sqli-buffer-function)
      (funcall sql-display-sqli-buffer-function buf))
     (t
      (message "Invalid setting of `sql-display-sqli-buffer-function'")
      (pop-to-buffer buf)))))

(defun sql-make-progress-reporter (buf message &optional min-value max-value current-value min-change min-time)
  "Make a progress reporter if BUF is not hidden."
  (unless (or (sql-buffer-hidden-p buf)
              (not sql-display-sqli-buffer-function))
    (make-progress-reporter message min-value max-value current-value min-change min-time)))

(defun sql-progress-reporter-update (reporter &optional value)
  "Report progress of an operation in the echo area."
  (when reporter
    (progress-reporter-update reporter value)))

(defun sql-progress-reporter-done (reporter)
  "Print reporter’s message followed by word \"done\" in echo area."
  (when reporter
    (progress-reporter-done reporter)))

;;; SMIE support

;; Needs a lot more love than I can provide.  --Stef

;; (require 'smie)

;; (defconst sql-smie-grammar
;;   (smie-prec2->grammar
;;    (smie-bnf->prec2
;;     ;; Partly based on https://www.h2database.com/html/grammar.html
;;     '((cmd ("SELECT" select-exp "FROM" select-table-exp)
;;            )
;;       (select-exp ("*") (exp) (exp "AS" column-alias))
;;       (column-alias)
;;       (select-table-exp (table-exp "WHERE" exp) (table-exp))
;;       (table-exp)
;;       (exp ("CASE" exp "WHEN" exp "THEN" exp "ELSE" exp "END")
;;            ("CASE" exp "WHEN" exp "THEN" exp "END"))
;;       ;; Random ad-hoc additions.
;;       (foo (foo "," foo))
;;       )
;;     '((assoc ",")))))

;; (defun sql-smie-rules (kind token)
;;   (pcase (cons kind token)
;;     (`(:list-intro . ,_) t)
;;     (`(:before . "(") (smie-rule-parent))))

;;; Motion Functions

(defun sql-statement-regexp (prod)
  (let* ((ansi-stmt (or (sql-get-product-feature 'ansi :statement) "select"))
         (prod-stmt (sql-get-product-feature prod  :statement)))
    (concat "^\\<"
            (if prod-stmt
                (concat "\\(" ansi-stmt "\\|" prod-stmt "\\)")
              ansi-stmt)
            "\\>")))

(defun sql-beginning-of-statement (arg)
  "Move to the beginning of the current SQL statement."
  (interactive "p")

  (let ((here (point))
        (regexp (sql-statement-regexp sql-product))
        last next)

    ;; Go to the end of the statement before the start we desire
    (setq last (or (sql-end-of-statement (- arg))
                   (point-min)))
    ;; And find the end after that
    (setq next (or (sql-end-of-statement 1)
                   (point-max)))

    ;; Our start must be between them
    (goto-char last)
    ;; Find a beginning-of-stmt that's not in a string or comment
    (while (and (re-search-forward regexp next t 1)
                (or (nth 3 (syntax-ppss))
                    (nth 7 (syntax-ppss))))
      (goto-char (match-end 0)))
    (goto-char
     (if (match-data)
        (match-beginning 0)
       last))
    (beginning-of-line)
    ;; If we didn't move, try again
    (when (= here (point))
      (sql-beginning-of-statement (* 2 (cl-signum arg))))))

(defun sql-end-of-statement (arg)
  "Move to the end of the current SQL statement."
  (interactive "p")
  (let ((term (or (sql-get-product-feature sql-product :terminator) ";"))
        (re-search (if (> 0 arg) 're-search-backward 're-search-forward))
        (here (point))
        (n 0))
    (when (consp term)
      (setq term (car term)))
    ;; Iterate until we've moved the desired number of stmt ends
    (while (not (= (cl-signum arg) 0))
      ;; if we're looking at the terminator, jump by 2
      (if (or (and (> 0 arg) (looking-back term nil))
              (and (< 0 arg) (looking-at term)))
          (setq n 2)
        (setq n 1))
      ;; If we found another end-of-stmt
      (if (not (apply re-search term nil t n nil))
          (setq arg 0)
        ;; count it if we're not in a string or comment
        (unless (or (nth 3 (syntax-ppss))
                    (nth 7 (syntax-ppss)))
          (setq arg (- arg (cl-signum arg))))))
    (goto-char (if (match-data)
                   (match-end 0)
                 here))))

;;; Small functions

(defun sql-magic-go (arg)
  "Insert \"o\" and call `comint-send-input'.
`sql-electric-stuff' must be the symbol `go'."
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (if (and (equal sql-electric-stuff 'go)
	   (save-excursion
	     (comint-bol nil)
	     (looking-at "go\\b")))
      (comint-send-input)))
(put 'sql-magic-go 'delete-selection t)

(defun sql-magic-semicolon (arg)
  "Insert semicolon and call `comint-send-input'.
`sql-electric-stuff' must be the symbol `semicolon'."
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (if (equal sql-electric-stuff 'semicolon)
       (comint-send-input)))
(put 'sql-magic-semicolon 'delete-selection t)

(defun sql-accumulate-and-indent ()
  "Continue SQL statement on the next line."
  (interactive)
  (if (fboundp 'comint-accumulate)
      (comint-accumulate)
    (newline))
  (indent-according-to-mode))

(defun sql-help-list-products (indent freep)
  "Generate listing of products available for use under SQLi.

List products with :free-software attribute set to FREEP.  Indent
each line with INDENT."

  (let (sqli-func doc)
    (setq doc "")
    (dolist (p sql-product-alist)
      (setq sqli-func (intern (concat "sql-" (symbol-name (car p)))))

      (if (and (fboundp sqli-func)
	       (eq (sql-get-product-feature (car p) :free-software) freep))
	(setq doc
	      (concat doc
		      indent
		      (or (sql-get-product-feature (car p) :name)
			  (symbol-name (car p)))
		      ":\t"
		      "\\["
		      (symbol-name sqli-func)
		      "]\n"))))
    doc))

(defun sql-help ()
  "Show short help for the SQL modes."
  (interactive)
  (describe-function 'sql-help))
(put 'sql-help 'function-documentation '(sql--make-help-docstring))

(defvar sql--help-docstring
  "Show short help for the SQL modes.
Use an entry function to open an interactive SQL buffer.  This buffer is
usually named `*SQL*'.  The name of the major mode is SQLi.

Use the following commands to start a specific SQL interpreter:

    \\\\FREE

Other non-free SQL implementations are also supported:

    \\\\NONFREE

But we urge you to choose a free implementation instead of these.

You can also use \\[sql-product-interactive] to invoke the
interpreter for the current `sql-product'.

Once you have the SQLi buffer, you can enter SQL statements in the
buffer.  The output generated is appended to the buffer and a new prompt
is generated.  See the In/Out menu in the SQLi buffer for some functions
that help you navigate through the buffer, the input history, etc.

If you have a really complex SQL statement or if you are writing a
procedure, you can do this in a separate buffer.  Put the new buffer in
`sql-mode' by calling \\[sql-mode].  The name of this buffer can be
anything.  The name of the major mode is SQL.

In this SQL buffer (SQL mode), you can send the region or the entire
buffer to the interactive SQL buffer (SQLi mode).  The results are
appended to the SQLi buffer without disturbing your SQL buffer.")

(defun sql--make-help-docstring ()
  "Return a docstring for `sql-help' listing loaded SQL products."
  (let ((doc sql--help-docstring))
    ;; Insert FREE software list
    (when (string-match "^\\(\\s-*\\)[\\][\\]FREE\\s-*$" doc 0)
      (setq doc (replace-match (sql-help-list-products (match-string 1 doc) t)
			       t t doc 0)))
    ;; Insert non-FREE software list
    (when (string-match "^\\(\\s-*\\)[\\][\\]NONFREE\\s-*$" doc 0)
      (setq doc (replace-match (sql-help-list-products (match-string 1 doc) nil)
			       t t doc 0)))
    doc))

(defun sql-default-value (var)
  "Fetch the value of a variable.

If the current buffer is in `sql-interactive-mode', then fetch
the global value, otherwise use the buffer local value."
  (if (derived-mode-p 'sql-interactive-mode)
      (default-value var)
    (buffer-local-value var (current-buffer))))

(defun sql-get-login-ext (symbol prompt history-var plist)
  "Prompt user with extended login parameters.

The global value of SYMBOL is the last value and the global value
of the SYMBOL is set based on the user's input.

If PLIST is nil, then the user is simply prompted for a string
value.

The property `:default' specifies the default value.  If the
`:number' property is non-nil then ask for a number.

The `:file' property prompts for a file name that must match the
regexp pattern specified in its value.

The `:completion' property prompts for a string specified by its
value.  (The property value is used as the PREDICATE argument to
`completing-read'.)

For both `:file' and `:completion', there can also be a
`:must-match' property that controls REQUIRE-MATCH parameter to
`completing-read'."

  (set-default
   symbol
   (let* ((default (plist-get plist :default))
          (last-value (sql-default-value symbol))
          (prompt-def (format-prompt prompt default))
          (use-dialog-box nil))
     (cond
      ((plist-member plist :file)
       (let ((file-name
              (read-file-name prompt-def
                              (file-name-directory last-value)
                              default
                              (if (plist-member plist :must-match)
                                  (plist-get plist :must-match)
                                t)
                              (file-name-nondirectory last-value)
                              (when (plist-get plist :file)
                                `(lambda (f)
                                   (if (not (file-regular-p f))
                                       t
                                     (string-match
                                      (concat "\\<" ,(plist-get plist :file) "\\>")
                                      (file-name-nondirectory f))))))))
         (if (string= file-name "")
             ""
           (expand-file-name file-name))))

      ((plist-member plist :completion)
       (completing-read prompt-def
                        (plist-get plist :completion)
                        nil
                        (if (plist-member plist :must-match)
                            (plist-get plist :must-match)
                          t)
                        last-value
                        history-var
                        default))

      ((plist-get plist :number)
       (read-number (concat prompt ": ") (or default last-value 0)))

      (t
       (read-string prompt-def last-value history-var default))))))

(defun sql-get-login (&rest what)
  "Get username, password and database from the user.

The variables `sql-user', `sql-password', `sql-server', and
`sql-database' can be customized.  They are used as the default values.
Usernames, servers and databases are stored in `sql-user-history',
`sql-server-history' and `database-history'.  Passwords are not stored
in a history.

Parameter WHAT is a list of tokens passed as arguments in the
function call.  The function asks for the username if WHAT
contains the symbol `user', for the password if it contains the
symbol `password', for the server if it contains the symbol
`server', and for the database if it contains the symbol
`database'.  The members of WHAT are processed in the order in
which they are provided.

If the `sql-password-wallet' is non-nil and WHAT contains the
`password' token, then the `password' token will be pushed to the
end to be sure that all of the values can be fed to the wallet.

Each token may also be a list with the token in the car and a
plist of options as the cdr.  The following properties are
supported:

    :file <filename-regexp>
    :completion <list-of-strings-or-function>
    :default <default-value>
    :number t

In order to ask the user for username, password and database, call the
function like this: (sql-get-login \\='user \\='password \\='database)."

  ;; Push the password to the end if we have a wallet
  (when (and sql-password-wallet
             (fboundp sql-password-search-wallet-function)
             (member 'password what))
    (setq what (append (cl-delete 'password what)
                       '(password))))

  ;; Prompt for each parameter
  (dolist (w what)
    (let ((plist (cdr-safe w)))
      (pcase (or (car-safe w) w)
        ('user
         (sql-get-login-ext 'sql-user "User" 'sql-user-history plist))

        ('password
         (setq-default sql-password
                       (if (and sql-password-wallet
                                (fboundp sql-password-search-wallet-function))
                           (let ((password (funcall sql-password-search-wallet-function
                                                    sql-password-wallet
                                                    sql-product
                                                    sql-user
                                                    sql-server
                                                    sql-database
                                                    sql-port)))
                             (if password
                                 password
                               (read-passwd "Password: " nil (sql-default-value 'sql-password))))
                         (read-passwd "Password: " nil (sql-default-value 'sql-password)))))

        ('server
         (sql-get-login-ext 'sql-server "Server" 'sql-server-history plist))

        ('database
         (sql-get-login-ext 'sql-database "Database"
                            'sql-database-history plist))

        ('port
         (sql-get-login-ext 'sql-port "Port"
                            nil (append '(:number t) plist)))))))

(defun sql-find-sqli-buffer (&optional product connection)
  "Return the name of the current default SQLi buffer or nil.
In order to qualify, the SQLi buffer must be alive, be in
`sql-interactive-mode' and have a process."
  (let ((buf  sql-buffer)
        (prod (or product sql-product)))
    (or
     ;; Current sql-buffer, if there is one.
     (and (sql-buffer-live-p buf prod connection)
          buf)
     ;; Global sql-buffer
     (and (setq buf (default-value 'sql-buffer))
          (sql-buffer-live-p buf prod connection)
          buf)
     ;; Look thru each buffer
     (car (apply #'append
                 (mapcar (lambda (b)
                             (and (sql-buffer-live-p b prod connection)
                                  (list (buffer-name b))))
                         (buffer-list)))))))

(defun sql-set-sqli-buffer-generally ()
  "Set SQLi buffer for all SQL buffers that have none.
This function checks all SQL buffers for their SQLi buffer.  If their
SQLi buffer is nonexistent or has no process, it is set to the current
default SQLi buffer.  The current default SQLi buffer is determined
using `sql-find-sqli-buffer'.  If `sql-buffer' is set,
`sql-set-sqli-hook' is run."
  (interactive)
  (save-excursion
    (let ((buflist (buffer-list))
	  (default-buffer (sql-find-sqli-buffer)))
      (setq-default sql-buffer default-buffer)
      (while (not (null buflist))
	(let ((candidate (car buflist)))
	  (set-buffer candidate)
	  (if (and (derived-mode-p 'sql-mode)
		   (not (sql-buffer-live-p sql-buffer)))
	      (progn
		(setq sql-buffer default-buffer)
		(when default-buffer
                  (run-hooks 'sql-set-sqli-hook)))))
	(setq buflist (cdr buflist))))))

(defun sql-set-sqli-buffer ()
  "Set the SQLi buffer SQL strings are sent to.

Call this function in a SQL buffer in order to set the SQLi buffer SQL
strings are sent to.  Calling this function sets `sql-buffer' and runs
`sql-set-sqli-hook'.

If you call it from a SQL buffer, this sets the local copy of
`sql-buffer'.

If you call it from anywhere else, it sets the global copy of
`sql-buffer'."
  (interactive)
  (let ((default-buffer (sql-find-sqli-buffer)))
    (if (null default-buffer)
        (sql-product-interactive)
      (let ((new-buffer (read-buffer "New SQLi buffer: " default-buffer t)))
        (if (null (sql-buffer-live-p new-buffer))
            (user-error "Buffer %s is not a working SQLi buffer" new-buffer)
          (when new-buffer
            (setq sql-buffer new-buffer)
            (run-hooks 'sql-set-sqli-hook)))))))

(defun sql-show-sqli-buffer ()
  "Display the current SQLi buffer.

This is the buffer SQL strings are sent to.
It is stored in the variable `sql-buffer'.
I
See also `sql-help' on how to create such a buffer."
  (interactive)
  (unless (and sql-buffer (buffer-live-p (get-buffer sql-buffer))
               (get-buffer-process sql-buffer))
    (sql-set-sqli-buffer))
  (display-buffer sql-buffer))

(defun sql-make-alternate-buffer-name (&optional product)
  "Return a string that can be used to rename a SQLi buffer.
This is used to set `sql-alternate-buffer-name' within
`sql-interactive-mode'.

If the session was started with `sql-connect' then the alternate
name would be the name of the connection.

Otherwise, it uses the parameters identified by the :sqlilogin
parameter.

If all else fails, the alternate name would be the user and
server/database name."

  (let ((name ""))

    ;; Build a name using the :sqli-login setting
    (setq name
          (apply #'concat
                 (cdr
                  (apply #'append nil
                         (sql-for-each-login
                          (sql-get-product-feature (or product sql-product) :sqli-login)
                          (lambda (token plist)
                              (pcase token
                                ('user
                                 (unless (string= "" sql-user)
                                   (list "/" sql-user)))
                                ('port
                                 (unless (or (not (numberp sql-port))
                                             (= 0 sql-port))
                                   (list ":" (number-to-string sql-port))))
                                ('server
                                 (unless (string= "" sql-server)
                                   (list "."
                                         (if (plist-member plist :file)
                                             (file-name-nondirectory sql-server)
                                           sql-server))))
                                ('database
                                 (unless (string= "" sql-database)
                                   (list "@"
                                         (if (plist-member plist :file)
                                             (file-name-nondirectory sql-database)
                                           sql-database))))

                                ;; (`password nil)
                                (_         nil))))))))

    ;; If there's a connection, use it and the name thus far
    (if sql-connection
        (format "<%s>%s" sql-connection (or name ""))

      ;; If there is no name, try to create something meaningful
      (if (string= "" (or name ""))
          (concat
           (if (string= "" sql-user)
               (if (string= "" (user-login-name))
                   ()
                 (concat (user-login-name) "/"))
             (concat sql-user "/"))
           (if (string= "" sql-database)
               (if (string= "" sql-server)
               (system-name)
               sql-server)
             sql-database))

        ;; Use the name we've got
        name))))

(defun sql-generate-unique-sqli-buffer-name (product base)
  "Generate a new, unique buffer name for a SQLi buffer.

Append a sequence number until a unique name is found."
  (let ((base-name (substring-no-properties
                    (if base
                        (if (stringp base)
                            base
                          (format "%S" base))
                      (or (sql-get-product-feature product :name)
                          (symbol-name product)))))
        buf-fmt-1st
        buf-fmt-rest)

    ;; Calculate buffer format
    (if (string-blank-p base-name)
        (setq buf-fmt-1st  "*SQL*"
              buf-fmt-rest "*SQL-%d*")
      (setq buf-fmt-1st  (format "*SQL: %s*" base-name)
            buf-fmt-rest (format "*SQL: %s-%%d*" base-name)))

    ;; See if we can find an unused buffer
    (let ((buf-name buf-fmt-1st)
          (i 1))
      (while (if (sql-is-sqli-buffer-p buf-name)
                 (comint-check-proc buf-name)
               (buffer-live-p (get-buffer buf-name)))
        ;; Check a sequence number on the BASE
        (setq buf-name (format buf-fmt-rest i)
              i (1+ i)))

      buf-name)))

(defun sql-rename-buffer (&optional new-name)
  "Rename a SQL interactive buffer.

Prompts for the new name if command is preceded by
\\[universal-argument].  If no buffer name is provided, then the
`sql-alternate-buffer-name' is used.

The actual buffer name set will be \"*SQL: NEW-NAME*\".  If
NEW-NAME is empty, then the buffer name will be \"*SQL*\"."
  (interactive "P")

  (if (not (derived-mode-p 'sql-interactive-mode))
      (user-error "Current buffer is not a SQL interactive buffer")

    (setq sql-alternate-buffer-name
          (substring-no-properties
           (cond
            ((stringp new-name)
             new-name)
            ((consp new-name)
             (read-string "Buffer name (\"*SQL: XXX*\"; enter `XXX'): "
                          sql-alternate-buffer-name))
            (t
             sql-alternate-buffer-name))))

    (rename-buffer
     (sql-generate-unique-sqli-buffer-name sql-product
                                           sql-alternate-buffer-name)
     t)))

(defun sql-copy-column ()
  "Copy current column to the end of buffer.
Inserts SELECT or commas if appropriate."
  (interactive)
  (let ((column))
    (save-excursion
      (setq column (buffer-substring-no-properties
		  (progn (forward-char 1) (backward-sexp 1) (point))
		  (progn (forward-sexp 1) (point))))
      (goto-char (point-max))
      (let ((bol (comint-line-beginning-position)))
	(cond
	 ;; if empty command line, insert SELECT
	 ((= bol (point))
	  (insert "SELECT "))
	 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
	 ((save-excursion
	    (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
				bol t))
	  (insert ", "))
	 ;; else insert a space
	 (t
	  (if (eq (preceding-char) ?\s)
	      nil
	    (insert " ")))))
      ;; in any case, insert the column
      (insert column)
      (message "%s" column))))

;; On Windows, SQL*Plus for Oracle turns on full buffering for stdout
;; if it is not attached to a character device; therefore placeholder
;; replacement by SQL*Plus is fully buffered.  The workaround lets
;; Emacs query for the placeholders.

(defvar sql-placeholder-history nil
  "History of placeholder values used.")

(defun sql-placeholders-filter (string)
  "Replace placeholders in STRING.
Placeholders are words starting with an ampersand like &this."

  (when sql-oracle-scan-on
    (let ((start 0)
          (replacement ""))
      (while (string-match "&?&\\(\\(?:\\sw\\|\\s_\\)+\\)[.]?" string start)
        (setq replacement (read-from-minibuffer
		           (format "Enter value for %s: "
                                   (propertize (match-string 1 string)
                                               'face 'font-lock-variable-name-face))
		           nil nil nil 'sql-placeholder-history)
              string (replace-match replacement t t string)
              start (+ (match-beginning 1) (length replacement))))))
  string)

;; Using DB2 interactively, newlines must be escaped with " \".
;; The space before the backslash is relevant.

(defun sql-escape-newlines-filter (string)
  "Escape newlines in STRING.
Every newline in STRING will be preceded with a space and a backslash."
  (if (not sql-db2-escape-newlines)
      string
    (let ((result "") (start 0) mb me)
      (while (string-match "\n" string start)
        (setq mb (match-beginning 0)
              me (match-end 0)
              result (concat result
                             (substring string start mb)
                             (if (and (> mb 1)
                                      (string-equal " \\" (substring string (- mb 2) mb)))
                                 "" " \\\n"))
              start me))
      (concat result (substring string start)))))

\f

;;; Input sender for SQLi buffers

(defvar sql-output-newline-count 0
  "Number of newlines in the input string.

Allows the suppression of continuation prompts.")

(defun sql-input-sender (proc string)
  "Send STRING to PROC after applying filters."

  (let* ((product (buffer-local-value 'sql-product (process-buffer proc)))
	 (filter  (sql-get-product-feature product :input-filter)))

    ;; Apply filter(s)
    (cond
     ((not filter)
      nil)
     ((functionp filter)
      (setq string (funcall filter string)))
     ((listp filter)
      (mapc (lambda (f) (setq string (funcall f string))) filter))
     (t nil))

    ;; Count how many newlines in the string
    (setq sql-output-newline-count
          (apply #'+ (mapcar (lambda (ch) (if (eq ch ?\n) 1 0))
                             string)))

    ;; Send the string
    (comint-simple-send proc string)))

;;; Strip out continuation prompts

(defvar sql-preoutput-hold nil)

(defun sql-interactive-remove-continuation-prompt (oline)
  "Strip out continuation prompts out of the OLINE.

Added to the `comint-preoutput-filter-functions' hook in a SQL
interactive buffer.  The complication to this filter is that the
continuation prompts may arrive in multiple chunks.  If they do,
then the function saves any unfiltered output in a buffer and
prepends that buffer to the next chunk to properly match the
broken-up prompt.

The filter goes into play only if something is already
accumulated, or we're waiting for continuation
prompts (`sql-output-newline-count' is positive).  In this case:
- Accumulate process output into `sql-preoutput-hold'.
- Remove any complete prompts / continuation prompts that we're waiting
  for.
- In case we're expecting more prompts - return all currently
  accumulated _complete_ lines, leaving the rest for the next
  invocation.  They will appear in the output immediately.  This way we
  don't accumulate large chunks of data for no reason.
- If we found all expected prompts - just return all current accumulated
  data."
  (when (and comint-prompt-regexp
             ;; We either already have something held, or expect
             ;; prompts
             (or sql-preoutput-hold
                 (and sql-output-newline-count
                      (> sql-output-newline-count 0))))
    (save-match-data
      ;; Add this text to what's left from the last pass
      (setq oline (concat sql-preoutput-hold oline)
            sql-preoutput-hold nil)

      ;; If we are looking for prompts
      (when (and sql-output-newline-count
                 (> sql-output-newline-count 0))
        ;; Loop thru each starting prompt and remove it
        (while (and (not (string-empty-p oline))
                    (> sql-output-newline-count 0)
                    (string-match comint-prompt-regexp oline))
          (setq oline (replace-match "" nil nil oline)
                sql-output-newline-count (1- sql-output-newline-count)))

        ;; If we've found all the expected prompts, stop looking
        (if (= sql-output-newline-count 0)
            (setq sql-output-newline-count nil)
          ;; Still more possible prompts, leave them for the next pass
          (setq sql-preoutput-hold oline
                oline "")))

      ;; Lines that are now complete may be passed further
      (when sql-preoutput-hold
        (let ((last-nl 0))
          (while (string-match "\n" sql-preoutput-hold last-nl)
            (setq last-nl (match-end 0)))
          ;; Return up to last nl, hold after the last nl
          (setq oline (substring sql-preoutput-hold 0 last-nl)
                sql-preoutput-hold (substring sql-preoutput-hold last-nl))
          (when (string-empty-p sql-preoutput-hold)
            (setq sql-preoutput-hold nil))))))
  oline)


;;; Sending the region to the SQLi buffer.
(defvar sql-debug-send nil
  "Display text sent to SQL process pragmatically.")

(defun sql-send-string (str)
  "Send the string STR to the SQL process."
  (interactive "sSQL Text: ")

  (let ((comint-input-sender-no-newline nil)
        (s (replace-regexp-in-string "[[:space:]\n\r]+\\'" "" str)))
    (if (sql-buffer-live-p sql-buffer)
	(progn
	  ;; Ignore the hoping around...
	  (save-excursion
	    ;; Set product context
	    (with-current-buffer sql-buffer
              (when sql-debug-send
                (message ">>SQL> %S" s))
              (insert "\n")
              (comint-set-process-mark)

	      ;; Send the string (trim the trailing whitespace)
	      (sql-input-sender (get-buffer-process (current-buffer)) s)

	      ;; Send a command terminator if we must
	      (sql-send-magic-terminator sql-buffer s sql-send-terminator)

              (when sql-pop-to-buffer-after-send-region
	        (message "Sent string to buffer %s" sql-buffer))))

	  ;; Display the sql buffer
	  (sql-display-buffer sql-buffer))

    ;; We don't have no stinkin' sql
    (user-error "No SQL process started"))))

(defun sql-send-region (start end)
  "Send a region to the SQL process."
  (interactive "r")
  (sql-send-string (buffer-substring-no-properties start end)))

(defun sql-send-paragraph ()
  "Send the current paragraph to the SQL process."
  (interactive)
  (let ((start (save-excursion
		 (backward-paragraph)
		 (point)))
	(end (save-excursion
	       (forward-paragraph)
	       (point))))
    (sql-send-region start end)))

(defun sql-send-buffer ()
  "Send the buffer contents to the SQL process."
  (interactive)
  (sql-send-region (point-min) (point-max)))

(defun sql-send-line-and-next ()
  "Send the current line to the SQL process and go to the next line."
  (interactive)
  (sql-send-region (line-beginning-position 1) (line-beginning-position 2))
  (beginning-of-line 2)
  (while (forward-comment 1)))  ; skip all comments and whitespace

(defun sql-send-magic-terminator (buf str terminator)
  "Send TERMINATOR to buffer BUF if its not present in STR."
  (let (comint-input-sender-no-newline pat term)
    ;; If flag is merely on(t), get product-specific terminator
    (if (eq terminator t)
	(setq terminator (sql-get-product-feature sql-product :terminator)))

    ;; If there is no terminator specified, use default ";"
    (unless terminator
      (setq terminator ";"))

    ;; Parse the setting into the pattern and the terminator string
    (cond ((stringp terminator)
	   (setq pat (regexp-quote terminator)
		 term terminator))
	  ((consp terminator)
	   (setq pat (car terminator)
		 term (cdr terminator)))
	  (t
	   nil))

    ;; Check to see if the pattern is present in the str already sent
    (unless (and pat term
		 (string-match-p (concat pat "\\'") str))
      (sql-input-sender (get-buffer-process buf) term))))

(defun sql-remove-tabs-filter (str)
  "Replace tab characters with spaces."
  (string-replace "\t" " " str))

(defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
  "Toggle `sql-pop-to-buffer-after-send-region'.

If given the optional parameter VALUE, sets
`sql-toggle-pop-to-buffer-after-send-region' to VALUE."
  (interactive "P")
  (if value
      (setq sql-pop-to-buffer-after-send-region value)
    (setq sql-pop-to-buffer-after-send-region
	  (null sql-pop-to-buffer-after-send-region))))

\f

;;; Redirect output functions

(defvar sql-debug-redirect nil
  "If non-nil, display messages related to the use of redirection.")

(defun sql-str-literal (s)
  (concat "'" (string-replace "[']" "''" s) "'"))

(defun sql-redirect (sqlbuf command &optional outbuf save-prior)
  "Execute the SQL command and send output to OUTBUF.

SQLBUF must be an active SQL interactive buffer.  OUTBUF may be
an existing buffer, or the name of a non-existing buffer.  If
omitted the output is sent to a temporary buffer which will be
killed after the command completes.  COMMAND should be a string
of commands accepted by the SQLi program.  COMMAND may also be a
list of SQLi command strings."

  (let* ((visible (and outbuf
                       (not (sql-buffer-hidden-p outbuf))))
         (this-save  save-prior)
         (next-save  t))

    (when visible
      (message "Executing SQL command..."))

    (if (consp command)
        (dolist (onecmd command)
          (sql-redirect-one sqlbuf onecmd outbuf this-save)
          (setq this-save next-save))
      (sql-redirect-one sqlbuf command outbuf save-prior))

    (when visible
      (message "Executing SQL command...done"))
    nil))

(defun sql-redirect-one (sqlbuf command outbuf save-prior)
  (when command
    (with-current-buffer sqlbuf
      (let ((buf  (get-buffer-create (or outbuf " *SQL-Redirect*")))
            (proc (get-buffer-process (current-buffer)))
            (comint-prompt-regexp (sql-get-product-feature sql-product
                                                           :prompt-regexp))
            (start nil))
        (with-current-buffer buf
          (setq-local view-no-disable-on-exit t)
          (read-only-mode -1)
          (unless save-prior
            (erase-buffer))
          (goto-char (point-max))
          (unless (zerop (buffer-size))
            (insert "\n"))
          (setq start (point)))

        (when sql-debug-redirect
          (message ">>SQL> %S" command))

        ;; Run the command
        (let ((inhibit-quit t)
              comint-preoutput-filter-functions)
          (with-local-quit
            (comint-redirect-send-command-to-process command buf proc nil t)
            (while (or quit-flag (null comint-redirect-completed))
              (accept-process-output nil 1)))

          (if quit-flag
              (comint-redirect-cleanup)
            ;; Clean up the output results
            (with-current-buffer buf
              ;; Remove trailing whitespace
              (goto-char (point-max))
              (when (looking-back "[ \t\f\n\r]*" start)
                (delete-region (match-beginning 0) (match-end 0)))
              ;; Remove echo if there was one
              (goto-char start)
              (when (looking-at (concat "^" (regexp-quote command) "[\\n]"))
                (delete-region (match-beginning 0) (match-end 0)))
              ;; Remove Ctrl-Ms
              (goto-char start)
              (while (re-search-forward "\r+$" nil t)
                (replace-match "" t t))
              (goto-char start))))))))

(defun sql-redirect-value (sqlbuf command &optional regexp regexp-groups)
  "Execute the SQL command and return part of result.

SQLBUF must be an active SQL interactive buffer.  COMMAND should
be a string of commands accepted by the SQLi program.  From the
output, the REGEXP is repeatedly matched and the list of
REGEXP-GROUPS submatches is returned.  This behaves much like
\\[comint-redirect-results-list-from-process] but instead of
returning a single submatch it returns a list of each submatch
for each match."

  (let ((outbuf " *SQL-Redirect-values*")
        (results nil))
    (sql-redirect sqlbuf command outbuf nil)
    (with-current-buffer outbuf
      (while (re-search-forward (or regexp "^.+$") nil t)
	(push
         (cond
          ;; no groups-return all of them
          ((null regexp-groups)
           (let ((i (/ (length (match-data)) 2))
                 (r nil))
             (while (> i 0)
               (setq i (1- i))
               (push (match-string i) r))
             r))
          ;; one group specified
          ((numberp regexp-groups)
           (match-string regexp-groups))
          ;; list of numbers; return the specified matches only
          ((consp regexp-groups)
           (mapcar (lambda (c)
                       (cond
                        ((numberp c) (match-string c))
                        ((stringp c) (match-substitute-replacement c))
                        (t (error "sql-redirect-value: Unknown REGEXP-GROUPS value - %s" c))))
                   regexp-groups))
          ;; String is specified; return replacement string
          ((stringp regexp-groups)
           (match-substitute-replacement regexp-groups))
          (t
           (error "sql-redirect-value: Unknown REGEXP-GROUPS value - %s"
                  regexp-groups)))
         results)))

    (when sql-debug-redirect
      (message ">>SQL> = %S" (reverse results)))

    (nreverse results)))

(defun sql-execute (sqlbuf outbuf command enhanced arg)
  "Execute a command in a SQL interactive buffer and capture the output.

The commands are run in SQLBUF and the output saved in OUTBUF.
COMMAND must be a string, a function or a list of such elements.
Functions are called with SQLBUF, OUTBUF and ARG as parameters;
strings are formatted with ARG and executed.

If the results are empty the OUTBUF is deleted, otherwise the
buffer is popped into a view window."
  (mapc
   (lambda (c)
       (cond
        ((stringp c)
         (sql-redirect sqlbuf (if arg (format c arg) c) outbuf) t)
        ((functionp c)
         (apply c sqlbuf outbuf enhanced arg nil))
        (t (error "Unknown sql-execute item %s" c))))
   (if (consp command) command (cons command nil)))

  (setq outbuf (get-buffer outbuf))
  (if (zerop (buffer-size outbuf))
      (kill-buffer outbuf)
    (let ((one-win (eq (selected-window)
                       (get-lru-window))))
      (with-current-buffer outbuf
        (set-buffer-modified-p nil)
        (setq-local revert-buffer-function
                    (lambda (_ignore-auto _noconfirm)
                      (sql-execute sqlbuf (buffer-name outbuf)
                                   command enhanced arg)))
        (special-mode))
      (pop-to-buffer outbuf)
      (when one-win
        (shrink-window-if-larger-than-buffer)))))

(defun sql-execute-feature (sqlbuf outbuf feature enhanced arg)
  "List objects or details in a separate display buffer."
  (let (command
        (product (buffer-local-value 'sql-product (get-buffer sqlbuf))))
    (setq command (sql-get-product-feature product feature))
    (unless command
      (error "%s does not support %s" product feature))
    (when (consp command)
      (setq command (if enhanced
                        (cdr command)
                      (car command))))
    (sql-execute sqlbuf outbuf command enhanced arg)))

(defvar sql-completion-object nil
  "A list of database objects used for completion.

The list is maintained in SQL interactive buffers.")

(defvar sql-completion-column nil
  "A list of column names used for completion.

The list is maintained in SQL interactive buffers.")

(defun sql-build-completions-1 (schema completion-list feature)
  "Generate a list of objects in the database for use as completions."
  (let ((f (sql-get-product-feature sql-product feature)))
    (when f
      (set completion-list
            (let (cl)
              (dolist (e (append (symbol-value completion-list)
                                 (apply f (current-buffer) (cons schema nil)))
                         cl)
                (unless (member e cl) (setq cl (cons e cl))))
              (sort cl #'string<))))))

(defun sql-build-completions (schema)
  "Generate a list of names in the database for use as completions."
  (sql-build-completions-1 schema 'sql-completion-object :completion-object)
  (sql-build-completions-1 schema 'sql-completion-column :completion-column))

(defvar sql-completion-sqlbuf nil)

(defun sql--completion-table (string pred action)
  (when sql-completion-sqlbuf
    (with-current-buffer sql-completion-sqlbuf
      (let ((schema (and (string-match "\\`\\(\\sw\\(:?\\sw\\|\\s_\\)*\\)[.]" string)
                         (downcase (match-string 1 string)))))

        ;; If we haven't loaded any object name yet, load local schema
        (unless sql-completion-object
          (sql-build-completions nil))

        ;; If they want another schema, load it if we haven't yet
        (when schema
          (let ((schema-dot (concat schema "."))
                (schema-len (1+ (length schema)))
                (names sql-completion-object)
                has-schema)

            (while (and (not has-schema) names)
              (setq has-schema (and
                                (>= (length (car names)) schema-len)
                                (string= schema-dot
                                         (downcase (substring (car names)
                                                              0 schema-len))))
                    names (cdr names)))
            (unless has-schema
              (sql-build-completions schema)))))

      ;; Try to find the completion
      (complete-with-action action sql-completion-object string pred))))

(defun sql-read-table-name (prompt)
  "Read the name of a database table."
  (let* ((tname
          (and (buffer-local-value 'sql-contains-names (current-buffer))
               (thing-at-point-looking-at
                (concat "\\_<\\sw\\(:?\\sw\\|\\s_\\)*"
                        "\\(?:[.]+\\sw\\(?:\\sw\\|\\s_\\)*\\)*\\_>"))
               (buffer-substring-no-properties (match-beginning 0)
                                               (match-end 0))))
         (sql-completion-sqlbuf (sql-find-sqli-buffer))
         (product (when sql-completion-sqlbuf
                    (with-current-buffer sql-completion-sqlbuf sql-product)))
         (completion-ignore-case t))

    (if product
        (if (sql-get-product-feature product :completion-object)
            (completing-read prompt #'sql--completion-table
                             nil nil tname)
          (read-from-minibuffer prompt tname))
      (user-error "There is no active SQLi buffer"))))

(defun sql-list-all (&optional enhanced)
  "List all database objects.
With optional prefix argument ENHANCED, displays additional
details or extends the listing to include other schemas objects."
  (interactive "P")
  (let ((sqlbuf (sql-find-sqli-buffer)))
    (unless sqlbuf
      (user-error "No SQL interactive buffer found"))
    (sql-execute-feature sqlbuf "*List All*" :list-all enhanced nil)
    (with-current-buffer sqlbuf
      ;; Contains the name of database objects
      (setq-local sql-contains-names t)
      (setq-local sql-buffer sqlbuf))))

(defun sql-list-table (name &optional enhanced)
  "List the details of a database table named NAME.
Displays the columns in the relation.  With optional prefix argument
ENHANCED, displays additional details about each column."
  (interactive
   (list (sql-read-table-name "Table name: ")
         current-prefix-arg))
  (let ((sqlbuf (sql-find-sqli-buffer)))
    (unless sqlbuf
      (user-error "No SQL interactive buffer found"))
    (unless name
      (user-error "No table name specified"))
    (sql-execute-feature sqlbuf (format "*List %s*" name)
                         :list-table enhanced name)))
\f

;;; SQL mode -- uses SQL interactive mode

;;;###autoload
(define-derived-mode sql-mode prog-mode "SQL"
  "Major mode to edit SQL.

You can send SQL statements to the SQLi buffer using
\\[sql-send-region].  Such a buffer must exist before you can do this.
See `sql-help' on how to create SQLi buffers.

\\{sql-mode-map}
Customization: Entry to this mode runs the `sql-mode-hook'.

When you put a buffer in SQL mode, the buffer stores the last SQLi
buffer created as its destination in the variable `sql-buffer'.  This
will be the buffer \\[sql-send-region] sends the region to.  If this
SQLi buffer is killed, \\[sql-send-region] is no longer able to
determine where the strings should be sent to.  You can set the
value of `sql-buffer' using \\[sql-set-sqli-buffer].

For information on how to create multiple SQLi buffers, see
`sql-interactive-mode'.

Note that SQL doesn't have an escape character unless you specify
one.  If you specify backslash as escape character in SQL, you
must tell Emacs.  Here's how to do that in your init file:

\(add-hook \\='sql-mode-hook
          (lambda ()
	    (modify-syntax-entry ?\\\\ \"\\\\\" sql-mode-syntax-table)))"
  :abbrev-table sql-mode-abbrev-table

  ;; (smie-setup sql-smie-grammar #'sql-smie-rules)
  (setq-local comment-start "--")
  ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
  (make-local-variable 'sql-buffer)
  ;; Add imenu support for sql-mode.  Note that imenu-generic-expression
  ;; is buffer-local, so we don't need a local-variable for it.  SQL is
  ;; case-insensitive, that's why we have to set imenu-case-fold-search.
  (setq imenu-generic-expression sql-imenu-generic-expression
	imenu-case-fold-search t)
  ;; Make `sql-send-paragraph' work on paragraphs that contain indented
  ;; lines.
  (setq-local paragraph-separate "[\f]*$")
  (setq-local paragraph-start "[\n\f]")
  ;; Abbrevs
  (setq-local abbrev-all-caps 1)
  ;; Contains the name of database objects
  (setq-local sql-contains-names t)
  (setq-local escaped-string-quote "'")
  (setq-local syntax-propertize-function
              (eval
               '(syntax-propertize-rules
                 ;; Handle escaped apostrophes within strings.
                 ((if (eq sql-product 'mysql)
                      "\\\\'"
                    "''")
                  (0
                   (if (save-excursion
                         (nth 3 (syntax-ppss (match-beginning 0))))
	               (string-to-syntax ".")
                     (forward-char -1)
                     nil)))
                 ;; Propertize rules to not have /- and -* start comments.
                 ("\\(/-\\)" (1 "."))
                 ("\\(-\\*\\)"
                  (1
                   (if (save-excursion
                         (not (ppss-comment-depth
                               (syntax-ppss (match-beginning 1)))))
                       ;; If we're outside a comment, we don't let -*
                       ;; start a comment.
	               (string-to-syntax ".")
                     ;; Inside a comment, ignore it to avoid -*/ not
                     ;; being interpreted as a comment end.
                     (forward-char -1)
                     nil))))
               t))
  ;; Set syntax and font-face highlighting
  ;; Catch changes to sql-product and highlight accordingly
  (sql-set-product (or sql-product 'ansi)) ; Fixes bug#13591
  (add-hook 'hack-local-variables-hook #'sql-highlight-product t t))

\f

;;; SQL interactive mode

(put 'sql-interactive-mode 'mode-class 'special)
(put 'sql-interactive-mode 'custom-mode-group 'SQL)
;; FIXME: Why not use `define-derived-mode'?
(define-derived-mode sql-interactive-mode comint-mode "SQLi[?]"
  "Major mode to use a SQL interpreter interactively.

Do not call this function by yourself.  The environment must be
initialized by an entry function specific for the SQL interpreter.
See `sql-help' for a list of available entry functions.

\\[comint-send-input] after the end of the process' output sends the
text from the end of process to the end of the current line.
\\[comint-send-input] before end of process output copies the current
line minus the prompt to the end of the buffer and sends it.
\\[comint-copy-old-input] just copies the current line.
Use \\[sql-accumulate-and-indent] to enter multi-line statements.

If you want to make multiple SQL buffers, rename the `*SQL*' buffer
using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
See `sql-help' for a list of available entry functions.  The last buffer
created by such an entry function is the current SQLi buffer.  SQL
buffers will send strings to the SQLi buffer current at the time of
their creation.  See `sql-mode' for details.

Sample session using two connections:

1. Create first SQLi buffer by calling an entry function.
2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
3. Create a SQL buffer \"test1.sql\".
4. Create second SQLi buffer by calling an entry function.
5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
6. Create a SQL buffer \"test2.sql\".

Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
will send the region to buffer \"*Connection 2*\".

If you accidentally suspend your process, use \\[comint-continue-subjob]
to continue it.  On some operating systems, this will not work because
the signals are not supported.

\\{sql-interactive-mode-map}
Customization: Entry to this mode runs the hooks on `comint-mode-hook'
and `sql-interactive-mode-hook' (in that order).  Before each input, the
hooks on `comint-input-filter-functions' are run.  After each SQL
interpreter output, the hooks on `comint-output-filter-functions' are
run.

Variable `sql-input-ring-file-name' controls the initialization of the
input ring history.

Variables `comint-output-filter-functions', a hook, and
`comint-scroll-to-bottom-on-input' and
`comint-scroll-to-bottom-on-output' control whether input and output
cause the window to scroll to the end of the buffer.

If you want to make SQL buffers limited in length, add the function
`comint-truncate-buffer' to `comint-output-filter-functions'.

Here is an example for your init file.  It keeps the SQLi buffer a
certain length.

\(add-hook \\='sql-interactive-mode-hook
    (lambda ()
        (setq comint-output-filter-functions #\\='comint-truncate-buffer)))

Here is another example.  It will always put point back to the statement
you entered, right above the output it created.

\(setq comint-output-filter-functions
       (lambda (STR) (comint-show-output)))"
  :syntax-table sql-mode-syntax-table
  ;; FIXME: The doc above uses `setq' on `comint-output-filter-functions',
  ;; whereas hooks should be manipulated with things like `add/remove-hook'.
  :after-hook (sql--adjust-interactive-setup)

  ;; Get the `sql-product' for this interactive session.
  (setq-local sql-product (or sql-interactive-product
                           sql-product))

  ;; Setup the mode.
  (setq mode-name
        (concat "SQLi[" (or (sql-get-product-feature sql-product :name)
                            (symbol-name sql-product)) "]"))

  ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
  ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
  ;; will have just one quote.  Therefore syntactic highlighting is
  ;; disabled for interactive buffers.  No imenu support.
  (sql-product-font-lock t nil)

  ;; Enable commenting and uncommenting of the region.
  (setq-local comment-start "--")
  ;; Abbreviation table init and case-insensitive.  It is not activated
  ;; by default.
  (setq local-abbrev-table sql-mode-abbrev-table)
  (setq abbrev-all-caps 1)
  ;; Exiting the process will call sql-stop.
  (let ((proc (get-buffer-process (current-buffer))))
    (when proc (set-process-sentinel proc #'sql-stop)))
  ;; Save the connection and login params
  (setq-local sql-user       sql-user)
  (setq-local sql-database   sql-database)
  (setq-local sql-server     sql-server)
  (setq-local sql-port       sql-port)
  (setq-local sql-connection sql-connection)
  (setq-default sql-connection nil)
  ;; Contains the name of database objects
  (setq-local sql-contains-names t)
  ;; Keep track of existing object names
  (setq-local sql-completion-object nil)
  (setq-local sql-completion-column nil)
  ;; Create a useful name for renaming this buffer later.
  (setq-local sql-alternate-buffer-name
              (sql-make-alternate-buffer-name))
  ;; User stuff.  Initialize before the hook.
  (setq-local sql-prompt-regexp
              (or (sql-get-product-feature sql-product :prompt-regexp) "^"))
  (setq-local sql-prompt-length
              (sql-get-product-feature sql-product :prompt-length))
  (setq-local sql-prompt-cont-regexp
              (sql-get-product-feature sql-product :prompt-cont-regexp))
  (make-local-variable 'sql-output-newline-count)
  (make-local-variable 'sql-preoutput-hold)
  (add-hook 'comint-preoutput-filter-functions
            #'sql-interactive-remove-continuation-prompt nil t)
  (make-local-variable 'sql-input-ring-separator)
  (make-local-variable 'sql-input-ring-file-name))

(defun sql--adjust-interactive-setup ()
  "Finish the mode's setup after running the mode hook."
  ;; Set comint based on user overrides.
  (setq comint-prompt-regexp
        (if sql-prompt-cont-regexp
            (concat "\\(?:\\(?:" sql-prompt-regexp "\\)"
                    "\\|\\(?:" sql-prompt-cont-regexp "\\)\\)")
          sql-prompt-regexp))
  (setq left-margin (or sql-prompt-length 0))
  ;; Install input sender
  (setq-local comint-input-sender #'sql-input-sender)
  ;; People wanting a different history file for each
  ;; buffer/process/client/whatever can change separator and file-name
  ;; on the sql-interactive-mode-hook.
  (let
      ((comint-input-ring-separator sql-input-ring-separator)
       (comint-input-ring-file-name sql-input-ring-file-name))
    (comint-read-input-ring t)))

(defun sql-stop (process event)
  "Called when the SQL process is stopped.

Writes the input history to a history file using
`comint-write-input-ring' and inserts a short message in the SQL buffer.

This function is a sentinel watching the SQL interpreter process.
Sentinels will always get the two parameters PROCESS and EVENT."
  (when (buffer-live-p (process-buffer process))
    (with-current-buffer (process-buffer process)
      (let
          ((comint-input-ring-separator sql-input-ring-separator)
           (comint-input-ring-file-name sql-input-ring-file-name))
        (comint-write-input-ring))

      (if (not buffer-read-only)
          (insert (format "\nProcess %s %s\n" process event))
        (message "Process %s %s" process event)))))

\f

;;; Connection handling

(defun sql-read-connection (prompt &optional initial default)
  "Read a connection name."
  (let ((completion-ignore-case t))
    (completing-read prompt
                     (mapcar #'car sql-connection-alist)
                     nil t initial 'sql-connection-history default)))

;;;###autoload
(defun sql-connect (connection &optional buf-name)
  "Connect to an interactive session using CONNECTION settings.

See `sql-connection-alist' to see how to define connections and
their settings.

The user will not be prompted for any login parameters if a value
is specified in the connection settings."

  ;; Prompt for the connection from those defined in the alist
  (interactive
   (if sql-connection-alist
       (list (sql-read-connection "Connection: ")
             current-prefix-arg)
     (user-error "No SQL Connections defined")))

  ;; Are there connections defined
  (if sql-connection-alist
      ;; Was one selected
      (when connection
        ;; Get connection settings
        (let ((connect-set (cdr (assoc-string connection sql-connection-alist t))))
          ;; Settings are defined
          (if connect-set
              ;; Set the desired parameters
              (let (param-var login-params set-vars rem-vars)
                ;; Set the parameters and start the interactive session
                (dolist (vv connect-set)
                  (let ((var (car vv))
                        (val (cadr vv)))
                    (set-default var (eval val)))) ;FIXME: Why `eval'?
                (setq-default sql-connection connection)

                ;; :sqli-login params variable
                (setq param-var
                      (sql-get-product-feature sql-product :sqli-login nil t))

                ;; :sqli-login params value
                (setq login-params (symbol-value param-var))

                ;; Params set in the connection
                (setq set-vars
                      (mapcar
                       (lambda (v)
                         (pcase (car v)
                           ('sql-user     'user)
                           ('sql-password 'password)
                           ('sql-server   'server)
                           ('sql-database 'database)
                           ('sql-port     'port)
                           (s             s)))
                       connect-set))

                ;; the remaining params (w/o the connection params)
                (setq rem-vars
                      (sql-for-each-login login-params
                                          (lambda (var vals)
                                            (unless (member var set-vars)
                                              (if vals (cons var vals) var)))))

                ;; Start the SQLi session with revised list of login parameters
                (cl-progv (list param-var) (list rem-vars)
                  (sql-product-interactive
                   sql-product
                   (or buf-name (format "<%s>" connection)))))

            (user-error "SQL Connection <%s> does not exist" connection)
            nil)))

    (user-error "No SQL Connections defined")
    nil))

(defun sql-save-connection (name)
  "Captures the connection information of the current SQLi session.

The information is appended to `sql-connection-alist' and
optionally is saved to the user's init file."

  (interactive "sNew connection name: ")

  (unless (derived-mode-p 'sql-interactive-mode)
    (user-error "Not in a SQL interactive mode!"))

  ;; Capture the buffer local settings
  (let* ((buf        (current-buffer))
         (connection (buffer-local-value 'sql-connection buf))
         (product    (buffer-local-value 'sql-product    buf))
         (user       (buffer-local-value 'sql-user       buf))
         (database   (buffer-local-value 'sql-database   buf))
         (server     (buffer-local-value 'sql-server     buf))
         (port       (buffer-local-value 'sql-port       buf)))

    (if connection
        (message "This session was started by a connection; it's already been saved.")

      (let ((login (sql-get-product-feature product :sqli-login))
            (alist sql-connection-alist)
            connect)

        ;; Remove the existing connection if the user says so
        (when (and (assoc name alist)
                   (yes-or-no-p (format "Replace connection definition <%s>? " name)))
          (setq alist (assq-delete-all name alist)))

        ;; Add the new connection if it doesn't exist
        (if (assoc name alist)
            (user-error "Connection <%s> already exists" name)
          (setq connect
                (cons name
                      (sql-for-each-login
                       `(product ,@login)
                       (lambda (token _plist)
                           (pcase token
                             ('product  `(sql-product  ',product))
                             ('user     `(sql-user     ,user))
                             ('database `(sql-database ,database))
                             ('server   `(sql-server   ,server))
                             ('port     `(sql-port     ,port)))))))

          (setq alist (append alist (list connect)))

          ;; confirm whether we want to save the connections
          (if (yes-or-no-p "Save the connections for future sessions? ")
              (customize-save-variable 'sql-connection-alist alist)
            (customize-set-variable 'sql-connection-alist alist)))))))

(defun sql-connection-menu-filter (tail)
  "Generate menu entries for using each connection."
  (append
   (mapcar
    (lambda (conn)
        (vector
         (format "Connection <%s>\t%s" (car conn)
                 (let ((sql-user "") (sql-database "")
                       (sql-server "") (sql-port 0))
                   (cl-progv
                       (mapcar #'car (cdr conn))
                       (mapcar #'cadr (cdr conn))
                     (sql-make-alternate-buffer-name))))
         (list 'sql-connect (car conn))
         t))
    sql-connection-alist)
   tail))

\f

;;; Entry functions for different SQL interpreters.
;;;###autoload
(defun sql-product-interactive (&optional product new-name)
  "Run PRODUCT interpreter as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just make sure buffer `*SQL*'
is displayed.

To specify the SQL product, prefix the call with
\\[universal-argument].  To set the buffer name as well, prefix
the call to \\[sql-product-interactive] with
\\[universal-argument] \\[universal-argument].

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")

  ;; Handle universal arguments if specified
  (when (not (or executing-kbd-macro noninteractive))
    (when (>= (prefix-numeric-value product) 16)
      (when (not new-name)
        (setq new-name '(4)))
      (setq product '(4))))

  ;; Get the value of product that we need
  (setq product
        (cond
         ((= (prefix-numeric-value product) 4) ; C-u, prompt for product
          (sql-read-product "SQL product: " sql-product))
         ((assoc product sql-product-alist) ; Product specified
          product)
         (t sql-product)))              ; Default to sql-product

  ;; If we have a product and it has an interactive mode
  (if product
      (when (sql-get-product-feature product :sqli-comint-func)
        ;; If no new name specified or new name in buffer name,
        ;; try to pop to an active SQL interactive for the same product
        (let ((buf (sql-find-sqli-buffer product sql-connection)))
          (if (and buf (or (not new-name)
                           (and (stringp new-name)
                                (string-match-p (regexp-quote new-name) buf))))
              (sql-display-buffer buf)

            ;; We have a new name or sql-buffer doesn't exist or match
            ;; Start by remembering where we start
            (let ((start-buffer (current-buffer))
                  new-sqli-buffer rpt)

              ;; Get credentials.
              (apply #'sql-get-login
                     (sql-get-product-feature product :sqli-login))

              ;; Connect to database.
              (setq rpt (sql-make-progress-reporter nil "Login"))

              (let ((sql-user       (default-value 'sql-user))
                    (sql-password   (default-value 'sql-password))
                    (sql-server     (default-value 'sql-server))
                    (sql-database   (default-value 'sql-database))
                    (sql-port       (default-value 'sql-port))
                    (default-directory
                                    (or sql-default-directory
                                        default-directory)))

                ;; The password wallet returns a function which supplies the password.
                (when (functionp sql-password)
                  (setq sql-password (funcall sql-password)))

                ;; Call the COMINT service
                (funcall (sql-get-product-feature product :sqli-comint-func)
                         product
                         (sql-get-product-feature product :sqli-options)
                         ;; generate a buffer name
                         (cond
                          ((not new-name)
                           (sql-generate-unique-sqli-buffer-name product nil))
                          ((consp new-name)
                           (sql-generate-unique-sqli-buffer-name product
                            (read-string
                             "Buffer name (\"*SQL: XXX*\"; enter `XXX'): "
                             (sql-make-alternate-buffer-name product))))
                          ((stringp new-name)
                           (if (or (string-prefix-p " " new-name)
                                   (string-match-p "\\`[*].*[*]\\'" new-name))
                               new-name
                             (sql-generate-unique-sqli-buffer-name product new-name)))
                          (t
                           (sql-generate-unique-sqli-buffer-name product new-name)))))

              ;; Set SQLi mode.
              (let ((sql-interactive-product product))
                (sql-interactive-mode))

              ;; Set the new buffer name
              (setq new-sqli-buffer (current-buffer))
              (setq-local sql-buffer (buffer-name new-sqli-buffer))

              ;; Set `sql-buffer' in the start buffer
              (with-current-buffer start-buffer
                (when (derived-mode-p 'sql-mode)
                  (setq sql-buffer (buffer-name new-sqli-buffer))
                  (run-hooks 'sql-set-sqli-hook)))

              ;; Also set the global value.
              (setq-default sql-buffer (buffer-name new-sqli-buffer))

              ;; Make sure the connection is complete
              ;; (Sometimes start up can be slow)
              ;;  and call the login hook
              (let ((proc (get-buffer-process new-sqli-buffer))
                    (secs sql-login-delay)
                    (step 0.3))
                (while (and proc
                            (memq (process-status proc) '(open run))
                            (or (accept-process-output proc step)
                                (<= 0.0 (setq secs (- secs step))))
                            (progn (goto-char (point-max))
                                   (not (re-search-backward sql-prompt-regexp 0 t))))
                  (sql-progress-reporter-update rpt)))

              (goto-char (point-max))
              (when (re-search-backward sql-prompt-regexp nil t)
                (run-hooks 'sql-login-hook))

              ;; All done.
              (sql-progress-reporter-done rpt)
              (goto-char (point-max))
              (let ((sql-display-sqli-buffer-function t))
                (sql-display-buffer new-sqli-buffer))
              (get-buffer new-sqli-buffer)))))
    (user-error "No default SQL product defined: set `sql-product'")))

(defun sql-comint-automatic-password (_)
  "Intercept password prompts when we know the password.
This must also do the job of detecting password prompts."
  (when (and
         sql-password
         (not (string= "" sql-password)))
    sql-password))

(defun sql-comint (product params &optional buf-name)
  "Set up a comint buffer to run the SQL processor.

PRODUCT is the SQL product.  PARAMS is a list of strings which are
passed as command line arguments.  BUF-NAME is the name of the new
buffer.  If nil, a name is chosen for it."

  (let ((program (sql-get-product-feature product :sqli-program)))
    ;; Make sure we can find the program.  `executable-find' does not
    ;; work for remote hosts; we suppress the check there.
    (unless (or (file-remote-p default-directory)
		(executable-find program))
      (error "Unable to locate SQL program `%s'" program))

    ;; Make sure buffer name is unique.
    ;;   if not specified, try *SQL* then *SQL-product*, then *SQL-product1*, ...
    ;;   otherwise, use *buf-name*
    (if buf-name
        (unless (or (string-prefix-p " " buf-name)
                    (string-match-p "\\`[*].*[*]\\'" buf-name))
          (setq buf-name (concat "*" buf-name "*")))
      (setq buf-name (sql-generate-unique-sqli-buffer-name product nil)))
    (set-text-properties 0 (length buf-name) nil buf-name)

    ;; Create the buffer first, because we want to set it up before
    ;; comint starts to run.
    (set-buffer (get-buffer-create buf-name))
    ;; Set up the automatic population of passwords, if supported.
    (when (sql-get-product-feature product :password-in-comint)
      (setq comint-password-function #'sql-comint-automatic-password))

    ;; Start the command interpreter in the buffer
    ;;   PROC-NAME is BUF-NAME without enclosing asterisks
    (let ((proc-name (replace-regexp-in-string "\\`[*]\\(.*\\)[*]\\'" "\\1" buf-name)))
      (set-buffer
       (apply #'make-comint-in-buffer
              proc-name buf-name program nil params)))))

;;;###autoload
(defun sql-oracle (&optional buffer)
  "Run sqlplus by Oracle as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-oracle-program'.  Login uses
the variables `sql-user', `sql-password', and `sql-database' as
defaults, if set.  Additional command line parameters can be stored in
the list `sql-oracle-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-oracle].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-oracle].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'oracle buffer))

(defun sql-comint-oracle (product options &optional buf-name)
  "Create comint buffer and connect to Oracle."
  ;; Produce user/password@database construct.  Password without user
  ;; is meaningless; database without user/password is meaningless,
  ;; because "@param" will ask sqlplus to interpret the script
  ;; "param".
  (let (parameter nlslang coding)
    (if (not (string= "" sql-user))
	(if (not (string= "" sql-password))
	    (setq parameter (concat sql-user "/" sql-password))
	  (setq parameter sql-user)))
    (if (and parameter (not (string= "" sql-database)))
	(setq parameter (concat parameter "@" sql-database)))
    ;; options must appear before the logon parameters
    (if parameter
	(setq parameter (append options (list parameter)))
      (setq parameter options))
    (sql-comint product parameter buf-name)
    ;; Set process coding system to agree with the interpreter
    (setq nlslang (or (getenv "NLS_LANG") "")
          coding  (dolist (cs
                           ;; Are we missing any common NLS character sets
                           '(("US8PC437"  . cp437)
                             ("EL8PC737"  . cp737)
                             ("WE8PC850"  . cp850)
                             ("EE8PC852"  . cp852)
                             ("TR8PC857"  . cp857)
                             ("WE8PC858"  . cp858)
                             ("IS8PC861"  . cp861)
                             ("IW8PC1507" . cp862)
                             ("N8PC865"   . cp865)
                             ("RU8PC866"  . cp866)
                             ("US7ASCII"  . us-ascii)
                             ("UTF8"      . utf-8)
                             ("AL32UTF8"  . utf-8)
                             ("AL16UTF16" . utf-16))
                           (or coding 'utf-8))
                    (when (string-match (format "\\.%s\\'" (car cs)) nlslang)
                      (setq coding (cdr cs)))))
    (set-process-coding-system (get-buffer-process (current-buffer))
                               coding coding)))

(defun sql-oracle-save-settings (sqlbuf)
  "Save most SQL*Plus settings so they may be reset by \\[sql-redirect]."
  ;; Note: does not capture the following settings:
  ;;
  ;; APPINFO
  ;; BTITLE
  ;; COMPATIBILITY
  ;; COPYTYPECHECK
  ;; MARKUP
  ;; RELEASE
  ;; REPFOOTER
  ;; REPHEADER
  ;; SQLPLUSCOMPATIBILITY
  ;; TTITLE
  ;; USER
  ;;

  (append
  ;; (apply #'concat (append
  ;;  '("SET")

   ;; option value...
   (sql-redirect-value
    sqlbuf
    (concat "SHOW ARRAYSIZE AUTOCOMMIT AUTOPRINT AUTORECOVERY AUTOTRACE"
            " CMDSEP COLSEP COPYCOMMIT DESCRIBE ECHO EDITFILE EMBEDDED"
            " ESCAPE FLAGGER FLUSH HEADING INSTANCE LINESIZE LNO LOBOFFSET"
            " LOGSOURCE LONG LONGCHUNKSIZE NEWPAGE NULL NUMFORMAT NUMWIDTH"
            " PAGESIZE PAUSE PNO RECSEP SERVEROUTPUT SHIFTINOUT SHOWMODE"
            " SPOOL SQLBLANKLINES SQLCASE SQLCODE SQLCONTINUE SQLNUMBER"
            " SQLPROMPT SUFFIX TAB TERMOUT TIMING TRIMOUT TRIMSPOOL VERIFY")
    "^.+$"
    "SET \\&")

   ;; option "c" (hex xx)
   (sql-redirect-value
    sqlbuf
    (concat "SHOW BLOCKTERMINATOR CONCAT DEFINE SQLPREFIX SQLTERMINATOR"
            " UNDERLINE HEADSEP RECSEPCHAR")
    "^\\(.+\\) (hex ..)$"
    "SET \\1")

   ;; FEEDBACK ON for 99 or more rows
   ;; feedback OFF
   (sql-redirect-value
    sqlbuf
    "SHOW FEEDBACK"
    "^\\(?:FEEDBACK ON for \\([[:digit:]]+\\) or more rows\\|feedback \\(OFF\\)\\)"
    "SET FEEDBACK \\1\\2")

   ;; wrap : lines will be wrapped
   ;; wrap : lines will be truncated
   (list (concat "SET WRAP "
                 (if (string=
                      (car (sql-redirect-value
                            sqlbuf
                            "SHOW WRAP"
                            "^wrap : lines will be \\(wrapped\\|truncated\\)" 1))
                      "wrapped")
                     "ON" "OFF")))))

(defun sql-oracle-restore-settings (sqlbuf saved-settings)
  "Restore the SQL*Plus settings in SAVED-SETTINGS."

  ;; Remove any settings that haven't changed
  (mapc
   (lambda (one-cur-setting)
       (setq saved-settings (delete one-cur-setting saved-settings)))
   (sql-oracle-save-settings sqlbuf))

  ;; Restore the changed settings
  (sql-redirect sqlbuf saved-settings))

(defun sql-oracle--list-object-name (obj-name)
  (format "CASE WHEN REGEXP_LIKE (%s, q'/^[A-Z0-9_#$]+$/','c') THEN %s ELSE '\"'|| %s ||'\"' END "
          obj-name obj-name obj-name))

(defun sql-oracle-list-all (sqlbuf outbuf enhanced _table-name)
  ;; Query from USER_OBJECTS or ALL_OBJECTS
  (let ((settings (sql-oracle-save-settings sqlbuf))
        (simple-sql
         (concat
          "SELECT INITCAP(x.object_type) AS SQL_EL_TYPE "
          ", " (sql-oracle--list-object-name "x.object_name") " AS SQL_EL_NAME "
          "FROM user_objects                    x "
          "WHERE x.object_type NOT LIKE '%% BODY' "
          "ORDER BY 2, 1;"))
        (enhanced-sql
         (concat
          "SELECT INITCAP(x.object_type) AS SQL_EL_TYPE "
          ", "  (sql-oracle--list-object-name "x.owner")
          " ||'.'|| "  (sql-oracle--list-object-name "x.object_name") " AS SQL_EL_NAME "
          "FROM all_objects x "
          "WHERE x.object_type NOT LIKE '%% BODY' "
          "AND x.owner <> 'SYS' "
          "ORDER BY 2, 1;")))

    (sql-redirect sqlbuf
                  (concat "SET LINESIZE 80 PAGESIZE 50000 TRIMOUT ON"
                          " TAB OFF TIMING OFF FEEDBACK OFF"))

    (sql-redirect sqlbuf
                  (list "COLUMN SQL_EL_TYPE  HEADING \"Type\" FORMAT A19"
                        "COLUMN SQL_EL_NAME  HEADING \"Name\""
                        (format "COLUMN SQL_EL_NAME  FORMAT A%d"
                                (if enhanced 60 35))))

    (sql-redirect sqlbuf
                  (if enhanced enhanced-sql simple-sql)
                  outbuf)

    (sql-redirect sqlbuf
                  '("COLUMN SQL_EL_NAME CLEAR"
                    "COLUMN SQL_EL_TYPE CLEAR"))

    (sql-oracle-restore-settings sqlbuf settings)))

(defun sql-oracle-list-table (sqlbuf outbuf _enhanced table-name)
  "Implements :list-table under Oracle."
  (let ((settings (sql-oracle-save-settings sqlbuf)))

    (sql-redirect sqlbuf
                  (format
                   (concat "SET LINESIZE %d PAGESIZE 50000"
                           " DESCRIBE DEPTH 1 LINENUM OFF INDENT ON")
                   (max 65 (min 120 (window-width)))))

    (sql-redirect sqlbuf (format "DESCRIBE %s" table-name)
                  outbuf)

    (sql-oracle-restore-settings sqlbuf settings)))

(defcustom sql-oracle-completion-types '("FUNCTION" "PACKAGE" "PROCEDURE"
                                         "SEQUENCE" "SYNONYM" "TABLE" "TRIGGER"
                                         "TYPE" "VIEW")
  "List of object types to include for completion under Oracle.

See the distinct values in ALL_OBJECTS.OBJECT_TYPE for possible values."
  :version "24.1"
  :type '(repeat string))

(defun sql-oracle-completion-object (sqlbuf schema)
  (sql-redirect-value
   sqlbuf
   (concat
    "SELECT CHR(1)||"
    (if schema
        (concat "CASE WHEN REGEXP_LIKE (owner, q'/^[A-Z0-9_#$]+$/','c') THEN owner ELSE '\"'|| owner ||'\"' END "
                "||'.'||"
                "CASE WHEN REGEXP_LIKE (object_name, q'/^[A-Z0-9_#$]+$/','c') THEN object_name ELSE '\"'|| object_name ||'\"' END "
                " AS o FROM all_objects "
                (format "WHERE owner = %s AND "
                        (sql-str-literal (if (string-match "^[\"]\\(.+\\)[\"]$" schema)
                                             (match-string 1 schema) (upcase schema)))))
      (concat "CASE WHEN REGEXP_LIKE (object_name, q'/^[A-Z0-9_#$]+$/','c') THEN object_name ELSE '\"'|| object_name ||'\"' END "
              " AS o FROM user_objects WHERE "))
    "temporary = 'N' AND generated = 'N' AND secondary = 'N' AND "
    "object_type IN ("
    (mapconcat (function sql-str-literal) sql-oracle-completion-types ",")
    ");")
   "^[\001]\\(.+\\)$" 1))
\f

;;;###autoload
(defun sql-sybase (&optional buffer)
  "Run isql by Sybase as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-sybase-program'.  Login uses
the variables `sql-server', `sql-user', `sql-password', and
`sql-database' as defaults, if set.  Additional command line parameters
can be stored in the list `sql-sybase-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-sybase].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-sybase].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'sybase buffer))

(defun sql-comint-sybase (product options &optional buf-name)
  "Create comint buffer and connect to Sybase."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append
          (if (not (string= "" sql-user))
              (list "-U" sql-user))
          (if (not (string= "" sql-password))
              (list "-P" sql-password))
          (if (not (string= "" sql-database))
              (list "-D" sql-database))
          (if (not (string= "" sql-server))
              (list "-S" sql-server))
          options)))
    (sql-comint product params buf-name)))

\f

;;;###autoload
(defun sql-informix (&optional buffer)
  "Run dbaccess by Informix as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-informix-program'.  Login uses
the variable `sql-database' as default, if set.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-informix].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-informix].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'informix buffer))

(defun sql-comint-informix (product options &optional buf-name)
  "Create comint buffer and connect to Informix."
  ;; username and password are ignored.
  (let ((db (if (string= "" sql-database)
		"-"
	      (if (string= "" sql-server)
		  sql-database
		(concat sql-database "@" sql-server)))))
    (sql-comint product (append `(,db "-") options) buf-name)))

\f

;;;###autoload
(defun sql-sqlite (&optional buffer)
  "Run sqlite as an inferior process.

SQLite is free software.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-sqlite-program'.  Login uses
the variables `sql-user', `sql-password', `sql-database', and
`sql-server' as defaults, if set.  Additional command line parameters
can be stored in the list `sql-sqlite-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-sqlite].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-sqlite].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'sqlite buffer))

(defun sql-comint-sqlite (product options &optional buf-name)
  "Create comint buffer and connect to SQLite."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append options
                 (if (not (string= "" sql-database))
                     `(,(expand-file-name sql-database))))))
    (sql-comint product params buf-name)))

(defun sql-sqlite-completion-object (sqlbuf _schema)
  (sql-redirect-value sqlbuf ".tables" "\\sw\\(?:\\sw\\|\\s_\\)*" 0))

\f

;;;###autoload
(defun sql-mysql (&optional buffer)
  "Run mysql by TcX as an inferior process.

Mysql versions 3.23 and up are free software.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-mysql-program'.  Login uses
the variables `sql-user', `sql-password', `sql-database', and
`sql-server' as defaults, if set.  Additional command line parameters
can be stored in the list `sql-mysql-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-mysql].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-mysql].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'mysql buffer))

(defun sql-comint-mysql (product options &optional buf-name)
  "Create comint buffer and connect to MySQL."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append
          options
          (if (not (string= "" sql-user))
              (list (concat "--user=" sql-user)))
          (if (not (string= "" sql-password))
              (list (concat "--password=" sql-password)))
          (if (not (= 0 sql-port))
              (list (concat "--port=" (number-to-string sql-port))))
          (if (not (string= "" sql-server))
              (list (concat "--host=" sql-server)))
          (if (not (string= "" sql-database))
              (list sql-database)))))
    (sql-comint product params buf-name)))

;;;###autoload
(defun sql-mariadb (&optional buffer)
    "Run mysql by MariaDB as an inferior process.

MariaDB is free software.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-mariadb-program'.  Login uses
the variables `sql-user', `sql-password', `sql-database', and
`sql-server' as defaults, if set.  Additional command line parameters
can be stored in the list `sql-mariadb-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-mariadb].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-mariadb].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'mariadb buffer))

(defun sql-comint-mariadb (product options &optional buf-name)
  "Create comint buffer and connect to MariaDB.

Use the MySQL comint driver since the two are compatible."
  (sql-comint-mysql product options buf-name))

\f

;;;###autoload
(defun sql-solid (&optional buffer)
  "Run solsql by Solid as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-solid-program'.  Login uses
the variables `sql-user', `sql-password', and `sql-server' as
defaults, if set.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-solid].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-solid].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'solid buffer))

(defun sql-comint-solid (product options &optional buf-name)
  "Create comint buffer and connect to Solid."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append
          (if (not (string= "" sql-server))
              (list sql-server))
          ;; It only makes sense if both username and password are there.
          (if (not (or (string= "" sql-user)
                       (string= "" sql-password)))
              (list sql-user sql-password))
          options)))
    (sql-comint product params buf-name)))

\f

;;;###autoload
(defun sql-ingres (&optional buffer)
  "Run sql by Ingres as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-ingres-program'.  Login uses
the variable `sql-database' as default, if set.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-ingres].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-ingres].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'ingres buffer))

(defun sql-comint-ingres (product options &optional buf-name)
  "Create comint buffer and connect to Ingres."
  ;; username and password are ignored.
  (sql-comint product
              (append (if (string= "" sql-database)
                          nil
                        (list sql-database))
                      options)
              buf-name))

\f

;;;###autoload
(defun sql-ms (&optional buffer)
  "Run osql by Microsoft as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-ms-program'.  Login uses the
variables `sql-user', `sql-password', `sql-database', and `sql-server'
as defaults, if set.  Additional command line parameters can be stored
in the list `sql-ms-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-ms].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-ms].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'ms buffer))

(defun sql-comint-ms (product options &optional buf-name)
  "Create comint buffer and connect to Microsoft SQL Server."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append
          (if (not (string= "" sql-user))
              (list "-U" sql-user))
          (if (not (string= "" sql-database))
              (list "-d" sql-database))
          (if (not (string= "" sql-server))
              (list "-S" sql-server))
          options)))
    (setq params
          (if (not (string= "" sql-password))
              `("-P" ,sql-password ,@params)
            (if (string= "" sql-user)
                ;; If neither user nor password is provided, use system
                ;; credentials.
                `("-E" ,@params)
              ;; If -P is passed to ISQL as the last argument without a
              ;; password, it's considered null.
              `(,@params "-P"))))
    (sql-comint product params buf-name)))

\f

;;;###autoload
(defun sql-postgres (&optional buffer)
  "Run psql by Postgres as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-postgres-program'.  Login uses
the variables `sql-database' and `sql-server' as default, if set.
Additional command line parameters can be stored in the list
`sql-postgres-options'.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-postgres].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-postgres].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.  If your output lines end with ^M,
your might try undecided-dos as a coding system.  If this doesn't help,
Try to set `comint-output-filter-functions' like this:

\(add-hook \\='comint-output-filter-functions #\\='comint-strip-ctrl-m \\='append)

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'postgres buffer))

(defun sql-comint-postgres (product options &optional buf-name)
  "Create comint buffer and connect to Postgres."
  ;; username and password are ignored.  Mark Stosberg suggests to add
  ;; the database at the end.  Jason Beegan suggests using --pset and
  ;; pager=off instead of \\o|cat.  The later was the solution by
  ;; Gregor Zych.  Jason's suggestion is the default value for
  ;; sql-postgres-options.
  (let ((params
         (append
          (if (not (= 0 sql-port))
              (list "-p" (number-to-string sql-port)))
          (if (not (string= "" sql-user))
              (list "-U" sql-user))
          (if (not (string= "" sql-server))
              (list "-h" sql-server))
          options
          (if (not (string= "" sql-database))
              (list sql-database)))))
    (sql-comint product params buf-name)))

(defun sql-postgres-completion-object (sqlbuf schema)
  (sql-redirect sqlbuf "\\t on")
  (let ((aligned
         (string= "aligned"
                  (car (sql-redirect-value
                        sqlbuf "\\a"
                        "Output format is \\(.*\\)[.]$" 1)))))
    (when aligned
      (sql-redirect sqlbuf "\\a"))
    (let* ((fs (or (car (sql-redirect-value
                         sqlbuf "\\f" "Field separator is \"\\(.\\)[.]$" 1))
                   "|"))
           (re (concat "^\\([^" fs "]*\\)" fs "\\([^" fs "]*\\)"
                       fs "[^" fs "]*" fs  "[^" fs "]*$"))
           (cl (if (not schema)
                   (sql-redirect-value sqlbuf "\\d" re '(1 2))
                 (append (sql-redirect-value
                          sqlbuf (format "\\dt %s.*" schema) re '(1 2))
                         (sql-redirect-value
                          sqlbuf (format "\\dv %s.*" schema) re '(1 2))
                         (sql-redirect-value
                          sqlbuf (format "\\ds %s.*" schema) re '(1 2))))))

      ;; Restore tuples and alignment to what they were.
      (sql-redirect sqlbuf "\\t off")
      (when (not aligned)
        (sql-redirect sqlbuf "\\a"))

      ;; Return the list of table names (public schema name can be omitted)
      (mapcar (lambda (tbl)
                  (if (string= (car tbl) "public")
                      (format "\"%s\"" (cadr tbl))
                    (format "\"%s\".\"%s\"" (car tbl) (cadr tbl))))
              cl))))

\f

;;;###autoload
(defun sql-interbase (&optional buffer)
  "Run isql by Interbase as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-interbase-program'.  Login
uses the variables `sql-user', `sql-password', and `sql-database' as
defaults, if set.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-interbase].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-interbase].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'interbase buffer))

(defun sql-comint-interbase (product options &optional buf-name)
  "Create comint buffer and connect to Interbase."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let ((params
         (append
          (if (not (string= "" sql-database))
              (list sql-database))      ; Add to the front!
          (if (not (string= "" sql-password))
              (list "-p" sql-password))
          (if (not (string= "" sql-user))
              (list "-u" sql-user))
          options)))
    (sql-comint product params buf-name)))

\f

;;;###autoload
(defun sql-db2 (&optional buffer)
  "Run db2 by IBM as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-db2-program'.  There is not
automatic login.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

If you use \\[sql-accumulate-and-indent] to send multiline commands to
db2, newlines will be escaped if necessary.  If you don't want that, set
`comint-input-sender' back to `comint-simple-send' by writing an after
advice.  See the elisp manual for more information.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-db2].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

To specify a coding system for converting non-ASCII characters
in the input and output to the process, use \\[universal-coding-system-argument]
before \\[sql-db2].  You can also specify this with \\[set-buffer-process-coding-system]
in the SQL buffer, after you start the process.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'db2 buffer))

(defun sql-comint-db2 (product options &optional buf-name)
  "Create comint buffer and connect to DB2."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (sql-comint product options buf-name))

;;;###autoload
(defun sql-linter (&optional buffer)
  "Run inl by RELEX as an inferior process.

If buffer `*SQL*' exists but no process is running, make a new process.
If buffer exists and a process is running, just switch to buffer
`*SQL*'.

Interpreter used comes from variable `sql-linter-program' - usually `inl'.
Login uses the variables `sql-user', `sql-password', `sql-database' and
`sql-server' as defaults, if set.  Additional command line parameters
can be stored in the list `sql-linter-options'.  Run inl -h to get help on
parameters.

`sql-database' is used to set the LINTER_MBX environment variable for
local connections, `sql-server' refers to the server name from the
`nodetab' file for the network connection (dbc_tcp or friends must run
for this to work).  If `sql-password' is an empty string, inl will use
an empty password.

The buffer is put in SQL interactive mode, giving commands for sending
input.  See `sql-interactive-mode'.

To set the buffer name directly, use \\[universal-argument]
before \\[sql-linter].  Once session has started,
\\[sql-rename-buffer] can be called separately to rename the
buffer.

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")
  (sql-product-interactive 'linter buffer))

(defun sql-comint-linter (product options &optional buf-name)
  "Create comint buffer and connect to Linter."
  ;; Put all parameters to the program (if defined) in a list and call
  ;; make-comint.
  (let* ((login
          (if (not (string= "" sql-user))
              (concat sql-user "/" sql-password)))
         (params
          (append
           (if (not (string= "" sql-server))
               (list "-n" sql-server))
           (list "-u" login)
           options)))
    (cl-letf (((getenv "LINTER_MBX")
               (unless (string= "" sql-database) sql-database)))
      (sql-comint product params buf-name))))

\f

(defcustom sql-vertica-program "vsql"
  "Command to start the Vertica client."
  :version "25.1"
  :type 'file)

(defcustom sql-vertica-options '("-P" "pager=off")
  "List of additional options for `sql-vertica-program'.
The default value disables the internal pager."
  :version "25.1"
  :type '(repeat string))

(defcustom sql-vertica-login-params '(user password database server)
  "List of login parameters needed to connect to Vertica."
  :version "25.1"
  :type 'sql-login-params)

(defun sql-comint-vertica (product options &optional buf-name)
  "Create comint buffer and connect to Vertica."
  (sql-comint product
              (nconc
               (and (not (string= "" sql-server))
                    (list "-h" sql-server))
               (and (not (string= "" sql-database))
                    (list "-d" sql-database))
               (and (not (string= "" sql-password))
                    (list "-w" sql-password))
               (and (not (string= "" sql-user))
                    (list "-U" sql-user))
               options)
              buf-name))

;;;###autoload
(defun sql-vertica (&optional buffer)
  "Run vsql as an inferior process."
  (interactive "P")
  (sql-product-interactive 'vertica buffer))

\f
(provide 'sql)

; LocalWords:  sql SQL SQLite sqlite Sybase Informix MySQL
; LocalWords:  Postgres SQLServer SQLi

;;; sql.el ends here

debug log:

solving 75d0cbbb367 ...
found 75d0cbbb367 in https://yhetil.org/emacs-devel/20230416133843.74970-1-nicolas@n16f.net/
found d8bd6bc2685 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 d8bd6bc2685b66e22aec4d58bcf98c002138c757	lisp/progmodes/sql.el

applying [1/1] https://yhetil.org/emacs-devel/20230416133843.74970-1-nicolas@n16f.net/
diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index d8bd6bc2685..75d0cbbb367 100644

Checking patch lisp/progmodes/sql.el...
Applied patch lisp/progmodes/sql.el cleanly.

index at:
100644 75d0cbbb367813c1c1ece2b2a145e8d2d5a05384	lisp/progmodes/sql.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).