Greenlion php sql parser

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL

License

SamKnows/greenlion-PHP-SQL-Parser

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Читайте также:  Эффекты перехода страниц

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL

Full support for the MySQL dialect for the following statement types

SELECT INSERT UPDATE DELETE REPLACE RENAME SHOW SET DROP CREATE INDEX CREATE TABLE EXPLAIN DESCRIBE 

Other SQL statement types

Other statements are returned as an array of tokens. This is not as structured as the information available about the above types. See the ParserManual for more information.

Since the MySQL SQL dialect is very close to SQL-92, this should work for most database applications that need a SQL parser. If using another database dialect, then you may want to change the reserved words — see the ParserManual. It supports UNION, subqueries and compound statements.

The parser is a self contained class. It has no external dependencies. The parser uses a small amount of regex.

The focus of the parser is complete and accurate support for the MySQL SQL dialect. The focus is not on optimizing for performance. It is expected that you will present syntactically valid queries.

Example Query

SELECT STRAIGHT_JOIN a, b, c FROM some_table an_alias WHERE d > 5;

Example Output (via print_r)

Array ( [OPTIONS] => Array ( [0] => STRAIGHT_JOIN ) [SELECT] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => a [sub_tree] => [alias] => `a` ) [1] => Array ( [expr_type] => colref [base_expr] => b [sub_tree] => [alias] => `b` ) [2] => Array ( [expr_type] => colref [base_expr] => c [sub_tree] => [alias] => `c` ) ) [FROM] => Array ( [0] => Array ( [table] => some_table [alias] => an_alias [join_type] => JOIN [ref_type] => [ref_clause] => [base_expr] => [sub_tree] => ) ) [WHERE] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => d [sub_tree] => ) [1] => Array ( [expr_type] => operator [base_expr] => > [sub_tree] => ) [2] => Array ( [expr_type] => const [base_expr] => 5 [sub_tree] => ) ) )

About

A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL

Источник

PHP-SQL-Parser

Other statements are returned as an array of tokens. This is not as structured as the information available about the above types. See the ParserManual for more information.

Other SQL dialects

Since the MySQL SQL dialect is very close to SQL-92, this should work for most database applications that need a SQL parser. If using another database dialect, then you may want to change the reserved words — see the ParserManual. It supports UNION, subqueries and compound statements.

External dependencies

The parser is a self contained class. It has no external dependencies. The parser uses a small amount of regex.

Focus

The focus of the parser is complete and accurate support for the MySQL SQL dialect. The focus is not on optimizing for performance. It is expected that you will present syntactically valid queries.

Manual

Example Output

Example Query

SELECT STRAIGHT_JOIN a, b, c FROM some_table an_alias WHERE d > 5;

Example Output (via print_r)

Array ( [OPTIONS] => Array ( [0] => STRAIGHT_JOIN ) [SELECT] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => a [sub_tree] => [alias] => `a` ) [1] => Array ( [expr_type] => colref [base_expr] => b [sub_tree] => [alias] => `b` ) [2] => Array ( [expr_type] => colref [base_expr] => c [sub_tree] => [alias] => `c` ) ) [FROM] => Array ( [0] => Array ( [table] => some_table [alias] => an_alias [join_type] => JOIN [ref_type] => [ref_clause] => [base_expr] => [sub_tree] => ) ) [WHERE] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => d [sub_tree] => ) [1] => Array ( [expr_type] => operator [base_expr] => > [sub_tree] => ) [2] => Array ( [expr_type] => const [base_expr] => 5 [sub_tree] => ) ) )

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Parser Manual

Clone this wiki locally

How to integrate PHP-SQL-Parser into your application

The parser comes with multiple PHP files, which are downloadable from https://github.com/greenlion/PHP-SQL-Parser/wiki/Downloads (stable version). It does not require any PECL packages. The latest development version is also accessible on http://code.google.com/p/php-sql-parser/source/browse/trunk.

  1. Download the SQL parser from: https://github.com/greenlion/PHP-SQL-Parser/wiki/Downloads and unzip it into your include directory.
  2. add require_once(‘php-sql-parser.php’) to your application
  3. Use the parser:
$parser = new PHPSQLParser(); $parsed = $parser->parse($sql); print_r($parsed);
  1. it is also possible to generate keyword positions during the parser step
  2. for every base_expr entry the parser stores the position within the original SQL string
$parser = new PHPSQLParser(); $parsed = $parser->parse($sql, true); print_r($parsed);

The best way to see how to use the parser is to look at the extensive examples, which you can get here:

  1. Download the SQL parser from: https://github.com/greenlion/PHP-SQL-Parser/wiki/Downloads and unzip it into your include directory.
  2. There is a file example.php, that contains a lot of examples. More examples you can find within the /tests folder.
  3. Execute the example:

There are two ways in which you can parse statements

/* The constructor simply calls the parse() method on the provided SQL for convenience.*/ $parser = new PHPSQLParser('select 1'); print_r($parser->parsed);
$parser = new PHPSQLParser(); print_r($parser->parse('select 2')); /* this is okay, the tree is saved in the _parsed_ property. /* get the tree for the last parsed statement */ $save = $parser->parsed;

There are no other public functions.

There are two ways in which you can create statements from parser output

/* The constructor simply calls the create() method on the provided parser tree output for convenience. */ $parser = new PHPSQLParser('select 1'); $creator = new PHPSQLCreator($parser->parsed); echo $creator->created;
$parser = new PHPSQLParser('select 2'); $creator = new PHPSQLCreator(); echo $creator->create($parser->parsed); /* this is okay, the SQL is saved in the _created_ property. */ /* get the SQL statement for the last parsed statement */ $save = $creator->created;

There are no other public functions.

The parsed representation returned by PHP-SQL-Parser is an associative array of important SQL sections and the information about the clauses in each of those sections. Because this is easier to visualize, I’ll provide a simple example. As I said, the parser splits up the query into sections. Later the manual will describe what sections are available each of the supported SQL statement types.

In the example the given query has three sections: SELECT, FROM, WHERE. You will see each of these sections in the parser output. Each of those sections contain items. Each item represents a keyword, a literal value, a subquery, an expression or a column reference.

In the following example, the SELECT section contains one item which is a column reference (colref). The FROM clause contains only one table. You’ll notice that it still says ‘JOIN’. Don’t be confused by this. Every table item is a join, but it may not have any join critera. Finally, the where clause consists of three items, a colref, an operator and a literal value (const).

There is a complex example which features almost all of the available SELECT syntax.

 require_once('php-sql-parser.php'); $parser=new PHPSQLParser(' SELECT a from some_table an_alias WHERE d > 5; ', true); print_r($parser->parsed);
Array ( [SELECT] => Array ( [0] => Array ( [expr_type] => colref [alias] => [base_expr] => a [sub_tree] => [position] => 8 ) ) [FROM] => Array ( [0] => Array ( [expr_type] => table [table] => some_table [alias] => Array ( [as] => [name] => an_alias [base_expr] => an_alias [position] => 29 ) [join_type] => JOIN [ref_type] => [ref_clause] => [base_expr] => some_table an_alias [sub_tree] => [position] => 18 ) ) [WHERE] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => d [sub_tree] => [position] => 45 ) [1] => Array ( [expr_type] => operator [base_expr] => > [sub_tree] => [position] => 47 ) [2] => Array ( [expr_type] => const [base_expr] => 5 [sub_tree] => [position] => 49 ) ) )

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Releases: greenlion/PHP-SQL-Parser

PHP-SQL-Parser 4.6

What’s Changed

  • fix «strpos empty needle» warning by @nicoder in #321
  • Add Additional Builders for CREATE statements by @atiernan in #363
  • Adds support for missing spaces near parentheses by @czoIg in #362
  • Add handling index hints in FROM and JOIN by @czoIg in #359
  • UnableToCalculatePositionException if a function is used inside JOIN condition by @czoIg in #358
  • Added badly documented data type alias by @xsist10 in #356
  • Fix issue 335 multiplication operator wrongly parsed as colref by @nicoder in #336
  • Prevent a warning in PHP 8.2 by @mmcev106 in #370

New Contributors

  • @atiernan made their first contribution in #363
  • @czoIg made their first contribution in #362
  • @xsist10 made their first contribution in #356
  • @mmcev106 made their first contribution in #370

Contributors

PHP-SQL-Parser 4.5.0 with PHP 8.1 support

This PHP-SQL-Parser release, version 4.4.0, has been updated to improve compatibility with newer PHP versions. Several community contributions to the parser and creator are included as well.

What’s Changed

  • Upgrade to phpunit 9 by @nicoder in #350
  • Corrected deprecated 3rd argument set to null in preg_split by @nabab in #346
  • #347 fixes deprecation warning with PHP 8.1 by @garethellis36 in #349
  • Fix support for orderByPosition (ORDER BY 1 ASC/ORDER BY 1 DESC) by @j-angnoe in #345
  • remove true , false from reseved values by @tsukasa-mixer in #343
  • Build User Varaiable Expression in FunctionBuilder by @Aramics in #340

New Contributors

  • @garethellis36 made their first contribution in #349
  • @j-angnoe made their first contribution in #345
  • @tsukasa-mixer made their first contribution in #343
  • @Aramics made their first contribution in #340

Contributors

Источник

Оцените статью