Update website

This commit is contained in:
Guilhem Lavaux 2024-11-23 20:45:29 +01:00
parent 41ce1aa076
commit ea0eb1c6e0
4222 changed files with 721797 additions and 14 deletions

View file

@ -0,0 +1,519 @@
# Change Log
## [5.7.0] - 2023-01-25
* Performance improvement to use less the `nextToken()` function (#397)
* Lexer - Solving ambiguity on function keywords (#385)
* Implement `ALTER EVENT` (#404)
* Add `ALTER EVENT` keywords (#404)
* Drop PHP 7.1 support
* Fix the alter operation table options `RENAME INDEX x TO y` (#405)
* Fix `CreateStatement` function's options (#406)
* Fix a PHP notice on Linter using `ANALYZE` (#413)
## [5.6.0] - 2023-01-02
* Add missing return types annotations
* Improve the WITH statements parser (#363)
* Add support for passing `Context::SQL_MODE*` constants to `Context::setMode` method
* Fix additional body tokens issue with `CREATE VIEW` statements (#371)
* Exclude from composer vendor bundle /tests and /phpunit.xml.dist
* Support table structure with `COMPRESSED` columns (#351)
* Add `#[\AllowDynamicProperties]` on `Statement` and `Expression` classes for PHP 8.2 support
* Support `ALTER` queries of `PARTITIONS` (#329)
* Fixed differentiating between `ANALYZE` and `EXPLAIN` statements (#386)
* Added "NOT" to the select options (#374)
* Implement the `EXPLAIN` Parser (#389)
* Context: Updated contexts to contain `multipoint` and `multipolygon` data types (#393)
* Support more keywords on `Expression` component (#399)
* Fix PHP 8.3 failing tests (#400)
## [5.5.0] - 2021-12-08
* Add WITH support (#165, #331)
* Fixed BufferedQuery when it has an odd number of backslashes in the end (#340)
* Fixed the issue that ignored the body tokens when creating views with union (#343)
* Fixed parser errors on "ALTER TABLE" statements to add columns with SET type (#168)
* Fixed PHP 8.1 fatal errors on classes that "implements ArrayAccess"
* Add new contexts for MariaDB 10.4, 10.5, 10.6 (#328)
* Fixed parser errors for "ALTER USER" with options (#342)
* Fixed handling of the procedures and functions's options where part of the body (#339)
* Fix PHP notice "Undefined index: name in src/Components/Key.php#206" for table keys using expressions (#347)
* Added support for MySQL 8.0 table structure KEY expressions (#347)
* Added support for KEY order (ASC/DESC) (#296)
* Added missing KEY options for MySQL and MariaDB (#348)
* Added support for ENFORCED and NOT ENFORCED on table create queries (#341)
* Performance improvement to use less the "ord()" function (#352)
* Added support for OVER() with an alias (AS) (#197)
## [5.4.2] - 2021-02-05
* Added check for quoted symbol to avoid parser error in case of keyword (#317)
* Allow PHP 8
## [5.4.1] - 2020-10-15
* Fix array_key_exists warning when parsing a "DEFAULT FALSE" token (#299)
## [5.4.0] - 2020-10-08
* EXISTS is also a function. (#297)
* Fix lexer to not allow numbers with letters (#300)
* Add support for INVISIBLE keyword (#292)
* Fix the "$" might be a character used in a name (#301)
* Fix use stream_select instead of non-blocking STDIN (#309)
* Add select validation to a create view statement (#310)
## [5.3.1] - 2020-03-20
* Revert some changes with the understanding of ANSI_QUOTES mode and identifiers
* Suggest motranslator 5.0 in README
## [5.3.0] - 2020-03-20
* Stop instanciating an object to check its class name. (#290)
* Replace sscanf by equivalent native PHP functions because sscanf can be disabled for security reasons. (#270)
* Allow phpunit 9
* Allow phpmyadmin/motranslator 5.0
* Fix for php error when "INSERT INTO x SET a = 1" is "INSERT INTO x SET = 1" (#295)
* Fixed lexer fails to detect "*" as a wildcard (#288)
* Fixed ANSI_QUOTES support (#284)
* Fixed parser mistakes with comments (#156)
## [5.2.0] - 2020-01-07
* Fix ALTER TABLE ... PRIMARY/UNIQUE KEY results in error (#267)
* Prevent overwrite of offset in Limit clause by parenthesis (#275)
* Allow SCHEMA to be used in CREATE Database statement (#231)
* Add missing options in SET statement (#255)
* Add support for DROP USER statement (#259)
* Fix php error "undefined index" when replacing a non existing clause (#249)
## [5.1.0] - 2019-11-12
* Fix for PHP deprecations messages about implode for php 7.4+ (#258)
* Parse CHECK keyword on table definition (#264)
* Parse truncate statement (#221)
* Fix wrong parsing of partitions (#265)
## [5.0.0] - 2019-05-09
* Drop support for PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7.0 and HHVM
* Enable strict mode on PHP files
* Fix redundant whitespaces in build() outputs (#228)
* Fix incorrect error on DEFAULT keyword in ALTER operation (#229)
* Fix incorrect outputs from Query::getClause (#233)
* Add support for reading an SQL file from stdin
* Fix for missing tokenize-query in Composer's vendor/bin/ directory
* Fix for PHP warnings with an incomplete CASE expression (#241)
* Fix for error message with multiple CALL statements (#223)
* Recognize the question mark character as a parameter (#242)
## [4.7.4] - YYYY-MM-DD
## [4.7.3] - 2021-12-08
- Fixed BufferedQuery when it has an odd number of backslashes in the end (#340)
- Fixed the issue that ignored the body tokens when creating views with union (#343)
- Fixed parser errors on "ALTER TABLE" statements to add columns with SET type (#168)
- Fixed parser errors for "ALTER USER" with options (#342)
- Fixed handling of the procedures and functions's options where part of the body (#339)
- Fix PHP notice "Undefined index: name in src/Components/Key.php#206" for table keys using functions (#347)
- Fix MySQL 8.0 table structure KEY expression not recognized (#347)
- Fix KEY order (ASC/DESC) not part of the KEY definition (#296)
- Fix missing KEY options for MySQL and MariaDB (#348)
- Fix validation error when using ENFORCED option (#341)
## [4.7.2] - 2021-02-05
- Added check for quoted symbol to avoid parser error in case of keyword (#317)
- Adjust PHP version constraint to not support PHP 8.0 on the 4.7 series (5.x series supports it)
## [4.7.1] - 2020-10-15
* Fix array_key_exists warning when parsing a "DEFAULT FALSE" token (#299)
## [4.7.0] - 2020-10-08
* EXISTS is also a function. (#297)
* Fix lexer to not allow numbers with letters (#300)
* Add support for INVISIBLE keyword (#292)
* Fix the "$" might be a character used in a name (#301)
* Fix use stream_select instead of non-blocking STDIN (#309)
* Add select validation to a create view statement (#310)
## [4.6.1] - 2020-03-20
* Revert some changes with the understanding of ANSI_QUOTES mode and identifiers
* Suggest motranslator 4.0 in README
## [4.6.0] - 2020-03-20
* Stop instanciating an object to check its class name. (#290)
* Replace sscanf by equivalent native PHP functions because sscanf can be disabled for security reasons. (#270)
* Allow phpunit 7
* Fix for php error when "INSERT INTO x SET a = 1" is "INSERT INTO x SET = 1" (#295)
* Fixed lexer fails to detect "*" as a wildcard (#288)
* Fixed ANSI_QUOTES support (#284)
* Fixed parser mistakes with comments (#156)
## [4.5.0] - 2020-01-07
* Fix ALTER TABLE ... PRIMARY/UNIQUE KEY results in error (#267)
* Prevent overwrite of offset in Limit clause by parenthesis (#275)
* Allow SCHEMA to be used in CREATE Database statement (#231)
* Add missing options in SET statement (#255)
* Add support for DROP USER statement (#259)
* Fix php error "undefined index" when replacing a non existing clause (#249)
## [4.4.0] - 2019-11-12
* Fix for PHP deprecations messages about implode for php 7.4+ (#258)
* Parse CHECK keyword on table definition (#264)
* Parse truncate statement (#221)
* Fix wrong parsing of partitions (#265)
## [4.3.2] - 2019-06-03
Backport fixes from 5.0.0 to QA branch:
* Fix redundant whitespaces in build() outputs (#228)
* Fix incorrect error on DEFAULT keyword in ALTER operation (#229)
* Fix incorrect outputs from Query::getClause (#233)
* Add support for reading an SQL file from stdin
* Fix for missing tokenize-query in Composer's vendor/bin/ directory
* Fix for PHP warnings with an incomplete CASE expression (#241)
* Fix for error message with multiple CALL statements (#223)
* Recognize the question mark character as a parameter (#242)
## [4.3.1] - 2019-01-05
* Fixed incorrect error thrown on DEFAULT keyword in ALTER statement (#218)
## [4.3.0] - 2018-12-25
* Add support for aliases on CASE expressions (#162 and #192)
* Add support for INDEX hints in SELECT statement (#199)
* Add support for LOCK and UNLOCK TABLES statement (#180)
* Add detection of extraneous comma in UPDATE statement (#160)
* Add detection of a missing comma between two ALTER operations (#189)
* Add missing support for STRAIGHT_JOIN (#196)
* Add support for end options in SET statement (#190)
* Fix building of RENAME statements (#201)
* Add support for PURGE statements (#207)
* Add support for COLLATE keyword (#190)
## [4.2.5] - 2018-10-31
* Fix issue with CREATE OR REPLACE VIEW.
## [4.2.4] - 2017-12-06
* Fix parsing of CREATE TABLE with per field COLLATE.
* Improved Context::loadClosest to better deal with corner cases.
* Localization updates.
## [4.2.3] - 2017-10-10
* Make mbstring extension optional (though Symfony polyfill).
* Fixed build CREATE TABLE query with PARTITIONS having ENGINE but not VALUES.
## [4.2.2] - 2017-09-28
* Added support for binding parameters.
## [4.2.1] - 2017-09-08
* Fixed minor bug in Query::getFlags.
* Localization updates.
## [4.2.0] - 2017-08-30
* Initial support for MariaDB SQL contexts.
* Add support for MariaDB 10.3 INTERSECT and EXCEPT.
## [4.1.10] - 2017-08-21
* Use custom LoaderException for context loading errors.
## [4.1.9] - 2017-07-12
* Various code cleanups.
* Improved error handling of several invalid statements.
## [4.1.8] - 2017-07-09
* Fixed parsing SQL comment at the end of query.
* Improved handing of non utf-8 strings.
* Added query flag for SET queries.
## [4.1.7] - 2017-06-06
* Fixed setting combination SQL Modes.
## [4.1.6] - 2017-06-01
* Fixed building query with GROUP BY clause.
## [4.1.5] - 2017-05-15
* Fixed invalid lexing of queries with : in strings.
* Properly handle maximal length of delimiter.
## [4.1.4] - 2017-05-05
* Fixed wrong extract of string tokens with escaped characters.
* Properly handle lowercase begin statement.
## [4.1.3] - 2017-04-06
* Added support for DELETE ... JOIN clauses.
* Changed BufferedQuery to include comments in output.
* Fixed parsing of inline comments.
## [4.1.2] - 2017-02-20
* Coding style improvements.
* Chinese localization.
* Improved order validatin for JOIN clauses.
* Improved pretty printing of JOIN clauses.
* Added support for LOAD DATA statements.
## [4.1.1] - 2017-02-07
* Localization using phpmyadmin/motranslator is now optional.
* Improved testsuite.
* Better handling of non upper cased not reserved keywords.
* Minor performance and coding style improvements.
## [4.1.0] - 2017-01-23
* Use phpmyadmin/motranslator to localize messages.
## [4.0.1] - 2017-01-23
* Fixed CLI wrappers for new API.
* Fixed README for new API.
## [4.0.0] - 2017-01-23
* Added PhpMyAdmin namespace prefix to follow PSR-4.
## [3.4.17] - 2017-01-20
* Coding style fixes.
* Fixed indentation in HTML formatting.
* Fixed parsing of unterminated variables.
* Improved comments lexing.
## [3.4.16] - 2017-01-06
* Coding style fixes.
* Properly handle operators AND, NOT, OR, XOR, DIV, MOD
## [3.4.15] - 2017-01-02
* Fix return value of Formatter.toString() when type is text
* Fix parsing of FIELDS and LINES options in SELECT..INTO
* PHP 7.2 compatibility.
* Better parameter passing to query formatter.
## [3.4.14] - 2016-11-30
* Improved parsing of UNION queries.
* Recognize BINARY function.
## [3.4.13] - 2016-11-15
* Fix broken incorrect clause order detection for Joins.
* Add parsing of end options in Select statements.
## [3.4.12] - 2016-11-09
* Added verification order of SELECT statement clauses.
## [3.4.11] - 2016-10-25
* Fixed parsing of ON UPDATE option in field definition of TIMESTAMP type with precision
* Fixed parsing of NATURAL JOIN, CROSS JOIN and related joins.
* Fixed parsing of BEGIN/END labels.
## [3.4.10] - 2016-10-03
* Fixed API regression on DELETE statement
## [3.4.9] - 2016-10-03
* Added support for CASE expressions
* Support for parsing and building DELETE statement
* Support for parsing subqueries in FROM clause
## [3.4.8] - 2016-09-22
* No change release to sync GitHub releases with Packagist
## [3.4.7] - 2016-09-20
* Fix parsing of DEFINER without backquotes
* Fixed escaping HTML entities in HTML formatter
* Fixed escaping of control chars in CLI formatter
## [3.4.6] - 2016-09-13
* Fix parsing of REPLACE INTO ...
* Fix parsing of INSERT ... ON DUPLICATE KEY UPDATE ...
* Extended testsuite
* Re-enabled PHP 5.3 support
## [3.4.5] - 2016-09-13
* Fix parsing of INSERT...SELECT and INSERT...SET syntax
* Fix parsing of CREATE TABLE ... PARTITION
* Fix parsing of SET CHARACTER SET, CHARSET, NAMES
* Add Support for 'CREATE TABLE `table_copy` LIKE `table`
## [3.4.4] - 2016-04-26
* Add support for FULL OUTER JOIN
## [3.4.3] - 2016-04-19
* Fix parsing of query with \
## [3.4.2] - 2016-04-07
* Recognize UNION DISTINCT
* Recognize REGEXP and RLIKE operators
## [3.4.1] - 2016-04-06
* Add FULLTEXT and SPATIAL keywords
* Properly parse CREATE TABLE [AS] SELECT
* Fix parsing of table with DEFAULT and COMMENT
## [3.4.0] - 2016-02-23
* Fix parsing DEFAULT value on CREATE
* Fix parsing of ALTER VIEW
## [3.3.1] - 2016-02-12
* Condition: Allow keyword `INTERVAL`.
## [3.3.0] - 2016-02-12
* Expression: Refactored parsing options.
## [3.2.0] - 2016-02-11
* Context: Added custom mode that avoids escaping when possible.
## [3.1.0] - 2016-02-10
* ArrayObj: Handle more complex expressions in arrays.
* BufferedQuery: Backslashes in comments escaped characters in comments.
* Condition: Allow `IF` in conditions.
* Context: Add `;` as operator.
* Context: Updated contexts to contain `BIT` data type.
* CreateStatement: The `DEFAULT` option may be an expression.
* DescribeStatement: Added `DESC` as alias for `DESCRIBE`.
* Expression: Rewrote expression parsing.
* Misc: Added PHPUnit's Code Coverage 3.0 as a dependency.
* Misc: Added support for PHP 5.4 back.
* Misc: Removed dependency to Ctype.
* Misc: Repository transferred from @udan11 to @phpMyAdmin.
* Misc: Updated `.gitignore` to ignore `composer.lock`.
* Misc: Updated Composer and Travis configuration for PHP 7 and PHPUnit 5.
* Tools: Documented tags in `ContextGenerator`.
## [3.0.8] - 2015-12-18
* Allow `NULL` in expressions.
* Downgraded PHPUnit to 4.8. Removed old PHP versions.
* Updated PHPUnit to 5.1 and fixed some of the tests.
* Added `UNION ALL` as a type of `UNION`.
* Expressions are permitted in `SET` operations.
* Added `STRAIGHT_JOIN` as a known type of join.
* Added missing definitions for `MATCH` and `AGAINST`.
* Added missing statement (`FLUSH` and `DEALLOCATE`).
## [3.0.7] - 2015-11-12
* Expressions may begin with a function that is also a reserved keyword (e.g. `IF`).
## [3.0.6] - 2015-11-12
* Fixed a bug where formatter split the function name and the parameters list.
## [3.0.5] - 2015-11-08
* Add GRANT as known statement.
* Use JOIN expressions for flag detection.
* Fix the order of clauses in SELECT statements involving UNIONs.
* Added dummy parsers for CREATE USER and SET PASSWORD statements.
* Accept NOT operator in conditions.
* Fixed DELIMITER statements in BufferedQuery.
* Added INSERT statement builder.
## [3.0.4] - 2015-10-21
* Fix error message in `SqlParser\Components\OptionsArray`.
## [3.0.3] - 2015-10-10
* Avoid building a field multiple times if clause has synonyms.
## [3.0.2] - 2015-10-10
* Add EXISTS as an acceptable keyword in conditions.
## [3.0.1] - 2015-10-06
* Handle backslashes separately for `SqlParser\Utils\BufferedQuery`. Fixes a bug where backslashes in combination with strings weren't handled properly.
## [3.0.0] - 2015-10-02
__Breaking changes:__
* `SqlParser\Components\Reference::$table` is now an instance of `SqlParser\Components\Expression` to support references from other tables.
## [2.1.3] - 2015-10-02
* Add definitions for all JOIN clauses.
## [2.1.2] - 2015-10-02
* Properly parse options when the value of the option is '='.
## [2.1.1] - 2015-09-30
* Only RANGE and LIST type partitions support VALUES.
## [2.1.0] - 2015-09-30
* Added utilities for handling tokens and tokens list.
## [2.0.3] - 2015-09-30
* Added missing NOT IN operator. This caused troubles when parsing conditions that contained the `NOT IN` operator.
## [2.0.2] - 2015-09-30
* Added support for `OUTER` as an optional keyword in joins.
## [2.0.1] - 2015-09-30
* Fixed a bug related to (sub)partitions options not being included in the built component. Also, the option `ENGINE` was unrecognized.
## [2.0.0] - 2015-09-25
* Better parsing for CREATE TABLE statements (related to breaking change 1).
* Added support for JSON data type.
* Refactoring and minor documentation improvements.
__Breaking changes:__
* `SqlParser\Components\Key::$columns` is now an array of arrays. Each array must contain a `name` key which represents the name of the column and an optional `length` key which represents the length of the column.
## [1.0.0] - 2015-08-20
* First release of this library.

View file

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View file

@ -0,0 +1,137 @@
# SQL Parser
A validating SQL lexer and parser with a focus on MySQL dialect.
## Code status
![Tests](https://github.com/phpmyadmin/sql-parser/workflows/Run%20tests/badge.svg?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/?branch=master)
[![codecov.io](https://codecov.io/github/phpmyadmin/sql-parser/coverage.svg?branch=master)](https://codecov.io/github/phpmyadmin/sql-parser?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/?branch=master)
[![Translation status](https://hosted.weblate.org/widgets/phpmyadmin/-/svg-badge.svg)](https://hosted.weblate.org/engage/phpmyadmin/?utm_source=widget)
[![Packagist](https://img.shields.io/packagist/dt/phpmyadmin/sql-parser.svg)](https://packagist.org/packages/phpmyadmin/sql-parser)
[![Open Source Helpers](https://www.codetriage.com/phpmyadmin/sql-parser/badges/users.svg)](https://www.codetriage.com/phpmyadmin/sql-parser)
[![Type coverage](https://shepherd.dev/github/phpmyadmin/sql-parser/coverage.svg)](https://shepherd.dev/github/phpmyadmin/sql-parser)
[![Infection MSI](https://badge.stryker-mutator.io/github.com/phpmyadmin/sql-parser/master)](https://infection.github.io)
## Installation
Please use [Composer][1] to install:
```sh
composer require phpmyadmin/sql-parser
```
## Documentation
The API documentation is available at
<https://develdocs.phpmyadmin.net/sql-parser/>.
## Usage
### Command line utilities
Command line utility to syntax highlight SQL query:
```sh
./vendor/bin/highlight-query --query "SELECT 1"
```
Command line utility to lint SQL query:
```sh
./vendor/bin/lint-query --query "SELECT 1"
```
Command line utility to tokenize SQL query:
```sh
./vendor/bin/tokenize-query --query "SELECT 1"
```
All commands are able to parse input from stdin (standard in), such as:
```sh
echo "SELECT 1" | ./vendor/bin/highlight-query
cat example.sql | ./vendor/bin/lint-query
```
### Formatting SQL query
```php
echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, ['type' => 'html']);
```
### Discoverying query type
```php
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Utils\Query;
$query = 'OPTIMIZE TABLE tbl';
$parser = new Parser($query);
$flags = Query::getFlags($parser->statements[0]);
echo $flags['querytype'];
```
### Parsing and building SQL query
```php
require __DIR__ . '/vendor/autoload.php';
$query1 = 'select * from a';
$parser = new PhpMyAdmin\SqlParser\Parser($query1);
// inspect query
var_dump($parser->statements[0]); // outputs object(PhpMyAdmin\SqlParser\Statements\SelectStatement)
// modify query by replacing table a with table b
$table2 = new \PhpMyAdmin\SqlParser\Components\Expression('', 'b', '', '');
$parser->statements[0]->from[0] = $table2;
// build query again from an array of object(PhpMyAdmin\SqlParser\Statements\SelectStatement) to a string
$statement = $parser->statements[0];
$query2 = $statement->build();
var_dump($query2); // outputs string(19) 'SELECT * FROM `b` '
// Change SQL mode
PhpMyAdmin\SqlParser\Context::setMode(PhpMyAdmin\SqlParser\Context::SQL_MODE_ANSI_QUOTES);
// build the query again using different quotes
$query2 = $statement->build();
var_dump($query2); // outputs string(19) 'SELECT * FROM "b" '
```
## Localization
You can localize error messages installing `phpmyadmin/motranslator` version `5.0` or newer:
```sh
composer require phpmyadmin/motranslator:^5.0
```
The locale is automatically detected from your environment, you can also set a different locale
**From cli**:
```sh
LC_ALL=pl ./vendor/bin/lint-query --query "SELECT 1"
```
**From php**:
```php
require __DIR__ . '/vendor/autoload.php';
$GLOBALS['lang'] = 'pl';
$query1 = 'select * from a';
$parser = new PhpMyAdmin\SqlParser\Parser($query1);
```
## More information
This library was originally created during the Google Summer of Code 2015 and has been used by phpMyAdmin since version 4.5.
[1]:https://getcomposer.org/

View file

@ -0,0 +1,96 @@
{
"name": "phpmyadmin/sql-parser",
"description": "A validating SQL lexer and parser with a focus on MySQL dialect.",
"license": "GPL-2.0-or-later",
"keywords": ["sql", "lexer", "parser", "analysis", "sql syntax highlighter", "sql lexer", "sql tokenizer", "sql parser", "sql linter", "query linter"],
"homepage": "https://github.com/phpmyadmin/sql-parser",
"authors": [
{
"name": "The phpMyAdmin Team",
"email": "developers@phpmyadmin.net",
"homepage": "https://www.phpmyadmin.net/team/"
}
],
"support": {
"issues": "https://github.com/phpmyadmin/sql-parser/issues",
"source": "https://github.com/phpmyadmin/sql-parser"
},
"funding": [
{
"type": "other",
"url": "https://www.phpmyadmin.net/donate/"
}
],
"require": {
"php": "^7.2 || ^8.0",
"symfony/polyfill-mbstring": "^1.3",
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"phpbench/phpbench": "^1.1",
"phpmyadmin/coding-standard": "^3.0",
"phpmyadmin/motranslator": "^4.0 || ^5.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.9.12",
"phpstan/phpstan-phpunit": "^1.3.3",
"phpunit/php-code-coverage": "*",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
"psalm/plugin-phpunit": "^0.16.1",
"vimeo/psalm": "^4.11",
"zumba/json-serializer": "^3.0"
},
"conflict": {
"phpmyadmin/motranslator": "<3.0"
},
"suggest": {
"ext-mbstring": "For best performance",
"phpmyadmin/motranslator": "Translate messages to your favorite locale"
},
"bin": [
"bin/highlight-query",
"bin/lint-query",
"bin/tokenize-query"
],
"autoload": {
"psr-4": {
"PhpMyAdmin\\SqlParser\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"PhpMyAdmin\\SqlParser\\Tests\\": "tests"
}
},
"scripts": {
"phpcbf": "@php phpcbf",
"phpcs": "@php phpcs",
"phpstan": "@php phpstan analyse",
"psalm": "@php psalm --no-diff",
"phpunit": "@php phpunit --color=always",
"phpbench": "@php phpbench run tests/benchmarks --report=aggregate",
"test": [
"@phpcs",
"@phpstan",
"@psalm",
"@phpunit"
],
"update:baselines": [
"@php phpstan analyse --generate-baseline",
"@php psalm --set-baseline=psalm-baseline.xml"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},
"archive": {
"exclude": [
"/tests",
"/phpunit.xml.dist"
]
}
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser;
use Exception;
use Stringable;
/**
* Defines a component that is later extended to parse specialized components or keywords.
*
* There is a small difference between *Component and *Keyword classes: usually, *Component parsers can be reused in
* multiple situations and *Keyword parsers count on the *Component classes to do their job.
*
* A component (of a statement) is a part of a statement that is common to multiple query types.
*/
abstract class Component implements Stringable
{
/**
* Parses the tokens contained in the given list in the context of the given
* parser.
*
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return mixed
*
* @throws Exception not implemented yet.
*/
public static function parse(
Parser $parser,
TokensList $list,
array $options = []
) {
// This method should be abstract, but it can't be both static and
// abstract.
throw new Exception(Translator::gettext('Not implemented yet.'));
}
/**
* Builds the string representation of a component of this type.
*
* In other words, this function represents the inverse function of
* `static::parse`.
*
* @param mixed $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return mixed
*
* @throws Exception not implemented yet.
*/
public static function build($component, array $options = [])
{
// This method should be abstract, but it can't be both static and
// abstract.
throw new Exception(Translator::gettext('Not implemented yet.'));
}
/**
* Builds the string representation of a component of this type.
*
* @see static::build
*
* @return string
*/
public function __toString()
{
return static::build($this);
}
}

View file

@ -0,0 +1,539 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function array_key_exists;
use function in_array;
use function is_numeric;
use function is_string;
/**
* Parses an alter operation.
*
* @final
*/
class AlterOperation extends Component
{
/**
* All database options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $DB_OPTIONS = [
'CHARACTER SET' => [
1,
'var',
],
'CHARSET' => [
1,
'var',
],
'DEFAULT CHARACTER SET' => [
1,
'var',
],
'DEFAULT CHARSET' => [
1,
'var',
],
'UPGRADE' => [
1,
'var',
],
'COLLATE' => [
2,
'var',
],
'DEFAULT COLLATE' => [
2,
'var',
],
];
/**
* All table options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $TABLE_OPTIONS = [
'ENGINE' => [
1,
'var=',
],
'AUTO_INCREMENT' => [
1,
'var=',
],
'AVG_ROW_LENGTH' => [
1,
'var',
],
'MAX_ROWS' => [
1,
'var',
],
'ROW_FORMAT' => [
1,
'var',
],
'COMMENT' => [
1,
'var',
],
'ADD' => 1,
'ALTER' => 1,
'ANALYZE' => 1,
'CHANGE' => 1,
'CHARSET' => 1,
'CHECK' => 1,
'COALESCE' => 1,
'CONVERT' => 1,
'DEFAULT CHARSET' => 1,
'DISABLE' => 1,
'DISCARD' => 1,
'DROP' => 1,
'ENABLE' => 1,
'IMPORT' => 1,
'MODIFY' => 1,
'OPTIMIZE' => 1,
'ORDER' => 1,
'REBUILD' => 1,
'REMOVE' => 1,
'RENAME' => 1,
'REORGANIZE' => 1,
'REPAIR' => 1,
'UPGRADE' => 1,
'COLUMN' => 2,
'CONSTRAINT' => 2,
'DEFAULT' => 2,
'BY' => 2,
'FOREIGN' => 2,
'FULLTEXT' => 2,
'KEY' => 2,
'KEYS' => 2,
'PARTITION' => 2,
'PARTITION BY' => 2,
'PARTITIONING' => 2,
'PRIMARY KEY' => 2,
'SPATIAL' => 2,
'TABLESPACE' => 2,
'INDEX' => [
2,
'var',
],
'CHARACTER SET' => 3,
'TO' => [
3,
'var',
],
];
/**
* All user options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $USER_OPTIONS = [
'ATTRIBUTE' => [
1,
'var',
],
'COMMENT' => [
1,
'var',
],
'REQUIRE' => [
1,
'var',
],
'BY' => [
2,
'expr',
],
'PASSWORD' => [
2,
'var',
],
'WITH' => [
2,
'var',
],
'ACCOUNT' => 1,
'DEFAULT' => 1,
'LOCK' => 2,
'UNLOCK' => 2,
'IDENTIFIED' => 3,
];
/**
* All view options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $VIEW_OPTIONS = ['AS' => 1];
/**
* All event options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $EVENT_OPTIONS = [
'ON SCHEDULE' => 1,
'EVERY' => [
2,
'expr',
],
'AT' => [
2,
'expr',
],
'STARTS' => [
3,
'expr',
],
'ENDS' => [
4,
'expr',
],
'ON COMPLETION PRESERVE' => 5,
'ON COMPLETION NOT PRESERVE' => 5,
'RENAME' => 6,
'TO' => [
7,
'var',
],
'ENABLE' => 8,
'DISABLE' => 8,
'DISABLE ON SLAVE' => 8,
'COMMENT' => [
9,
'var',
],
'DO' => 10,
];
/**
* Options of this operation.
*
* @var OptionsArray
*/
public $options;
/**
* The altered field.
*
* @var Expression|string|null
*/
public $field;
/**
* The partitions.
*
* @var Component[]|ArrayObj|null
*/
public $partitions;
/**
* Unparsed tokens.
*
* @var Token[]|string
*/
public $unknown = [];
/**
* @param OptionsArray $options options of alter operation
* @param Expression|string|null $field altered field
* @param Component[]|ArrayObj|null $partitions partitions definition found in the operation
* @param Token[] $unknown unparsed tokens found at the end of operation
*/
public function __construct(
$options = null,
$field = null,
$partitions = null,
$unknown = []
) {
$this->partitions = $partitions;
$this->options = $options;
$this->field = $field;
$this->unknown = $unknown;
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return AlterOperation
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* Counts brackets.
*
* @var int
*/
$brackets = 0;
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ---------------------[ options ]---------------------> 1
*
* 1 ----------------------[ field ]----------------------> 2
*
* 1 -------------[ PARTITION / PARTITION BY ]------------> 3
*
* 2 -------------------------[ , ]-----------------------> 0
*
* @var int
*/
$state = 0;
/**
* partition state.
*
* @var int
*/
$partitionState = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping comments.
if ($token->type === Token::TYPE_COMMENT) {
continue;
}
// Skipping whitespaces.
if ($token->type === Token::TYPE_WHITESPACE) {
if ($state === 2) {
// When parsing the unknown part, the whitespaces are
// included to not break anything.
$ret->unknown[] = $token;
continue;
}
}
if ($state === 0) {
$ret->options = OptionsArray::parse($parser, $list, $options);
// Not only when aliasing but also when parsing the body of an event, we just list the tokens of the
// body in the unknown tokens list, as they define their own statements.
if ($ret->options->has('AS') || $ret->options->has('DO')) {
for (; $list->idx < $list->count; ++$list->idx) {
if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
break;
}
$ret->unknown[] = $list->tokens[$list->idx];
}
break;
}
$state = 1;
if ($ret->options->has('PARTITION') || $token->value === 'PARTITION BY') {
$state = 3;
$list->getPrevious(); // in order to check whether it's partition or partition by.
}
} elseif ($state === 1) {
$ret->field = Expression::parse(
$parser,
$list,
[
'breakOnAlias' => true,
'parseField' => 'column',
]
);
if ($ret->field === null) {
// No field was read. We go back one token so the next
// iteration will parse the same token, but in state 2.
--$list->idx;
}
$state = 2;
} elseif ($state === 2) {
if (is_string($token->value) || is_numeric($token->value)) {
$arrayKey = $token->value;
} else {
$arrayKey = $token->token;
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
--$brackets;
} elseif (($token->value === ',') && ($brackets === 0)) {
break;
}
} elseif (! self::checkIfTokenQuotedSymbol($token)) {
if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();
if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') {
// To avoid adding the tokens between the SET() parentheses to the unknown tokens
$list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
} elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {
// to avoid adding the `DEFAULT` token to the unknown tokens.
++$list->idx;
} else {
// We have reached the end of ALTER operation and suddenly found
// a start to new statement, but have not find a delimiter between them
$parser->error(
'A new statement was found, but no delimiter between it and the previous one.',
$token
);
break;
}
} elseif (
(array_key_exists($arrayKey, self::$DB_OPTIONS)
|| array_key_exists($arrayKey, self::$TABLE_OPTIONS))
&& ! self::checkIfColumnDefinitionKeyword($arrayKey)
) {
// This alter operation has finished, which means a comma
// was missing before start of new alter operation
$parser->error('Missing comma before start of a new alter operation.', $token);
break;
}
}
$ret->unknown[] = $token;
} elseif ($state === 3) {
if ($partitionState === 0) {
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();
if (
($token->type === Token::TYPE_KEYWORD)
&& (($token->keyword === 'PARTITION BY')
|| ($token->keyword === 'PARTITION' && $nextToken && $nextToken->value !== '('))
) {
$partitionState = 1;
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION')) {
$partitionState = 2;
}
--$list->idx; // to decrease the idx by one, because the last getNext returned and increased it.
// reverting the effect of the getNext
$list->getPrevious();
$list->getPrevious();
++$list->idx; // to index the idx by one, because the last getPrevious returned and decreased it.
} elseif ($partitionState === 1) {
// Building the expression used for partitioning.
if (empty($ret->field)) {
$ret->field = '';
}
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
} elseif ($partitionState === 2) {
$ret->partitions = ArrayObj::parse(
$parser,
$list,
['type' => PartitionDefinition::class]
);
}
}
}
if ($ret->options->isEmpty()) {
$parser->error('Unrecognized alter operation.', $list->tokens[$list->idx]);
}
--$list->idx;
return $ret;
}
/**
* @param AlterOperation $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
$ret = $component->options . ' ';
if (isset($component->field) && ($component->field !== '')) {
$ret .= $component->field . ' ';
}
$ret .= TokensList::build($component->unknown);
if (isset($component->partitions)) {
$ret .= PartitionDefinition::build($component->partitions);
}
return $ret;
}
/**
* Check if token's value is one of the common keywords
* between column and table alteration
*
* @param string $tokenValue Value of current token
*
* @return bool
*/
private static function checkIfColumnDefinitionKeyword($tokenValue)
{
$commonOptions = [
'AUTO_INCREMENT',
'COMMENT',
'DEFAULT',
'CHARACTER SET',
'COLLATE',
'PRIMARY',
'UNIQUE',
'PRIMARY KEY',
'UNIQUE KEY',
];
// Since these options can be used for
// both table as well as a specific column in the table
return in_array($tokenValue, $commonOptions);
}
/**
* Check if token is symbol and quoted with backtick
*
* @param Token $token token to check
*
* @return bool
*/
private static function checkIfTokenQuotedSymbol($token)
{
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
}
}

View file

@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\Translator;
use function count;
use function sprintf;
/**
* `VALUES` keyword parser.
*
* @final
*/
class Array2d extends Component
{
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return ArrayObj[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = [];
/**
* The number of values in each set.
*
* @var int
*/
$count = -1;
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ----------------------[ array ]----------------------> 1
*
* 1 ------------------------[ , ]------------------------> 0
* 1 -----------------------[ else ]----------------------> (END)
*
* @var int
*/
$state = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
// No keyword is expected.
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
break;
}
if ($state === 0) {
if ($token->value !== '(') {
break;
}
$arr = ArrayObj::parse($parser, $list, $options);
$arrCount = count($arr->values);
if ($count === -1) {
$count = $arrCount;
} elseif ($arrCount !== $count) {
$parser->error(
sprintf(
Translator::gettext('%1$d values were expected, but found %2$d.'),
$count,
$arrCount
),
$token
);
}
$ret[] = $arr;
$state = 1;
} elseif ($state === 1) {
if ($token->value !== ',') {
break;
}
$state = 0;
}
}
if ($state === 0) {
$parser->error('An opening bracket followed by a set of values was expected.', $list->tokens[$list->idx]);
}
--$list->idx;
return $ret;
}
/**
* @param ArrayObj[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
return ArrayObj::build($component);
}
}

View file

@ -0,0 +1,181 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function is_array;
use function strlen;
use function trim;
/**
* Parses an array.
*
* @final
*/
class ArrayObj extends Component
{
/**
* The array that contains the unprocessed value of each token.
*
* @var string[]
*/
public $raw = [];
/**
* The array that contains the processed value of each token.
*
* @var string[]
*/
public $values = [];
/**
* @param string[] $raw the unprocessed values
* @param string[] $values the processed values
*/
public function __construct(array $raw = [], array $values = [])
{
$this->raw = $raw;
$this->values = $values;
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return ArrayObj|Component[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = empty($options['type']) ? new static() : [];
/**
* The last raw expression.
*
* @var string
*/
$lastRaw = '';
/**
* The last value.
*
* @var string
*/
$lastValue = '';
/**
* Counts brackets.
*
* @var int
*/
$brackets = 0;
/**
* Last separator (bracket or comma).
*
* @var bool
*/
$isCommaLast = false;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
$lastRaw .= $token->token;
$lastValue = trim($lastValue) . ' ';
continue;
}
if (($brackets === 0) && (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '('))) {
$parser->error('An opening bracket was expected.', $token);
break;
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
if (++$brackets === 1) { // 1 is the base level.
continue;
}
} elseif ($token->value === ')') {
if (--$brackets === 0) { // Array ended.
break;
}
} elseif ($token->value === ',') {
if ($brackets === 1) {
$isCommaLast = true;
if (empty($options['type'])) {
$ret->raw[] = trim($lastRaw);
$ret->values[] = trim($lastValue);
$lastRaw = $lastValue = '';
}
}
continue;
}
}
if (empty($options['type'])) {
$lastRaw .= $token->token;
$lastValue .= $token->value;
} else {
$ret[] = $options['type']::parse(
$parser,
$list,
empty($options['typeOptions']) ? [] : $options['typeOptions']
);
}
}
// Handling last element.
//
// This is treated differently to treat the following cases:
//
// => []
// [,] => ['', '']
// [] => []
// [a,] => ['a', '']
// [a] => ['a']
$lastRaw = trim($lastRaw);
if (empty($options['type']) && ((strlen($lastRaw) > 0) || ($isCommaLast))) {
$ret->raw[] = $lastRaw;
$ret->values[] = trim($lastValue);
}
return $ret;
}
/**
* @param ArrayObj|ArrayObj[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
if (is_array($component)) {
return implode(', ', $component);
}
if (! empty($component->raw)) {
return '(' . implode(', ', $component->raw) . ')';
}
return '(' . implode(', ', $component->values) . ')';
}
}

View file

@ -0,0 +1,303 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function count;
/**
* Parses a reference to a CASE expression.
*
* @final
*/
class CaseExpression extends Component
{
/**
* The value to be compared.
*
* @var Expression|null
*/
public $value;
/**
* The conditions in WHEN clauses.
*
* @var Condition[][]
*/
public $conditions = [];
/**
* The results matching with the WHEN clauses.
*
* @var Expression[]
*/
public $results = [];
/**
* The values to be compared against.
*
* @var Expression[]
*/
public $compare_values = [];
/**
* The result in ELSE section of expr.
*
* @var Expression|null
*/
public $else_result;
/**
* The alias of this CASE statement.
*
* @var string|null
*/
public $alias;
/**
* The sub-expression.
*
* @var string
*/
public $expr = '';
public function __construct()
{
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return CaseExpression
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* State of parser.
*
* @var int
*/
$state = 0;
/**
* Syntax type (type 0 or type 1).
*
* @var int
*/
$type = 0;
++$list->idx; // Skip 'CASE'
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$newCondition = Condition::parse($parser, $list);
$type = 1;
$state = 1;
$ret->conditions[] = $newCondition;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
} else {
$ret->value = Expression::parse($parser, $list);
$type = 0;
$state = 1;
}
} elseif ($state === 1) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$newValue = Expression::parse($parser, $list);
$state = 2;
$ret->compare_values[] = $newValue;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
}
} else {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $newResult;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
}
} elseif ($state === 2) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$ret->results[] = $newResult;
$state = 1;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
}
}
}
if ($state !== 3) {
$parser->error('Unexpected end of CASE expression', $list->tokens[$list->idx - 1]);
} else {
// Parse for alias of CASE expression
$asFound = false;
for (; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
// Handle optional AS keyword before alias
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
if ($asFound || ! empty($ret->alias)) {
$parser->error('Potential duplicate alias of CASE expression.', $token);
break;
}
$asFound = true;
continue;
}
if (
$asFound
&& $token->type === Token::TYPE_KEYWORD
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED || $token->flags & Token::FLAG_KEYWORD_FUNCTION)
) {
$parser->error('An alias expected after AS but got ' . $token->value, $token);
$asFound = false;
break;
}
if (
$asFound
|| $token->type === Token::TYPE_STRING
|| ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === Token::TYPE_NONE
) {
// An alias is expected (the keyword `AS` was previously found).
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
break;
}
$ret->alias = $token->value;
$asFound = false;
continue;
}
break;
}
if ($asFound) {
$parser->error('An alias was expected after AS.', $list->tokens[$list->idx - 1]);
}
$ret->expr = self::build($ret);
}
--$list->idx;
return $ret;
}
/**
* @param CaseExpression $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
$ret = 'CASE ';
if (isset($component->value)) {
// Syntax type 0
$ret .= $component->value . ' ';
$valuesCount = count($component->compare_values);
$resultsCount = count($component->results);
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) {
$ret .= 'WHEN ' . $component->compare_values[$i] . ' ';
$ret .= 'THEN ' . $component->results[$i] . ' ';
}
} else {
// Syntax type 1
$valuesCount = count($component->conditions);
$resultsCount = count($component->results);
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) {
$ret .= 'WHEN ' . Condition::build($component->conditions[$i]) . ' ';
$ret .= 'THEN ' . $component->results[$i] . ' ';
}
}
if (isset($component->else_result)) {
$ret .= 'ELSE ' . $component->else_result . ' ';
}
$ret .= 'END';
if ($component->alias) {
$ret .= ' AS ' . Context::escape($component->alias);
}
return $ret;
}
}

View file

@ -0,0 +1,239 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function in_array;
use function is_array;
use function trim;
/**
* `WHERE` keyword parser.
*
* @final
*/
class Condition extends Component
{
/**
* Logical operators that can be used to delimit expressions.
*
* @var string[]
*/
public static $DELIMITERS = [
'&&',
'||',
'AND',
'OR',
'XOR',
];
/**
* List of allowed reserved keywords in conditions.
*
* @var array<string, int>
*/
public static $ALLOWED_KEYWORDS = [
'ALL' => 1,
'AND' => 1,
'BETWEEN' => 1,
'EXISTS' => 1,
'IF' => 1,
'IN' => 1,
'INTERVAL' => 1,
'IS' => 1,
'LIKE' => 1,
'MATCH' => 1,
'NOT IN' => 1,
'NOT NULL' => 1,
'NOT' => 1,
'NULL' => 1,
'OR' => 1,
'REGEXP' => 1,
'RLIKE' => 1,
'SOUNDS' => 1,
'XOR' => 1,
];
/**
* Identifiers recognized.
*
* @var array<int, mixed>
*/
public $identifiers = [];
/**
* Whether this component is an operator.
*
* @var bool
*/
public $isOperator = false;
/**
* The condition.
*
* @var string
*/
public $expr;
/**
* @param string $expr the condition or the operator
*/
public function __construct($expr = null)
{
$this->expr = trim((string) $expr);
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return Condition[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = [];
$expr = new static();
/**
* Counts brackets.
*
* @var int
*/
$brackets = 0;
/**
* Whether there was a `BETWEEN` keyword before or not.
*
* It is required to keep track of them because their structure contains
* the keyword `AND`, which is also an operator that delimits
* expressions.
*
* @var bool
*/
$betweenBefore = false;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if ($token->type === Token::TYPE_COMMENT) {
continue;
}
// Replacing all whitespaces (new lines, tabs, etc.) with a single
// space character.
if ($token->type === Token::TYPE_WHITESPACE) {
$expr->expr .= ' ';
continue;
}
// Conditions are delimited by logical operators.
if (in_array($token->value, static::$DELIMITERS, true)) {
if ($betweenBefore && ($token->value === 'AND')) {
// The syntax of keyword `BETWEEN` is hard-coded.
$betweenBefore = false;
} else {
// The expression ended.
$expr->expr = trim($expr->expr);
if (! empty($expr->expr)) {
$ret[] = $expr;
}
// Adding the operator.
$expr = new static($token->value);
$expr->isOperator = true;
$ret[] = $expr;
// Preparing to parse another condition.
$expr = new static();
continue;
}
}
if (
($token->type === Token::TYPE_KEYWORD)
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
&& ! ($token->flags & Token::FLAG_KEYWORD_FUNCTION)
) {
if ($token->value === 'BETWEEN') {
$betweenBefore = true;
}
if (($brackets === 0) && empty(static::$ALLOWED_KEYWORDS[$token->value])) {
break;
}
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
if ($brackets === 0) {
break;
}
--$brackets;
}
}
$expr->expr .= $token->token;
if (
($token->type !== Token::TYPE_NONE)
&& (($token->type !== Token::TYPE_KEYWORD)
|| ($token->flags & Token::FLAG_KEYWORD_RESERVED))
&& ($token->type !== Token::TYPE_STRING)
&& ($token->type !== Token::TYPE_SYMBOL)
) {
continue;
}
if (in_array($token->value, $expr->identifiers)) {
continue;
}
$expr->identifiers[] = $token->value;
}
// Last iteration was not processed.
$expr->expr = trim($expr->expr);
if (! empty($expr->expr)) {
$ret[] = $expr;
}
--$list->idx;
return $ret;
}
/**
* @param Condition[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
if (is_array($component)) {
return implode(' ', $component);
}
return $component->expr;
}
}

View file

@ -0,0 +1,362 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function is_array;
use function trim;
/**
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*
* @final
*/
class CreateDefinition extends Component
{
/**
* All field options.
*
* @var array<string, bool|int|array<int, int|string|array<string, bool>>>
* @psalm-var array<string, (bool|positive-int|array{
* 0: positive-int,
* 1: ('var'|'var='|'expr'|'expr='),
* 2?: array<string, bool>
* })>
*/
public static $FIELD_OPTIONS = [
// Tells the `OptionsArray` to not sort the options.
// See the note below.
'_UNSORTED' => true,
'NOT NULL' => 1,
'NULL' => 1,
'DEFAULT' => [
2,
'expr',
['breakOnAlias' => true],
],
/* Following are not according to grammar, but MySQL happily accepts
* these at any location */
'CHARSET' => [
2,
'var',
],
'COLLATE' => [
3,
'var',
],
'AUTO_INCREMENT' => 3,
'PRIMARY' => 4,
'PRIMARY KEY' => 4,
'UNIQUE' => 4,
'UNIQUE KEY' => 4,
'COMMENT' => [
5,
'var',
],
'COLUMN_FORMAT' => [
6,
'var',
],
'ON UPDATE' => [
7,
'expr',
],
// Generated columns options.
'GENERATED ALWAYS' => 8,
'AS' => [
9,
'expr',
['parenthesesDelimited' => true],
],
'VIRTUAL' => 10,
'PERSISTENT' => 11,
'STORED' => 11,
'CHECK' => [
12,
'expr',
['parenthesesDelimited' => true],
],
'INVISIBLE' => 13,
'ENFORCED' => 14,
'NOT' => 15,
'COMPRESSED' => 16,
// Common entries.
//
// NOTE: Some of the common options are not in the same order which
// causes troubles when checking if the options are in the right order.
// I should find a way to define multiple sets of options and make the
// parser select the right set.
//
// 'UNIQUE' => 4,
// 'UNIQUE KEY' => 4,
// 'COMMENT' => [5, 'var'],
// 'NOT NULL' => 1,
// 'NULL' => 1,
// 'PRIMARY' => 4,
// 'PRIMARY KEY' => 4,
];
/**
* The name of the new column.
*
* @var string|null
*/
public $name;
/**
* Whether this field is a constraint or not.
*
* @var bool|null
*/
public $isConstraint;
/**
* The data type of thew new column.
*
* @var DataType|null
*/
public $type;
/**
* The key.
*
* @var Key|null
*/
public $key;
/**
* The table that is referenced.
*
* @var Reference|null
*/
public $references;
/**
* The options of this field.
*
* @var OptionsArray|null
*/
public $options;
/**
* @param string|null $name the name of the field
* @param OptionsArray|null $options the options of this field
* @param DataType|Key|null $type the data type of this field or the key
* @param bool $isConstraint whether this field is a constraint or not
* @param Reference|null $references references
*/
public function __construct(
$name = null,
$options = null,
$type = null,
$isConstraint = false,
$references = null
) {
$this->name = $name;
$this->options = $options;
if ($type instanceof DataType) {
$this->type = $type;
} elseif ($type instanceof Key) {
$this->key = $type;
$this->isConstraint = $isConstraint;
$this->references = $references;
}
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return CreateDefinition[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = [];
$expr = new static();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 -----------------------[ ( ]------------------------> 1
*
* 1 --------------------[ CONSTRAINT ]------------------> 1
* 1 -----------------------[ key ]----------------------> 2
* 1 -------------[ constraint / column name ]-----------> 2
*
* 2 --------------------[ data type ]-------------------> 3
*
* 3 ---------------------[ options ]--------------------> 4
*
* 4 --------------------[ REFERENCES ]------------------> 4
*
* 5 ------------------------[ , ]-----------------------> 1
* 5 ------------------------[ ) ]-----------------------> 6 (-1)
*
* @var int
*/
$state = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
if ($state === 0) {
if (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '(')) {
$parser->error('An opening bracket was expected.', $token);
break;
}
$state = 1;
} elseif ($state === 1) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'CONSTRAINT') {
$expr->isConstraint = true;
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_KEY)) {
$expr->key = Key::parse($parser, $list);
$state = 4;
} elseif ($token->type === Token::TYPE_SYMBOL || $token->type === Token::TYPE_NONE) {
$expr->name = $token->value;
if (! $expr->isConstraint) {
$state = 2;
}
} elseif ($token->type === Token::TYPE_KEYWORD) {
if ($token->flags & Token::FLAG_KEYWORD_RESERVED) {
// Reserved keywords can't be used
// as field names without backquotes
$parser->error(
'A symbol name was expected! '
. 'A reserved keyword can not be used '
. 'as a column name without backquotes.',
$token
);
return $ret;
}
// Non-reserved keywords are allowed without backquotes
$expr->name = $token->value;
$state = 2;
} else {
$parser->error('A symbol name was expected!', $token);
return $ret;
}
} elseif ($state === 2) {
$expr->type = DataType::parse($parser, $list);
$state = 3;
} elseif ($state === 3) {
$expr->options = OptionsArray::parse($parser, $list, static::$FIELD_OPTIONS);
$state = 4;
} elseif ($state === 4) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'REFERENCES') {
++$list->idx; // Skipping keyword 'REFERENCES'.
$expr->references = Reference::parse($parser, $list);
} else {
--$list->idx;
}
$state = 5;
} elseif ($state === 5) {
if (! empty($expr->type) || ! empty($expr->key)) {
$ret[] = $expr;
}
$expr = new static();
if ($token->value === ',') {
$state = 1;
} elseif ($token->value === ')') {
$state = 6;
++$list->idx;
break;
} else {
$parser->error('A comma or a closing bracket was expected.', $token);
$state = 0;
break;
}
}
}
// Last iteration was not saved.
if (! empty($expr->type) || ! empty($expr->key)) {
$ret[] = $expr;
}
if (($state !== 0) && ($state !== 6)) {
$parser->error('A closing bracket was expected.', $list->tokens[$list->idx - 1]);
}
--$list->idx;
return $ret;
}
/**
* @param CreateDefinition|CreateDefinition[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
if (is_array($component)) {
return "(\n " . implode(",\n ", $component) . "\n)";
}
$tmp = '';
if ($component->isConstraint) {
$tmp .= 'CONSTRAINT ';
}
if (isset($component->name) && ($component->name !== '')) {
$tmp .= Context::escape($component->name) . ' ';
}
if (! empty($component->type)) {
$tmp .= DataType::build(
$component->type,
['lowercase' => true]
) . ' ';
}
if (! empty($component->key)) {
$tmp .= $component->key . ' ';
}
if (! empty($component->references)) {
$tmp .= 'REFERENCES ' . $component->references . ' ';
}
$tmp .= $component->options;
return trim($tmp);
}
}

View file

@ -0,0 +1,175 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function strtolower;
use function strtoupper;
use function trim;
/**
* Parses a data type.
*
* @final
*/
class DataType extends Component
{
/**
* All data type options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $DATA_TYPE_OPTIONS = [
'BINARY' => 1,
'CHARACTER SET' => [
2,
'var',
],
'CHARSET' => [
2,
'var',
],
'COLLATE' => [
3,
'var',
],
'UNSIGNED' => 4,
'ZEROFILL' => 5,
];
/**
* The name of the data type.
*
* @var string
*/
public $name;
/**
* The parameters of this data type.
*
* Some data types have no parameters.
* Numeric types might have parameters for the maximum number of digits,
* precision, etc.
* String types might have parameters for the maximum length stored.
* `ENUM` and `SET` have parameters for possible values.
*
* For more information, check the MySQL manual.
*
* @var int[]|string[]
*/
public $parameters = [];
/**
* The options of this data type.
*
* @var OptionsArray
*/
public $options;
/**
* @param string $name the name of this data type
* @param int[]|string[] $parameters the parameters (size or possible values)
* @param OptionsArray $options the options of this data type
*/
public function __construct(
$name = null,
array $parameters = [],
$options = null
) {
$this->name = $name;
$this->parameters = $parameters;
$this->options = $options;
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return DataType|null
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 -------------------[ data type ]--------------------> 1
*
* 1 ----------------[ size and options ]----------------> 2
*
* @var int
*/
$state = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
if ($state === 0) {
$ret->name = strtoupper((string) $token->value);
if (($token->type !== Token::TYPE_KEYWORD) || (! ($token->flags & Token::FLAG_KEYWORD_DATA_TYPE))) {
$parser->error('Unrecognized data type.', $token);
}
$state = 1;
} elseif ($state === 1) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
$parameters = ArrayObj::parse($parser, $list);
++$list->idx;
$ret->parameters = ($ret->name === 'ENUM') || ($ret->name === 'SET') ?
$parameters->raw : $parameters->values;
}
$ret->options = OptionsArray::parse($parser, $list, static::$DATA_TYPE_OPTIONS);
++$list->idx;
break;
}
}
if (empty($ret->name)) {
return null;
}
--$list->idx;
return $ret;
}
/**
* @param DataType $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
$name = empty($options['lowercase']) ?
$component->name : strtolower($component->name);
$parameters = '';
if (! empty($component->parameters)) {
$parameters = '(' . implode(',', $component->parameters) . ')';
}
return trim($name . $parameters . ' ' . $component->options);
}
}

View file

@ -0,0 +1,487 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function is_array;
use function rtrim;
use function strlen;
use function trim;
/**
* Parses a reference to an expression (column, table or database name, function
* call, mathematical expression, etc.).
*
* @final
*/
#[\AllowDynamicProperties]
class Expression extends Component
{
/**
* List of allowed reserved keywords in expressions.
*
* @var array<string, int>
*/
private static $ALLOWED_KEYWORDS = [
'AND' => 1,
'AS' => 1,
'BETWEEN' => 1,
'CASE' => 1,
'DUAL' => 1,
'DIV' => 1,
'IS' => 1,
'MOD' => 1,
'NOT' => 1,
'NOT NULL' => 1,
'NULL' => 1,
'OR' => 1,
'OVER' => 1,
'REGEXP' => 1,
'RLIKE' => 1,
'XOR' => 1,
];
/**
* The name of this database.
*
* @var string|null
*/
public $database;
/**
* The name of this table.
*
* @var string|null
*/
public $table;
/**
* The name of the column.
*
* @var string|null
*/
public $column;
/**
* The sub-expression.
*
* @var string|null
*/
public $expr = '';
/**
* The alias of this expression.
*
* @var string|null
*/
public $alias;
/**
* The name of the function.
*
* @var mixed
*/
public $function;
/**
* The type of subquery.
*
* @var string|null
*/
public $subquery;
/**
* Syntax:
* new Expression('expr')
* new Expression('expr', 'alias')
* new Expression('database', 'table', 'column')
* new Expression('database', 'table', 'column', 'alias')
*
* If the database, table or column name is not required, pass an empty
* string.
*
* @param string|null $database The name of the database or the expression.
* @param string|null $table The name of the table or the alias of the expression.
* @param string|null $column the name of the column
* @param string|null $alias the name of the alias
*/
public function __construct($database = null, $table = null, $column = null, $alias = null)
{
if (($column === null) && ($alias === null)) {
$this->expr = $database; // case 1
$this->alias = $table; // case 2
} else {
$this->database = $database; // case 3
$this->table = $table; // case 3
$this->column = $column; // case 3
$this->alias = $alias; // case 4
}
}
/**
* Possible options:.
*
* `field`
*
* First field to be filled.
* If this is not specified, it takes the value of `parseField`.
*
* `parseField`
*
* Specifies the type of the field parsed. It may be `database`,
* `table` or `column`. These expressions may not include
* parentheses.
*
* `breakOnAlias`
*
* If not empty, breaks when the alias occurs (it is not included).
*
* `breakOnParentheses`
*
* If not empty, breaks when the first parentheses occurs.
*
* `parenthesesDelimited`
*
* If not empty, breaks after last parentheses occurred.
*
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return Expression|null
*
* @throws ParserException
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* Whether current tokens make an expression or a table reference.
*
* @var bool
*/
$isExpr = false;
/**
* Whether a period was previously found.
*
* @var bool
*/
$dot = false;
/**
* Whether an alias is expected. Is 2 if `AS` keyword was found.
*
* @var bool
*/
$alias = false;
/**
* Counts brackets.
*
* @var int
*/
$brackets = 0;
/**
* Keeps track of the last two previous tokens.
*
* @var Token[]
*/
$prev = [
null,
null,
];
// When a field is parsed, no parentheses are expected.
if (! empty($options['parseField'])) {
$options['breakOnParentheses'] = true;
$options['field'] = $options['parseField'];
}
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if ($isExpr) {
$ret->expr .= $token->token;
}
continue;
}
if ($token->type === Token::TYPE_KEYWORD) {
if (($brackets > 0) && empty($ret->subquery) && ! empty(Parser::$STATEMENT_PARSERS[$token->keyword])) {
// A `(` was previously found and this keyword is the
// beginning of a statement, so this is a subquery.
$ret->subquery = $token->keyword;
} elseif (
($token->flags & Token::FLAG_KEYWORD_FUNCTION)
&& (empty($options['parseField'])
&& ! $alias)
) {
$isExpr = true;
} elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED) && ($brackets === 0)) {
if (empty(self::$ALLOWED_KEYWORDS[$token->keyword])) {
// A reserved keyword that is not allowed in the
// expression was found so the expression must have
// ended and a new clause is starting.
break;
}
if ($token->keyword === 'AS') {
if (! empty($options['breakOnAlias'])) {
break;
}
if ($alias) {
$parser->error('An alias was expected.', $token);
break;
}
$alias = true;
continue;
}
if ($token->keyword === 'CASE') {
// For a use of CASE like
// 'SELECT a = CASE .... END, b=1, `id`, ... FROM ...'
$tempCaseExpr = CaseExpression::parse($parser, $list);
$ret->expr .= CaseExpression::build($tempCaseExpr);
$isExpr = true;
continue;
}
$isExpr = true;
} elseif ($brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias) {
/* End of expression */
break;
}
}
if (
($token->type === Token::TYPE_NUMBER)
|| ($token->type === Token::TYPE_BOOL)
|| (($token->type === Token::TYPE_SYMBOL)
&& ($token->flags & Token::FLAG_SYMBOL_VARIABLE))
|| (($token->type === Token::TYPE_SYMBOL)
&& ($token->flags & Token::FLAG_SYMBOL_PARAMETER))
|| (($token->type === Token::TYPE_OPERATOR)
&& ($token->value !== '.'))
) {
if (! empty($options['parseField'])) {
break;
}
// Numbers, booleans and operators (except dot) are usually part
// of expressions.
$isExpr = true;
}
if ($token->type === Token::TYPE_OPERATOR) {
if (! empty($options['breakOnParentheses']) && (($token->value === '(') || ($token->value === ')'))) {
// No brackets were expected.
break;
}
if ($token->value === '(') {
++$brackets;
if (
empty($ret->function) && ($prev[1] !== null)
&& (($prev[1]->type === Token::TYPE_NONE)
|| ($prev[1]->type === Token::TYPE_SYMBOL)
|| (($prev[1]->type === Token::TYPE_KEYWORD)
&& ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION)))
) {
$ret->function = $prev[1]->value;
}
} elseif ($token->value === ')') {
if ($brackets === 0) {
// Not our bracket
break;
}
--$brackets;
if ($brackets === 0) {
if (! empty($options['parenthesesDelimited'])) {
// The current token is the last bracket, the next
// one will be outside the expression.
$ret->expr .= $token->token;
++$list->idx;
break;
}
} elseif ($brackets < 0) {
// $parser->error('Unexpected closing bracket.', $token);
// $brackets = 0;
break;
}
} elseif ($token->value === ',') {
// Expressions are comma-delimited.
if ($brackets === 0) {
break;
}
}
}
// Saving the previous tokens.
$prev[0] = $prev[1];
$prev[1] = $token;
if ($alias) {
// An alias is expected (the keyword `AS` was previously found).
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
break;
}
$ret->alias = $token->value;
$alias = false;
} elseif ($isExpr) {
// Handling aliases.
if (
$brackets === 0
&& ($prev[0] === null
|| (($prev[0]->type !== Token::TYPE_OPERATOR || $prev[0]->token === ')')
&& ($prev[0]->type !== Token::TYPE_KEYWORD
|| ! ($prev[0]->flags & Token::FLAG_KEYWORD_RESERVED))))
&& (($prev[1]->type === Token::TYPE_STRING)
|| ($prev[1]->type === Token::TYPE_SYMBOL
&& ! ($prev[1]->flags & Token::FLAG_SYMBOL_VARIABLE))
|| ($prev[1]->type === Token::TYPE_NONE))
) {
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
break;
}
$ret->alias = $prev[1]->value;
} else {
$currIdx = $list->idx;
--$list->idx;
$beforeToken = $list->getPrevious();
$list->idx = $currIdx;
// columns names tokens are of type NONE, or SYMBOL (`col`), and the columns options
// would start with a token of type KEYWORD, in that case, we want to have a space
// between the tokens.
if (
$ret->expr !== null &&
$beforeToken &&
($beforeToken->type === Token::TYPE_NONE ||
$beforeToken->type === Token::TYPE_SYMBOL || $beforeToken->type === Token::TYPE_STRING) &&
$token->type === Token::TYPE_KEYWORD
) {
$ret->expr = rtrim($ret->expr, ' ') . ' ';
}
$ret->expr .= $token->token;
}
} elseif (! $isExpr) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '.')) {
// Found a `.` which means we expect a column name and
// the column name we parsed is actually the table name
// and the table name is actually a database name.
if (! empty($ret->database) || $dot) {
$parser->error('Unexpected dot.', $token);
}
$ret->database = $ret->table;
$ret->table = $ret->column;
$ret->column = null;
$dot = true;
$ret->expr .= $token->token;
} else {
$field = empty($options['field']) ? 'column' : $options['field'];
if (empty($ret->$field)) {
$ret->$field = $token->value;
$ret->expr .= $token->token;
$dot = false;
} else {
// No alias is expected.
if (! empty($options['breakOnAlias'])) {
break;
}
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
break;
}
$ret->alias = $token->value;
}
}
}
}
if ($alias) {
$parser->error('An alias was expected.', $list->tokens[$list->idx - 1]);
}
// White-spaces might be added at the end.
$ret->expr = trim((string) $ret->expr);
if ($ret->expr === '') {
return null;
}
--$list->idx;
return $ret;
}
/**
* @param Expression|Expression[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
if (is_array($component)) {
return implode(', ', $component);
}
if ($component->expr !== '' && $component->expr !== null) {
$ret = $component->expr;
} else {
$fields = [];
if (isset($component->database) && ($component->database !== '')) {
$fields[] = $component->database;
}
if (isset($component->table) && ($component->table !== '')) {
$fields[] = $component->table;
}
if (isset($component->column) && ($component->column !== '')) {
$fields[] = $component->column;
}
$ret = implode('.', Context::escape($fields));
}
if (! empty($component->alias)) {
$ret .= ' AS ' . Context::escape($component->alias);
}
return $ret;
}
}

View file

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function count;
use function implode;
use function is_array;
use function preg_match;
use function strlen;
use function substr;
/**
* Parses a list of expressions delimited by a comma.
*
* @final
*/
class ExpressionArray extends Component
{
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return Expression[]
*
* @throws ParserException
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = [];
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ----------------------[ array ]---------------------> 1
*
* 1 ------------------------[ , ]------------------------> 0
* 1 -----------------------[ else ]----------------------> (END)
*
* @var int
*/
$state = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
if (
($token->type === Token::TYPE_KEYWORD)
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
&& ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION))
&& ($token->value !== 'DUAL')
&& ($token->value !== 'NULL')
&& ($token->value !== 'CASE')
&& ($token->value !== 'NOT')
) {
// No keyword is expected.
break;
}
if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD && $token->value === 'CASE') {
$expr = CaseExpression::parse($parser, $list, $options);
} else {
$expr = Expression::parse($parser, $list, $options);
}
if ($expr === null) {
break;
}
$ret[] = $expr;
$state = 1;
} elseif ($state === 1) {
if ($token->value !== ',') {
break;
}
$state = 0;
}
}
if ($state === 0) {
$parser->error('An expression was expected.', $list->tokens[$list->idx]);
}
--$list->idx;
if (is_array($ret)) {
$retIndex = count($ret) - 1;
if (isset($ret[$retIndex])) {
$expr = $ret[$retIndex]->expr;
if (preg_match('/\s*--\s.*$/', $expr, $matches)) {
$found = $matches[0];
$ret[$retIndex]->expr = substr($expr, 0, strlen($expr) - strlen($found));
}
}
}
return $ret;
}
/**
* @param Expression[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
$ret = [];
foreach ($component as $frag) {
$ret[] = $frag::build($frag);
}
return implode(', ', $ret);
}
}

View file

@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function is_array;
/**
* Parses a function call.
*
* @final
*/
class FunctionCall extends Component
{
/**
* The name of this function.
*
* @var string|null
*/
public $name;
/**
* The list of parameters.
*
* @var ArrayObj|null
*/
public $parameters;
/**
* @param string|null $name the name of the function to be called
* @param string[]|ArrayObj|null $parameters the parameters of this function
*/
public function __construct($name = null, $parameters = null)
{
$this->name = $name;
if (is_array($parameters)) {
$this->parameters = new ArrayObj($parameters);
} elseif ($parameters instanceof ArrayObj) {
$this->parameters = $parameters;
}
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return FunctionCall
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ----------------------[ name ]-----------------------> 1
*
* 1 --------------------[ parameters ]-------------------> (END)
*
* @var int
*/
$state = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}
if ($state === 0) {
$ret->name = $token->value;
$state = 1;
} elseif ($state === 1) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
$ret->parameters = ArrayObj::parse($parser, $list);
}
break;
}
}
return $ret;
}
/**
* @param FunctionCall $component the component to be built
* @param array<string, mixed> $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
return $component->name . $component->parameters;
}
}

Some files were not shown because too many files have changed in this diff Show more