@@ -28,8 +28,9 @@ function __($str)
2828
2929namespace SqlParser {
3030
31- use SqlParser \Statements \SelectStatement ;
3231 use SqlParser \Exceptions \ParserException ;
32+ use SqlParser \Statements \SelectStatement ;
33+ use SqlParser \Statements \TransactionStatement ;
3334
3435 /**
3536 * Takes multiple tokens (contained in a Lexer instance) as input and builds a
@@ -50,47 +51,54 @@ class Parser
5051 */
5152 public static $ STATEMENT_PARSERS = array (
5253
53- 'EXPLAIN ' => 'SqlParser \\Statements \\ExplainStatement ' ,
54+ 'EXPLAIN ' => 'SqlParser \\Statements \\ExplainStatement ' ,
5455
5556 // Table Maintenance Statements
5657 // https://dev.mysql.com/doc/refman/5.7/en/table-maintenance-sql.html
57- 'ANALYZE ' => 'SqlParser \\Statements \\AnalyzeStatement ' ,
58- 'BACKUP ' => 'SqlParser \\Statements \\BackupStatement ' ,
59- 'CHECK ' => 'SqlParser \\Statements \\CheckStatement ' ,
60- 'CHECKSUM ' => 'SqlParser \\Statements \\ChecksumStatement ' ,
61- 'OPTIMIZE ' => 'SqlParser \\Statements \\OptimizeStatement ' ,
62- 'REPAIR ' => 'SqlParser \\Statements \\RepairStatement ' ,
63- 'RESTORE ' => 'SqlParser \\Statements \\RestoreStatement ' ,
58+ 'ANALYZE ' => 'SqlParser \\Statements \\AnalyzeStatement ' ,
59+ 'BACKUP ' => 'SqlParser \\Statements \\BackupStatement ' ,
60+ 'CHECK ' => 'SqlParser \\Statements \\CheckStatement ' ,
61+ 'CHECKSUM ' => 'SqlParser \\Statements \\ChecksumStatement ' ,
62+ 'OPTIMIZE ' => 'SqlParser \\Statements \\OptimizeStatement ' ,
63+ 'REPAIR ' => 'SqlParser \\Statements \\RepairStatement ' ,
64+ 'RESTORE ' => 'SqlParser \\Statements \\RestoreStatement ' ,
6465
6566 // Database Administration Statements
6667 // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-server-administration.html
67- 'SET ' => '' ,
68- 'SHOW ' => 'SqlParser \\Statements \\ShowStatement ' ,
68+ 'SET ' => '' ,
69+ 'SHOW ' => 'SqlParser \\Statements \\ShowStatement ' ,
6970
7071 // Data Definition Statements.
7172 // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-definition.html
72- 'ALTER ' => 'SqlParser \\Statements \\AlterStatement ' ,
73- 'CREATE ' => 'SqlParser \\Statements \\CreateStatement ' ,
74- 'DROP ' => 'SqlParser \\Statements \\DropStatement ' ,
75- 'RENAME ' => 'SqlParser \\Statements \\RenameStatement ' ,
76- 'TRUNCATE ' => 'SqlParser \\Statements \\TruncateStatement ' ,
73+ 'ALTER ' => 'SqlParser \\Statements \\AlterStatement ' ,
74+ 'CREATE ' => 'SqlParser \\Statements \\CreateStatement ' ,
75+ 'DROP ' => 'SqlParser \\Statements \\DropStatement ' ,
76+ 'RENAME ' => 'SqlParser \\Statements \\RenameStatement ' ,
77+ 'TRUNCATE ' => 'SqlParser \\Statements \\TruncateStatement ' ,
7778
7879 // Data Manipulation Statements.
7980 // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-manipulation.html
80- 'CALL ' => 'SqlParser \\Statements \\CallStatement ' ,
81- 'DELETE ' => 'SqlParser \\Statements \\DeleteStatement ' ,
82- 'DO ' => '' ,
83- 'HANDLER ' => '' ,
84- 'INSERT ' => 'SqlParser \\Statements \\InsertStatement ' ,
85- 'LOAD ' => '' ,
86- 'REPLACE ' => 'SqlParser \\Statements \\ReplaceStatement ' ,
87- 'SELECT ' => 'SqlParser \\Statements \\SelectStatement ' ,
88- 'UPDATE ' => 'SqlParser \\Statements \\UpdateStatement ' ,
81+ 'CALL ' => 'SqlParser \\Statements \\CallStatement ' ,
82+ 'DELETE ' => 'SqlParser \\Statements \\DeleteStatement ' ,
83+ 'DO ' => '' ,
84+ 'HANDLER ' => '' ,
85+ 'INSERT ' => 'SqlParser \\Statements \\InsertStatement ' ,
86+ 'LOAD ' => '' ,
87+ 'REPLACE ' => 'SqlParser \\Statements \\ReplaceStatement ' ,
88+ 'SELECT ' => 'SqlParser \\Statements \\SelectStatement ' ,
89+ 'UPDATE ' => 'SqlParser \\Statements \\UpdateStatement ' ,
8990
9091 // Prepared Statements.
9192 // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html
92- 'PREPARE ' => '' ,
93- 'EXECUTE ' => '' ,
93+ 'PREPARE ' => '' ,
94+ 'EXECUTE ' => '' ,
95+
96+ // Transactional and Locking Statements
97+ // https://dev.mysql.com/doc/refman/5.7/en/commit.html
98+ 'START TRANSACTION ' => 'SqlParser \\Statements \\TransactionStatement ' ,
99+ 'BEGIN ' => 'SqlParser \\Statements \\TransactionStatement ' ,
100+ 'COMMIT ' => 'SqlParser \\Statements \\TransactionStatement ' ,
101+ 'ROLLBACK ' => 'SqlParser \\Statements \\TransactionStatement ' ,
94102 );
95103
96104 /**
@@ -311,6 +319,13 @@ public function __construct($list = null, $strict = false)
311319 public function parse ()
312320 {
313321
322+ /**
323+ * Last transaction.
324+ *
325+ * @var TransactionStatement
326+ */
327+ $ lastTransaction = null ;
328+
314329 /**
315330 * Last parsed statement.
316331 * @var Statement $lastStatement
@@ -400,21 +415,40 @@ public function parse()
400415 $ statement ->last = $ list ->idx ;
401416 $ prevLastIdx = $ list ->idx ;
402417
403- // Finally, storing the statement .
418+ // Handles unions .
404419 if (($ inUnion )
405420 && ($ lastStatement instanceof SelectStatement)
406421 && ($ statement instanceof SelectStatement)
407422 ) {
423+
408424 /**
409425 * Last SELECT statement.
410426 * @var SelectStatement $lastStatement
411427 */
412428 $ lastStatement ->union [] = $ statement ;
413429 $ inUnion = false ;
430+ continue ;
431+ }
432+
433+ // Handles transactions.
434+ if ($ statement instanceof TransactionStatement) {
435+ if ($ statement ->type === TransactionStatement::TYPE_BEGIN ) {
436+ $ lastTransaction = $ statement ;
437+ $ this ->statements [] = $ statement ;
438+ } elseif ($ statement ->type === TransactionStatement::TYPE_END ) {
439+ $ lastTransaction ->end = $ statement ;
440+ $ lastTransaction = null ;
441+ }
442+ continue ;
443+ }
444+
445+ // Finally, storing the statement.
446+ if ($ lastTransaction !== null ) {
447+ $ lastTransaction ->statements [] = $ statement ;
414448 } else {
415449 $ this ->statements [] = $ statement ;
416- $ lastStatement = $ statement ;
417450 }
451+ $ lastStatement = $ statement ;
418452
419453 }
420454 }
0 commit comments