/
Import.php
1242 lines (1047 loc) · 41.5 KB
/
Import.php
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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Import;
use PhpMyAdmin\Config;
use PhpMyAdmin\Current;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\File;
use PhpMyAdmin\FileListing;
use PhpMyAdmin\Message;
use PhpMyAdmin\Plugins\ImportPlugin;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\DeleteStatement;
use PhpMyAdmin\SqlParser\Statements\InsertStatement;
use PhpMyAdmin\SqlParser\Statements\ReplaceStatement;
use PhpMyAdmin\SqlParser\Statements\SetStatement;
use PhpMyAdmin\SqlParser\Statements\UpdateStatement;
use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use function __;
use function abs;
use function array_key_last;
use function array_map;
use function count;
use function explode;
use function function_exists;
use function htmlspecialchars;
use function implode;
use function in_array;
use function is_numeric;
use function max;
use function mb_chr;
use function mb_ord;
use function mb_stripos;
use function mb_strlen;
use function mb_strtoupper;
use function mb_substr;
use function mb_substr_count;
use function preg_grep;
use function preg_match;
use function preg_quote;
use function preg_replace;
use function rtrim;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function substr;
use function time;
use function trim;
/**
* Library that provides common import functions that are used by import plugins
*/
class Import
{
private string|null $importRunBuffer = null;
public function __construct()
{
Config::getInstance()->selectedServer['DisableIS'] = false;
}
/**
* Checks whether timeout is getting close
*/
public function checkTimeout(): bool
{
if (ImportSettings::$maximumTime === 0) {
return false;
}
if (ImportSettings::$timeoutPassed) {
return true;
/* 5 in next row might be too much */
}
if (time() - ImportSettings::$timestamp > ImportSettings::$maximumTime - 5) {
ImportSettings::$timeoutPassed = true;
return true;
}
return false;
}
/**
* Runs query inside import buffer. This is needed to allow displaying
* of last SELECT, SHOW or HANDLER results and similar nice stuff.
*
* @param string $sql query to run
* @param string[] $sqlData SQL parse data storage
*/
public function executeQuery(string $sql, array &$sqlData): void
{
$GLOBALS['error'] ??= null;
$dbi = DatabaseInterface::getInstance();
$GLOBALS['result'] = $dbi->tryQuery($sql);
// USE query changes the database, son need to track
// while running multiple queries
$isUseQuery = mb_stripos($sql, 'use ') !== false;
ImportSettings::$message = '# ';
if ($GLOBALS['result'] === false) {
ImportSettings::$failedQueries[] = ['sql' => $sql, 'error' => $dbi->getError()];
ImportSettings::$message .= __('Error');
if (! Config::getInstance()->settings['IgnoreMultiSubmitErrors']) {
$GLOBALS['error'] = true;
return;
}
} else {
$aNumRows = (int) $GLOBALS['result']->numRows();
$aAffectedRows = (int) @$dbi->affectedRows();
if ($aNumRows > 0) {
ImportSettings::$message .= __('Rows') . ': ' . $aNumRows;
} elseif ($aAffectedRows > 0) {
$message = Message::getMessageForAffectedRows($aAffectedRows);
ImportSettings::$message .= $message->getMessage();
} else {
ImportSettings::$message .= __('MySQL returned an empty result set (i.e. zero rows).');
}
if ($aNumRows > 0 || $isUseQuery) {
$sqlData[] = $sql;
}
}
if (! ImportSettings::$sqlQueryDisabled) {
$GLOBALS['sql_query'] .= ImportSettings::$message . "\n";
}
// If a 'USE <db>' SQL-clause was found and the query
// succeeded, set our current $db to the new one
if ($GLOBALS['result'] != false) {
$dbNameInsideUse = $this->lookForUse($sql);
if ($dbNameInsideUse !== '') {
Current::$database = $dbNameInsideUse;
$GLOBALS['reload'] = true;
}
}
$pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im';
if ($GLOBALS['result'] == false || ! preg_match($pattern, $sql)) {
return;
}
$GLOBALS['reload'] = true;
}
/**
* Runs query inside import buffer. This is needed to allow displaying
* of last SELECT, SHOW or HANDLER results and similar nice stuff.
*
* @param string $sql query to run
* @param string[] $sqlData SQL parse data storage
*/
public function runQuery(string $sql, array &$sqlData): void
{
$GLOBALS['complete_query'] ??= null;
$GLOBALS['display_query'] ??= null;
ImportSettings::$readMultiply = 1;
if ($this->importRunBuffer === null) {
// Do we have something to push into buffer?
$this->importRunBuffer = $sql !== '' ? $sql . ';' : null;
return;
}
// Should we skip something?
if (ImportSettings::$skipQueries > 0) {
ImportSettings::$skipQueries--;
// Do we have something to push into buffer?
$this->importRunBuffer = $sql !== '' ? $sql . ';' : null;
return;
}
ImportSettings::$maxSqlLength = max(
ImportSettings::$maxSqlLength,
mb_strlen($this->importRunBuffer),
);
if (! ImportSettings::$sqlQueryDisabled) {
$GLOBALS['sql_query'] .= $this->importRunBuffer;
}
ImportSettings::$executedQueries++;
if (ImportSettings::$runQuery && ImportSettings::$executedQueries < 50) {
ImportSettings::$goSql = true;
if (! ImportSettings::$sqlQueryDisabled) {
$GLOBALS['complete_query'] = $GLOBALS['sql_query'];
$GLOBALS['display_query'] = $GLOBALS['sql_query'];
} else {
$GLOBALS['complete_query'] = '';
$GLOBALS['display_query'] = '';
}
$GLOBALS['sql_query'] = $this->importRunBuffer;
$sqlData[] = $this->importRunBuffer;
} elseif (ImportSettings::$runQuery) {
/* Handle rollback from go_sql */
if (ImportSettings::$goSql && $sqlData !== []) {
$queries = $sqlData;
$sqlData = [];
ImportSettings::$goSql = false;
foreach ($queries as $query) {
$this->executeQuery($query, $sqlData);
if ($GLOBALS['error']) {
break;
}
}
}
if (! $GLOBALS['error']) {
$this->executeQuery($this->importRunBuffer, $sqlData);
}
}
// check length of query unless we decided to pass it to /sql
// (if $run_query is false, we are just displaying so show
// the complete query in the textarea)
if (! ImportSettings::$goSql && ImportSettings::$runQuery && ! empty($GLOBALS['sql_query'])) {
if (
mb_strlen($GLOBALS['sql_query']) > 50000
|| ImportSettings::$executedQueries > 50
|| ImportSettings::$maxSqlLength > 1000
) {
$GLOBALS['sql_query'] = '';
ImportSettings::$sqlQueryDisabled = true;
}
}
// Do we have something to push into buffer?
$this->importRunBuffer = $sql !== '' ? $sql . ';' : null;
}
/**
* Looks for the presence of USE to possibly change current db
*/
public function lookForUse(string $buffer): string
{
if (preg_match('@^[\s]*USE[[:space:]]+([\S]+)@i', $buffer, $match)) {
$db = trim($match[1]);
$db = trim($db, ';'); // for example, USE abc;
// $db must not contain the escape characters generated by backquote()
// ( used in buildSql() as: backquote($db_name), and then called
// in runQuery() which in turn calls lookForUse() )
return Util::unQuote($db);
}
return '';
}
/**
* Returns next part of imported file/buffer
*
* @param int $size size of buffer to read (this is maximal size function will return)
*
* @return string|bool part of file/buffer
*/
public function getNextChunk(File|null $importHandle = null, int $size = 32768): string|bool
{
// Add some progression while reading large amount of data
if (ImportSettings::$readMultiply <= 8) {
$size *= ImportSettings::$readMultiply;
} else {
$size *= 8;
}
ImportSettings::$readMultiply++;
// We can not read too much
if ($size > ImportSettings::$readLimit) {
$size = ImportSettings::$readLimit;
}
if ($this->checkTimeout()) {
return false;
}
if (ImportSettings::$finished) {
return true;
}
if (ImportSettings::$importFile === 'none') {
// Well this is not yet supported and tested,
// but should return content of textarea
if (mb_strlen($GLOBALS['import_text']) < $size) {
ImportSettings::$finished = true;
return $GLOBALS['import_text'];
}
$r = mb_substr($GLOBALS['import_text'], 0, $size);
ImportSettings::$offset += $size;
$GLOBALS['import_text'] = mb_substr($GLOBALS['import_text'], $size);
return $r;
}
if ($importHandle === null) {
return false;
}
$result = $importHandle->read($size);
ImportSettings::$finished = $importHandle->eof();
ImportSettings::$offset += $size;
if (ImportSettings::$charsetConversion) {
return Encoding::convertString(ImportSettings::$charsetOfFile, 'utf-8', $result);
}
if (ImportSettings::$offset === $size) {
return $this->skipByteOrderMarksFromContents($result);
}
return $result;
}
/**
* Skip possible byte order marks (I do not think we need more
* charsets, but feel free to add more, you can use wikipedia for
* reference: <https://en.wikipedia.org/wiki/Byte_Order_Mark>)
*
* @todo BOM could be used for charset autodetection
*/
public function skipByteOrderMarksFromContents(string $contents): string
{
// Do not use mb_ functions they are sensible to mb_internal_encoding()
// UTF-8
if (str_starts_with($contents, "\xEF\xBB\xBF")) {
return substr($contents, 3);
}
// UTF-16 BE, LE
if (str_starts_with($contents, "\xFE\xFF") || str_starts_with($contents, "\xFF\xFE")) {
return substr($contents, 2);
}
return $contents;
}
/**
* Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
*
* This functions uses recursion to build the Excel column name.
*
* The column number (1-26) is converted to the responding
* ASCII character (A-Z) and returned.
*
* If the column number is bigger than 26 (= num of letters in alphabet),
* an extra character needs to be added. To find this extra character,
* the number is divided by 26 and this value is passed to another instance
* of the same function (hence recursion). In that new instance the number is
* evaluated again, and if it is still bigger than 26, it is divided again
* and passed to another instance of the same function. This continues until
* the number is smaller than 26. Then the last called function returns
* the corresponding ASCII character to the function that called it.
* Each time a called function ends an extra character is added to the column name.
* When the first function is reached, the last character is added and the complete
* column name is returned.
*
* @param int $num the column number
*
* @return string The column's "Excel" name
*/
public function getColumnAlphaName(int $num): string
{
$capitalA = 65; // ASCII value for capital "A"
$colName = '';
/** @infection-ignore-all */
if ($num > 26) {
$div = (int) ($num / 26);
$remain = $num % 26;
// subtract 1 of divided value in case the modulus is 0,
// this is necessary because A-Z has no 'zero'
if ($remain === 0) {
$div--;
}
// recursive function call
$colName = $this->getColumnAlphaName($div);
// use modulus as new column number
$num = $remain;
}
if ($num === 0) {
// use 'Z' if column number is 0,
// this is necessary because A-Z has no 'zero'
$colName .= mb_chr($capitalA + 26 - 1);
} else {
// convert column number to ASCII character
$colName .= mb_chr($capitalA + $num - 1);
}
return $colName;
}
/**
* Returns the column number based on the Excel name.
* So "A" = 1, "Z" = 26, "AA" = 27, etc.
*
* Basically this is a base26 (A-Z) to base10 (0-9) conversion.
* It iterates through all characters in the column name and
* calculates the corresponding value, based on character value
* (A = 1, ..., Z = 26) and position in the string.
*
* @param string $name column name(i.e. "A", or "BC", etc.)
*
* @return int The column number
*/
public function getColumnNumberFromName(string $name): int
{
if ($name === '') {
return 0;
}
$name = mb_strtoupper($name);
$numChars = mb_strlen($name);
$columnNumber = 0;
for ($i = 0; $i < $numChars; ++$i) {
// read string from back to front
$charPos = $numChars - 1 - $i;
// convert capital character to ASCII value
// and subtract 64 to get corresponding decimal value
// ASCII value of "A" is 65, "B" is 66, etc.
// Decimal equivalent of "A" is 1, "B" is 2, etc.
$number = mb_ord($name[$charPos]) - 64;
// base26 to base10 conversion : multiply each number
// with corresponding value of the position, in this case
// $i=0 : 1; $i=1 : 26; $i=2 : 676; ...
$columnNumber += $number * 26 ** $i;
}
return (int) $columnNumber;
}
/**
* Obtains the size of the given cell
*
* @param DecimalSize|int $lastCumulativeSize Last cumulative column size
* @param ColumnType|null $lastCumulativeType Last cumulative column type
* @param ColumnType $currentCellType Type of the current cell
* @param string $cell The current cell
*
* @return DecimalSize|int Size of the given cell in the type-appropriate format
*
* @todo Handle the error cases more elegantly
*/
private function detectSize(
DecimalSize|int $lastCumulativeSize,
ColumnType|null $lastCumulativeType,
ColumnType $currentCellType,
string $cell,
): DecimalSize|int {
$currSize = mb_strlen($cell);
/**
* If the cell is NULL, don't treat it as a varchar
*/
if ($cell === 'NULL') {
return $lastCumulativeSize;
}
if ($currentCellType === ColumnType::Varchar) {
if ($lastCumulativeType === ColumnType::Varchar) {
if ($currSize >= $lastCumulativeSize) {
return $currSize;
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === ColumnType::Decimal) {
if ($currSize >= $lastCumulativeSize->precision) {
return $currSize;
}
return $lastCumulativeSize->precision;
}
if ($lastCumulativeType === ColumnType::BigInt || $lastCumulativeType === ColumnType::Int) {
if ($currSize >= $lastCumulativeSize) {
return $currSize;
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === null || $lastCumulativeType === ColumnType::None) {
/**
* This is the first row to be analyzed
*/
return $currSize;
}
} elseif ($currentCellType === ColumnType::Decimal) {
if ($lastCumulativeType === ColumnType::Varchar) {
/* Convert $last_cumulative_size from varchar to decimal format */
$size = DecimalSize::fromCell($cell);
if ($size->precision >= $lastCumulativeSize) {
return $size->precision;
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === ColumnType::Decimal) {
$size = DecimalSize::fromCell($cell);
if ($size->precision > $lastCumulativeSize->precision || $size->scale > $lastCumulativeSize->scale) {
/* Take the largest of both types */
return DecimalSize::fromPrecisionAndScale(
max($size->precision, $lastCumulativeSize->precision),
max($size->scale, $lastCumulativeSize->scale),
);
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === ColumnType::BigInt || $lastCumulativeType === ColumnType::Int) {
/* Convert $last_cumulative_size from int to decimal format */
$size = DecimalSize::fromCell($cell);
if ($size->precision >= $lastCumulativeSize) {
return $size;
}
return DecimalSize::fromPrecisionAndScale($lastCumulativeSize, $size->scale);
}
if ($lastCumulativeType === null || $lastCumulativeType === ColumnType::None) {
/**
* This is the first row to be analyzed
*/
/* First row of the column */
return DecimalSize::fromCell($cell);
}
} elseif ($currentCellType === ColumnType::BigInt || $currentCellType === ColumnType::Int) {
if ($lastCumulativeType === ColumnType::Varchar) {
if ($currSize >= $lastCumulativeSize) {
return $currSize;
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === ColumnType::Decimal) {
$oldInt = $lastCumulativeSize->precision - $lastCumulativeSize->scale;
$newInt = mb_strlen($cell);
if ($oldInt >= $newInt) {
/* Use old decimal size */
return $lastCumulativeSize;
}
return DecimalSize::fromPrecisionAndScale(
$newInt + $lastCumulativeSize->scale,
$lastCumulativeSize->scale,
);
}
if ($lastCumulativeType === ColumnType::BigInt || $lastCumulativeType === ColumnType::Int) {
if ($currSize >= $lastCumulativeSize) {
return $currSize;
}
return $lastCumulativeSize;
}
if ($lastCumulativeType === null || $lastCumulativeType === ColumnType::None) {
/**
* This is the first row to be analyzed
*/
return $currSize;
}
}
/**
* An error has DEFINITELY occurred
*/
/**
* TODO: Handle this MUCH more elegantly
*/
return -1;
}
public function detectType(ColumnType|null $lastCumulativeType, string|null $cell): ColumnType
{
/**
* If numeric, determine if decimal, int or bigint
* Else, we call it varchar for simplicity
*/
if ($cell === 'NULL') {
return $lastCumulativeType ?? ColumnType::None;
}
if (! is_numeric($cell)) {
return ColumnType::Varchar;
}
if (
$cell === (string) (float) $cell
&& str_contains($cell, '.')
&& mb_substr_count($cell, '.') === 1
) {
return ColumnType::Decimal;
}
if (abs((int) $cell) > 2147483647) {
return ColumnType::BigInt;
}
if ($cell !== (string) (int) $cell) {
return ColumnType::Varchar;
}
return ColumnType::Int;
}
/**
* Determines if the column types are int, decimal, or string
*
* @link https://wiki.phpmyadmin.net/pma/Import
*
* @return AnalysedColumn[]
*/
public function analyzeTable(ImportTable $table): array
{
/* Get number of rows in table */
/* Get number of columns */
$numberOfColumns = count($table->columns);
$columns = [];
for ($i = 0; $i < $numberOfColumns; ++$i) {
$columns[] = new AnalysedColumn(ColumnType::None, 0);
}
/* Analyze each column */
for ($i = 0; $i < $numberOfColumns; ++$i) {
/* Analyze the column in each row */
foreach ($table->rows as $row) {
$cellValue = $row[$i];
/* Determine type of the current cell */
$currType = $this->detectType($columns[$i]->type, $cellValue === null ? null : (string) $cellValue);
/* Determine size of the current cell */
$columns[$i]->size = $this->detectSize(
$columns[$i]->size,
$columns[$i]->type,
$currType,
(string) $cellValue,
);
/**
* If a type for this column has already been declared,
* only alter it if it was a number and a varchar was found
*/
if ($currType === ColumnType::None) {
continue;
}
if ($currType === ColumnType::Varchar) {
$columns[$i]->type = ColumnType::Varchar;
} elseif ($currType === ColumnType::Decimal) {
if ($columns[$i]->type !== ColumnType::Varchar) {
$columns[$i]->type = ColumnType::Decimal;
}
} elseif ($currType === ColumnType::BigInt) {
if ($columns[$i]->type !== ColumnType::Varchar && $columns[$i]->type !== ColumnType::Decimal) {
$columns[$i]->type = ColumnType::BigInt;
}
} elseif ($currType === ColumnType::Int) {
if (
$columns[$i]->type !== ColumnType::Varchar
&& $columns[$i]->type !== ColumnType::Decimal
&& $columns[$i]->type !== ColumnType::BigInt
) {
$columns[$i]->type = ColumnType::Int;
}
}
}
}
/* Check to ensure that all types are valid */
foreach ($columns as $column) {
if ($column->type !== ColumnType::None) {
continue;
}
$column->type = ColumnType::Varchar;
$column->size = 10;
}
return $columns;
}
/**
* Builds and executes SQL statements to create the database and tables
* as necessary, as well as insert all the data.
*
* @link https://wiki.phpmyadmin.net/pma/Import
*
* @param ImportTable[] $tables
* @param AnalysedColumn[][]|null $analyses Analyses of the tables
* @param string[]|null $additionalSql Additional SQL to be executed
* @param string[] $sqlData List of SQL to be executed
*/
public function buildSql(
string $dbName,
array $tables,
array|null $analyses = null,
array|null $additionalSql = null,
array &$sqlData = [],
): void {
/* Needed to quell the beast that is Message */
ImportSettings::$importNotice = '';
/* Run the $additional_sql statements supplied by the caller plug-in */
if ($additionalSql != null) {
/* Clean the SQL first */
/**
* Only match tables for now, because CREATE IF NOT EXISTS
* syntax is lacking or nonexisting for views, triggers,
* functions, and procedures.
*
* See: https://bugs.mysql.com/bug.php?id=15287
*
* To the best of my knowledge this is still an issue.
*
* $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
*/
$pattern = '/CREATE [^`]*(TABLE)/';
$replacement = 'CREATE \\1 IF NOT EXISTS';
// Change CREATE statements to CREATE IF NOT EXISTS to support inserting into existing structures.
foreach ($additionalSql as $i => $singleAdditionalSql) {
$additionalSql[$i] = preg_replace($pattern, $replacement, $singleAdditionalSql);
/* Execute the resulting statements */
$this->runQuery($additionalSql[$i], $sqlData);
}
}
if ($analyses !== null) {
/* TODO: Do more checking here to make sure they really are matched */
if (count($tables) !== count($analyses)) {
ResponseRenderer::getInstance()->callExit();
}
/* Create SQL code to create the tables */
foreach ($tables as $i => $table) {
$lastColumnKey = array_key_last($table->columns);
$tempSQLStr = 'CREATE TABLE IF NOT EXISTS '
. Util::backquote($dbName)
. '.' . Util::backquote($table->tableName) . ' (';
foreach ($table->columns as $j => $column) {
$size = $analyses[$i][$j]->size;
if ($size === 0) {
$size = 10;
}
$tempSQLStr .= Util::backquote($column) . ' ' . match ($analyses[$i][$j]->type) {
ColumnType::None => 'NULL',
ColumnType::Varchar => 'varchar',
ColumnType::Int => 'int',
ColumnType::Decimal => 'decimal',
ColumnType::BigInt => 'bigint',
ColumnType::Geometry => 'geometry',
};
if ($analyses[$i][$j]->type !== ColumnType::Geometry) {
$tempSQLStr .= '(' . $size . ')';
}
if ($j === $lastColumnKey) {
continue;
}
$tempSQLStr .= ', ';
}
$tempSQLStr .= ')';
/**
* Each SQL statement is executed immediately
* after it is formed so that we don't have
* to store them in a (possibly large) buffer
*/
$this->runQuery($tempSQLStr, $sqlData);
}
}
/**
* Create the SQL statements to insert all the data
*
* Only one insert query is formed for each table
*/
$dbi = DatabaseInterface::getInstance();
foreach ($tables as $tableIndex => $table) {
$numCols = count($table->columns);
$lastColumnKey = array_key_last($table->columns);
if ($table->rows === []) {
break;
}
$tempSQLStr = 'INSERT INTO ' . Util::backquote($dbName) . '.'
. Util::backquote($table->tableName) . ' (';
$tempSQLStr .= implode(', ', array_map(Util::backquote(...), $table->columns));
$tempSQLStr .= ') VALUES ';
$lastRowKey = array_key_last($table->rows);
foreach ($table->rows as $rowIndex => $row) {
$tempSQLStr .= '(';
for ($columnIndex = 0; $columnIndex < $numCols; ++$columnIndex) {
// If fully formatted SQL, no need to enclose
// with apostrophes, add slashes etc.
if (
$analyses !== null
&& $analyses[$tableIndex][$columnIndex]->isFullyFormattedSql
) {
$tempSQLStr .= (string) $row[$columnIndex];
} else {
if ($analyses !== null) {
$isVarchar = $analyses[$tableIndex][$columnIndex]->type === ColumnType::Varchar;
} else {
$isVarchar = ! is_numeric($row[$columnIndex]);
}
/* Don't put quotes around NULL fields */
if ((string) $row[$columnIndex] === 'NULL') {
$isVarchar = false;
}
$tempSQLStr .= $isVarchar
? $dbi->quoteString((string) $row[$columnIndex])
: (string) $row[$columnIndex];
}
if ($columnIndex === $lastColumnKey) {
continue;
}
$tempSQLStr .= ', ';
}
$tempSQLStr .= ')';
if ($rowIndex !== $lastRowKey) {
$tempSQLStr .= ",\n ";
}
/* Delete the row after we are done with it */
unset($table->rows[$rowIndex]);
}
/**
* Each SQL statement is executed immediately
* after it is formed so that we don't have
* to store them in a (possibly large) buffer
*/
$this->runQuery($tempSQLStr, $sqlData);
}
/**
* A work in progress
*/
/**
* Add the viewable structures from $additional_sql
* to $tables so they are also displayed
*/
$viewPattern = '@VIEW `[^`]+`\.`([^`]+)@';
$tablePattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@';
/* Check a third pattern to make sure its not a "USE `db_name`;" statement */
/** @var string $sql */
foreach ($additionalSql ?? [] as $sql) {
$regs = [];
preg_match($viewPattern, $sql, $regs);
if ($regs === []) {
preg_match($tablePattern, $sql, $regs);
}
if ($regs === []) {
continue;
}
foreach ($tables as $table) {
if ($regs[1] === $table->tableName) {
continue 2;
}
}
$tables[] = new ImportTable($regs[1]);
}
$message = $this->getSuccessMessage($dbName, $tables, $dbi);
ImportSettings::$importNotice = $message;
}
public function handleRollbackRequest(string $sqlQuery): void
{
$sqlDelimiter = $_POST['sql_delimiter'];
$queries = explode($sqlDelimiter, $sqlQuery);
$dbi = DatabaseInterface::getInstance();
foreach ($queries as $sqlQuery) {
if ($sqlQuery === '') {
continue;
}
// Check each query for ROLLBACK support.
if ($this->checkIfRollbackPossible($sqlQuery)) {
continue;
}
$sqlError = $dbi->getError();
$error = $sqlError !== '' ? $sqlError : __(
'Only INSERT, UPDATE, DELETE, REPLACE and SET (without options like GLOBAL) '
. 'SQL queries containing transactional engine tables can be rolled back.',
);
$response = ResponseRenderer::getInstance();
$response->addJSON('message', Message::rawError($error));
$response->callExit();
}
// If everything fine, START a transaction.
$dbi->query('START TRANSACTION');
}
/**
* Checks if ROLLBACK is possible for a SQL query or not.
*
* @param string $sqlQuery SQL query
*/
public function checkIfRollbackPossible(string $sqlQuery): bool
{
$parser = new Parser($sqlQuery);
if (empty($parser->statements[0])) {
return true;
}
$statement = $parser->statements[0];
// Check if query is supported.
if (
! ($statement instanceof InsertStatement
|| $statement instanceof UpdateStatement
|| $statement instanceof DeleteStatement
|| $statement instanceof ReplaceStatement
|| (
$statement instanceof SetStatement
&& $statement->options !== null
&& $statement->options->isEmpty()
)
)
) {
return false;
}
// Get table_references from the query.
$tables = Query::getTables($statement);
// Check if each table is 'InnoDB'.
foreach ($tables as $table) {
if (! $this->isTableTransactional($table)) {
return false;
}
}