-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling of command SHOW FULL COLUMNS FOR {table_name}
Handling multiple databases for create queries, then sql is CREATE TABLE `database`.`table` and in same script is `information_schema`.`table` Fixing PDO::getAttribute() missing method Handling Multiple insert sql.
- Loading branch information
Andrew Svirin
committed
Oct 5, 2021
1 parent
5724968
commit 3544708
Showing
8 changed files
with
327 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Vimeo\MysqlEngine\Parser; | ||
|
||
use Vimeo\MysqlEngine\Query\CreateQuery; | ||
use Vimeo\MysqlEngine\Query\Expression\Expression; | ||
use Vimeo\MysqlEngine\TokenType; | ||
use Vimeo\MysqlEngine\Query\InsertQuery; | ||
|
||
final class InsertMultipleParser | ||
{ | ||
/** | ||
* @return array<string, CreateQuery> | ||
*/ | ||
public function parse(string $sql): array | ||
{ | ||
return self::walk($this->splitStatements($sql)); | ||
} | ||
|
||
/** | ||
* @var list<string> | ||
*/ | ||
private $tokens = []; | ||
|
||
/** | ||
* @var array<int, array{0:int, 1:int}> | ||
*/ | ||
private $sourceMap = []; | ||
|
||
/** | ||
* @return non-empty-list<string> | ||
*/ | ||
private function splitStatements(string $sql): array | ||
{ | ||
$re_split_sql = '% | ||
# Match an SQL record ending with ";" | ||
\s* # Discard leading whitespace. | ||
( # $1: Trimmed non-empty SQL record. | ||
(?: # Group for content alternatives. | ||
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # Either a single quoted string, | ||
| "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # or a double quoted string, | ||
| /\*[^*]*\*+(?:[^*/][^*]*\*+)*/ # or a multi-line comment, | ||
| \#.* # or a # single line comment, | ||
| --.* # or a -- single line comment, | ||
| [^"\';#] # or one non-["\';#-] | ||
)+ # One or more content alternatives | ||
(?:;|$) # Record end is a ; or string end. | ||
) # End $1: Trimmed SQL record. | ||
%xs'; | ||
|
||
if (preg_match_all($re_split_sql, $sql, $matches)) { | ||
$statements = $matches[1]; | ||
} | ||
|
||
return $statements ?? []; | ||
} | ||
|
||
/** | ||
* @param array<string> $statements | ||
* | ||
* @return array<string, InsertQuery> | ||
*/ | ||
private static function walk(array $statements) | ||
{ | ||
$result = []; | ||
|
||
foreach ($statements as $statement) { | ||
$statement = trim($statement); | ||
if (false === stripos($statement, 'INSERT INTO')) { | ||
continue; | ||
} | ||
$statement = rtrim($statement, ';'); | ||
|
||
$result[] = SQLParser::parse($statement); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.