Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExportSql.class.php « export « plugins « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5dcfba289918d3dfed5e5f99c15d1fb7844d699a (plain)
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
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Set of functions used to build SQL dumps of tables
 *
 * @package    PhpMyAdmin-Export
 * @subpackage SQL
 */
if (! defined('PHPMYADMIN')) {
    exit;
}

/* Get the export interface */
require_once 'libraries/plugins/ExportPlugin.class.php';

/**
 * Handles the export for the SQL class
 *
 * @package    PhpMyAdmin-Export
 * @subpackage SQL
 */
class ExportSql extends ExportPlugin
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->setProperties();

        // Avoids undefined variables, use NULL so isset() returns false
        if (! isset($GLOBALS['sql_backquotes'])) {
            $GLOBALS['sql_backquotes'] = null;
        }
    }

    /**
     * Sets the export SQL properties
     *
     * @return void
     */
    protected function setProperties()
    {
        global $plugin_param;

        $hide_sql = false;
        $hide_structure = false;
        if ($plugin_param['export_type'] == 'table'
            && ! $plugin_param['single_table']
        ) {
            $hide_structure = true;
            $hide_sql = true;
        }

        if (! $hide_sql) {
            $props = 'libraries/properties/';
            include_once "$props/plugins/ExportPluginProperties.class.php";
            include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
            include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
            include_once "$props/options/groups/OptionsPropertySubgroup.class.php";
            include_once "$props/options/items/BoolPropertyItem.class.php";
            include_once "$props/options/items/MessageOnlyPropertyItem.class.php";
            include_once "$props/options/items/RadioPropertyItem.class.php";
            include_once "$props/options/items/SelectPropertyItem.class.php";
            include_once "$props/options/items/TextPropertyItem.class.php";
            include_once "$props/options/items/NumberPropertyItem.class.php";

            $exportPluginProperties = new ExportPluginProperties();
            $exportPluginProperties->setText('SQL');
            $exportPluginProperties->setExtension('sql');
            $exportPluginProperties->setMimeType('text/x-sql');
            $exportPluginProperties->setOptionsText(__('Options'));

            // create the root group that will be the options field for
            // $exportPluginProperties
            // this will be shown as "Format specific options"
            $exportSpecificOptions = new OptionsPropertyRootGroup();
            $exportSpecificOptions->setName("Format Specific Options");

            // general options main group
            $generalOptions = new OptionsPropertyMainGroup();
            $generalOptions->setName("general_opts");

            // comments
            $subgroup = new OptionsPropertySubgroup();
            $subgroup->setName("include_comments");
            $leaf = new BoolPropertyItem();
            $leaf->setName('include_comments');
            $leaf->setText(
                __(
                    'Display comments <i>(includes info such as export'
                    . ' timestamp, PHP version, and server version)</i>'
                )
            );
            $subgroup->setSubgroupHeader($leaf);

            $leaf = new TextPropertyItem();
            $leaf->setName('header_comment');
            $leaf->setText(
                __('Additional custom header comment (\n splits lines):')
            );
            $subgroup->addProperty($leaf);
            $leaf = new BoolPropertyItem();
            $leaf->setName('dates');
            $leaf->setText(
                __(
                    'Include a timestamp of when databases were created, last'
                    . ' updated, and last checked'
                )
            );
            $subgroup->addProperty($leaf);
            if (! empty($GLOBALS['cfgRelation']['relation'])) {
                $leaf = new BoolPropertyItem();
                $leaf->setName('relation');
                $leaf->setText(__('Display foreign key relationships'));
                $subgroup->addProperty($leaf);
            }
            if (! empty($GLOBALS['cfgRelation']['mimework'])) {
                $leaf = new BoolPropertyItem();
                $leaf->setName('mime');
                $leaf->setText(__('Display MIME types'));
                $subgroup->addProperty($leaf);
            }
            $generalOptions->addProperty($subgroup);

            // enclose in a transaction
            $leaf = new BoolPropertyItem();
            $leaf->setName("use_transaction");
            $leaf->setText(__('Enclose export in a transaction'));
            $leaf->setDoc(
                array(
                    'programs',
                    'mysqldump',
                    'option_mysqldump_single-transaction'
                )
            );
            $generalOptions->addProperty($leaf);

            // disable foreign key checks
            $leaf = new BoolPropertyItem();
            $leaf->setName("disable_fk");
            $leaf->setText(__('Disable foreign key checks'));
            $leaf->setDoc(
                array(
                    'manual_MySQL_Database_Administration',
                    'server-system-variables',
                    'sysvar_foreign_key_checks'
                )
            );
            $generalOptions->addProperty($leaf);

            // export views as tables
            $leaf = new BoolPropertyItem();
            $leaf->setName("views_as_tables");
            $leaf->setText(__('Export views as tables'));
            $generalOptions->addProperty($leaf);

            // compatibility maximization
            $compats = $GLOBALS['dbi']->getCompatibilities();
            if (count($compats) > 0) {
                $values = array();
                foreach ($compats as $val) {
                    $values[$val] = $val;
                }

                $leaf = new SelectPropertyItem();
                $leaf->setName("compatibility");
                $leaf->setText(
                    __(
                        'Database system or older MySQL server to maximize output'
                        . ' compatibility with:'
                    )
                );
                $leaf->setValues($values);
                $leaf->setDoc(
                    array(
                        'manual_MySQL_Database_Administration',
                        'Server_SQL_mode'
                    )
                );
                $generalOptions->addProperty($leaf);

                unset($values);
            }

            // server export options
            if ($plugin_param['export_type'] == 'server') {
                $leaf = new BoolPropertyItem();
                $leaf->setName("drop_database");
                $leaf->setText(
                    sprintf(__('Add %s statement'), '<code>DROP DATABASE</code>')
                );
                $generalOptions->addProperty($leaf);
            }

            // what to dump (structure/data/both)
            $subgroup = new OptionsPropertySubgroup();
            $subgroup->setName("dump_table");
            $subgroup->setText("Dump table");
            $leaf = new RadioPropertyItem();
            $leaf->setName('structure_or_data');
            $leaf->setValues(
                array(
                    'structure' => __('structure'),
                    'data' => __('data'),
                    'structure_and_data' => __('structure and data')
                )
            );
            $subgroup->setSubgroupHeader($leaf);
            $generalOptions->addProperty($subgroup);

            // add the main group to the root group
            $exportSpecificOptions->addProperty($generalOptions);

            // structure options main group
            if (! $hide_structure) {
                $structureOptions = new OptionsPropertyMainGroup();
                $structureOptions->setName("structure");
                $structureOptions->setText(__('Object creation options'));
                $structureOptions->setForce('data');

                // begin SQL Statements
                $subgroup = new OptionsPropertySubgroup();
                $leaf = new MessageOnlyPropertyItem();
                $leaf->setName('add_statements');
                $leaf->setText(__('Add statements:'));
                $subgroup->setSubgroupHeader($leaf);

                if ($plugin_param['export_type'] != 'table') {
                    $leaf = new BoolPropertyItem();
                    $leaf->setName('create_database');
                    $create_clause = '<code>CREATE DATABASE / USE</code>';
                    $leaf->setText(sprintf(__('Add %s statement'), $create_clause));
                    $subgroup->addProperty($leaf);
                }

                if ($plugin_param['export_type'] == 'table') {
                    if (PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])) {
                        $drop_clause = '<code>DROP VIEW</code>';
                    } else {
                        $drop_clause = '<code>DROP TABLE</code>';
                    }
                } else {
                    if (PMA_DRIZZLE) {
                        $drop_clause = '<code>DROP TABLE</code>';
                    } else {
                        $drop_clause = '<code>DROP TABLE / VIEW / PROCEDURE'
                            . ' / FUNCTION / EVENT</code>';
                    }
                }

                $drop_clause .= '<code> / TRIGGER</code>';

                $leaf = new BoolPropertyItem();
                $leaf->setName('drop_table');
                $leaf->setText(sprintf(__('Add %s statement'), $drop_clause));
                $subgroup->addProperty($leaf);

                // Add table structure option
                $leaf = new BoolPropertyItem();
                $leaf->setName('create_table');
                $leaf->setText(
                    sprintf(__('Add %s statement'), '<code>CREATE TABLE</code>')
                );
                $subgroup->addProperty($leaf);

                // Add view option
                $leaf = new BoolPropertyItem();
                $leaf->setName('create_view');
                $leaf->setText(
                    sprintf(__('Add %s statement'), '<code>CREATE VIEW</code>')
                );
                $subgroup->addProperty($leaf);

                // Drizzle doesn't support procedures and functions
                if (! PMA_DRIZZLE) {
                    $leaf = new BoolPropertyItem();
                    $leaf->setName('procedure_function');
                    $leaf->setText(
                        sprintf(
                            __('Add %s statement'),
                            '<code>CREATE PROCEDURE / FUNCTION / EVENT</code>'
                        )
                    );
                    $subgroup->addProperty($leaf);
                }

                // Add triggers option
                $leaf = new BoolPropertyItem();
                $leaf->setName('create_trigger');
                $leaf->setText(
                    sprintf(__('Add %s statement'), '<code>CREATE TRIGGER</code>')
                );
                $subgroup->addProperty($leaf);

                // begin CREATE TABLE statements
                $subgroup_create_table = new OptionsPropertySubgroup();
                $leaf = new BoolPropertyItem();
                $leaf->setName('create_table_statements');
                $leaf->setText(__('<code>CREATE TABLE</code> options:'));
                $subgroup_create_table->setSubgroupHeader($leaf);
                $leaf = new BoolPropertyItem();
                $leaf->setName('if_not_exists');
                $leaf->setText('<code>IF NOT EXISTS</code>');
                $subgroup_create_table->addProperty($leaf);
                $leaf = new BoolPropertyItem();
                $leaf->setName('auto_increment');
                $leaf->setText('<code>AUTO_INCREMENT</code>');
                $subgroup_create_table->addProperty($leaf);
                $subgroup->addProperty($subgroup_create_table);
                $structureOptions->addProperty($subgroup);

                $leaf = new BoolPropertyItem();
                $leaf->setName("backquotes");
                $leaf->setText(
                    __(
                        'Enclose table and column names with backquotes '
                        . '<i>(Protects column and table names formed with'
                        . ' special characters or keywords)</i>'
                    )
                );

                $structureOptions->addProperty($leaf);

                // add the main group to the root group
                $exportSpecificOptions->addProperty($structureOptions);
            }

            // begin Data options
            $dataOptions = new OptionsPropertyMainGroup();
            $dataOptions->setName("data");
            $dataOptions->setText(__('Data creation options'));
            $dataOptions->setForce('structure');
            $leaf = new BoolPropertyItem();
            $leaf->setName("truncate");
            $leaf->setText(__('Truncate table before insert'));
            $dataOptions->addProperty($leaf);

            // begin SQL Statements
            $subgroup = new OptionsPropertySubgroup();
            $leaf = new MessageOnlyPropertyItem();
            $leaf->setText(__('Instead of <code>INSERT</code> statements, use:'));
            $subgroup->setSubgroupHeader($leaf);
            // Not supported in Drizzle
            if (! PMA_DRIZZLE) {
                $leaf = new BoolPropertyItem();
                $leaf->setName("delayed");
                $leaf->setText(__('<code>INSERT DELAYED</code> statements'));
                $leaf->setDoc(
                    array(
                        'manual_MySQL_Database_Administration',
                        'insert_delayed'
                    )
                );
                $subgroup->addProperty($leaf);
            }
            $leaf = new BoolPropertyItem();
            $leaf->setName("ignore");
            $leaf->setText(__('<code>INSERT IGNORE</code> statements'));
            $leaf->setDoc(
                array(
                        'manual_MySQL_Database_Administration',
                        'insert'
                )
            );
            $subgroup->addProperty($leaf);
            $dataOptions->addProperty($subgroup);

            // Function to use when dumping dat
            $leaf = new SelectPropertyItem();
            $leaf->setName("type");
            $leaf->setText(__('Function to use when dumping data:'));
            $leaf->setValues(
                array(
                    'INSERT' => 'INSERT',
                    'UPDATE' => 'UPDATE',
                    'REPLACE' => 'REPLACE'
                )
            );
            $dataOptions->addProperty($leaf);

            /* Syntax to use when inserting data */
            $subgroup = new OptionsPropertySubgroup();
            $leaf = new MessageOnlyPropertyItem();
            $leaf->setText(__('Syntax to use when inserting data:'));
            $subgroup->setSubgroupHeader($leaf);
            $leaf = new RadioPropertyItem();
            $leaf->setName("insert_syntax");
            $leaf->setText(__('<code>INSERT IGNORE</code> statements'));
            $leaf->setValues(
                array(
                    'complete' => __(
                        'include column names in every <code>INSERT</code> statement'
                        . ' <br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
                        . ' tbl_name (col_A,col_B,col_C) VALUES (1,2,3)</code>'
                    ),
                    'extended' => __(
                        'insert multiple rows in every <code>INSERT</code> statement'
                        . '<br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
                        . ' tbl_name VALUES (1,2,3), (4,5,6), (7,8,9)</code>'
                    ),
                    'both' => __(
                        'both of the above<br /> &nbsp; &nbsp; &nbsp; Example:'
                        . ' <code>INSERT INTO tbl_name (col_A,col_B) VALUES (1,2,3),'
                        . ' (4,5,6), (7,8,9)</code>'
                    ),
                    'none' => __(
                        'neither of the above<br /> &nbsp; &nbsp; &nbsp; Example:'
                        . ' <code>INSERT INTO tbl_name VALUES (1,2,3)</code>'
                    )
                )
            );
            $subgroup->addProperty($leaf);
            $dataOptions->addProperty($subgroup);

            // Max length of query
            $leaf = new NumberPropertyItem();
            $leaf->setName("max_query_size");
            $leaf->setText(__('Maximal length of created query'));
            $dataOptions->addProperty($leaf);

            // Dump binary columns in hexadecimal
            $leaf = new BoolPropertyItem();
            $leaf->setName("hex_for_binary");
            $leaf->setText(
                __(
                    'Dump binary columns in hexadecimal notation'
                    . ' <i>(for example, "abc" becomes 0x616263)</i>'
                )
            );
            $dataOptions->addProperty($leaf);

            // Drizzle works only with UTC timezone
            if (! PMA_DRIZZLE) {
                // Dump time in UTC
                $leaf = new BoolPropertyItem();
                $leaf->setName("utc_time");
                $leaf->setText(
                    __(
                        'Dump TIMESTAMP columns in UTC <i>(enables TIMESTAMP columns'
                        . ' to be dumped and reloaded between servers in different'
                        . ' time zones)</i>'
                    )
                );
                $dataOptions->addProperty($leaf);
            }

            // add the main group to the root group
            $exportSpecificOptions->addProperty($dataOptions);

            // set the options for the export plugin property item
            $exportPluginProperties->setOptions($exportSpecificOptions);
            $this->properties = $exportPluginProperties;
        }
    }

    /**
     * Exports routines (procedures and functions)
     *
     * @param string $db      Database
     * @param array  $aliases Aliases of db/table/columns
     *
     * @return bool Whether it succeeded
     */
    public function exportRoutines($db, $aliases = array())
    {
        global $crlf;

        $db_alias = $db;
        $this->initAlias($aliases, $db_alias);

        $text = '';
        $delimiter = '$$';

        $procedure_names = $GLOBALS['dbi']
            ->getProceduresOrFunctions($db, 'PROCEDURE');
        $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');

        if ($procedure_names || $function_names) {
            $text .= $crlf
                . 'DELIMITER ' . $delimiter . $crlf;
        }

        if ($procedure_names) {
            $text .=
                $this->_exportComment()
              . $this->_exportComment(__('Procedures'))
              . $this->_exportComment();
            $used_alias = false;
            $proc_query = '';
            foreach ($procedure_names as $procedure_name) {
                if (! empty($GLOBALS['sql_drop_table'])) {
                    $proc_query .= 'DROP PROCEDURE IF EXISTS '
                        . PMA_Util::backquote($procedure_name)
                        . $delimiter . $crlf;
                }
                $create_query = $GLOBALS['dbi']
                    ->getDefinition($db, 'PROCEDURE', $procedure_name);
                $create_query = $this->replaceWithAliases(
                    $create_query, $aliases, $db, '', $flag
                );
                // One warning per database
                if ($flag) {
                    $used_alias = true;
                }
                $proc_query .= $create_query . $delimiter . $crlf . $crlf;
            }
            if ($used_alias) {
                $text .= $this->_exportComment(
                    __(
                        'It appears your database uses procedures;'
                    )
                )
                . $this->_exportComment(
                    __('alias export may not work reliably in all cases.')
                )
                . $this->_exportComment();
            }
            $text .= $proc_query;
        }

        if ($function_names) {
            $text .=
                $this->_exportComment()
              . $this->_exportComment(__('Functions'))
              . $this->_exportComment();
            $used_alias = false;
            $function_query = '';
            foreach ($function_names as $function_name) {
                if (! empty($GLOBALS['sql_drop_table'])) {
                    $function_query .= 'DROP FUNCTION IF EXISTS '
                        . PMA_Util::backquote($function_name)
                        . $delimiter . $crlf;
                }
                $create_query = $GLOBALS['dbi']
                    ->getDefinition($db, 'FUNCTION', $function_name);
                $create_query = $this->replaceWithAliases(
                    $create_query, $aliases, $db, '', $flag
                );
                // One warning per database
                if ($flag) {
                    $used_alias = true;
                }
                $function_query .= $create_query . $delimiter . $crlf . $crlf;
            }
            if ($used_alias) {
                $text .= $this->_exportComment(
                    __('It appears your database uses functions;')
                )
                . $this->_exportComment(
                    __('alias export may not work reliably in all cases.')
                )
                . $this->_exportComment();
            }
            $text .= $function_query;
        }

        if ($procedure_names || $function_names) {
            $text .= 'DELIMITER ;' . $crlf;
        }

        if (! empty($text)) {
            return PMA_exportOutputHandler($text);
        } else {
            return false;
        }
    }

    /**
     * Possibly outputs comment
     *
     * @param string $text Text of comment
     *
     * @return string The formatted comment
     */
    private function _exportComment($text = '')
    {
        if (isset($GLOBALS['sql_include_comments'])
            && $GLOBALS['sql_include_comments']
        ) {
            // see http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html
            return '--' . (empty($text) ? '' : ' ') . $text . $GLOBALS['crlf'];
        } else {
            return '';
        }
    }

    /**
     * Possibly outputs CRLF
     *
     * @return string $crlf or nothing
     */
    private function _possibleCRLF()
    {
        if (isset($GLOBALS['sql_include_comments'])
            && $GLOBALS['sql_include_comments']
        ) {
            return $GLOBALS['crlf'];
        } else {
            return '';
        }
    }

    /**
     * Outputs export footer
     *
     * @return bool Whether it succeeded
     */
    public function exportFooter()
    {
        global $crlf, $mysql_charset_map;

        $foot = '';

        if (isset($GLOBALS['sql_disable_fk'])) {
            $foot .=  'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
        }

        if (isset($GLOBALS['sql_use_transaction'])) {
            $foot .=  'COMMIT;' . $crlf;
        }

        // restore connection settings
        $charset_of_file = isset($GLOBALS['charset_of_file'])
            ? $GLOBALS['charset_of_file'] : '';
        if (! empty($GLOBALS['asfile'])
            && isset($mysql_charset_map[$charset_of_file])
            && ! PMA_DRIZZLE
        ) {
            $foot .=  $crlf
                . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;'
                . $crlf
                . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;'
                . $crlf
                . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;'
                . $crlf;
        }

        /* Restore timezone */
        if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
            $GLOBALS['dbi']->query('SET time_zone = "' . $GLOBALS['old_tz'] . '"');
        }

        return PMA_exportOutputHandler($foot);
    }

    /**
     * Outputs export header. It is the first method to be called, so all
     * the required variables are initialized here.
     *
     * @return bool Whether it succeeded
     */
    public function exportHeader()
    {
        global $crlf, $cfg;
        global $mysql_charset_map;

        if (isset($GLOBALS['sql_compatibility'])) {
            $tmp_compat = $GLOBALS['sql_compatibility'];
            if ($tmp_compat == 'NONE') {
                $tmp_compat = '';
            }
            $GLOBALS['dbi']->tryQuery('SET SQL_MODE="' . $tmp_compat . '"');
            unset($tmp_compat);
        }
        $head  =  $this->_exportComment('phpMyAdmin SQL Dump')
               .  $this->_exportComment('version ' . PMA_VERSION)
               .  $this->_exportComment('http://www.phpmyadmin.net')
               .  $this->_exportComment();
        $host_string = __('Host:') . ' ' .  $cfg['Server']['host'];
        if (! empty($cfg['Server']['port'])) {
            $host_string .= ':' . $cfg['Server']['port'];
        }
        $head .= $this->_exportComment($host_string);
        $head .=
            $this->_exportComment(
                __('Generation Time:') . ' '
                .  PMA_Util::localisedDate()
            )
            .  $this->_exportComment(
                __('Server version:') . ' ' . PMA_MYSQL_STR_VERSION
            )
            .  $this->_exportComment(__('PHP Version:') . ' ' . phpversion())
            .  $this->_possibleCRLF();

        if (isset($GLOBALS['sql_header_comment'])
            && ! empty($GLOBALS['sql_header_comment'])
        ) {
            // '\n' is not a newline (like "\n" would be), it's the characters
            // backslash and n, as explained on the export interface
            $lines = explode('\n', $GLOBALS['sql_header_comment']);
            $head .= $this->_exportComment();
            foreach ($lines as $one_line) {
                $head .= $this->_exportComment($one_line);
            }
            $head .= $this->_exportComment();
        }

        if (isset($GLOBALS['sql_disable_fk'])) {
            $head .= 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
        }

        // We want exported AUTO_INCREMENT columns to have still same value,
        // do this only for recent MySQL exports
        if ((! isset($GLOBALS['sql_compatibility'])
            || $GLOBALS['sql_compatibility'] == 'NONE')
            && ! PMA_DRIZZLE
        ) {
            $head .= 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . $crlf;
        }

        if (isset($GLOBALS['sql_use_transaction'])) {
            $head .= 'SET AUTOCOMMIT = 0;' . $crlf
                   . 'START TRANSACTION;' . $crlf;
        }

        /* Change timezone if we should export timestamps in UTC */
        if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
            $head .= 'SET time_zone = "+00:00";' . $crlf;
            $GLOBALS['old_tz'] = $GLOBALS['dbi']
                ->fetchValue('SELECT @@session.time_zone');
            $GLOBALS['dbi']->query('SET time_zone = "+00:00"');
        }

        $head .= $this->_possibleCRLF();

        if (! empty($GLOBALS['asfile']) && ! PMA_DRIZZLE) {
            // we are saving as file, therefore we provide charset information
            // so that a utility like the mysql client can interpret
            // the file correctly
            if (isset($GLOBALS['charset_of_file'])
                && isset($mysql_charset_map[$GLOBALS['charset_of_file']])
            ) {
                // we got a charset from the export dialog
                $set_names = $mysql_charset_map[$GLOBALS['charset_of_file']];
            } else {
                // by default we use the connection charset
                $set_names = $mysql_charset_map['utf-8'];
            }
            $head .=  $crlf
                . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT='
                . '@@CHARACTER_SET_CLIENT */;' . $crlf
                . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS='
                . '@@CHARACTER_SET_RESULTS */;' . $crlf
                . '/*!40101 SET @OLD_COLLATION_CONNECTION='
                . '@@COLLATION_CONNECTION */;' . $crlf
                . '/*!40101 SET NAMES ' . $set_names . ' */;' . $crlf . $crlf;
        }

        return PMA_exportOutputHandler($head);
    }

    /**
     * Outputs CREATE DATABASE statement
     *
     * @param string $db       Database name
     * @param string $db_alias Aliases of db
     *
     * @return bool Whether it succeeded
     */
    public function exportDBCreate($db, $db_alias = '')
    {
        global $crlf;

        if (empty($db_alias)) {
            $db_alias = $db;
        }
        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }
        if (isset($GLOBALS['sql_drop_database'])) {
            if (! PMA_exportOutputHandler(
                'DROP DATABASE '
                . (isset($GLOBALS['sql_backquotes'])
                ? PMA_Util::backquoteCompat($db_alias, $compat) : $db_alias)
                . ';' . $crlf
            )) {
                return false;
            }
        }
        if (!isset($GLOBALS['sql_create_database'])) {
            return true;
        }

        $create_query = 'CREATE DATABASE IF NOT EXISTS '
            . (isset($GLOBALS['sql_backquotes'])
            ? PMA_Util::backquoteCompat($db_alias, $compat) : $db_alias);
        $collation = PMA_getDbCollation($db);
        if (PMA_DRIZZLE) {
            $create_query .= ' COLLATE ' . $collation;
        } else {
            /** @var PMA_String $pmaString */
            $pmaString = $GLOBALS['PMA_String'];

            if ($pmaString->strpos($collation, '_')) {
                $create_query .= ' DEFAULT CHARACTER SET '
                    . $pmaString->substr(
                        $collation,
                        0,
                        $pmaString->strpos($collation, '_')
                    )
                    . ' COLLATE ' . $collation;
            } else {
                $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
            }
        }
        $create_query .= ';' . $crlf;
        if (! PMA_exportOutputHandler($create_query)) {
            return false;
        }
        if (isset($GLOBALS['sql_backquotes'])
            && ((isset($GLOBALS['sql_compatibility'])
            && $GLOBALS['sql_compatibility'] == 'NONE')
            || PMA_DRIZZLE)
        ) {
            $result = PMA_exportOutputHandler(
                'USE ' . PMA_Util::backquoteCompat($db_alias, $compat)
                . ';' . $crlf
            );
        } else {
            $result = PMA_exportOutputHandler('USE ' . $db_alias . ';' . $crlf);
        }
        return $result;
    }

    /**
     * Outputs database header
     *
     * @param string $db       Database name
     * @param string $db_alias Alias of db
     *
     * @return bool Whether it succeeded
     */
    public function exportDBHeader($db, $db_alias = '')
    {
        if (empty($db_alias)) {
            $db_alias = $db;
        }
        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }
        $head = $this->_exportComment()
            . $this->_exportComment(
                __('Database:') . ' '
                . (isset($GLOBALS['sql_backquotes'])
                ? PMA_Util::backquoteCompat($db_alias, $compat)
                : '\'' . $db_alias . '\'')
            )
            . $this->_exportComment();
        return PMA_exportOutputHandler($head);
    }

    /**
     * Outputs database footer
     *
     * @param string $db Database name
     *
     * @return bool Whether it succeeded
     */
    public function exportDBFooter($db)
    {
        global $crlf;

        $result = true;

        //add indexes to the sql dump file
        if (isset($GLOBALS['sql_indexes'])) {
            $result = PMA_exportOutputHandler($GLOBALS['sql_indexes']);
            unset($GLOBALS['sql_indexes']);
        }
        //add auto increments to the sql dump file
        if (isset($GLOBALS['sql_auto_increments'])) {
            $result = PMA_exportOutputHandler($GLOBALS['sql_auto_increments']);
            unset($GLOBALS['sql_auto_increments']);
        }
        //add constraints to the sql dump file
        if (isset($GLOBALS['sql_constraints'])) {
            $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']);
            unset($GLOBALS['sql_constraints']);
        }

        if (($GLOBALS['sql_structure_or_data'] == 'structure'
            || $GLOBALS['sql_structure_or_data'] == 'structure_and_data')
            && isset($GLOBALS['sql_procedure_function'])
        ) {
            $text = '';
            $delimiter = '$$';

            $event_names = $GLOBALS['dbi']->fetchResult(
                'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE'
                . ' EVENT_SCHEMA= \''
                . PMA_Util::sqlAddSlashes($db, true)
                . '\';'
            );

            if ($event_names) {
                $text .= $crlf
                  . 'DELIMITER ' . $delimiter . $crlf;

                $text .=
                    $this->_exportComment()
                    . $this->_exportComment(__('Events'))
                    . $this->_exportComment();

                foreach ($event_names as $event_name) {
                    if (! empty($GLOBALS['sql_drop_table'])) {
                        $text .= 'DROP EVENT '
                            . PMA_Util::backquote($event_name)
                            . $delimiter . $crlf;
                    }
                    $text .= $GLOBALS['dbi']
                        ->getDefinition($db, 'EVENT', $event_name)
                        . $delimiter . $crlf . $crlf;
                }

                $text .= 'DELIMITER ;' . $crlf;
            }

            if (! empty($text)) {
                $result = PMA_exportOutputHandler($text);
            }
        }
        return $result;
    }

    /**
     * Returns a stand-in CREATE definition to resolve view dependencies
     *
     * @param string $db      the database name
     * @param string $view    the view name
     * @param string $crlf    the end of line sequence
     * @param array  $aliases Aliases of db/table/columns
     *
     * @return string resulting definition
     */
    public function getTableDefStandIn($db, $view, $crlf, $aliases = array())
    {
        $db_alias = $db;
        $view_alias = $view;
        $this->initAlias($aliases, $db_alias, $view_alias);
        $create_query = '';
        if (! empty($GLOBALS['sql_drop_table'])) {
            $create_query .= 'DROP VIEW IF EXISTS '
                . PMA_Util::backquote($view_alias)
                . ';' . $crlf;
        }

        $create_query .= 'CREATE TABLE ';

        if (isset($GLOBALS['sql_if_not_exists'])
            && $GLOBALS['sql_if_not_exists']
        ) {
            $create_query .= 'IF NOT EXISTS ';
        }
        $create_query .= PMA_Util::backquote($view_alias) . ' (' . $crlf;
        $tmp = array();
        $columns = $GLOBALS['dbi']->getColumnsFull($db, $view);
        foreach ($columns as $column_name => $definition) {
            $col_alias = $column_name;
            if (!empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) {
                $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias];
            }
            $tmp[] = PMA_Util::backquote($col_alias) . ' ' .
                $definition['Type'] . $crlf;
        }
        $create_query .= implode(',', $tmp) . ');' . $crlf;
        return($create_query);
    }

    /**
     * Returns CREATE definition that matches $view's structure
     *
     * @param string $db            the database name
     * @param string $view          the view name
     * @param string $crlf          the end of line sequence
     * @param bool   $add_semicolon whether to add semicolon and end-of-line at
     *                              the end
     * @param array  $aliases       Aliases of db/table/columns
     *
     * @return string resulting schema
     */
    private function _getTableDefForView(
        $db,
        $view,
        $crlf,
        $add_semicolon = true,
        $aliases = array()
    ) {
        $db_alias = $db;
        $view_alias = $view;
        $this->initAlias($aliases, $db_alias, $view_alias);
        $create_query = "CREATE TABLE";
        if (isset($GLOBALS['sql_if_not_exists'])) {
            $create_query .= " IF NOT EXISTS ";
        }
        $create_query .= PMA_Util::backquote($view_alias) . "(" . $crlf;

        $columns = $GLOBALS['dbi']->getColumns($db, $view, null, true);

        $firstCol = true;
        foreach ($columns as $column) {
            $col_alias = $column['Field'];
            if (!empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) {
                $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias];
            }
            $extracted_columnspec = PMA_Util::extractColumnSpec($column['Type']);

            if (! $firstCol) {
                $create_query .= "," . $crlf;
            }
            $create_query .= "    " . PMA_Util::backquote($col_alias);
            $create_query .= " " . $column['Type'];
            if ($extracted_columnspec['can_contain_collation']
                && ! empty($column['Collation'])
            ) {
                $create_query .= " COLLATE " . $column['Collation'];
            }
            if ($column['Null'] == 'NO') {
                $create_query .= " NOT NULL";
            }
            if (isset($column['Default'])) {
                 $create_query .= " DEFAULT '"
                     . PMA_Util::sqlAddSlashes($column['Default']) . "'";
            } else if ($column['Null'] == 'YES') {
                 $create_query .= " DEFAULT NULL";
            }
            if (! empty($column['Comment'])) {
                $create_query .= " COMMENT '"
                    . PMA_Util::sqlAddSlashes($column['Comment']) . "'";
            }
            $firstCol = false;
        }
        $create_query .= $crlf . ")" . ($add_semicolon ? ';' : '') . $crlf;

        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }
        if ($compat == 'MSSQL') {
            $create_query = $this->_makeCreateTableMSSQLCompatible(
                $create_query
            );
        }
        return $create_query;
    }

    /**
     * Returns $table's CREATE definition
     *
     * @param string $db                        the database name
     * @param string $table                     the table name
     * @param string $crlf                      the end of line sequence
     * @param string $error_url                 the url to go back in case
     *                                          of error
     * @param bool   $show_dates                whether to include creation/
     *                                          update/check dates
     * @param bool   $add_semicolon             whether to add semicolon and
     *                                          end-of-line at the end
     * @param bool   $view                      whether we're handling a view
     * @param bool   $update_indexes_increments whether we need to update
     *                                          two global variables
     * @param array  $aliases                   Aliases of db/table/columns
     *
     * @return string resulting schema
     */
    public function getTableDef(
        $db,
        $table,
        $crlf,
        $error_url,
        $show_dates = false,
        $add_semicolon = true,
        $view = false,
        $update_indexes_increments = true,
        $aliases = array()
    ) {
        global $sql_drop_table, $sql_backquotes, $sql_constraints,
            $sql_constraints_query, $sql_indexes, $sql_indexes_query,
            $sql_auto_increments,$sql_drop_foreign_keys;

        $db_alias = $db;
        $table_alias = $table;
        $this->initAlias($aliases, $db_alias, $table_alias);

        $schema_create = '';
        $auto_increment = '';
        $new_crlf = $crlf;

        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }

        // need to use PMA_DatabaseInterface::QUERY_STORE
        // with $GLOBALS['dbi']->numRows() in mysqli
        $result = $GLOBALS['dbi']->query(
            'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
            . ' WHERE Name = \'' . PMA_Util::sqlAddSlashes($table) . '\'',
            null,
            PMA_DatabaseInterface::QUERY_STORE
        );
        if ($result != false) {
            if ($GLOBALS['dbi']->numRows($result) > 0) {
                $tmpres = $GLOBALS['dbi']->fetchAssoc($result);
                if (PMA_DRIZZLE && $show_dates) {
                    // Drizzle doesn't give Create_time and Update_time in
                    // SHOW TABLE STATUS, add it
                    $sql ="SELECT
                            TABLE_CREATION_TIME AS Create_time,
                            TABLE_UPDATE_TIME AS Update_time
                        FROM data_dictionary.TABLES
                        WHERE TABLE_SCHEMA = '"
                        . PMA_Util::sqlAddSlashes($db) . "'
                          AND TABLE_NAME = '"
                        . PMA_Util::sqlAddSlashes($table) . "'";
                    $tmpres = array_merge(
                        $GLOBALS['dbi']->fetchSingleRow($sql), $tmpres
                    );
                }
                // Here we optionally add the AUTO_INCREMENT next value,
                // but starting with MySQL 5.0.24, the clause is already included
                // in SHOW CREATE TABLE so we'll remove it below
                // It's required for Drizzle because SHOW CREATE TABLE uses
                // the value from table's creation time
                if (isset($GLOBALS['sql_auto_increment'])
                    && ! empty($tmpres['Auto_increment'])
                ) {
                    $auto_increment .= ' AUTO_INCREMENT='
                        . $tmpres['Auto_increment'] . ' ';
                }

                if ($show_dates
                    && isset($tmpres['Create_time'])
                    && ! empty($tmpres['Create_time'])
                ) {
                    $schema_create .= $this->_exportComment(
                        __('Creation:') . ' '
                        . PMA_Util::localisedDate(
                            strtotime($tmpres['Create_time'])
                        )
                    );
                    $new_crlf = $this->_exportComment() . $crlf;
                }

                if ($show_dates
                    && isset($tmpres['Update_time'])
                    && ! empty($tmpres['Update_time'])
                ) {
                    $schema_create .= $this->_exportComment(
                        __('Last update:') . ' '
                        . PMA_Util::localisedDate(
                            strtotime($tmpres['Update_time'])
                        )
                    );
                    $new_crlf = $this->_exportComment() . $crlf;
                }

                if ($show_dates
                    && isset($tmpres['Check_time'])
                    && ! empty($tmpres['Check_time'])
                ) {
                    $schema_create .= $this->_exportComment(
                        __('Last check:') . ' '
                        . PMA_Util::localisedDate(
                            strtotime($tmpres['Check_time'])
                        )
                    );
                    $new_crlf = $this->_exportComment() . $crlf;
                }
            }
            $GLOBALS['dbi']->freeResult($result);
        }

        $schema_create .= $new_crlf;

        // no need to generate a DROP VIEW here, it was done earlier
        if (! empty($sql_drop_table) && ! PMA_Table::isView($db, $table)) {
            $schema_create .= 'DROP TABLE IF EXISTS '
                . PMA_Util::backquote($table_alias, $sql_backquotes) . ';'
                . $crlf;
        }

        // Complete table dump,
        // Whether to quote table and column names or not
        // Drizzle always quotes names
        if (! PMA_DRIZZLE) {
            if ($sql_backquotes) {
                $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 1');
            } else {
                $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 0');
            }
        }

        // I don't see the reason why this unbuffered query could cause problems,
        // because SHOW CREATE TABLE returns only one row, and we free the
        // results below. Nonetheless, we got 2 user reports about this
        // (see bug 1562533) so I removed the unbuffered mode.
        // $result = $GLOBALS['dbi']->query('SHOW CREATE TABLE ' . backquote($db)
        // . '.' . backquote($table), null, PMA_DatabaseInterface::QUERY_UNBUFFERED);
        //
        // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not
        // produce a displayable result for the default value of a BIT
        // column, nor does the mysqldump command. See MySQL bug 35796
        $result = $GLOBALS['dbi']->tryQuery(
            'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
            . PMA_Util::backquote($table)
        );
        // an error can happen, for example the table is crashed
        $tmp_error = $GLOBALS['dbi']->getError();
        if ($tmp_error) {
            return $this->_exportComment(__('in use') . '(' . $tmp_error . ')');
        }

        /** @var PMA_String $pmaString */
        $pmaString = $GLOBALS['PMA_String'];

        $warning = '';
        if ($result != false && ($row = $GLOBALS['dbi']->fetchRow($result))) {
            $create_query = $row[1];
            unset($row);
            // Convert end of line chars to one that we want (note that MySQL
            // doesn't return query it will accept in all cases)
            if ($pmaString->strpos($create_query, "(\r\n ")) {
                $create_query = str_replace("\r\n", $crlf, $create_query);
            } elseif ($pmaString->strpos($create_query, "(\n ")) {
                $create_query = str_replace("\n", $crlf, $create_query);
            } elseif ($pmaString->strpos($create_query, "(\r ")) {
                $create_query = str_replace("\r", $crlf, $create_query);
            }

            /*
             * Drop database name from VIEW creation.
             *
             * This is a bit tricky, but we need to issue SHOW CREATE TABLE with
             * database name, but we don't want name to show up in CREATE VIEW
             * statement.
             */
            if ($view) {
                $create_query = preg_replace(
                    '/' . preg_quote(PMA_Util::backquote($db)) . '\./',
                    '',
                    $create_query
                );
            }
            // substitute aliases in create query
            $create_query = $this->replaceWithAliases(
                $create_query, $aliases, $db, $table, $flag
            );
            // One warning per view
            if ($flag && $view) {
                $warning = $this->_exportComment()
                    . $this->_exportComment(
                        __('It appears your database uses views;')
                    )
                    . $this->_exportComment(
                        __('alias export may not work reliably in all cases.')
                    )
                    . $this->_exportComment();
            }
            // Should we use IF NOT EXISTS?
            if (isset($GLOBALS['sql_if_not_exists'])) {
                $create_query = preg_replace(
                    '/^CREATE TABLE/',
                    'CREATE TABLE IF NOT EXISTS',
                    $create_query
                );
            }

            if ($compat == 'MSSQL') {
                $create_query = $this->_makeCreateTableMSSQLCompatible(
                    $create_query
                );
            }

            // Drizzle (checked on 2011.03.13) returns ROW_FORMAT surrounded
            // with quotes, which is not accepted by parser
            if (PMA_DRIZZLE) {
                $create_query = preg_replace(
                    '/ROW_FORMAT=\'(\S+)\'/',
                    'ROW_FORMAT=$1',
                    $create_query
                );
            }

            //are there any constraints to cut out?
            if (preg_match('@CONSTRAINT|KEY@', $create_query)) {
                $has_constraints = 0;
                $has_indexes = 0;

                //if there are constraints
                if (preg_match(
                    '@CONSTRAINT@',
                    $create_query
                )) {
                    $has_constraints = 1;
                    // comments -> constraints for dumped tables
                    if (! isset($sql_constraints)) {
                        if (isset($GLOBALS['no_constraints_comments'])) {
                            $sql_constraints = '';
                        } else {
                            $sql_constraints = $crlf
                                . $this->_exportComment()
                                . $this->_exportComment(
                                    __('Constraints for dumped tables')
                                )
                                . $this->_exportComment();
                        }
                    }
                        // comments for current table
                    if (! isset($GLOBALS['no_constraints_comments'])) {
                        $sql_constraints .= $crlf
                        . $this->_exportComment()
                        . $this->_exportComment(
                            __('Constraints for table')
                            . ' '
                            . PMA_Util::backquoteCompat($table_alias, $compat)
                        )
                        . $this->_exportComment();
                    }
                    $sql_constraints_query .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($table_alias, $compat)
                    . $crlf;
                    $sql_constraints .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($table_alias,  $compat)
                    . $crlf;
                    $sql_drop_foreign_keys .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($db_alias, $compat) . '.'
                    . PMA_Util::backquoteCompat($table_alias, $compat)
                    . $crlf;
                }
                //if there are indexes
                // (look for KEY followed by whitespace to avoid matching
                //  keyworks like PACK_KEYS)
                if ($update_indexes_increments && preg_match(
                    '@KEY[\s]+@',
                    $create_query
                )) {
                    $has_indexes = 1;

                    // comments -> indexes for dumped tables
                    if (! isset($sql_indexes)) {
                        if (isset($GLOBALS['no_constraints_comments'])) {
                            $sql_indexes = '';
                        } else {
                            $sql_indexes = $crlf
                                . $this->_exportComment()
                                . $this->_exportComment(
                                    __('Indexes for dumped tables')
                                )
                                . $this->_exportComment();
                        }
                    }
                    // comments for current table
                    if (! isset($GLOBALS['no_constraints_comments'])) {
                        $sql_indexes .= $crlf
                        . $this->_exportComment()
                        . $this->_exportComment(
                            __('Indexes for table')
                            . ' '
                            . PMA_Util::backquoteCompat($table_alias, $compat)
                        )
                        . $this->_exportComment();
                    }
                    $sql_indexes_query .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($table_alias, $compat)
                    . $crlf;

                    $sql_indexes .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($table_alias,  $compat)
                    . $crlf;
                }
                if ($update_indexes_increments && preg_match(
                    '@AUTO_INCREMENT@',
                    $create_query
                )) {
                    // comments -> auto increments for dumped tables
                    if (! isset($sql_auto_increments)) {
                        if (isset($GLOBALS['no_constraints_comments'])) {
                            $sql_auto_increments = '';
                        } else {
                            $sql_auto_increments = $crlf
                                . $this->_exportComment()
                                . $this->_exportComment(
                                    __('AUTO_INCREMENT for dumped tables')
                                )
                                . $this->_exportComment();
                        }
                    }
                    // comments for current table
                    if (! isset($GLOBALS['no_constraints_comments'])) {
                        $sql_auto_increments .= $crlf
                        . $this->_exportComment()
                        . $this->_exportComment(
                            __('AUTO_INCREMENT for table')
                            . ' '
                            . PMA_Util::backquoteCompat($table_alias, $compat)
                        )
                        . $this->_exportComment();
                    }
                    $sql_auto_increments .= 'ALTER TABLE '
                    . PMA_Util::backquoteCompat($table_alias, $compat)
                    . $crlf;
                }

                // Split the query into lines, so we can easily handle it.
                // We know lines are separated by $crlf (done few lines above).
                $sql_lines = explode($crlf, $create_query);
                $sql_count = count($sql_lines);

                // lets find first line with constraints
                $first_occur = -1;
                for ($i = 0; $i < $sql_count; $i++) {
                    if (preg_match(
                        '@[\s]+(CONSTRAINT|KEY)@',
                        $sql_lines[$i]
                    ) && $first_occur == -1) {
                        $first_occur = $i;
                    }
                }

                for ($k = 0; $k < $sql_count; $k++) {
                    if ($update_indexes_increments && preg_match(
                        '( AUTO_INCREMENT | AUTO_INCREMENT,| AUTO_INCREMENT$)',
                        $sql_lines[$k]
                    )) {
                        //removes extra space at the beginning, if there is
                        $sql_lines[$k] = ltrim($sql_lines[$k], ' ');
                        //creates auto increment code
                        $sql_auto_increments .= "MODIFY " . $sql_lines[$k];
                        //removes auto increment code from table definition
                        $sql_lines[$k] = str_replace(
                            " AUTO_INCREMENT", "", $sql_lines[$k]
                        );
                    }
                    if (isset($GLOBALS['sql_auto_increment'])
                        && $update_indexes_increments && preg_match(
                            '@[\s]+(AUTO_INCREMENT=)@',
                            $sql_lines[$k]
                        )) {
                        //adds auto increment value
                        $increment_value = $pmaString->substr(
                            $sql_lines[$k],
                            $pmaString->strpos($sql_lines[$k], "AUTO_INCREMENT")
                        );
                        $increment_value_array = explode(' ', $increment_value);
                        $sql_auto_increments .= $increment_value_array[0] . ";";

                    }
                }

                if ($sql_auto_increments != '') {
                    $sql_auto_increments = $pmaString->substr(
                        $sql_auto_increments, 0, -1
                    ) . ';';
                }
                // If we really found a constraint
                if ($first_occur != $sql_count) {
                    // lets find first line
                    $sql_lines[$first_occur - 1] = preg_replace(
                        '@,$@',
                        '',
                        $sql_lines[$first_occur - 1]
                    );

                    $first = true;
                    for ($j = $first_occur; $j < $sql_count; $j++) {
                        //removes extra space at the beginning, if there is
                        $sql_lines[$j]=ltrim($sql_lines[$j], ' ');

                        //if it's a constraint
                        if (preg_match(
                            '@CONSTRAINT|FOREIGN[\s]+KEY@',
                            $sql_lines[$j]
                        )) {
                            if (! $first) {
                                $sql_constraints .= $crlf;
                            }
                            $posConstraint = $pmaString->strpos(
                                $sql_lines[$j],
                                'CONSTRAINT'
                            );
                            if ($posConstraint === false) {
                                $tmp_str = preg_replace(
                                    '/(FOREIGN[\s]+KEY)/',
                                    'ADD \1',
                                    $sql_lines[$j]
                                );

                                $sql_constraints_query .= $tmp_str;
                                $sql_constraints .= $tmp_str;

                            } else {
                                $tmp_str = preg_replace(
                                    '/(CONSTRAINT)/',
                                    'ADD \1',
                                    $sql_lines[$j]
                                );

                                $sql_constraints_query .= $tmp_str;
                                $sql_constraints .= $tmp_str;
                                preg_match(
                                    '/(CONSTRAINT)([\s])([\S]*)([\s])/',
                                    $sql_lines[$j],
                                    $matches
                                );
                                if (! $first) {
                                    $sql_drop_foreign_keys .= ', ';
                                }
                                $sql_drop_foreign_keys .= 'DROP FOREIGN KEY '
                                    . $matches[3];
                            }
                            $first = false;
                        } else if ($update_indexes_increments && preg_match(
                            '@KEY[\s]+@',
                            $sql_lines[$j]
                        )) {
                            //if it's a index
                            $tmp_str = " ADD " . $sql_lines[$j];
                            $sql_indexes_query .= $tmp_str;
                            $sql_indexes .= $tmp_str;
                        } else {
                            break;
                        }
                    }
                    //removes superfluous comma at the end
                    $sql_indexes = rtrim($sql_indexes, ',');
                    $sql_indexes_query = rtrim($sql_indexes_query, ',');
                    //removes superfluous semicolon at the end
                    if ($has_constraints == 1) {
                        $sql_constraints .= ';' . $crlf;
                        $sql_constraints_query .= ';';
                    }
                    if ($has_indexes == 1) {
                        $sql_indexes .= ';' . $crlf;
                        $sql_indexes_query .= ';';
                    }
                    //remove indexes and constraints from the $create_query
                    $create_query = implode(
                        $crlf,
                        array_slice($sql_lines, 0, $first_occur)
                    )
                    . $crlf
                    . implode(
                        $crlf,
                        array_slice($sql_lines, $j, $sql_count - 1)
                    );
                    unset($sql_lines);
                }
            }
            $schema_create .= $create_query;
        }

        // remove a possible "AUTO_INCREMENT = value" clause
        // that could be there starting with MySQL 5.0.24
        // in Drizzle it's useless as it contains the value given at table
        // creation time
        if (preg_match('/AUTO_INCREMENT\s*=\s*([0-9])+/', $schema_create)) {
            if ($compat == 'MSSQL' || $auto_increment == '') {
                $auto_increment = ' ';
            }
            $schema_create = preg_replace(
                '/\sAUTO_INCREMENT\s*=\s*([0-9])+\s/',
                $auto_increment,
                $schema_create
            );
        }

        $GLOBALS['dbi']->freeResult($result);
        return $warning . $schema_create . ($add_semicolon ? ';' . $crlf : '');
    } // end of the 'getTableDef()' function

    /**
     * Returns $table's comments, relations etc.
     *
     * @param string $db          database name
     * @param string $table       table name
     * @param string $crlf        end of line sequence
     * @param bool   $do_relation whether to include relation comments
     * @param bool   $do_mime     whether to include mime comments
     * @param array  $aliases     Aliases of db/table/columns
     *
     * @return string resulting comments
     */
    private function _getTableComments(
        $db,
        $table,
        $crlf,
        $do_relation = false,
        $do_mime = false,
        $aliases = array()
    ) {
        global $cfgRelation, $sql_backquotes;

        $db_alias = $db;
        $table_alias = $table;
        $this->initAlias($aliases, $db_alias, $table_alias);

        $schema_create = '';

        // Check if we can use Relations
        list($res_rel, $have_rel) = PMA_getRelationsAndStatus(
            $do_relation && ! empty($cfgRelation['relation']),
            $db,
            $table
        );

        if ($do_mime && $cfgRelation['mimework']) {
            if (! ($mime_map = PMA_getMIME($db, $table, true))) {
                unset($mime_map);
            }
        }

        if (isset($mime_map) && count($mime_map) > 0) {
            $schema_create .= $this->_possibleCRLF()
            . $this->_exportComment()
            . $this->_exportComment(
                __('MIME TYPES FOR TABLE') . ' '
                . PMA_Util::backquote($table, $sql_backquotes) . ':'
            );
            @reset($mime_map);
            foreach ($mime_map as $mime_field => $mime) {
                $schema_create .=
                    $this->_exportComment(
                        '  '
                        . PMA_Util::backquote($mime_field, $sql_backquotes)
                    )
                    . $this->_exportComment(
                        '      '
                        . PMA_Util::backquote(
                            $mime['mimetype'],
                            $sql_backquotes
                        )
                    );
            }
            $schema_create .= $this->_exportComment();
        }

        if ($have_rel) {
            $schema_create .= $this->_possibleCRLF()
                . $this->_exportComment()
                . $this->_exportComment(
                    __('RELATIONS FOR TABLE') . ' '
                    . PMA_Util::backquote($table_alias, $sql_backquotes)
                    . ':'
                );

            foreach ($res_rel as $rel_field => $rel) {
                if ($rel_field != 'foreign_keys_data') {
                    $rel_field_alias = !empty(
                        $aliases[$db]['tables'][$table]['columns'][$rel_field]
                    ) ? $aliases[$db]['tables'][$table]['columns'][$rel_field]
                      : $rel_field;
                    $schema_create .=
                        $this->_exportComment(
                            '  '
                            . PMA_Util::backquote($rel_field_alias, $sql_backquotes)
                        )
                        . $this->_exportComment(
                            '      '
                            . PMA_Util::backquote(
                                $rel['foreign_table'],
                                $sql_backquotes
                            )
                            . ' -> '
                            . PMA_Util::backquote(
                                $rel['foreign_field'],
                                $sql_backquotes
                            )
                        );
                } else {
                    foreach ($rel as $one_key) {
                        foreach ($one_key['index_list'] as $index => $field) {
                            $rel_field_alias = !empty(
                                $aliases[$db]['tables'][$table]['columns'][$field]
                            ) ? $aliases[$db]['tables'][$table]['columns'][$field]
                              : $field;
                            $schema_create .=
                                $this->_exportComment(
                                    '  '
                                    . PMA_Util::backquote(
                                        $rel_field_alias,
                                        $sql_backquotes
                                    )
                                )
                                . $this->_exportComment(
                                    '      '
                                    . PMA_Util::backquote(
                                        $one_key['ref_table_name'],
                                        $sql_backquotes
                                    )
                                    . ' -> '
                                    . PMA_Util::backquote(
                                        $one_key['ref_index_list'][$index],
                                        $sql_backquotes
                                    )
                                );
                        }
                    }
                }
            }
            $schema_create .= $this->_exportComment();
        }

        return $schema_create;

    } // end of the '_getTableComments()' function

    /**
     * Outputs table's structure
     *
     * @param string $db          database name
     * @param string $table       table name
     * @param string $crlf        the end of line sequence
     * @param string $error_url   the url to go back in case of error
     * @param string $export_mode 'create_table','triggers','create_view',
     *                            'stand_in'
     * @param string $export_type 'server', 'database', 'table'
     * @param bool   $relation    whether to include relation comments
     * @param bool   $comments    whether to include the pmadb-style column
     *                            comments as comments in the structure; this is
     *                            deprecated but the parameter is left here
     *                            because export.php calls exportStructure()
     *                            also for other export types which use this
     *                            parameter
     * @param bool   $mime        whether to include mime comments
     * @param bool   $dates       whether to include creation/update/check dates
     * @param array  $aliases     Aliases of db/table/columns
     *
     * @return bool Whether it succeeded
     */
    public function exportStructure(
        $db,
        $table,
        $crlf,
        $error_url,
        $export_mode,
        $export_type,
        $relation = false,
        $comments = false,
        $mime = false,
        $dates = false,
        $aliases = array()
    ) {
        $db_alias = $db;
        $table_alias = $table;
        $this->initAlias($aliases, $db_alias, $table_alias);
        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }

        $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
            ? PMA_Util::backquoteCompat($table_alias, $compat)
            : '\'' . $table_alias . '\'';
        $dump = $this->_possibleCRLF()
            . $this->_exportComment(str_repeat('-', 56))
            . $this->_possibleCRLF()
            . $this->_exportComment();

        switch($export_mode) {
        case 'create_table':
            $dump .= $this->_exportComment(
                __('Table structure for table') . ' ' . $formatted_table_name
            );
            $dump .= $this->_exportComment();
            $dump .= $this->getTableDef(
                $db, $table, $crlf, $error_url, $dates,
                true, false, true, $aliases
            );
            $dump .= $this->_getTableComments(
                $db, $table, $crlf, $relation, $mime, $aliases
            );
            break;
        case 'triggers':
            $dump = '';
            $delimiter = '$$';
            $triggers = $GLOBALS['dbi']->getTriggers($db, $table, $delimiter);
            if ($triggers) {
                $dump .=  $this->_possibleCRLF()
                    . $this->_exportComment()
                    . $this->_exportComment(
                        __('Triggers') . ' ' . $formatted_table_name
                    )
                    . $this->_exportComment();
                $used_alias = false;
                $trigger_query = '';
                foreach ($triggers as $trigger) {
                    if (! empty($GLOBALS['sql_drop_table'])) {
                        $trigger_query .= $trigger['drop'] . ';' . $crlf;
                    }

                    $trigger_query .= 'DELIMITER ' . $delimiter . $crlf;
                    $trigger_query .= $this->replaceWithAliases(
                        $trigger['create'], $aliases, $db, $table, $flag
                    );
                    if ($flag) {
                        $used_alias = true;
                    }
                    $trigger_query .= 'DELIMITER ;' . $crlf;
                }
                // One warning per table.
                if ($used_alias) {
                    $dump .= $this->_exportComment(
                        __('It appears your table uses triggers;')
                    )
                    . $this->_exportComment(
                        __('alias export may not work reliably in all cases.')
                    )
                    . $this->_exportComment();
                }
                $dump .= $trigger_query;
            }
            break;
        case 'create_view':
            if (empty($GLOBALS['sql_views_as_tables'])) {
                $dump .=
                    $this->_exportComment(
                        __('Structure for view')
                        . ' '
                        . $formatted_table_name
                    )
                    . $this->_exportComment();
                // delete the stand-in table previously created (if any)
                if ($export_type != 'table') {
                    $dump .= 'DROP TABLE IF EXISTS '
                        . PMA_Util::backquote($table_alias) . ';' . $crlf;
                }
                $dump .= $this->getTableDef(
                    $db, $table, $crlf, $error_url, $dates,
                    true, true, true, $aliases
                );
            } else {
                $dump .=
                $this->_exportComment(
                    sprintf(
                        __('Structure for view %s exported as a table'),
                        $formatted_table_name
                    )
                )
                . $this->_exportComment();
                // delete the stand-in table previously created (if any)
                if ($export_type != 'table') {
                    $dump .= 'DROP TABLE IF EXISTS '
                        . PMA_Util::backquote($table_alias) . ';' . $crlf;
                }
                $dump .= $this->_getTableDefForView(
                    $db, $table, $crlf, true, $aliases
                );
            }
            break;
        case 'stand_in':
            $dump .=
                $this->_exportComment(
                    __('Stand-in structure for view') . ' ' . $formatted_table_name
                )
                . $this->_exportComment();
            // export a stand-in definition to resolve view dependencies
            $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases);
        } // end switch

        // this one is built by getTableDef() to use in table copy/move
        // but not in the case of export
        unset($GLOBALS['sql_constraints_query']);

        return PMA_exportOutputHandler($dump);
    }

    /**
     * Outputs the content of a table in SQL format
     *
     * @param string $db        database name
     * @param string $table     table name
     * @param string $crlf      the end of line sequence
     * @param string $error_url the url to go back in case of error
     * @param string $sql_query SQL query for obtaining data
     * @param array  $aliases   Aliases of db/table/columns
     *
     * @return bool Whether it succeeded
     */
    public function exportData(
        $db, $table, $crlf, $error_url, $sql_query, $aliases = array()
    ) {
        global $current_row, $sql_backquotes;

        $db_alias = $db;
        $table_alias = $table;
        $this->initAlias($aliases, $db_alias, $table_alias);

        if (isset($GLOBALS['sql_compatibility'])) {
            $compat = $GLOBALS['sql_compatibility'];
        } else {
            $compat = 'NONE';
        }

        $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
            ? PMA_Util::backquoteCompat($table_alias, $compat)
            : '\'' . $table_alias . '\'';

        // Do not export data for a VIEW, unless asked to export the view as a table
        // (For a VIEW, this is called only when exporting a single VIEW)
        if (PMA_Table::isView($db, $table)
            && empty($GLOBALS['sql_views_as_tables'])
        ) {
            $head = $this->_possibleCRLF()
              . $this->_exportComment()
              . $this->_exportComment('VIEW ' . ' ' . $formatted_table_name)
              . $this->_exportComment(__('Data:') . ' ' . __('None'))
              . $this->_exportComment()
              . $this->_possibleCRLF();

            if (! PMA_exportOutputHandler($head)) {
                return false;
            }
            return true;
        }

        $result = $GLOBALS['dbi']->tryQuery(
            $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
        );
        // a possible error: the table has crashed
        $tmp_error = $GLOBALS['dbi']->getError();
        if ($tmp_error) {
            return PMA_exportOutputHandler(
                $this->_exportComment(
                    __('Error reading data:') . ' (' . $tmp_error . ')'
                )
            );
        }

        if ($result == false) {
            $GLOBALS['dbi']->freeResult($result);
            return true;
        }

        $fields_cnt = $GLOBALS['dbi']->numFields($result);

        // Get field information
        $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
        $field_flags = array();
        for ($j = 0; $j < $fields_cnt; $j++) {
            $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j);
        }

        $field_set = array();
        for ($j = 0; $j < $fields_cnt; $j++) {
            $col_as = $fields_meta[$j]->name;
            if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
                $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
            }
            $field_set[$j] = PMA_Util::backquoteCompat(
                $col_as,
                $compat,
                $sql_backquotes
            );
        }

        if (isset($GLOBALS['sql_type'])
            && $GLOBALS['sql_type'] == 'UPDATE'
        ) {
            // update
            $schema_insert  = 'UPDATE ';
            if (isset($GLOBALS['sql_ignore'])) {
                $schema_insert .= 'IGNORE ';
            }
            // avoid EOL blank
            $schema_insert .= PMA_Util::backquoteCompat(
                $table_alias,
                $compat,
                $sql_backquotes
            ) . ' SET';
        } else {
            // insert or replace
            if (isset($GLOBALS['sql_type'])
                && $GLOBALS['sql_type'] == 'REPLACE'
            ) {
                $sql_command = 'REPLACE';
            } else {
                $sql_command = 'INSERT';
            }

            // delayed inserts?
            if (isset($GLOBALS['sql_delayed'])) {
                $insert_delayed = ' DELAYED';
            } else {
                $insert_delayed = '';
            }

            // insert ignore?
            if (isset($GLOBALS['sql_type'])
                && $GLOBALS['sql_type'] == 'INSERT'
                && isset($GLOBALS['sql_ignore'])
            ) {
                $insert_delayed .= ' IGNORE';
            }
            //truncate table before insert
            if (isset($GLOBALS['sql_truncate'])
                && $GLOBALS['sql_truncate']
                && $sql_command == 'INSERT'
            ) {
                $truncate = 'TRUNCATE TABLE '
                    . PMA_Util::backquoteCompat(
                        $table_alias,
                        $compat,
                        $sql_backquotes
                    ) . ";";
                $truncatehead = $this->_possibleCRLF()
                    . $this->_exportComment()
                    . $this->_exportComment(
                        __('Truncate table before insert') . ' '
                        . $formatted_table_name
                    )
                    . $this->_exportComment()
                    . $crlf;
                PMA_exportOutputHandler($truncatehead);
                PMA_exportOutputHandler($truncate);
            }

            // scheme for inserting fields
            if ($GLOBALS['sql_insert_syntax'] == 'complete'
                || $GLOBALS['sql_insert_syntax'] == 'both'
            ) {
                $fields        = implode(', ', $field_set);
                $schema_insert = $sql_command . $insert_delayed . ' INTO '
                    . PMA_Util::backquoteCompat(
                        $table_alias,
                        $compat,
                        $sql_backquotes
                    )
                    // avoid EOL blank
                    . ' (' . $fields . ') VALUES';
            } else {
                $schema_insert = $sql_command . $insert_delayed . ' INTO '
                    . PMA_Util::backquoteCompat(
                        $table_alias,
                        $compat,
                        $sql_backquotes
                    )
                    . ' VALUES';
            }
        }

        //\x08\\x09, not required
        $search      = array("\x00", "\x0a", "\x0d", "\x1a");
        $replace     = array('\0', '\n', '\r', '\Z');
        $current_row = 0;
        $query_size  = 0;
        if (($GLOBALS['sql_insert_syntax'] == 'extended'
            || $GLOBALS['sql_insert_syntax'] == 'both')
            && (! isset($GLOBALS['sql_type'])
            || $GLOBALS['sql_type'] != 'UPDATE')
        ) {
            $separator      = ',';
            $schema_insert .= $crlf;
        } else {
            $separator      = ';';
        }

        /** @var PMA_String $pmaString */
        $pmaString = $GLOBALS['PMA_String'];
        while ($row = $GLOBALS['dbi']->fetchRow($result)) {
            if ($current_row == 0) {
                $head = $this->_possibleCRLF()
                    . $this->_exportComment()
                    . $this->_exportComment(
                        __('Dumping data for table') . ' '
                        . $formatted_table_name
                    )
                    . $this->_exportComment()
                    . $crlf;
                if (! PMA_exportOutputHandler($head)) {
                    return false;
                }
            }
             // We need to SET IDENTITY_INSERT ON for MSSQL
            if (isset($GLOBALS['sql_compatibility'])
                && $GLOBALS['sql_compatibility'] == 'MSSQL'
                && $current_row == 0
            ) {
                if (! PMA_exportOutputHandler(
                    'SET IDENTITY_INSERT '
                    . PMA_Util::backquoteCompat(
                        $table_alias,
                        $compat
                    )
                    . ' ON ;' . $crlf
                )) {
                    return false;
                }
            }
            $current_row++;
            $values = array();
            for ($j = 0; $j < $fields_cnt; $j++) {
                // NULL
                if (! isset($row[$j]) || is_null($row[$j])) {
                    $values[] = 'NULL';
                } elseif ($fields_meta[$j]->numeric
                    && $fields_meta[$j]->type != 'timestamp'
                    && ! $fields_meta[$j]->blob
                ) {
                    // a number
                    // timestamp is numeric on some MySQL 4.1, BLOBs are
                    // sometimes numeric
                    $values[] = $row[$j];
                } elseif (stristr($field_flags[$j], 'BINARY') !== false
                    && isset($GLOBALS['sql_hex_for_binary'])
                ) {
                    // a true BLOB
                    // - mysqldump only generates hex data when the --hex-blob
                    //   option is used, for fields having the binary attribute
                    //   no hex is generated
                    // - a TEXT field returns type blob but a real blob
                    //   returns also the 'binary' flag

                    // empty blobs need to be different, but '0' is also empty
                    // :-(
                    if (empty($row[$j]) && $row[$j] != '0') {
                        $values[] = '\'\'';
                    } else {
                        $values[] = '0x' . bin2hex($row[$j]);
                    }
                } elseif ($fields_meta[$j]->type == 'bit') {
                    // detection of 'bit' works only on mysqli extension
                    $values[] = "b'" . PMA_Util::sqlAddSlashes(
                        PMA_Util::printableBitValue(
                            $row[$j], $fields_meta[$j]->length
                        )
                    )
                        . "'";
                } else {
                    // something else -> treat as a string
                    $values[] = '\''
                        . str_replace(
                            $search, $replace,
                            PMA_Util::sqlAddSlashes($row[$j])
                        )
                        . '\'';
                } // end if
            } // end for

            // should we make update?
            if (isset($GLOBALS['sql_type'])
                && $GLOBALS['sql_type'] == 'UPDATE'
            ) {

                $insert_line = $schema_insert;
                for ($i = 0; $i < $fields_cnt; $i++) {
                    if (0 == $i) {
                        $insert_line .= ' ';
                    }
                    if ($i > 0) {
                        // avoid EOL blank
                        $insert_line .= ',';
                    }
                    $insert_line .= $field_set[$i] . ' = ' . $values[$i];
                }

                list($tmp_unique_condition, $tmp_clause_is_unique)
                    = PMA_Util::getUniqueCondition(
                        $result,
                        $fields_cnt,
                        $fields_meta,
                        $row
                    );
                $insert_line .= ' WHERE ' . $tmp_unique_condition;
                unset($tmp_unique_condition, $tmp_clause_is_unique);

            } else {

                // Extended inserts case
                if ($GLOBALS['sql_insert_syntax'] == 'extended'
                    || $GLOBALS['sql_insert_syntax'] == 'both'
                ) {
                    if ($current_row == 1) {
                        $insert_line  = $schema_insert . '('
                            . implode(', ', $values) . ')';
                    } else {
                        $insert_line  = '(' . implode(', ', $values) . ')';
                        $insertLineSize = $pmaString->strlen($insert_line);
                        $sql_max_size = $GLOBALS['sql_max_query_size'];
                        if (isset($sql_max_size)
                            && $sql_max_size > 0
                            && $query_size + $insertLineSize > $sql_max_size
                        ) {
                            if (! PMA_exportOutputHandler(';' . $crlf)) {
                                return false;
                            }
                            $query_size  = 0;
                            $current_row = 1;
                            $insert_line = $schema_insert . $insert_line;
                        }
                    }
                    $query_size += $pmaString->strlen($insert_line);
                    // Other inserts case
                } else {
                    $insert_line = $schema_insert
                        . '(' . implode(', ', $values) . ')';
                }
            }
            unset($values);

            if (! PMA_exportOutputHandler(
                ($current_row == 1 ? '' : $separator . $crlf)
                . $insert_line
            )) {
                return false;
            }

        } // end while

        if ($current_row > 0) {
            if (! PMA_exportOutputHandler(';' . $crlf)) {
                return false;
            }
        }

        // We need to SET IDENTITY_INSERT OFF for MSSQL
        if (isset($GLOBALS['sql_compatibility'])
            && $GLOBALS['sql_compatibility'] == 'MSSQL'
            && $current_row > 0
        ) {
            $outputSucceeded = PMA_exportOutputHandler(
                $crlf . 'SET IDENTITY_INSERT '
                . PMA_Util::backquoteCompat($table_alias, $compat)
                . ' OFF;' . $crlf
            );
            if (! $outputSucceeded) {
                return false;
            }
        }

        $GLOBALS['dbi']->freeResult($result);
        return true;
    } // end of the 'exportData()' function

    /**
     * Make a create table statement compatible with MSSQL
     *
     * @param string $create_query MySQL create table statement
     *
     * @return string MSSQL compatible create table statement
     */
    private function _makeCreateTableMSSQLCompatible($create_query)
    {
        // In MSSQL
        // 1. No 'IF NOT EXISTS' in CREATE TABLE
        // 2. DATE field doesn't exists, we will use DATETIME instead
        // 3. UNSIGNED attribute doesn't exist
        // 4. No length on INT, TINYINT, SMALLINT, BIGINT and no precision on
        //    FLOAT fields
        // 5. No KEY and INDEX inside CREATE TABLE
        // 6. DOUBLE field doesn't exists, we will use FLOAT instead

        $create_query = preg_replace(
            "/^CREATE TABLE IF NOT EXISTS/",
            'CREATE TABLE',
            $create_query
        );
        // first we need  to replace all lines ended with '" DATE ...,\n'
        // last preg_replace preserve us from situation with date text
        // inside DEFAULT field value
        $create_query = preg_replace(
            "/\" date DEFAULT NULL(,)?\n/",
            '" datetime DEFAULT NULL$1' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            "/\" date NOT NULL(,)?\n/",
            '" datetime NOT NULL$1' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/" date NOT NULL DEFAULT \'([^\'])/',
            '" datetime NOT NULL DEFAULT \'$1',
            $create_query
        );

        // next we need to replace all lines ended with ') UNSIGNED ...,'
        // last preg_replace preserve us from situation with unsigned text
        // inside DEFAULT field value
        $create_query = preg_replace(
            "/\) unsigned NOT NULL(,)?\n/",
            ') NOT NULL$1' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            "/\) unsigned DEFAULT NULL(,)?\n/",
            ') DEFAULT NULL$1' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/\) unsigned NOT NULL DEFAULT \'([^\'])/',
            ') NOT NULL DEFAULT \'$1',
            $create_query
        );

        // we need to replace all lines ended with
        // '" INT|TINYINT([0-9]{1,}) ...,' last preg_replace preserve us
        // from situation with int([0-9]{1,}) text inside DEFAULT field
        // value
        $create_query = preg_replace(
            '/" (int|tinyint|smallint|bigint)\([0-9]+\) DEFAULT NULL(,)?\n/',
            '" $1 DEFAULT NULL$2' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL(,)?\n/',
            '" $1 NOT NULL$2' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL DEFAULT \'([^\'])/',
            '" $1 NOT NULL DEFAULT \'$2',
            $create_query
        );

        // we need to replace all lines ended with
        // '" FLOAT|DOUBLE([0-9,]{1,}) ...,'
        // last preg_replace preserve us from situation with
        // float([0-9,]{1,}) text inside DEFAULT field value
        $create_query = preg_replace(
            '/" (float|double)(\([0-9]+,[0-9,]+\))? DEFAULT NULL(,)?\n/',
            '" float DEFAULT NULL$3' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL(,)?\n/',
            '" float NOT NULL$3' . "\n",
            $create_query
        );
        $create_query = preg_replace(
            '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL DEFAULT \'([^\'])/',
            '" float NOT NULL DEFAULT \'$3',
            $create_query
        );

        // @todo remove indexes from CREATE TABLE

        return $create_query;
    }

    /**
     * replaces db/table/column names with their aliases
     *
     * @param string $sql_query SQL query in which aliases are to be substituted
     * @param array  $aliases   Alias information for db/table/column
     * @param string $db        the database name
     * @param string $table     the tablename
     * @param string &$flag     the flag denoting whether any replacement was done
     *
     * @return string query replaced with aliases
     */
    public function replaceWithAliases(
        $sql_query, $aliases, $db, $table = '', &$flag = null
    ) {
        $flag = false;
        // Return original sql query if no aliases are provided.
        if (!is_array($aliases) || empty($aliases) || empty($sql_query)) {
            return $sql_query;
        }
        $supported_query_types = array(
            'CREATE' => true,
        );
        $supported_query_ons = array(
            'TABLE' => true,
            'VIEW' => true,
            'TRIGGER' => true,
            'FUNCTION' => true,
            'PROCEDURE' => true
        );
        $identifier_types = array(
            'alpha_identifier',
            'quote_backtick'
        );
        $query_type = '';
        $query_on = '';
        // Adjustment value for each pos value
        // of token after replacement
        $offset = 0;
        $open_braces = 0;
        $in_create_table_fields = false;
        // flag to force end query parsing
        $query_end = false;
        // Convert all line feeds to Unix style
        $sql_query = str_replace("\r\n", "\n", $sql_query);
        $sql_query = str_replace("\r", "\n", $sql_query);
        $tokens = PMA_SQP_parse($sql_query);
        $ref_seen = false;
        $ref_table_seen = false;
        $old_table = $table;
        $on_seen = false;
        $size = $tokens['len'];

        /** @var PMA_String $pmaString */
        $pmaString = $GLOBALS['PMA_String'];

        for ($i = 0; $i < $size && !$query_end; $i++) {
            $type = $tokens[$i]['type'];
            $data = $tokens[$i]['data'];
            $data_next = isset($tokens[$i+1]['data'])
                ? $tokens[$i+1]['data'] : '';
            $data_prev = ($i > 0) ? $tokens[$i-1]['data'] : '';
            $d_unq = PMA_Util::unQuote($data);
            $d_unq_next = PMA_Util::unQuote($data_next);
            $d_unq_prev = PMA_Util::unQuote($data_prev);
            $d_upper = $pmaString->strtoupper($d_unq);
            $d_upper_next = $pmaString->strtoupper($d_unq_next);
            $d_upper_prev = $pmaString->strtoupper($d_unq_prev);
            $pos = $tokens[$i]['pos'] + $offset;
            if ($type === 'alpha_reservedWord') {
                if ($query_type === ''
                    && !empty($supported_query_types[$d_upper])
                ) {
                    $query_type = $d_upper;
                } elseif ($query_on === ''
                    && !empty($supported_query_ons[$d_upper])
                ) {
                    $query_on = $d_upper;
                }
            }
            // CREATE TABLE - Alias replacement
            if ($query_type === 'CREATE' && $query_on === 'TABLE') {
                // replace create table name
                if (!$in_create_table_fields
                    && in_array($type, $identifier_types)
                    && !empty($aliases[$db]['tables'][$table]['alias'])
                ) {
                    $sql_query = $this->substituteAlias(
                        $sql_query, $data,
                        $aliases[$db]['tables'][$table]['alias'],
                        $pos, $offset
                    );
                    $flag = true;
                } elseif ($type === 'punct_bracket_open_round') {
                    // CREATE TABLE fields started
                    if (!$in_create_table_fields) {
                        $in_create_table_fields = true;
                    }
                    $open_braces++;
                } elseif ($type === 'punct_bracket_close_round') {
                    // end our parsing after last )
                    // no columns appear after that
                    if ($in_create_table_fields && $open_braces === 0) {
                        $query_end = true;
                    }
                    // End of Foreign key reference
                    if ($ref_seen) {
                        $ref_seen = $ref_table_seen = false;
                        $table = $old_table;
                    }
                    $open_braces--;
                    // handles Foreign key references
                } elseif ($type === 'alpha_reservedWord'
                    && $d_upper === 'REFERENCES'
                ) {
                    $ref_seen = true;
                } elseif (in_array($type, $identifier_types)
                    && $ref_seen === true && !$ref_table_seen
                ) {
                    $table = $d_unq;
                    $ref_table_seen = true;
                    if (!empty($aliases[$db]['tables'][$table]['alias'])) {
                        $sql_query = $this->substituteAlias(
                            $sql_query, $data,
                            $aliases[$db]['tables'][$table]['alias'],
                            $pos, $offset
                        );
                        $flag = true;
                    }
                    // Replace column names
                } elseif (in_array($type, $identifier_types)
                    && !empty($aliases[$db]['tables'][$table]['columns'][$d_unq])
                ) {
                    $sql_query = $this->substituteAlias(
                        $sql_query, $data,
                        $aliases[$db]['tables'][$table]['columns'][$d_unq],
                        $pos, $offset
                    );
                    $flag = true;
                }
                // CREATE TRIGGER - Alias replacement
            } elseif ($query_type === 'CREATE' && $query_on === 'TRIGGER') {
                // Skip till 'ON' in encountered
                if (!$on_seen && $type === 'alpha_reservedWord'
                    && $d_upper === 'ON'
                ) {
                    $on_seen = true;
                } elseif ($on_seen && in_array($type, $identifier_types)) {
                    if (!$ref_table_seen
                        && !empty($aliases[$db]['tables'][$d_unq]['alias'])
                    ) {
                        $ref_table_seen = true;
                        $sql_query = $this->substituteAlias(
                            $sql_query, $data,
                            $aliases[$db]['tables'][$d_unq]['alias'],
                            $pos, $offset
                        );
                        $flag = true;
                    } else {
                        // search for identifier alias
                        $alias = $this->getAlias($aliases, $d_unq);
                        if (!empty($alias)) {
                            $sql_query = $this->substituteAlias(
                                $sql_query, $data, $alias, $pos, $offset
                            );
                            $flag = true;
                        }
                    }
                }
                // CREATE PROCEDURE|FUNCTION|VIEW - Alias replacement
            } elseif ($query_type === 'CREATE'
                && ($query_on === 'FUNCTION'
                || $query_on === 'PROCEDURE'
                || $query_on === 'VIEW')
            ) {
                // LANGUAGE SQL | (READS|MODIFIES) SQL DATA
                // characteristics are skipped
                if ($type === 'alpha_identifier'
                    && (($d_upper === 'LANGUAGE' && $d_upper_next === 'SQL')
                    || ($d_upper === 'DATA' && $d_upper_prev === 'SQL'))
                ) {
                    continue;
                    // No need to process further in case of VIEW
                    // when 'WITH' keyword has been detected
                } elseif ($query_on === 'VIEW'
                    && $type === 'alpha_reservedWord' && $d_upper === 'WITH'
                ) {
                    $query_end = true;
                } elseif (in_array($type, $identifier_types)) {
                    // search for identifier alias
                    $alias = $this->getAlias($aliases, $d_unq);
                    if (!empty($alias)) {
                        $sql_query = $this->substituteAlias(
                            $sql_query, $data, $alias, $pos, $offset
                        );
                        $flag = true;
                    };
                }
            }
        }
        return $sql_query;
    }

    /**
     * substitutes alias in query at given position
     * Note: pos is the value from PMA_SQP_parse() + offset
     *
     * @param string $sql_query the SQL query
     * @param string $data      the data to be replaced
     * @param string $alias     the replacement
     * @param string $pos       the position of alias
     * @param string &$offset   the change in pos occured after substitution
     *
     * @return string replaced query with alias
     */
    public function substituteAlias($sql_query, $data, $alias, $pos, &$offset = null)
    {
        if (!empty($GLOBALS['sql_backquotes'])) {
            $alias = PMA_Util::backquote($alias);
        }
        $alias_len = $GLOBALS['PMA_String']->strlen($alias);
        $data_len = $GLOBALS['PMA_String']->strlen($data);
        if (isset($offset)) {
            $offset += ($alias_len - $data_len);
        }
        $sql_query = substr_replace(
            $sql_query, $alias, $pos - $data_len, $data_len
        );
        return $sql_query;
    }
}