File: mysql_sql_parser.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (2722 lines) | stat: -rw-r--r-- 90,922 bytes parent folder | download
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
/* 
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
 *
 * This program 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; version 2 of the
 * License.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

#include <glib.h>
#include <boost/signals2.hpp>
#include <cctype>

#include "base/util_functions.h"

#include "mysql_sql_parser.h"
#include "grtsqlparser/module_utils.h"
#include "mysql_sql_parser_utils.h"
#include "grtdb/db_object_helpers.h"

#include "base/string_utilities.h"
#include <sstream>
#include <iterator>
#include <boost/lambda/bind.hpp>

using namespace grt;
using namespace base;

class TableStorageEngines
{
public:
  TableStorageEngines() {}
  void init(GRT *grt)
  {
    grt::ListRef<db_mysql_StorageEngine> engines;
    grt::Module *module= grt->get_module("DbMySQL");
    if (!module)
      throw std::logic_error("module DbMySQL not found");
    grt::BaseListRef args(grt);
    engines= grt::ListRef<db_mysql_StorageEngine>::cast_from(module->call_function("getKnownEngines", args));
    if (!engines.is_valid())
      throw std::logic_error("no known storage engines");
    for (grt::ListRef<db_mysql_StorageEngine>::const_iterator iter= engines.begin(); iter != engines.end(); ++iter)
    {
      const std::string name= (*iter)->name();
      _names_index[base::tolower(name)]= name;
    }
  }

  std::string normalize_name(const std::string &name) const
  {
    std::string lower_cased_name= tolower(name);
    std::map<std::string, std::string>::const_iterator i= _names_index.find(lower_cased_name);
    return (_names_index.end() == i) ? name : i->second;
  }

private:
  std::map<std::string, std::string> _names_index;
};
static TableStorageEngines __table_storage_engines;


Mysql_sql_parser::Null_state_keeper::~Null_state_keeper()
{
  _sql_parser->_fk_refs.clear();
  boost::function<Parse_result ()> f = boost::lambda::constant(pr_irrelevant);
  _sql_parser->_process_specific_create_statement.clear();
  _sql_parser->_datatype_cache= grt::DictRef();
  _sql_parser->_created_objects= grt::ListRef<GrtObject>();
  _sql_parser->_processing_create_statements= true;
  _sql_parser->_processing_alter_statements= true;
  _sql_parser->_processing_drop_statements= true;
  _sql_parser->_set_old_names= true;
  _sql_parser->_reuse_existing_objects= false;
  _sql_parser->_reusing_existing_obj= false;
  _sql_parser->_stick_to_active_schema= false;
  _sql_parser->_gen_fk_names_when_empty= true;
  _sql_parser->_strip_sql= true;
  _sql_parser->_last_parse_result= pr_irrelevant;
  _sql_parser->_sql_script_codeset= StringRef("");
  _sql_parser->_triggers_owner_table= db_mysql_TableRef();

  _sql_parser->_shape_schema = boost::bind(f);
  _sql_parser->_shape_table = boost::bind(f);
  _sql_parser->_shape_view = boost::bind(f);
  _sql_parser->_shape_routine = boost::bind(f);
  _sql_parser->_shape_trigger = boost::bind(f);
  _sql_parser->_shape_index = boost::bind(f);
  _sql_parser->_shape_logfile_group = boost::bind(f);
  _sql_parser->_shape_tablespace = boost::bind(f);
  _sql_parser->_shape_serverlink = boost::bind(f);

  static struct TableStorageEnginesInitializer
  {
    TableStorageEnginesInitializer(GRT *grt) { __table_storage_engines.init(grt); }
  } table_storage_engines_initializer(_sql_parser->_grt);
}
#define NULL_STATE_KEEPER Null_state_keeper _nsk(this);

#define ACTIVE_SCHEMA_KEEPER Active_schema_keeper _sk(this);

#define ENSURE(call, text) \
  try \
  { \
    call; \
  } \
  catch (const Parse_exception& e) \
  { \
    std::string err_text= text; \
    throw Parse_exception(err_text); \
  }


Mysql_sql_parser::Mysql_sql_parser(grt::GRT *grt)
:
Sql_parser_base(grt),
Mysql_sql_parser_base(grt),
Sql_parser(grt)
{
  NULL_STATE_KEEPER
}


void Mysql_sql_parser::set_options(grt::DictRef options)
{
  Mysql_sql_parser_base::set_options(options);

  if (!options.is_valid())
    return;

  overwrite_default_option<grt::StringRef>(_sql_script_codeset, "sql_script_codeset", options, true);
  overwrite_default_option(_created_objects, "created_objects", options, false);
  overwrite_default_option<grt::IntegerRef>(_gen_fk_names_when_empty, "gen_fk_names_when_empty", options);
  overwrite_default_option<grt::IntegerRef>(_case_sensitive_identifiers, "case_sensitive_identifiers", options);
  overwrite_default_option<grt::IntegerRef>(_processing_create_statements, "processing_create_statements", options);
  overwrite_default_option<grt::IntegerRef>(_processing_alter_statements, "processing_alter_statements", options);
  overwrite_default_option<grt::IntegerRef>(_processing_drop_statements, "processing_drop_statements", options);
  overwrite_default_option<grt::IntegerRef>(_reuse_existing_objects, "reuse_existing_objects", options);
}


int Mysql_sql_parser::parse_sql_script(db_CatalogRef catalog, const std::string &sql, grt::DictRef options)
{
  return parse_sql_script(catalog, sql, false, options);
}


int Mysql_sql_parser::parse_sql_script_file(db_CatalogRef catalog, const std::string &filename, grt::DictRef options)
{
  return parse_sql_script(catalog, filename, true, options);
}


int Mysql_sql_parser::parse_sql_script(db_CatalogRef &catalog, const std::string &sql, bool from_file, grt::DictRef& options)
{
  if (!catalog.is_valid())
    return pr_invalid;

  NULL_STATE_KEEPER

  _catalog= db_mysql_CatalogRef::cast_from(catalog);

  set_options(options);

  add_log_message("Started parsing MySQL SQL script.", 3);

  set_progress_state(0.f, _("Parsing MySQL SQL Script..."));

  build_datatype_cache();

  // change current schema to default, it will be used for objects without specified schema
  db_mysql_SchemaRef default_schema;
  int initial_schemata_count= -1;

  initial_schemata_count = (int)_catalog->schemata().count();
  if (0 == initial_schemata_count)
    default_schema= set_active_schema("default_schema"); // this will add schema to schemata if necessary
  else
  {
    default_schema= db_mysql_SchemaRef::cast_from(_catalog->defaultSchema());
    if (!default_schema.is_valid())
      default_schema= _catalog->schemata().get(0);
    set_active_schema(*default_schema->name());
  }

  _process_sql_statement= boost::bind(&Mysql_sql_parser::process_sql_statement, this, _1);

  int res;
  Mysql_sql_parser_fe sql_parser_fe(_grtm->get_app_option_string("SqlMode"));
  sql_parser_fe.processing_create_statements= _processing_create_statements;
  sql_parser_fe.processing_alter_statements= _processing_alter_statements;
  sql_parser_fe.processing_drop_statements= _processing_drop_statements;

  const std::string *sql_script= &sql;
  std::string sql_in_utf8;
  if (!_sql_script_codeset.empty() && (_sql_script_codeset != "UTF8"))
  {
    std::ifstream ifs(sql.c_str(), std::ios_base::in|std::ios_base::binary);
    if (ifs)
    {
      ifs >> std::noskipws;
      std::string sql_original;
      std::copy(std::istream_iterator<char>(ifs), std::istream_iterator<char>(), std::back_inserter(sql_original));

      gsize bytes_read;
      gsize bytes_written;
      GError *error= NULL;
      char *sql_converted= g_convert(sql_original.c_str(), sql_original.length(), "UTF-8", _sql_script_codeset.c_str(), &bytes_read, &bytes_written, &error);
      if (!error)
      {
        sql_in_utf8= sql_converted;
        from_file= false;
        sql_script= &sql_in_utf8;
      }
      else
        g_free(error);
      g_free(sql_converted);
    }
  }

  if (from_file)
    res= Mysql_sql_parser_base::parse_sql_script_file(sql_parser_fe, *sql_script);
  else
    // TODO: optimize to avoid copies of the input (especially for large scripts).
    res= Mysql_sql_parser_base::parse_sql_script(sql_parser_fe, sql_script->c_str());

  set_progress_state(0.9f, _("Creating foreign key references..."));

  // 2-nd stage: now, when all tables are created it's safe to set foreign key references
  set_fk_references();

  // remove default schema if it was created by this procedure & after all it's empty
  if (0 == initial_schemata_count
      && default_schema.is_valid()
      && 0 == default_schema->tables().count()
      && 0 == default_schema->views().count()
      && 0 == default_schema->routines().count())
    _catalog->schemata().remove_value(default_schema);

  set_progress_state(1.f, _("Finished parsing MySQL SQL script."));

  {
    std::ostringstream oss;
    oss << "Finished parsing MySQL SQL script. Totally processed statements: successful (" << _processed_obj_count <<
      "), errors (" << _err_count <<
       "), warnings (" << _warn_count << ").";
    add_log_message(oss.str(), 3);
  }

  return res;
}


int Mysql_sql_parser::process_sql_statement(const SqlAstNode *tree)
{
  _reusing_existing_obj= false;
  _last_parse_result= pr_irrelevant;

  if (!tree)
  {
    report_sql_error(_err_tok_lineno, true, _err_tok_line_pos, _err_tok_len, _err_msg, 2);
    _last_parse_result= pr_invalid;
    return 1;
  }

  try
  {
    _last_parse_result= pr_irrelevant;

    if (const SqlAstNode *item= tree->subitem(sql::_statement, sql::_create))
      _last_parse_result= process_create_statement(item);
    else if (const SqlAstNode *item= tree->subitem(sql::_statement, sql::_drop))
      _last_parse_result= process_drop_statement(item);
    else if (const SqlAstNode *item= tree->subitem(sql::_statement, sql::_alter))
      _last_parse_result= process_alter_statement(item);
    else if (const SqlAstNode *item= tree->subitem(sql::_statement, sql::_use))
      process_use_schema_statement(item);

    if (pr_processed == _last_parse_result)
      ++_processed_obj_count; // count only processed statements (for clarity)
  }
  catch(const Parse_exception& e)
  {
    std::string msg_text= e.what() + EOL + 
      "SQL statement" + EOL +
      cut_sql_statement(strip_sql_statement(sql_statement(), true));
    add_log_message(msg_text, e.flag());
    ++_err_count;

    _last_parse_result= pr_invalid;
    return 1; // error count
  }

  return 0; // error count
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_statement(const SqlAstNode *tree)
{
  typedef Parse_result (Mysql_sql_parser::*statement_processor)(const SqlAstNode *);
  static statement_processor proc_arr[]=
  {
    &Mysql_sql_parser::process_create_table_statement,
    &Mysql_sql_parser::process_create_index_statement,
    &Mysql_sql_parser::process_create_view_statement,
    &Mysql_sql_parser::process_create_routine_statement,
    &Mysql_sql_parser::process_create_trigger_statement,
    &Mysql_sql_parser::process_create_server_link_statement,
    &Mysql_sql_parser::process_create_tablespace_statement,
    &Mysql_sql_parser::process_create_logfile_group_statement,
    &Mysql_sql_parser::process_create_schema_statement,
  };

  if (_process_specific_create_statement)
    return _process_specific_create_statement(tree);
  else
  {
    for (size_t n= 0; n < ARR_CAPACITY(proc_arr); ++n)
    {
      statement_processor proc= proc_arr[n];
      Parse_result result= (this->*proc)(tree);
      if (pr_irrelevant != result)
        return result;
    }
  }

  return pr_irrelevant;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_statement(const SqlAstNode *tree)
{
  typedef Parse_result (Mysql_sql_parser::*statement_processor)(const SqlAstNode *);
  static statement_processor proc_arr[]=
  {
    &Mysql_sql_parser::process_drop_schema_statement,
    &Mysql_sql_parser::process_drop_table_statement,
    &Mysql_sql_parser::process_drop_view_statement,
    &Mysql_sql_parser::process_drop_routine_statement,
    &Mysql_sql_parser::process_drop_trigger_statement,
  };

  for (size_t n= 0; n < ARR_CAPACITY(proc_arr); ++n)
  {
    statement_processor proc= proc_arr[n];
    Parse_result result= (this->*proc)(tree);
    if (pr_irrelevant != result)
      return result;
  }

  return pr_irrelevant;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_alter_statement(const SqlAstNode *tree)
{
  typedef Parse_result (Mysql_sql_parser::*statement_processor)(const SqlAstNode *);
  static statement_processor proc_arr[]=
  {
    &Mysql_sql_parser::process_alter_table_statement,
  };

  for (size_t n= 0; n < ARR_CAPACITY(proc_arr); ++n)
  {
    statement_processor proc= proc_arr[n];
    Parse_result result= (this->*proc)(tree);
    if (pr_irrelevant != result)
      return result;
  }

  return pr_irrelevant;
}


void Mysql_sql_parser::build_datatype_cache()
{
  _datatype_cache= DictRef(_grt);
  ListRef<db_SimpleDatatype> datatypes= _catalog->simpleDatatypes();
  db_SimpleDatatypeRef datatype;
  for (size_t n= 0; n < datatypes.count(); n++)
  {
    datatype= datatypes.get(n);
    _datatype_cache.set(datatype->name(), datatype);
  }
}


void Mysql_sql_parser::do_transactable_list_insert(ListRef<GrtObject> list, GrtObjectRef object)
{
  // this insert is important to be before check of _reusing_existing_obj
  // other classes rely on this order
  if (_created_objects.is_valid())
    _created_objects.insert(object);

  // time for this check is important - don't move
  if (_reusing_existing_obj)
    return;

  list.insert(object);
}


void Mysql_sql_parser::log_db_obj_created(const GrtNamedObjectRef &obj1, const GrtNamedObjectRef &obj2, const GrtNamedObjectRef &obj3)
{
  if (_reusing_existing_obj)
    return;
  log_db_obj_operation("Created", obj1, obj2, obj3);
}


void Mysql_sql_parser::log_db_obj_dropped(const GrtNamedObjectRef &obj1, const GrtNamedObjectRef &obj2, const GrtNamedObjectRef &obj3)
{
  log_db_obj_operation("Dropped", obj1, obj2, obj3);
}


void Mysql_sql_parser::log_db_obj_operation(const std::string &op_name, const GrtNamedObjectRef &obj1, const GrtNamedObjectRef &obj2, const GrtNamedObjectRef &obj3)
{
  const GrtNamedObjectRef obj= obj3.is_valid() ? obj3 : (obj2.is_valid() ? obj2 : obj1);
  
  std::string text;
  text
    .append(op_name)
    .append(" ")
    .append(obj->get_metaclass()->get_attribute("caption"))
    .append(": ");
  if (obj1.is_valid()) text.append(obj1->name());
  if (obj2.is_valid()) text.append(".").append(obj2->name());
  if (obj3.is_valid()) text.append(".").append(obj3->name());
  add_log_message(text, 3);
}


void Mysql_sql_parser::set_fk_references()
{
  grt::ListRef<db_mysql_Schema> schemata= _catalog->schemata();

  for (Fk_ref_collection::iterator i= _fk_refs.begin(); i != _fk_refs.end(); ++i)
  {
    // referred table
    db_mysql_SchemaRef ref_schema= find_named_object_in_list(schemata, i->ref_schema_name(), _case_sensitive_identifiers);
    db_mysql_TableRef ref_table;
    if (ref_schema.is_valid())
    {
      ref_table= find_named_object_in_list(ref_schema->tables(), i->ref_table_name(), _case_sensitive_identifiers);
      if (!ref_table.is_valid())
      {
        {
          std::string msg_text;
          msg_text.append("Table `")
            .append(i->ref_schema_name())
            .append("`.`")
            .append(i->ref_table_name())
            .append("` not found. Stub was created.");
          add_log_message(msg_text, 1);
        }
        create_stub_table(
          ref_schema,
          ref_table,
          i->ref_table_name());
      }
      (*i).operator db_ForeignKeyRef &()->referencedTable(ref_table);
    }
    else
      add_log_message(std::string("Could not find refschema ") + i->ref_schema_name() + " for reftable " + i->ref_table_name(), 1);

    // ref columns
    int column_index= 0;
    for (Fk_ref::String_collection::iterator c= i->ref_column_names().begin();
      c != i->ref_column_names().end(); ++c, ++column_index)
    {
      db_ForeignKeyRef &fk= *i;
      db_mysql_TableRef owner_table= db_mysql_TableRef::cast_from(i->owner_table());
      db_mysql_ColumnRef column= find_named_object_in_list(ref_table->columns(), *c, false); // mysql columns are always case-insensitive

      if (!column.is_valid())
      {
        std::string msg_text;
        msg_text
          .append("Table `")
          .append(db_SchemaRef::cast_from(owner_table->owner())->name())
          .append("`.`")
          .append(owner_table->name())
          .append("` : Foreign key `").append(fk->name())
          .append("` : Referred column `")
          .append(*ref_schema->name())
          .append("`.`")
          .append(*ref_table->name())
          .append("`.`")
          .append(*c)
          .append("` not found. ");

        if (ref_table->isStub())
        {
          create_stub_column(ref_table,
            column,
            *c,
            db_mysql_ColumnRef::cast_from(fk->columns().get(column_index)));
          fk->referencedColumns().insert(column);
          if (ref_table->tableEngine().empty())
            ref_table->tableEngine(owner_table->tableEngine());
          msg_text.append("Stub was created.");
        }
        else
        {
          owner_table->foreignKeys().gremove_value(fk);
          msg_text.append("Foreign key was skipped.");
          //! throw Parse_exception(err_text);
        }

        add_log_message(msg_text, 1);
      }
      else
      {
        fk->referencedColumns().insert(column);

        // add index for foreign key if it doesn't exist yet
        ListRef<db_Column> fk_columns= fk->columns();
        size_t ref_col_count= fk_columns.count();
        db_IndexRef found_index;
        ListRef<db_Index> indices= owner_table->indices();
        {
          for (size_t n= 0, count= indices.count(); n < count; ++n)
          {
            db_IndexRef index= indices.get(n);
            ListRef<db_IndexColumn> ind_columns= index->columns();
            bool fk_index_exists= true;
            for (size_t n= 0, count= ind_columns.count(); n < ref_col_count; ++n)
            {
              if ((n >= count) || (fk_columns.get(n) != ind_columns.get(n)->referencedColumn()))
              {
                fk_index_exists= false;
                break;
              }
            }
            if (fk_index_exists)
            {
              found_index= index;
              break;
            }
          }
        }

        if (found_index.is_valid())
        {
          if ((*found_index->indexType()).empty())
            found_index->indexType("INDEX");
            //found_index->indexType("FOREIGN");
          // new field for direct mapping --alfredo 10/04/16
          fk->index(found_index);
        }
        else
        {
          db_mysql_IndexRef index(owner_table.get_grt());
          index->owner(owner_table);
          set_obj_name(index, fk->name());
          //index->indexType("FOREIGN");
          index->indexType("INDEX");
          // new field for direct mapping --alfredo 10/04/16
          fk->index(index);

          for (size_t n= 0; n < ref_col_count; ++n)
          {
            db_ColumnRef column= fk_columns.get(n);
            db_mysql_IndexColumnRef index_column(index.get_grt());
            index_column->owner(index);
            index_column->referencedColumn(column);
            index->columns().insert(index_column);
          }

          indices.insert(index);
        }
      }
    }
  }
}


void Mysql_sql_parser::set_obj_sql_def(db_DatabaseDdlObjectRef obj)
{
  obj->sqlDefinition(strip_sql_statement(sql_statement(), _strip_sql));
}


void Mysql_sql_parser::set_obj_name(GrtNamedObjectRef obj, const std::string &val)
{
  SET_STR(obj->name, val)
  if (_set_old_names)
    obj->oldName(obj->name());
}


db_mysql_SchemaRef Mysql_sql_parser::set_active_schema(const std::string &schema_name)
{
  return _active_schema= ensure_schema_created(schema_name, false);
}


db_mysql_SchemaRef Mysql_sql_parser::ensure_schema_created(const std::string &schema_name, bool check_obj_name_uniqueness)
{
  if (schema_name.empty())
    return _active_schema;

  // try to find existing one first
  db_mysql_SchemaRef schema= find_named_object_in_list(_catalog->schemata(), schema_name, _case_sensitive_identifiers);
  if (!schema.is_valid()) // create if not found
  {
    schema= db_mysql_SchemaRef(_grt);
    schema->owner(_catalog);

    std::string time= base::fmttime(0, DATETIME_FMT);
    schema->createDate(time);
    schema->lastChangeDate(time);

    set_obj_name(schema, schema_name);
    
    {
      Cs_collation_setter schema_cs_collation_setter= cs_collation_setter(db_SchemaRef(schema), db_CatalogRef(_catalog), true);
      schema_cs_collation_setter.charset_name(_catalog->defaultCharacterSetName());
      schema_cs_collation_setter.collation_name(_catalog->defaultCollationName());
    }
    if(_shape_schema)
      _shape_schema(schema);
    do_transactable_list_insert(_catalog->schemata(), schema);
    log_db_obj_created(schema);
  }
  else if (check_obj_name_uniqueness)
    blame_existing_obj(false, schema);

  return schema;
}


void Mysql_sql_parser::create_stub_table(db_mysql_SchemaRef &schema, db_mysql_TableRef &obj, const std::string &obj_name)
{
  obj= db_mysql_TableRef(_grt);
  obj->owner(schema);
  obj->isStub(1);
  set_obj_name(obj, obj_name);
  schema->tables().insert(obj);
}


void Mysql_sql_parser::create_stub_column(db_mysql_TableRef &table, db_mysql_ColumnRef &obj, const std::string &obj_name, db_mysql_ColumnRef tpl_obj)
{
  obj= db_mysql_ColumnRef(_grt);
  obj->owner(table);
  set_obj_name(obj, obj_name);
  
  obj->simpleType(tpl_obj->simpleType());
  obj->userType(tpl_obj->userType());
  obj->structuredType(tpl_obj->structuredType());
  obj->precision(tpl_obj->precision());
  obj->scale(tpl_obj->scale());
  obj->length(tpl_obj->length());
  obj->datatypeExplicitParams(tpl_obj->datatypeExplicitParams());
  obj->formattedType(tpl_obj->formattedType());

  StringListRef tpl_flags= tpl_obj->flags();
  StringListRef flags= obj->flags();
  for (size_t c= tpl_flags.count(), i= 0; i < c; i++)
    flags.ginsert(tpl_flags.get(i));

  obj->characterSetName(tpl_obj->characterSetName());
  obj->collationName(tpl_obj->collationName());

  table->columns().insert(obj);
}


void Mysql_sql_parser::process_field_type_item(const SqlAstNode *item, db_mysql_ColumnRef &column)
{
  if (item)
  {
    // datatype
    {
      db_SimpleDatatypeRef datatype= map_datatype(item, _datatype_cache);
      if (!datatype.is_valid())
      {
        std::string sql_text= item->restore_sql_text(_sql_statement);
        std::string msg_text= "Mapping failed for datatype `" + sql_text + "`";
        add_log_message(msg_text, 1);
      }
      else
        column->simpleType(datatype);
    }

    // datatypeExplicitParams
    {
      const SqlAstNode *string_list_item= item->subitem(sql::_string_list);
      if (string_list_item)
      {
        std::string sql_text;
          sql_text
            .append("(")
            .append(string_list_item->restore_sql_text(_sql_statement))
            .append(")");
        column->datatypeExplicitParams(sql_text);
      }
    }

    // length
    {
      static sql::symbol path1[]= { sql::_field_length, sql::_ };
      static sql::symbol path2[]= { sql::_opt_field_length, sql::_field_length, sql::_ };
      static sql::symbol * paths[]= { path1, path2 };

      static sql::symbol names[]= { sql::_LONG_NUM, sql::_ULONGLONG_NUM, sql::_DECIMAL_NUM, sql::_NUM };

      const SqlAstNode *searched_item= item->search_by_paths(paths, ARR_CAPACITY(paths));
      if (searched_item)
        searched_item= searched_item->search_by_names(names, ARR_CAPACITY(names));
      if (column->simpleType().is_valid() && *column->simpleType()->numericPrecision() != bec::EMPTY_TYPE_PRECISION)
        SET_INT_I(column->precision, searched_item)
      else
        SET_INT_I(column->length, searched_item)
    }

    // float_options
    {
      std::string scale= "";
      std::string precision= "";
      const SqlAstNode* float_item = item->subitem(sql::_float_options);
      if (float_item)
        process_float_options_item(float_item, &precision, &scale);
      const SqlAstNode* precision_item = item->subitem(sql::_opt_precision);
      if (precision_item == NULL)
      {
        static sql::symbol precision_path[] = { sql::_real_type, sql::_precision, sql::_ };
        precision_item = item->subitem_by_path(precision_path);
      }
      if (precision_item)
        process_float_options_item(precision_item, &precision, &scale);
      if (!scale.empty())
        SET_INT(column->scale, scale)
      if (!precision.empty())
        SET_INT(column->precision, precision)
    }

    // field options
    {
      StringListRef flags(column->flags());
      concatenate_items(item->subitem(sql::_field_options, sql::_field_opt_list), flags, true);
    }

    // charset
    {
      static sql::symbol path11[]= { sql::_opt_binary, sql::_ascii, sql::_ };
      static sql::symbol path12[]= { sql::_opt_binary, sql::_unicode, sql::_ };
      static sql::symbol path13[]= { sql::_opt_binary, sql::_ };
      static sql::symbol * paths1[]= { path11, path12, path13 };

      static sql::symbol path21[]= { sql::_charset_name, sql::_ };
      static sql::symbol path22[]= { sql::_ASCII_SYM, sql::_ };
      static sql::symbol path23[]= { sql::_BYTE_SYM, sql::_ };
      static sql::symbol path24[]= { sql::_UNICODE_SYM, sql::_ };
      static sql::symbol * paths2[]= { path21, path22, path23, path24 };

      const SqlAstNode *searched_item= item->search_by_paths(paths1, ARR_CAPACITY(paths1));
      if (searched_item)
        searched_item= searched_item->search_by_paths(paths2, ARR_CAPACITY(paths2));
      if (searched_item)
        SET_STR_I(
          cs_collation_setter(column, db_mysql_TableRef::cast_from(column->owner()), false).charset_name,
          searched_item)
    }

    // binary
    {
      static sql::symbol path1[]= { sql::_opt_binary, sql::_BINARY, sql::_ };
      static sql::symbol path2[]= { sql::_opt_binary, sql::_opt_bin_mod, sql::_BINARY, sql::_ };
      static sql::symbol path3[]= { sql::_opt_binary, sql::_charset_name, sql::_BINARY, sql::_ };
      static sql::symbol path4[]= { sql::_opt_bin_mod, sql::_BINARY, sql::_ };
      static sql::symbol * paths[]= { path1, path2, path3, path4 };

      const SqlAstNode *searched_item= item->search_by_paths(paths, ARR_CAPACITY(paths));
      if (searched_item)
        column->flags().insert("BINARY");
    }
  }
}


void Mysql_sql_parser::process_field_attributes_item(const SqlAstNode *item, db_mysql_ColumnRef &column, db_mysql_TableRef &table)
{
  bool explicitDefaultValue= false;
  bool explicitNullValue= false;

  if (item)
  {
    // it was decided that defaultValue will also contain 'ON UPDATE CURRENT_TIMESTAMP' clause
    // do not remove this line until change is consistent with both subseq(sql::_DEFAULT) and subseq(sql::_ON, UPDATE_SYM)
    for (SqlAstNode::SubItemList::const_iterator it= item->subitems()->begin(); it != item->subitems()->end(); ++it)
    {
      const SqlAstNode *subitem= *it;
      if (subitem->name_equals(sql::_attribute))
      {
        const SqlAstNode *aux_item;

        if (subitem->subitem(sql::_AUTO_INC))
        {
          // set auto_increment only for numeric types
          if (column->simpleType().is_valid() && column->simpleType()->group().is_valid() &&
            are_strings_eq_ci(column->simpleType()->group()->name(), "NUMERIC"))
            column->autoIncrement(1);
        }
        else if (subitem->subseq(sql::_not, sql::_NULL_SYM))
        {
          column->isNotNull(1);
          explicitNullValue= true;
        }
        else if (subitem->subitem(sql::_NULL_SYM))
        {
          column->isNotNull(0);
          explicitNullValue= true;
        }
        else if ((aux_item= subitem->subseq(sql::_COMMENT_SYM, sql::_TEXT_STRING_sys)))
          SET_STR_I(column->comment, aux_item)
        else if ((aux_item= subitem->subseq(sql::_COLLATE_SYM, sql::_collation_name)))
          SET_STR_I(cs_collation_setter(column, table, false).collation_name, aux_item)
        else if ((aux_item= subitem->subseq(sql::_DEFAULT, sql::_now_or_signed_literal)))
        {
          // this is a fix for 'ON UPDATE CURRENT_TIMESTAMP' case.
          // If we have have already assigned the text 'ON UPDATE CURRENT_TIMESTAMP'
          // to the default value, then now we should not overwrite but
          std::string text;
          if (column->defaultValue().is_valid())
            text.assign(column->defaultValue());

          SET_SQL_I(column->defaultValue, aux_item);

          if (!text.empty())
          {
            std::string defv(column->defaultValue());
            defv.append(text);
            bec::ColumnHelper::set_default_value(column, defv);
          }

          if ((aux_item->subitem(sql::_signed_literal, sql::_literal, sql::_NULL_SYM)))
            column->defaultValueIsNull(1);
          explicitDefaultValue= true;
        }
        // This also handles the 'ON UPDATE CURRENT_TIMESTAMP' case as
        // CURRENT_TIMESTAMP is a synonymof NOW.
        else if ((aux_item = subitem->subseq(sql::_ON, sql::_UPDATE_SYM, sql::_now)))
        {
          std::string text;
          // it was decided that 'ON UPDATE CURRENT_TIMESTAMP' should be stored in defaultValue to not create new rarely-used field
          if (column->defaultValue().is_valid())
          {
            text= *column->defaultValue();
            text.append(" ");
          }

          // NOW is a subtree now with optional precision.
          aux_item = subitem->subitem(sql::_now);
          text.append(subitem->subitem(sql::_ON)->value()).append(" ")
            .append(subitem->subitem(sql::_UPDATE_SYM)->value()).append(" ")
            .append(aux_item->subitem(sql::_NOW_SYM)->value());

          bec::ColumnHelper::set_default_value(column, text);
        }
        else if ((aux_item= subitem->subitem(sql::_UNIQUE_SYM)) || (aux_item= subitem->subitem(sql::_KEY_SYM)))
        {
          db_mysql_IndexRef index(table.get_grt());
          index->owner(table);

          // index type
          if (aux_item->name_equals(sql::_UNIQUE_SYM))
          {
            index->unique(1);
            index->indexType("UNIQUE");
          }
          else
          {
            index->isPrimary(1);
            table->primaryKey(index);
            index->indexType("PRIMARY");
            //if ((*index->name()).empty()) // server resets any explicitly specified pk constraint names
              set_obj_name(index, "PRIMARY");
          }

          // index columns
          {
            db_mysql_IndexColumnRef index_column(table.get_grt());
            index_column->owner(index);

            // column
            index_column->referencedColumn(column);

            index->columns().insert(index_column);
          }

          table->indices().insert(index);
        }
      }
    }
  }
  
  if (column->simpleType().is_valid() && 
    are_strings_eq_ci(column->simpleType()->name(), "TIMESTAMP"))
  {
    if (!explicitNullValue)
      column->isNotNull(1);
  }

  if (!column->isNotNull() && !explicitDefaultValue)
    bec::ColumnHelper::set_default_value(column, "NULL");
}


std::string Mysql_sql_parser::process_field_name_item(const SqlAstNode *item, GrtNamedObjectRef obj, std::string *name3, std::string *name2, std::string *name1)
{
  std::string name= "";

  if (name1)
    name1->clear();
  if (name2)
    name2->clear();
  if (name3)
    name3->clear();

  if (item)
  {
    size_t n= 4;
    for (SqlAstNode::SubItemList::const_reverse_iterator it= item->subitems()->rbegin(), end= item->subitems()->rend(); it != end; ++it)
    {
      const SqlAstNode *subitem= *it;
      if (!subitem->name_equals(sql::_44)) // 44 == ascii(',')
      {
        switch (--n)
        {
          case 1:
          {
            if (name1)
              *name1= subitem->value();
            break;
          }
          case 2:
          {
            if (name2)
              *name2= subitem->value();
            break;
          }
          case 3:
          {
            name= subitem->value();
            if (name3)
              *name3= name;
            break;
          }
        }
      }
    }

    if (obj.is_valid())
      set_obj_name(obj, name);
  }

  return name;
}


std::string Mysql_sql_parser::process_float_options_item(const SqlAstNode *item, std::string *precision, std::string *scale)
{
  std::string precision_= "";

  if (precision)
    precision->clear();
  if (scale)
    scale->clear();

  if (item)
  {
    const SqlAstNode *scale_item= item->subitem(sql::_precision);
    if (scale_item)
    {
      const SqlAstNode *num_item;

      if ((num_item= scale_item->subitem(sql::_NUM)))
        precision_= num_item->value();

      if (scale &&
          ((num_item= scale_item->find_subseq(scale_item->subitem(sql::_44), sql::_NUM)))) // 44 == ascii(',')
        *scale= num_item->value();
    }
    else
    {
      const SqlAstNode *num_item;
      if ((num_item= item->subitem(sql::_NUM)))
        precision_= num_item->value();
    }
  }

  if (precision)
    *precision= precision_;

  return precision_;
}


std::string Mysql_sql_parser::process_obj_full_name_item(const SqlAstNode *item, db_mysql_SchemaRef *schema)
{
  std::string obj_name;
  std::string schema_name= "";

  if (!item)
    return obj_name;

  if (3 == item->subitems()->size()) // ident.ident
    schema_name= (*item->subitems()->begin())->value();
  obj_name= (*item->subitems()->rbegin())->value();

  // this will add schema to schemata if necessary
  db_mysql_SchemaRef schema_= ensure_schema_created(schema_name, false);

  // if schema that object is sticked to doesn't correspond to the one pointed in DDL
  if (_stick_to_active_schema && (schema_ != _active_schema))
  {
    // give the object a special name
    const char *spec_name_postfix= "_WRONG_SCHEMA";
    if (std::string::npos == obj_name.find(spec_name_postfix))
      obj_name.append(spec_name_postfix);

    // also substitute schema by active schema
    schema_= _active_schema;
  }

  if (schema)
    *schema= schema_;

  return obj_name;
}


void Mysql_sql_parser::process_index_item(const SqlAstNode *tree, db_mysql_TableRef &table)
{
  db_mysql_IndexRef obj(_grt);
  obj->owner(table);

  // name
  {
    static sql::symbol path1[]= { sql::_opt_ident, sql::_field_ident, sql::_ };
    static sql::symbol path2[]= { sql::_opt_constraint, sql::_constraint, sql::_opt_ident, sql::_field_ident, sql::_ };
    static sql::symbol * paths[]= { path1, path2 };

    const SqlAstNode *item= tree->search_by_paths(paths, ARR_CAPACITY(paths));
    process_field_name_item(item, obj);
  }
  
  // index type
  {
    std::string index_type;

    static sql::symbol path1[]= { sql::_normal_key_type, sql::_ };
    static sql::symbol path2[]= { sql::_fulltext, sql::_ };
    static sql::symbol path3[]= { sql::_spatial, sql::_ };
    static sql::symbol path4[]= { sql::_constraint_key_type, sql::_ };
    static sql::symbol * paths[]= { path1, path2, path3, path4 };

    const SqlAstNode *item= tree->search_by_paths(paths, ARR_CAPACITY(paths));
    if (item)
    {
      if (item->subitem(sql::_PRIMARY_SYM))
      {
        obj->isPrimary(1);
        table->primaryKey(obj);
        //if ((*obj->name()).empty()) // server resets any explicitly specified pk constraint names
          set_obj_name(obj, "PRIMARY");
        index_type= "PRIMARY";
      }
      else if (item->subitem(sql::_UNIQUE_SYM))
      {
        obj->unique(1);
        index_type= "UNIQUE";
      }
      else
        index_type= item->restore_sql_text(_sql_statement);
    }
    else
      index_type= "INDEX";

    obj->indexType(shape_index_type(index_type));
  }

  // index kind
  process_index_kind_item(obj, tree->subitem(sql::_key_alg, sql::_key_using_alg, sql::_btree_or_rtree));

  // columns
  {
    const SqlAstNode *items= tree->subitem(sql::_key_list);
    if (items)
    {
      db_mysql_IndexColumnRef index_column(_grt);
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_key_part))
        {
          index_column= db_mysql_IndexColumnRef(_grt);
          index_column->owner(obj);

          // column
          const SqlAstNode *name_item= item->subitem(sql::_ident);
          std::string column_name= (name_item ? name_item->value() : "");
          db_mysql_ColumnRef column= find_named_object_in_list(table->columns(), column_name, false); // mysql columns are always case-insensitive
          if (!column.is_valid())
          {
            std::string err_text;
            err_text
              .append("Column `")
              .append(column_name)
              .append("` not found");
            throw Parse_exception(err_text);
          }
          index_column->referencedColumn(column);
          
          // stored function
          // there is nothing relevant in separate 'CREATE' statement (v5.1)
          //index_column.storedFunction("");

          // stored function
          // there is nothing relevant in separate 'CREATE' statement (v5.1)
          //index_column.comment("");

          // length
          SET_INT_SI(index_column->columnLength, item, sql::_NUM);

          obj->columns().insert(index_column);
        }
        else if (item->name_equals(sql::_order_dir))
        {
          // order direction
          index_column->descend(are_strings_eq_ci("DESC", item->value()) ? 1 : 0);
        }
      }
    }
  }

  // options
  process_index_options_item(obj, tree);

  table->indices().insert(obj);
}


void Mysql_sql_parser::process_fk_item(const SqlAstNode *tree, db_mysql_TableRef &table)
{
  db_mysql_ForeignKeyRef obj(_grt);
  obj->owner(table);
  Fk_ref fk_ref(obj);

  // name
  {
    static sql::symbol path1[]= { sql::_opt_ident, sql::_field_ident, sql::_ };
    static sql::symbol path2[]= { sql::_opt_constraint, sql::_constraint, sql::_opt_ident, sql::_field_ident, sql::_ };
    static sql::symbol * paths[]= { path1, path2 };

    const SqlAstNode *item= tree->search_by_paths(paths, ARR_CAPACITY(paths));
    process_field_name_item(item, obj);

    if (_gen_fk_names_when_empty && obj->name().operator std::string().empty())
    {
      std::string name= bec::TableHelper::generate_foreign_key_name();
      set_obj_name(obj, name);
    }
  }

  // own columns
  {
    const SqlAstNode *items= tree->subitem(sql::_key_list);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); it++)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_key_part))
        {
          const SqlAstNode *colname_item= item->subitem(sql::_ident);

          if (colname_item)
          {
            std::string colname= colname_item->value();

            db_mysql_ColumnRef column= find_named_object_in_list(table->columns(), colname, false); // mysql columns are always case-insensitive
            if (!column.is_valid())
            {
              std::string err_text;
              err_text
                .append("Column `")
                .append(colname)
                .append("` not found");
              throw Parse_exception(err_text);
            }
            else
              obj->columns().insert(column);
          }
        }
      }
    }
  }

  // cardinality/mandatory
  {
    obj->referencedMandatory(1);
    grt::ListRef<db_Column> columns= obj->columns();
    for (size_t n= 0, count= columns.count(); n < count; ++n)
    {
      if (!columns.get(n)->isNotNull())
      {
        obj->referencedMandatory(0);
        break;
      }
    }
  }
  obj->many(1);

  process_fk_references_item(tree->subitem(sql::_references), obj, fk_ref);

  table->foreignKeys().insert(obj);
  _fk_refs.push_back(fk_ref);
}


void Mysql_sql_parser::process_fk_references_item(const SqlAstNode *tree, db_mysql_ForeignKeyRef &fk, Fk_ref &fk_ref)
{
  // tree is a 'references' item
  if (!tree)
    return;

  // referred table - use fk decorator to store names - set references at next stage
  db_mysql_SchemaRef ref_schema;
  db_mysql_TableRef ref_table;
  {
    // reset reuse_existing that could've been set for the owner table object, but would
    // prevent a stub schema to be created by process_obj_full_name_item() in case its necessary
    Val_keeper<bool> reusing_existing_objects_keeper(&_reusing_existing_obj);
    _reusing_existing_obj= false;
    
    std::string ref_obj_name= process_obj_full_name_item(tree->subitem(sql::_table_ident), &ref_schema);
    fk_ref.ref_schema_name(ref_schema->name());
    fk_ref.ref_table_name(ref_obj_name);
  }

  // ref columns names - use fk decorator to store names - set references at next stage
  {
    const SqlAstNode *items= tree->subitem(sql::_opt_ref_list, sql::_ref_list);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); it++)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_ident))
          fk_ref.ref_column_names().push_back(item->value());
      }
    }
  }

  // on update/delete rule
  if (const SqlAstNode *opt_on_update_delete_item= tree->subitem(sql::_opt_on_update_delete))
  {
    if (const SqlAstNode *delete_option_item= opt_on_update_delete_item->find_subseq(sql::_DELETE_SYM, sql::_delete_option))
      fk->deleteRule(delete_option_item->restore_sql_text(_sql_statement));
    if (const SqlAstNode *delete_option_item= opt_on_update_delete_item->find_subseq(sql::_UPDATE_SYM, sql::_delete_option))
      fk->updateRule(delete_option_item->restore_sql_text(_sql_statement));
  }
}


void Mysql_sql_parser::process_index_options_item(db_mysql_IndexRef &obj, const SqlAstNode *item)
{
  static sql::symbol path1[]= { sql::_normal_key_options, sql::_normal_key_opts, sql::_ };
  static sql::symbol path2[]= { sql::_fulltext_key_options, sql::_fulltext_key_opts, sql::_ };
  static sql::symbol path3[]= { sql::_spatial_key_options, sql::_spatial_key_opts, sql::_ };
  static sql::symbol * paths[]= { path1, path2, path3 };

  if (const SqlAstNode *items= item->search_by_paths(paths, ARR_CAPACITY(paths)))
  {
    for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
    {
      const SqlAstNode *item= *it;
      switch (item->name())
      {
      case sql::_normal_key_opt:
      case sql::_fulltext_key_opt:
      case sql::_spatial_key_opt:
        if (const SqlAstNode *aux_item= item->subseq(sql::_key_using_alg))
          process_index_kind_item(obj, aux_item->subitem(sql::_btree_or_rtree));
        else if (/*const SqlAstNode *aux_item= */item->subitem(sql::_all_key_opt, sql::_KEY_BLOCK_SIZE))
          SET_INT_SI(obj->keyBlockSize, item, sql::_all_key_opt, sql::_ulong_num)
        else if (item->subseq(sql::_WITH, sql::_PARSER_SYM))
          SET_STR_SI(obj->withParser, item, sql::_IDENT_sys)
       else if (item->subitem(sql::_all_key_opt, sql::_COMMENT_SYM))
            SET_STR_SI(obj->comment, item, sql::_all_key_opt, sql::_TEXT_STRING_sys)
        break;
      default:
        break;
      }
    }
  }
}


void Mysql_sql_parser::process_index_kind_item(db_mysql_IndexRef &obj, const SqlAstNode *item)
{
  if (!item)
    return;

  const std::string index_kind= item->restore_sql_text(_sql_statement);
  if (!index_kind.empty())
    obj->indexKind(shape_index_kind(index_kind));
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_table_statement(const SqlAstNode *tree)
{
  const SqlAstNode *create2_item= tree->subitem(sql::_create2);

  // check if statement is relevant
  if (!create2_item)
    return pr_irrelevant;

  ACTIVE_SCHEMA_KEEPER

  db_mysql_SchemaRef schema;
  db_mysql_TableRef obj;

  // try to find previously created stub object with the same name
  {
    const SqlAstNode *table_ident_item= tree->find_subseq(sql::_TABLE_SYM, sql::_table_ident);
    if (!table_ident_item)
      table_ident_item= tree->find_subseq(sql::_TABLE_SYM, sql::_opt_if_not_exists, sql::_table_ident);
    std::string obj_name= process_obj_full_name_item(table_ident_item, &schema);

    step_progress(obj_name);

    _active_schema= schema;

    // check for same-named view
    {
      db_mysql_ViewRef obj= find_named_object_in_list(schema->views(), obj_name, _case_sensitive_identifiers);
      if (obj.is_valid())
      {
        Val_keeper<bool> reuse_existing_objects_keeper(&_reuse_existing_objects);
        _reuse_existing_objects= false;
        blame_existing_obj(true, obj, schema);
      }
    }

    {
      Val_keeper<bool> reuse_existing_objects_keeper(&_reuse_existing_objects);
      _reuse_existing_objects= true;
      obj= create_or_find_named_obj(schema->tables(), obj_name, _case_sensitive_identifiers, schema);
    }

    if (_reusing_existing_obj)
    {
      if (obj->isStub())
      {
        obj->isStub(0); // reuse equally named stub

        std::string msg_text;
        msg_text
          .append("Previously created stub for table `")
          .append(*schema->name())
          .append("`.`")
          .append(obj_name)
          .append("` was found. Reusing.");
        add_log_message(msg_text, 1);
      }
      else
        blame_existing_obj(true, obj, schema);
    }
    else
    {
      // name
      std::string name= process_obj_full_name_item(table_ident_item, NULL);
      if (obj.is_valid())
        set_obj_name(obj, name);
    }
  }

  // options
  {
    const SqlAstNode *items= create2_item->subitem(sql::_create2a, sql::_opt_create_table_options, sql::_create_table_options);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_create_table_option))
        {
          const SqlAstNode *aux_item= NULL;

          if (item->subseq(sql::_ENGINE_SYM) || item->subseq(sql::_TYPE_SYM))
          {
            SET_STR_SI(obj->tableEngine, item, sql::_storage_engines)
            obj->tableEngine(__table_storage_engines.normalize_name(obj->tableEngine()));
          }
          else if (item->subseq(sql::_ROW_FORMAT_SYM))
            SET_STR_SI(obj->rowFormat, item, sql::_row_types)
          else if (item->subseq(sql::_KEY_BLOCK_SIZE))
            SET_STR_SI(obj->keyBlockSize, item, sql::_ulong_num)
          else if (item->subseq(sql::_AUTO_INC))
            SET_STR_SI(obj->nextAutoInc, item, sql::_ulonglong_num)
          else if ((aux_item= item->subseq(sql::_default_charset)))
            SET_STR_SI(
              cs_collation_setter(obj, schema, false).charset_name,
              aux_item,
              sql::_charset_name_or_default)
          else if ((aux_item= item->subseq(sql::_default_collation)))
            SET_STR_SI(
              cs_collation_setter(obj, schema, false).collation_name,
              aux_item,
              sql::_collation_name_or_default)
          else if (item->subseq(sql::_DELAY_KEY_WRITE_SYM))
            SET_INT_SI(obj->delayKeyWrite, item, sql::_ulong_num)
          else if (item->subseq(sql::_COMMENT_SYM))
            SET_STR_SI(obj->comment, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_DATA_SYM, sql::_DIRECTORY_SYM))
            SET_STR_SI(obj->tableDataDir, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_INDEX_SYM, sql::_DIRECTORY_SYM))
            SET_STR_SI(obj->tableIndexDir, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_PACK_KEYS_SYM))
          {
            SET_STR_SI(obj->packKeys, item, sql::_DEFAULT)
            SET_STR_SI(obj->packKeys, item, sql::_ulong_num)
          }
          else if (item->subseq(sql::_AVG_ROW_LENGTH))
            SET_STR_SI(obj->avgRowLength, item, sql::_ulong_num)
          else if (item->subseq(sql::_MIN_ROWS))
            SET_STR_SI(obj->minRows, item, sql::_ulonglong_num)
          else if (item->subseq(sql::_MAX_ROWS))
            SET_STR_SI(obj->maxRows, item, sql::_ulonglong_num)
          else if (item->subseq(sql::_CHECKSUM_SYM))
            SET_INT_SI(obj->checksum, item, sql::_ulong_num)
          else if (item->subseq(sql::_INSERT_METHOD))
            SET_STR_SI(obj->mergeInsert, item, sql::_merge_insert_types)
          else if (item->subseq(sql::_UNION_SYM))
          {
            /*
              server cuts schema qualification from table names (only for those located within the same schema with table being defined)
              in union list when using 'show create' statement.
              that's why to avoid false differences during merge procedure, all table names are explicitly normalized (fully qualified names only).
            */
            const SqlAstNode *items= item->subitem(sql::_opt_table_list, sql::_table_list);
            if (items && items->subitems())
            {
              std::string table_list;
              for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
              {
                const SqlAstNode *item= *it;
                if (item->name_equals(sql::_table_name))
                {
                  db_mysql_SchemaRef schema;
                  std::string obj_name= process_obj_full_name_item(item->subitem(sql::_table_ident), &schema);
                  if (!table_list.empty())
                    table_list.append(", ");
                  table_list.append(qualify_obj_name(obj_name, schema->name()));
                }
              }
              if (!table_list.empty())
                obj->mergeUnion(table_list);
            }
          }
        }
      }
    }
  }

  // partitioning
  {
    class Partition_definition
    {
      static void parse_options(db_mysql_PartitionDefinitionRef part_obj, const SqlAstNode *part_options)
      {
        for (SqlAstNode::SubItemList::const_iterator it= part_options->subitems()->begin(),
          it_end= part_options->subitems()->end();
          it != it_end; ++it)
        {
          const SqlAstNode *part_option= *it;
          if (!part_option->name_equals(sql::_opt_part_option))
            continue;

          const SqlAstNode *part_option_name;
          
          if ((part_option_name= part_option->subitem(sql::_MAX_ROWS)))
            SET_STR_SI(part_obj->maxRows, part_option, sql::_real_ulonglong_num)
          else if ((part_option_name= part_option->subitem(sql::_MIN_ROWS)))
            SET_STR_SI(part_obj->minRows, part_option, sql::_real_ulonglong_num)
          else if ((part_option_name= part_option->subitem(sql::_DATA_SYM)))
            SET_STR_SI(part_obj->dataDirectory, part_option, sql::_TEXT_STRING_sys)
          else if ((part_option_name= part_option->subitem(sql::_INDEX_SYM)))
            SET_STR_SI(part_obj->indexDirectory, part_option, sql::_TEXT_STRING_sys)
          else if ((part_option_name= part_option->subitem(sql::_COMMENT_SYM)))
            SET_STR_SI(part_obj->comment, part_option, sql::_TEXT_STRING_sys)
          // TODO: process ENGINE (not supported by server as of 5.1.22)
        }
      }
    
      // returns count of subpartitions
      static void parse_subpartitions(db_mysql_PartitionDefinitionRef part_obj, const SqlAstNode *subpart_list)
      {
        for (SqlAstNode::SubItemList::const_iterator it= subpart_list->subitems()->begin(),
          it_end= subpart_list->subitems()->end();
          it != it_end; ++it)
        {
          const SqlAstNode *subpart_item= *it;
          if (!subpart_item->name_equals(sql::_sub_part_definition))
            continue;

          db_mysql_PartitionDefinitionRef subpart_obj(part_obj.get_grt());

          SET_STR_SI(subpart_obj->name, subpart_item, sql::_sub_name);
          parse_options(subpart_obj, subpart_item->subitem(sql::_opt_part_options, sql::_opt_part_option_list));

          part_obj->subpartitionDefinitions().insert(subpart_obj);
        }
      }

    public:
      static db_mysql_PartitionDefinitionRef parse(GRT *grt, const SqlAstNode *part_item, const std::string &_sql_statement)
      {
        db_mysql_PartitionDefinitionRef part_obj(grt);

        const SqlAstNode *part_attr;

        if ((part_attr= part_item->subitem(sql::_part_name)))
          part_obj->name(part_attr->value());

        if (const SqlAstNode *opt_part_values_item= part_item->subitem(sql::_opt_part_values))
        {
          static sql::symbol path1[]= { sql::_part_func_max, sql::_MAX_VALUE_SYM, sql::_ };
          static sql::symbol path2[]= { sql::_part_func_max, sql::_part_value_item, sql::_part_value_item_list, sql::_ };
          static sql::symbol path3[]= { sql::_part_values_in, sql::_part_value_item, sql::_part_value_item_list, sql::_ };
          static sql::symbol path4[]= { sql::_part_values_in, sql::_part_value_list, sql::_ };
          static sql::symbol * paths[]= { path1, path2, path3, path4 };

          part_attr= opt_part_values_item->search_by_paths(paths, ARR_CAPACITY(paths));

          SET_SQL_I(part_obj->value, part_attr)
        }

        if ((part_attr= part_item->subitem(sql::_opt_part_options, sql::_opt_part_option_list)))
          parse_options(part_obj, part_attr);

        if ((part_attr= part_item->subitem(sql::_opt_sub_partition, sql::_sub_part_list)))
          parse_subpartitions(part_obj, part_attr);

        return part_obj;
      }
    };  // class Partition_definition

    // clear partition list in case this is a reused table
    obj->partitionDefinitions().remove_all();

    const SqlAstNode *partition= create2_item->subitem(sql::_create2a, sql::_opt_create_partitioning, sql::_partitioning, sql::_partition);
    if (partition)
    {
      bool is_range_or_list_partition= (partition->subitem(sql::_part_type_def, sql::_RANGE_SYM) != NULL)
        || (partition->subitem(sql::_part_type_def, sql::_LIST_SYM) != NULL);

      const SqlAstNode *item;

      if ((item= partition->subitem(sql::_part_type_def)))
      {
        std::string part_type;
        const SqlAstNode *part_exprt_item= NULL;

        if (item->subitem(sql::_LINEAR_SYM))
          part_type.append("LINEAR ");

        if (item->subitem(sql::_HASH_SYM))
          part_type.append("HASH");
        else if (item->subitem(sql::_KEY_SYM))
        {
          part_type.append("KEY");
          part_exprt_item= item->subitem(sql::_part_field_list, sql::_part_field_item_list);
        }
        else if (item->subitem(sql::_RANGE_SYM))
          part_type.append("RANGE");
        else if (item->subitem(sql::_LIST_SYM))
          part_type.append("LIST");

        obj->partitionType(part_type);

        if (!part_exprt_item)
          part_exprt_item= item->subitem(sql::_part_func, sql::_part_func_expr);

        SET_SQL_I(obj->partitionExpression, part_exprt_item);
      }

      if ((item= partition->subitem(sql::_opt_num_parts)))
      {
        if (!is_range_or_list_partition) // is not valid for range partitions
          SET_INT_SI(obj->partitionCount, item, sql::_real_ulong_num);
      }

      if ((item= partition->subitem(sql::_opt_sub_part)))
      {
        if (is_range_or_list_partition) // valid only for range partitions
        {
          std::string subpart_type;

          if (item->subitem(sql::_LINEAR_SYM))
            subpart_type.append("LINEAR ");

          if (item->subitem(sql::_HASH_SYM))
            subpart_type.append("HASH");
          else if (item->subitem(sql::_KEY_SYM))
            subpart_type.append("KEY");

          obj->subpartitionType(subpart_type);

          SET_SQL_I(obj->subpartitionExpression, item->subitem(sql::_sub_part_func, sql::_part_func_expr));
        }
      }

      if ((item= partition->subitem(sql::_part_defs)))
      {
        if (is_range_or_list_partition) // valid only for range partitions
        {
          const SqlAstNode *part_def_list= item->subitem(sql::_part_def_list);

          for (SqlAstNode::SubItemList::const_iterator jt= part_def_list->subitems()->begin(),
            jt_end= part_def_list->subitems()->end();
            jt != jt_end; ++jt)
          {
            const SqlAstNode *part= *jt;
            if (part->name_equals(sql::_part_definition))
            {
              db_mysql_PartitionDefinitionRef part_obj= Partition_definition::parse(obj.get_grt(), part, _sql_statement);
              obj->partitionDefinitions().insert(part_obj);
            }
          }
        }
      }

      if (obj->partitionCount() == 0)
        obj->partitionCount((long)obj->partitionDefinitions().count());
      if (obj->partitionDefinitions().count() > 0)
        obj->subpartitionCount((long)obj->partitionDefinitions().get(0)->subpartitionDefinitions().count());
    }
  }

  // columns, indices, foreign keys
  {
    const SqlAstNode *items= create2_item->subitem(sql::_create2a, sql::_create_field_list, sql::_field_list);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_field_list_item))
        {
          const SqlAstNode *aux_item= NULL;

          if ((aux_item= item->subitem(sql::_column_def)))
          {
            db_mysql_ColumnRef column;

            // field
            if ((aux_item= aux_item->subitem(sql::_field_spec)))
            {
              bool reusing_column= false;

              // if current table is reusing stub object then try to find existing column with the same name
              if (_reusing_existing_obj)
              {
                std::string field_name= process_field_name_item(aux_item->subitem(sql::_field_ident));
                column= find_named_object_in_list(obj->columns(), field_name, false); // mysql columns are always case-insensitive
              }

              if (column.is_valid())
                reusing_column= true;
              else
              {
                column= db_mysql_ColumnRef(_grt);
                column->owner(obj);
              }

              // name
              process_field_name_item(aux_item->subitem(sql::_field_ident), column);

              // type
              process_field_type_item(aux_item->subitem(sql::_type), column);

              // attributes
              process_field_attributes_item(aux_item->subitem(sql::_opt_attribute, sql::_opt_attribute_list), column, obj);

              if (!reusing_column)
                obj->columns().insert(column);
            }

            if ((aux_item= item->subitem(sql::_column_def, sql::_references)))
            {
              db_mysql_ForeignKeyRef fk(_grt);
              fk->owner(obj);
              Fk_ref fk_ref(fk);

              // own columns
              fk->columns().insert(column);

              // name
              if (_gen_fk_names_when_empty)
              {
                std::string name= bec::TableHelper::generate_foreign_key_name();
                set_obj_name(fk, name);
              }

              // cardinality/mandatory
              fk->referencedMandatory(1);
              grt::ListRef<db_Column> columns= fk->columns();
              for (size_t n= 0, count= columns.count(); n < count; ++n)
              {
                if (!columns.get(n)->isNotNull())
                {
                  fk->referencedMandatory(0);
                  break;
                }
              }
              fk->many(1);

              // references
              process_fk_references_item(aux_item, fk, fk_ref);

              obj->foreignKeys().insert(fk);
              _fk_refs.push_back(fk_ref);
            }
          }
          else if ((aux_item= item->subitem(sql::_key_def)))
          {
            if ((aux_item->find_subseq(sql::_FOREIGN, sql::_KEY_SYM)))
              ENSURE(process_fk_item(aux_item, obj),
                "Table `" + *obj->name() + "` : " + e.what())
            else if ((aux_item->subitem(sql::_key_list)))
              ENSURE(process_index_item(aux_item, obj),
                "Table `" + *obj->name() + "` : " + e.what())
          }
        }
      }
    }
  }

  if(_shape_table)
    _shape_table(obj);
  do_transactable_list_insert(schema->tables(), obj);
  if (!obj->isStub())
    log_db_obj_created(schema, obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_view_statement(const SqlAstNode *tree)
{
  const SqlAstNode *view_tail= NULL;
  {
    static sql::symbol path1[]= { sql::_view_or_trigger_or_sp_or_event, sql::_definer_tail, sql::_ };
    static sql::symbol path2[]= { sql::_view_or_trigger_or_sp_or_event, sql::_no_definer_tail, sql::_ };
    static sql::symbol path3[]= { sql::_view_or_trigger_or_sp_or_event, sql::_ };
    static sql::symbol * paths[]= { path1, path2, path3 };

    view_tail= tree->search_by_paths(paths, ARR_CAPACITY(paths));
    if (view_tail)
      view_tail= view_tail->subitem(sql::_view_tail);
  }

  // check if statement is relevant
  if (!view_tail)
    return pr_irrelevant;

  db_mysql_SchemaRef schema;
  const SqlAstNode *table_ident_item= view_tail->find_subseq(sql::_VIEW_SYM, sql::_table_ident);
  std::string obj_name= process_obj_full_name_item(table_ident_item, &schema);
  step_progress(obj_name);

  // check for same-named table
  {
    db_mysql_TableRef obj= find_named_object_in_list(schema->tables(), obj_name, _case_sensitive_identifiers);
    if (obj.is_valid())
    {
      Val_keeper<bool> reuse_existing_objects_keeper(&_reuse_existing_objects);
      _reuse_existing_objects= false;
      blame_existing_obj(true, obj, schema);
    }
  }

  db_mysql_ViewRef obj= create_or_find_named_obj(schema->views(), obj_name, _case_sensitive_identifiers, schema);
  SET_SQL_SI(obj->definer, tree, sql::_view_or_trigger_or_sp_or_event, sql::_definer_opt, sql::_definer, sql::_user)

  const SqlAstNode* algorithm_node = tree->subitem(sql::_view_or_trigger_or_sp_or_event, sql::_view_replace_or_algorithm, sql::_view_algorithm);
  int algorithm = 0;
  if (algorithm_node->subitem(sql::_UNDEFINED_SYM))
      algorithm = 0;
  else if (algorithm_node->subitem(sql::_MERGE_SYM))
      algorithm = 1;
  else if (algorithm_node->subitem(sql::_TEMPTABLE_SYM))
      algorithm = 2;
  obj->algorithm(algorithm);

  const SqlAstNode* select_node = view_tail->find_subseq(sql::_view_select);
  std::string select;
  if (select_node)
      select = select_node->restore_sql_text(_sql_statement);
  obj->sqlBody(select);

  // name
  std::string name= process_obj_full_name_item(table_ident_item, NULL);
  if (obj.is_valid())
    set_obj_name(obj, name);

  // sql_statement
  set_obj_sql_def(obj);

  // check option
  if (view_tail->subitem(sql::_view_check_option))
    obj->withCheckCondition(1);

  _shape_view(obj);
  do_transactable_list_insert(schema->views(), obj);
  log_db_obj_created(schema, obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_routine_statement(const SqlAstNode *tree)
{
  const SqlAstNode *routine_tail= NULL;
  {
    static sql::symbol path1[]= { sql::_view_or_trigger_or_sp_or_event, sql::_definer_tail, sql::_ };
    static sql::symbol path2[]= { sql::_view_or_trigger_or_sp_or_event, sql::_no_definer_tail, sql::_ };
    static sql::symbol * paths[]= { path1, path2 };

    routine_tail= tree->search_by_paths(paths, ARR_CAPACITY(paths));
  }
  if (routine_tail)
  {
    static sql::symbol path1[]= { sql::_sp_tail, sql::_ };
    static sql::symbol path2[]= { sql::_sf_tail, sql::_ };
    static sql::symbol * paths[]= { path1, path2 };

    routine_tail= routine_tail->search_by_paths(paths, ARR_CAPACITY(paths));
  }

  // check if statement is relevant
  if (!routine_tail)
    return pr_irrelevant;

  std::string routine_type;
  if (routine_tail->subitem(sql::_FUNCTION_SYM))
    routine_type = "function";
  else if (routine_tail->subitem(sql::_PROCEDURE_SYM))
    routine_type = "procedure";
  
  db_mysql_SchemaRef schema;
  const SqlAstNode *routine_ident_item= routine_tail->subitem(sql::_sp_name);
  std::string obj_name= process_obj_full_name_item(routine_ident_item, &schema);
  step_progress(obj_name);

  db_mysql_RoutineRef obj= create_or_find_named_routine(schema->routines(), obj_name, false, routine_type,
                                                        schema); // mysql routines are always case-insensitive

  SET_SQL_SI(obj->definer, tree, sql::_view_or_trigger_or_sp_or_event, sql::_definer, sql::_user)
  std::string sql_body;
  const SqlAstNode *sql_body_node = routine_tail->find_subseq(sql::_sp_proc_stmt);
  if(sql_body_node)
      obj->sqlBody(sql_body_node->restore_sql_text(_sql_statement));

  // name
  std::string name= process_obj_full_name_item(routine_ident_item, NULL);
  if (obj.is_valid())
    set_obj_name(obj, name);

  {
    const SqlAstNode *param_list_item= NULL;

    obj->routineType(routine_type);
    if (routine_type == "procedure")
    {
      param_list_item= routine_tail->subitem(sql::_sp_pdparam_list, sql::_sp_pdparams);
    }
    else if (routine_type == "function")
    {
      param_list_item= routine_tail->subitem(sql::_sp_fdparam_list, sql::_sp_fdparams);

      // return datatype
      SET_SQL_SI(obj->returnDatatype, routine_tail, sql::_type_with_opt_collate, sql::_type)
    }

    // params
    {
      ListRef<db_mysql_RoutineParam> params= obj->params();
      
      if (_reusing_existing_obj)
        for (ssize_t n= params.count(); n > 0; --n)
          params.remove_value(params[n-1]);

      if (param_list_item)
      {
        for (SqlAstNode::SubItemList::const_iterator it= param_list_item->subitems()->begin(); it != param_list_item->subitems()->end(); ++it)
        {
          const SqlAstNode *item= *it;
          if (item->name_equals(sql::_sp_pdparam) || item->name_equals(sql::_sp_fdparam))
          {
            db_mysql_RoutineParamRef param(_grt);
            param->owner(obj);

            SET_STR_SI(param->name, item, sql::_ident)
            SET_STR_SI(param->paramType, item, sql::_sp_opt_inout)
            
            // datatype
            SET_SQL_SI(param->datatype, item, sql::_type_with_opt_collate, sql::_type)

            params.insert(param);
          }
        }
      }
    }
  }

  // comment
  {
    const SqlAstNode *sp_c_chistics= routine_tail->subitem(sql::_sp_c_chistics);
    if (sp_c_chistics)
    {
      for (SqlAstNode::SubItemList::const_iterator it= sp_c_chistics->subitems()->begin(), it_end= sp_c_chistics->subitems()->end(); it != it_end; ++it)
      {
        const SqlAstNode *item= *it;
        if ((item= item->subitem(sql::_sp_chistic))
            && (item= item->find_subseq(sql::_COMMENT_SYM, sql::_TEXT_STRING_sys)))
        {
          SET_STR_I(obj->comment, item)
          break;
        }
      }
    }
  }

  // sql_statement
  set_obj_sql_def(obj);

  _shape_routine(obj);
  do_transactable_list_insert(schema->routines(), obj);
  log_db_obj_created(schema, obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_index_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->find_subseq(sql::_INDEX_SYM, sql::_ident))
    return pr_irrelevant;

  // table
  db_mysql_SchemaRef schema;
  db_mysql_TableRef table;
  {
    // table + schema
    std::string table_name= process_obj_full_name_item(tree->subitem(sql::_table_ident), &schema);

    table= find_named_object_in_list(schema->tables(), table_name, _case_sensitive_identifiers);
    if (!table.is_valid())
    {
      {
        std::string msg_text;
        msg_text
          .append("Table `")
          .append(*schema->name())
          .append("`.`")
          .append(table_name)
          .append("` not found. Stub was created.");
        add_log_message(msg_text, 1);
      }
      create_stub_table(schema, table, table_name);
    }
  }

  const SqlAstNode *ident_item= tree->find_subseq(sql::_INDEX_SYM, sql::_ident);
  std::string obj_name= (ident_item) ? ident_item->value() : "";
  step_progress(obj_name);
  db_mysql_IndexRef obj= create_or_find_named_obj(table->indices(), obj_name, false, schema, table); // mysql indexes are always case-insensitive

  set_obj_name(obj, obj_name);

  // is primary
  // primary index can't be created by separate 'CREATE' statement (v5.1)
  //obj->isPrimary(0);

  // deferability
  // there is nothing relevant in separate 'CREATE' statement (v5.1)
  //obj->deferability(0);

  // index type
  {
    static sql::symbol names[]= { sql::_opt_unique, sql::_fulltext, sql::_spatial };
    std::string index_type;
    if (const SqlAstNode *item= tree->search_by_names(names, ARR_CAPACITY(names)))
    {
      index_type= item->restore_sql_text(_sql_statement);
      if (item->name_equals(sql::_opt_unique))
        obj->unique(1);
    }
    else
    {
      index_type= "INDEX";
    }
    obj->indexType(shape_index_type(index_type));
  }

  // index kind
  process_index_kind_item(obj, tree->subitem(sql::_key_alg, sql::_key_using_alg, sql::_btree_or_rtree));

  // columns
  {
    const SqlAstNode *items= tree->subitem(sql::_key_list);
    if (items)
    {
      db_mysql_IndexColumnRef index_column(_grt);
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_key_part))
        {
          index_column= db_mysql_IndexColumnRef(_grt);
          index_column->owner(obj);

          // referred column
          const SqlAstNode *name_item= item->subitem(sql::_ident);
          std::string column_name= (name_item ? name_item->value() : "");
          db_mysql_ColumnRef column= find_named_object_in_list(table->columns(), column_name, false); // mysql columns are always case-insensitive
          if (!column.is_valid())
          {
            std::string err_text;
            err_text
              .append("Column `")
              .append(column_name)
              .append("` not found");
            throw Parse_exception(err_text);
          }
          index_column->referencedColumn(column);
          
          // stored function
          // there is nothing relevant in separate 'CREATE' statement (v5.1)
          //index_column.storedFunction("");

          // stored function
          // there is nothing relevant in separate 'CREATE' statement (v5.1)
          //index_column.comment("");

          // length
          SET_INT_SI(index_column->columnLength, item, sql::_NUM);

          obj->columns().insert(index_column);
        }
        else if (item->name_equals(sql::_order_dir))
        {
          // order direction
          index_column->descend(are_strings_eq_ci("DESC", item->value()) ? 1 : 0);
        }
      }
    }
  }

  // options
  process_index_options_item(obj, tree);

  _shape_index(obj);
  do_transactable_list_insert(table->indices(), obj);
  log_db_obj_created(schema, table, obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_logfile_group_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_CREATE, sql::_LOGFILE_SYM, sql::_GROUP_SYM))
    return pr_irrelevant;

  const SqlAstNode *info_item= tree->subitem(sql::_logfile_group_info);
  const SqlAstNode *ident_item= info_item->subitem(sql::_logfile_group_name);
  std::string obj_name= (ident_item) ? ident_item->value() : "";
  step_progress(obj_name);
  db_mysql_LogFileGroupRef obj= create_or_find_named_obj(_catalog->logFileGroups(), obj_name, _case_sensitive_identifiers);

  set_obj_name(obj, obj_name);

  // undofile
  SET_STR_SI(obj->undoFile,
    info_item, sql::_add_log_file, sql::_lg_undofile, sql::_TEXT_STRING_sys);

  // options
  {
    const SqlAstNode *items= info_item->subitem(sql::_logfile_group_option_list, sql::_logfile_group_options);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_logfile_group_option))
        {
          const SqlAstNode *aux_item;
          if ((aux_item= item->subitem(sql::_opt_ts_initial_size)))
            SET_INT_SI(obj->initialSize, aux_item, sql::_size_number)
          else if ((aux_item= item->subitem(sql::_opt_ts_undo_buffer_size)))
            SET_INT_SI(obj->undoBufferSize, aux_item, sql::_size_number)
          else if ((aux_item= item->subitem(sql::_opt_ts_engine)))
            SET_STR_SI(obj->engine, aux_item, sql::_storage_engines)
        }
      }
    }
  }

  _shape_logfile_group(obj);
  do_transactable_list_insert(_catalog->logFileGroups(), obj);
  log_db_obj_created(obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_tablespace_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_CREATE, sql::_TABLESPACE))
    return pr_irrelevant;

  const SqlAstNode *info_item= tree->subitem(sql::_tablespace_info);
  const SqlAstNode *ident_item= info_item->subitem(sql::_tablespace_name);
  std::string obj_name= (ident_item) ? ident_item->value() : "";
  step_progress(obj_name);
  db_mysql_TablespaceRef obj= create_or_find_named_obj(_catalog->tablespaces(), obj_name, _case_sensitive_identifiers);

  set_obj_name(obj, obj_name);

  // datafile
  SET_STR_SI(obj->dataFile,
    info_item, sql::_ts_datafile, sql::_TEXT_STRING_sys);

  // logfile group
  {
    std::string logfile_group_name= get_str_attr_from_subitem(info_item, sql::_opt_logfile_group_name, sql::_ident);
    db_mysql_LogFileGroupRef logfile_group= find_named_object_in_list(_catalog->logFileGroups(), logfile_group_name, _case_sensitive_identifiers);
    if (!logfile_group.is_valid())
    {
      std::string err_text;
      err_text
        .append("Log file group `")
        .append(logfile_group_name)
        .append("` not found");
      throw Parse_exception(err_text);
    }
    obj->logFileGroup(logfile_group);
  }

  // options
  {
    const SqlAstNode *items= info_item->subitem(sql::_tablespace_option_list, sql::_tablespace_options);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_tablespace_option))
        {
          const SqlAstNode *aux_item;
          if ((aux_item= item->subitem(sql::_opt_ts_initial_size)))
            SET_INT_SI(obj->initialSize, aux_item, sql::_size_number)
          else if ((aux_item= item->subitem(sql::_opt_ts_extent_size)))
            SET_INT_SI(obj->extentSize, aux_item, sql::_size_number)
          else if ((aux_item= item->subitem(sql::_opt_ts_engine)))
            SET_STR_SI(obj->engine, aux_item, sql::_storage_engines)
        }
      }
    }
  }

  _shape_tablespace(obj);
  do_transactable_list_insert(_catalog->tablespaces(), obj);
  log_db_obj_created(obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_server_link_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_CREATE, sql::_server_def))
    return pr_irrelevant;

  const SqlAstNode *server_def_item= tree->subitem(sql::_server_def);
  const SqlAstNode *ident_item= server_def_item->find_subseq(sql::_SERVER_SYM, sql::_ident_or_text);
  std::string obj_name= (ident_item) ? ident_item->value() : "";
  step_progress(obj_name);
  db_mysql_ServerLinkRef obj= create_or_find_named_obj(_catalog->serverLinks(), obj_name, _case_sensitive_identifiers);

  set_obj_name(obj, obj_name);

  // foreign data wrapper
  SET_STR_I(obj->wrapperName,
    server_def_item->find_subseq(sql::_FOREIGN, sql::_DATA_SYM, sql::_WRAPPER_SYM, sql::_ident_or_text))

  // options
  {
    const SqlAstNode *items= server_def_item->subitem(sql::_server_options_list);
    if (items)
    {
      for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
      {
        const SqlAstNode *item= *it;
        if (item->name_equals(sql::_server_option))
        {
          if (item->subseq(sql::_HOST_SYM))
            SET_STR_SI(obj->host, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_DATABASE))
            SET_STR_SI(obj->schema, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_USER))
            SET_STR_SI(obj->user, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_PASSWORD))
            SET_STR_SI(obj->password, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_SOCKET_SYM))
            SET_STR_SI(obj->socket, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_OWNER_SYM))
            SET_STR_SI(obj->ownerUser, item, sql::_TEXT_STRING_sys)
          else if (item->subseq(sql::_PORT_SYM))
            SET_STR_SI(obj->port, item, sql::_ulong_num)
        }
      }
    }
  }

  _shape_serverlink(obj);
  do_transactable_list_insert(_catalog->serverLinks(), obj);
  log_db_obj_created(obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_trigger_statement(const SqlAstNode *tree)
{
  const SqlAstNode *trigger_tail= NULL;
  {
    static sql::symbol path1[]= { sql::_view_or_trigger_or_sp_or_event, sql::_definer_tail, sql::_ };
    static sql::symbol path2[]= { sql::_view_or_trigger_or_sp_or_event, sql::_no_definer_tail, sql::_ };
    static sql::symbol * paths[]= { path1, path2 };

    trigger_tail= tree->search_by_paths(paths, ARR_CAPACITY(paths));
    if (trigger_tail)
      trigger_tail= trigger_tail->subitem(sql::_trigger_tail);
  }

  // check if statement is relevant
  if (!trigger_tail || !trigger_tail->subseq(sql::_TRIGGER_SYM))
    return pr_irrelevant;

  // table + schema
  db_mysql_SchemaRef schema;
  db_mysql_TableRef table;
  {
    const SqlAstNode *table_ident= trigger_tail->subitem(sql::_table_ident);
    std::string table_name;
    {
      std::string schema_name;
      Mysql_sql_parser_base::process_obj_full_name_item(table_ident, schema_name, table_name);
    }

    if (_triggers_owner_table.is_valid())
    {
      schema= db_mysql_SchemaRef::cast_from(_triggers_owner_table->owner());
      table= _triggers_owner_table;
    }
    else
    {
      process_obj_full_name_item(table_ident, &schema);
      table= find_named_object_in_list(schema->tables(), table_name, _case_sensitive_identifiers);
    }

    if (!table.is_valid())
    {
      {
        std::string msg_text;
        msg_text
          .append("Table `")
          .append(*schema->name())
          .append("`.`")
          .append(table_name)
          .append("` not found. Stub was created.");
        add_log_message(msg_text, 1);
      }
      create_stub_table(schema, table, table_name);
    }
  }

  const SqlAstNode *ident_item= trigger_tail->subitem(sql::_sp_name);
  std::string obj_name= process_obj_full_name_item(ident_item, NULL);
  step_progress(obj_name);
  db_mysql_TriggerRef obj= create_or_find_named_obj(table->triggers(), obj_name, _case_sensitive_identifiers, schema, table);

  // name
  std::string name= process_obj_full_name_item(ident_item, NULL);
  if (obj.is_valid())
    set_obj_name(obj, name);

  const SqlAstNode *body_tail = trigger_tail->subitem(sql::_sp_proc_stmt);
  if (body_tail)
    obj->sqlBody(body_tail->restore_sql_text(_sql_statement));

  // definer
  SET_SQL_SI(obj->definer, tree, sql::_view_or_trigger_or_sp_or_event, sql::_definer, sql::_user)

  // timing
  SET_STR_SI(obj->timing, trigger_tail, sql::_trg_action_time)

  // event
  SET_STR_SI(obj->event, trigger_tail, sql::_trg_event)

  /*
  // orientation
  if (trigger_tail->find_subseq(sql::_FOR_SYM, sql::_EACH_SYM, sql::_ROW_SYM))
    obj->orientation("ROW");
    */

  // enabled
  obj->enabled(1);

  // sql_statement
  set_obj_sql_def(obj);

  _shape_trigger(obj);
  do_transactable_list_insert(table->triggers(), obj);
  log_db_obj_created(schema, table, obj);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_create_schema_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_CREATE, sql::_DATABASE))
    return pr_irrelevant;

  const SqlAstNode *item= tree->subitem(sql::_ident);

  if (!item)
    throw Parse_exception("Invalid 'create database' statement");

  step_progress(item->value());

  db_mysql_SchemaRef schema= ensure_schema_created(item->value(), true);
  if (schema.is_valid())
  {
    // options
    {
      const SqlAstNode *items= tree->subitem(sql::_opt_create_database_options, sql::_create_database_options);
      if (items)
      {
        for (SqlAstNode::SubItemList::const_iterator it= items->subitems()->begin(); it != items->subitems()->end(); ++it)
        {
          const SqlAstNode *item= *it;
          if (item->name_equals(sql::_create_database_option))
          {
            if (const SqlAstNode *aux_item= item->subitem(sql::_default_charset, sql::_charset_name_or_default))
              SET_STR_I(cs_collation_setter(db_SchemaRef(schema), db_CatalogRef(_catalog), true).charset_name, aux_item)
            else if (const SqlAstNode *aux_item= item->subitem(sql::_default_collation, sql::_collation_name_or_default))
              SET_STR_I(cs_collation_setter(db_SchemaRef(schema), db_CatalogRef(_catalog), true).collation_name, aux_item)
          }
        }
      }
    }
  }

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_schema_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_DROP, sql::_DATABASE))
    return pr_irrelevant;

  bool if_exists= (NULL != tree->subitem(sql::_if_exists));

  const SqlAstNode *item= tree->subitem(sql::_ident);

  if (!item)
    throw Parse_exception("Invalid 'create database' statement");

  std::string obj_name= item->value();
  step_progress(obj_name);
  drop_obj(_catalog->schemata(), obj_name, if_exists);

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_table_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subitem(sql::_table_or_tables))
    return pr_irrelevant;

  bool if_exists= (NULL != tree->subitem(sql::_if_exists));

  db_mysql_SchemaRef schema;
  const SqlAstNode *table_list= tree->subitem(sql::_table_list);
  for (SqlAstNode::SubItemList::const_iterator it= table_list->subitems()->begin(), it_end= table_list->subitems()->end();
    it != it_end; ++it)
  {
    const SqlAstNode *item= *it;
    if (!item->name_equals(sql::_table_name))
      continue;
    item= item->subitem(sql::_table_ident);
    std::string obj_name= process_obj_full_name_item(item, &schema);
    step_progress(obj_name);
    drop_obj(schema->tables(), obj_name, if_exists, schema);
  }

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_view_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_DROP, sql::_VIEW_SYM))
    return pr_irrelevant;

  bool if_exists= (NULL != tree->subitem(sql::_if_exists));

  db_mysql_SchemaRef schema;
  const SqlAstNode *table_list= tree->subitem(sql::_table_list);
  for (SqlAstNode::SubItemList::const_iterator it= table_list->subitems()->begin(), it_end= table_list->subitems()->end();
    it != it_end; ++it)
  {
    const SqlAstNode *item= *it;
    if (!item->name_equals(sql::_table_name))
      continue;
    item= item->subitem(sql::_table_ident);
    std::string obj_name= process_obj_full_name_item(item, &schema);
    step_progress(obj_name);
    drop_obj(schema->views(), obj_name, if_exists, schema);
  }

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_routine_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_DROP, sql::_FUNCTION_SYM) || !tree->subseq(sql::_DROP, sql::_PROCEDURE_SYM))
    return pr_irrelevant;
/*
  bool if_exists= (NULL != tree->subitem(sql::_if_exists));

  db_mysql_SchemaRef schema;
  const SqlAstNode *ident_item= tree->subitem(sql::_sp_name);
  std::string obj_name= process_obj_full_name_item(ident_item, &schema);
  step_progress(obj_name);

  drop_obj(schema->routines(), obj_name, if_exists, &schema);
*/
  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_drop_trigger_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_DROP, sql::_TRIGGER_SYM))
    return pr_irrelevant;
/*
  bool if_exists= (NULL != tree->subitem(sql::_if_exists));

  db_mysql_SchemaRef schema;
  const SqlAstNode *ident_item= tree->subitem(sql::_sp_name);
  std::string obj_name= process_obj_full_name_item(ident_item, &schema);
  step_progress(obj_name);

  //! TODO:
  //! search in tables for trigger to be dropped.
  //! delete trigger object from app tree.
  drop_obj(schema->routines(), obj_name, if_exists, &schema, &table);
*/    
  return pr_processed;
}


template <typename T>
bool Mysql_sql_parser::drop_obj(
  grt::ListRef<T> obj_list,
  const std::string &obj_name,
  bool if_exists,
  GrtNamedObjectRef owner,
  GrtNamedObjectRef grand_owner)
{
  grt::Ref<T> obj= find_named_object_in_list(obj_list, obj_name, _case_sensitive_identifiers);
  if (obj.is_valid())
  {
    GrtNamedObjectRef obj1= grand_owner;
    GrtNamedObjectRef obj2= owner;
    GrtNamedObjectRef obj3= obj;

    // order from top level container to leaf, filling rest with NULL
    if (!obj1.is_valid()) std::swap(obj1, obj2);
    if (!obj2.is_valid()) std::swap(obj2, obj3);
    if (!obj1.is_valid()) std::swap(obj1, obj2);

    log_db_obj_dropped(obj1, obj2, obj3);
    obj_list.remove_value(obj);
    return true;
  }
  return false;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_alter_table_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  const SqlAstNode *alter_list= tree->subitem(sql::_alter_commands, sql::_alter_list);
  if (!alter_list)
    return pr_irrelevant;

  // table + schema
  db_mysql_SchemaRef schema;
  db_mysql_TableRef obj;
  {
    std::string table_name= process_obj_full_name_item(tree->subitem(sql::_table_ident), &schema);
    obj= find_named_object_in_list(schema->tables(), table_name, _case_sensitive_identifiers);
  }
  if (!obj.is_valid())
    return pr_irrelevant;

  for (SqlAstNode::SubItemList::const_iterator it= alter_list->subitems()->begin(), it_end= alter_list->subitems()->end();
    it != it_end; ++it)
  {
    const SqlAstNode *alter_list_item= *it;
    if (alter_list_item->name_equals(sql::_alter_list_item))
    {
      const SqlAstNode *item= (*it)->subitem(sql::_key_def);
      if (item)
      {
        if (item->find_subseq(sql::_FOREIGN, sql::_KEY_SYM))
          ENSURE(process_fk_item(item, obj),
            "Table `" + *obj->name() + "` : " + e.what())
        else if (item->subitem(sql::_key_list))
          ENSURE(process_index_item(item, obj),
            "Table `" + *obj->name() + "` : " + e.what())
      }
    }
  }

  return pr_processed;
}


Mysql_sql_parser::Parse_result
Mysql_sql_parser::process_use_schema_statement(const SqlAstNode *tree)
{
  // check if statement is relevant
  if (!tree->subseq(sql::_USE_SYM))
    return pr_irrelevant;

  const SqlAstNode *item= tree->subitem(sql::_ident);

  if (!item)
    throw Parse_exception("Invalid 'use' statement");

  set_active_schema(item->value());

  return pr_processed;
}


void Mysql_sql_parser::blame_existing_obj(bool critical, const GrtNamedObjectRef &obj, const GrtNamedObjectRef &container1, const GrtNamedObjectRef &container2)
{
  if (_reuse_existing_objects)
    return;

  std::string err_text;
  err_text
    .append("Previously created ")
    .append(obj.get_metaclass()->get_attribute("caption"))
    .append(" `");
  if (container1.is_valid())
  {
    err_text
      .append(*container1->name())
      .append("`.`");
  }
  if (container2.is_valid())
  {
    err_text
      .append(*container2->name())
      .append("`.`");
  }
  err_text
    .append(*obj->name())
    .append("` was found. Statement ignored.");
  if (critical)
    throw Parse_exception(err_text);
  else
    add_log_message(err_text, 1);
}


template <typename T>
grt::Ref<T> Mysql_sql_parser::create_or_find_named_obj(const grt::ListRef<T>& obj_list, const std::string &obj_name, bool case_sensitive, const GrtNamedObjectRef &container1, const GrtNamedObjectRef &container2)
{
  std::string time= base::fmttime(0, DATETIME_FMT);

  grt::Ref<T> obj;
  if (grt::Ref<T>::can_wrap(get_active_object()))
  {
    obj= grt::Ref<T>::cast_from(get_active_object());
    _reusing_existing_obj= true;
  }
  else
  {
    obj= find_named_object_in_list(obj_list, obj_name, case_sensitive);
    if (obj.is_valid())
    {
      blame_existing_obj(true, obj, container1, container2);
      _reusing_existing_obj= true;
    }
    else
    {
      obj= grt::Ref<T>(_grt);
      obj->owner(container2.is_valid() ? container2 : (container1.is_valid() ? container1 : GrtNamedObjectRef(_catalog)));
      try { obj.set_member("createDate", StringRef(time)); } catch (std::exception&) {}
    }
  }

  try { obj.set_member("lastChangeDate", StringRef(time)); } catch (std::exception&) {}

  return obj;
}


template <typename T>
grt::Ref<T> Mysql_sql_parser::create_or_find_named_routine(const grt::ListRef<T>& obj_list, const std::string &obj_name, bool case_sensitive, const std::string &routine_type, const GrtNamedObjectRef &container1, const GrtNamedObjectRef &container2)
{
  std::string time= base::fmttime(0, DATETIME_FMT);
  
  grt::Ref<T> obj;
  if (grt::Ref<T>::can_wrap(get_active_object()))
  {
    obj= grt::Ref<T>::cast_from(get_active_object());
    _reusing_existing_obj= true;
  }
  else
  {
    for (size_t c = obj_list.count(), i = 0; i < c; i++)
    {
      grt::Ref<T> item(obj_list[i]);
      if (*item->routineType() == routine_type)
      {
        if(base::string_compare(item->name(), obj_name, case_sensitive) == 0)
        {
          obj = item;
          break;
        }
      }
    }
    if (obj.is_valid())
    {
      blame_existing_obj(true, obj, container1, container2);
      _reusing_existing_obj= true;
    }
    else
    {
      obj= grt::Ref<T>(_grt);
      obj->owner(container2.is_valid() ? container2 : (container1.is_valid() ? container1 : GrtNamedObjectRef(_catalog)));
      try { obj.set_member("createDate", StringRef(time)); } catch (std::exception&) {}
    }
  }
  
  try { obj.set_member("lastChangeDate", StringRef(time)); } catch (std::exception&) {}
  
  return obj;
}