### Execute Example Script Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Run the main example script to see various parser functionalities in action. This requires the parser to be downloaded and included. ```bash php examples/example.php ``` -------------------------------- ### Install PHP_CodeSniffer Source: https://github.com/greenlion/php-sql-parser/blob/master/libs/codesniffer/usage.txt Install PHP_CodeSniffer using the PEAR package manager. This is a prerequisite for running code checks. ```bash pear install PHP_CodeSniffer ``` -------------------------------- ### Execute PHPUnit Tests Source: https://github.com/greenlion/php-sql-parser/blob/master/tests/readme.txt Run PHPUnit tests after installing Composer dependencies. Specify the test case file or directory to execute. ```bash $PROJECT_ROOT/vendor/bin/phpunit --bootstrap $PROJECT_ROOT/tests/bootstrap.php ``` -------------------------------- ### Basic SQL Parsing Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Instantiate the parser and parse a SQL string. The parsed output is then printed. ```php $parser = new PHPSQLParser(); $parsed = $parser->parse($sql); print_r($parsed); ``` -------------------------------- ### Create SQL using create() Method Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Instantiate PHPSQLCreator and then use the create() method to generate SQL from parsed data. The created SQL is saved in the _created_ property. ```php $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; ``` -------------------------------- ### Create SQL using Constructor Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Instantiate PHPSQLCreator with parsed SQL data. The constructor automatically calls the create() method for convenience. ```php /* 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; ``` -------------------------------- ### Parse a SELECT Query Source: https://github.com/greenlion/php-sql-parser/blob/master/README.md Demonstrates parsing a complex SELECT query with options, table aliases, and a WHERE clause. The output shows the structured representation of the parsed SQL. ```sql SELECT STRAIGHT_JOIN a, b, c FROM some_table an_alias WHERE d > 5; ``` ```php 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] => ) ) ) ``` -------------------------------- ### Parse SQL using Constructor Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Instantiate PHPSQLParser with a SQL string. The constructor automatically calls the parse() method for convenience. ```php /* The constructor simply calls the parse() method on the provided SQL for convenience.*/ $parser = new PHPSQLParser('select 1'); print_r($parser->parsed); ``` -------------------------------- ### Parse SQL using parse() Method Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Instantiate PHPSQLParser and then use the parse() method to process a SQL string. The parsed tree is saved in the _parsed_ property. ```php $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; ``` -------------------------------- ### Check Code with PHP_CodeSniffer Source: https://github.com/greenlion/php-sql-parser/blob/master/libs/codesniffer/usage.txt Execute PHP_CodeSniffer from the command line to check code against a specified standard and redirect errors to a file. Review the errors using 'vi'. ```bash phpcs --standard=/path/to/the/PhOSCo/folder /path/to/the/src/folder >/tmp/code-errors.txt vi /tmp/code-errors.txt ``` -------------------------------- ### SQL Parsing with Keyword Positions Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Parse a SQL string and generate keyword positions. The parser stores the position within the original SQL string for every base_expr entry. ```php $parser = new PHPSQLParser(); $parsed = $parser->parse($sql, true); print_r($parsed); ``` -------------------------------- ### Configure PHP_CodeSniffer as Eclipse External Tool Source: https://github.com/greenlion/php-sql-parser/blob/master/libs/codesniffer/usage.txt Set up PHP_CodeSniffer as an external tool in Eclipse for on-demand code analysis. Configure the tool's location, arguments, and build settings. ```bash --standard="${project_loc}/libs/codesniffer/PhOSCo" "${selected_resource_loc}" ``` -------------------------------- ### Parse SQL Query Source: https://github.com/greenlion/php-sql-parser/wiki/Parser-Manual Use this snippet to parse a simple SQL SELECT query and view the resulting parsed structure. Ensure the PHPSQLParser class is included. ```php 5; ', true); print_r($parser->parsed); ``` ```php 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 ) ) ) ``` -------------------------------- ### SQL Subquery Parsing Source: https://github.com/greenlion/php-sql-parser/wiki/Complex-Example This snippet illustrates the parsing of a SQL subquery within a larger statement. It shows how the parser identifies and structures subqueries, including their SELECT clauses and aliases. ```php $sql = "SELECT (select c1+c2 from t1 inner_t1 limit 1) as subquery FROM t4"; $parser = new asic_sql_parser($sql); print_r( $parser->parse( [ 'expr_type' => [ 'expression' ] ] ) ); ``` -------------------------------- ### Complex SQL Parse Output Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This output represents a complex SQL query's parsed structure, detailing the HAVING, ORDER, and LIMIT clauses. It's useful for debugging or analyzing parsed SQL statements. ```php [HAVING] => Array ( [0] => Array ( [expr_type] => aggregate_function [base_expr] => sum [sub_tree] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => c2 [sub_tree] => [position] => 533 ) ) [position] => 529 ) [1] => Array ( [expr_type] => operator [base_expr] => > [sub_tree] => [position] => 537 ) [2] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 539 ) ) [ORDER] => Array ( [0] => Array ( [expr_type] => pos [base_expr] => 2 [direction] => ASC [position] => 550 ) [1] => Array ( [expr_type] => alias [base_expr] => c1 [direction] => DESC [position] => 553 ) ) [LIMIT] => Array ( [offset] => 0 [rowcount] => 10 ) ) ``` -------------------------------- ### Complex SQL Query Parse Output Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet displays the structured output of parsing a complex SQL query. It highlights the hierarchical representation of the query, including subqueries, column references, operators, table joins with aliases, and limit clauses. This output is useful for understanding how the parser breaks down intricate SQL statements. ```json { "expr_type": "subquery", "base_expr": "(select c1+c2 from t1 inner_t1 limit 1)", "sub_tree": { "SELECT": [ { "expr_type": "expression", "alias": null, "base_expr": "c1+c2", "sub_tree": [ { "expr_type": "colref", "base_expr": "c1", "sub_tree": null, "position": 196 }, { "expr_type": "operator", "base_expr": "+", "sub_tree": null, "position": 198 }, { "expr_type": "colref", "base_expr": "c2", "sub_tree": null, "position": 199 } ], "position": 196 } ], "FROM": [ { "expr_type": "table", "table": "t1", "alias": { "as": null, "name": "inner_t1", "base_expr": "inner_t1", "position": 210 }, "join_type": "JOIN", "ref_type": null, "ref_clause": null, "base_expr": "t1 inner_t1", "sub_tree": null, "position": 207 } ], "LIMIT": { "offset": null, "rowcount": 1 } }, "position": 188 } ``` -------------------------------- ### Complex SQL Query Parse Output Source: https://github.com/greenlion/php-sql-parser/wiki/Complex-Example This snippet displays the detailed array structure representing a parsed SQL query. It includes the breakdown of WHERE clauses with column references, operators, constants, and bracketed expressions, as well as the GROUP BY clause. ```php Array ( [expr_type] => SELECT [base_expr] => [sub_tree] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => col1 [sub_tree] => [position] => 6 ) [1] => Array ( [expr_type] => operator [base_expr] =>, [sub_tree] => [position] => 10 ) [2] => Array ( [expr_type] => colref [base_expr] => col2 [sub_tree] => [position] => 12 ) ) [FROM] => Array ( [0] => Array ( [expr_type] => table [base_expr] => some_table [sub_tree] => [position] => 27 ) ) [WHERE] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => x [sub_tree] => [position] => 43 ) [1] => Array ( [expr_type] => operator [base_expr] => = [sub_tree] => [position] => 45 ) [2] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 47 ) [3] => Array ( [expr_type] => operator [base_expr] => and [sub_tree] => [position] => 51 ) [4] => Array ( [expr_type] => bracket_expression [base_expr] => (y = 2) [sub_tree] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => y [sub_tree] => [position] => 56 ) [1] => Array ( [expr_type] => operator [base_expr] => = [sub_tree] => [position] => 58 ) [2] => Array ( [expr_type] => const [base_expr] => 2 [sub_tree] => [position] => 60 ) ) [position] => 55 ) [5] => Array ( [expr_type] => operator [base_expr] => and [sub_tree] => [position] => 63 ) [6] => Array ( [expr_type] => bracket_expression [base_expr] => ("zebra" = "orange" or 1 = 1) [sub_tree] => Array ( [0] => Array ( [expr_type] => const [base_expr] => "zebra" [sub_tree] => [position] => 70 ) [1] => Array ( [expr_type] => operator [base_expr] => = [sub_tree] => [position] => 78 ) [2] => Array ( [expr_type] => const [base_expr] => "orange" [sub_tree] => [position] => 80 ) [3] => Array ( [expr_type] => operator [base_expr] => or [sub_tree] => [position] => 90 ) [4] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 93 ) [5] => Array ( [expr_type] => operator [base_expr] => = [sub_tree] => [position] => 95 ) [6] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 97 ) ) [position] => 69 ) ) [GROUP] => Array ( [0] => Array ( [expr_type] => pos [base_expr] => 1 [position] => 517 ) [1] => Array ( [expr_type] => pos [base_expr] => 2 [position] => 520 ) ) ) ``` -------------------------------- ### Complex SQL CASE Statement Parsing Source: https://github.com/greenlion/php-sql-parser/wiki/Complex-Example This snippet shows the parsed structure of a SQL CASE statement used to determine product status based on quantity. It highlights how the parser breaks down conditional logic, comparisons, and string literals. ```php $sql = "SELECT Status = CASE WHEN quantity > 0 THEN 'in stock' ELSE 'out of stock' END FROM products"; $parser = new asic_sql_parser($sql); print_r( $parser->parse( [ 'expr_type' => [ 'const', 'operator', 'reserved', 'colref' ] ] ) ); ``` -------------------------------- ### Parsed Column Reference Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet represents a simple column reference within the parsed SQL. It includes the base expression for the column name. ```json { "expr_type": "colref", "alias": null, "base_expr": "t4.c1", "sub_tree": null, "position": 181 } ``` -------------------------------- ### Parse Complex SQL Query Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet demonstrates how to parse a complex SQL query string using the PHPSQLParser class. It includes various SQL features like DISTINCT, CASE statements, subqueries, joins, WHERE clauses with complex conditions, GROUP BY, HAVING, ORDER BY, LIMIT, INTO OUTFILE, and FOR UPDATE. Ensure the PHPSQLParser class is included via require_once. ```php require_once('php-sql-parser.php'); $sql = 'select DISTINCT 1+2 c1, 1+ 2 as `c2`, sum(c2),sum(c3) as sum_c3,"Status" = CASE WHEN quantity > 0 THEN \'in stock\' ELSE \'out of stock\' END case_statement , t4.c1, (select c1+c2 from t1 inner_t1 limit 1) as subquery into @a1, @a2, @a3 from t1 the_t1 left outer join t2 using(c1,c2) join t3 as tX ON tX.c1 = the_t1.c1 join t4 t4_x using(x) where c1 = 1 and c2 in (1,2,3, "apple") and exists ( select 1 from some_other_table another_table where x > 1) and ("zebra" = "orange" or 1 = 1) group by 1, 2 having sum(c2) > 1 ORDER BY 2, c1 DESC LIMIT 0, 10 into outfile "/xyz" FOR UPDATE LOCK IN SHARE MODE'; $parser = new PHPSQLParser($sql, true); print_r($parser->parsed); ``` -------------------------------- ### Complex SQL Query Parse Output Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet displays the detailed array output generated by parsing a complex SQL query. It shows the hierarchical structure of SELECT clauses, including expressions, aliases, aggregate functions, and case statements. ```php Array ( [SELECT] => Array ( [0] => Array ( [expr_type] => expression [alias] => Array ( [as] => [name] => c1 [base_expr] => c1 [position] => 22 ) [base_expr] => 1+2 [sub_tree] => Array ( [0] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 16 ) [1] => Array ( [expr_type] => operator [base_expr] => + [sub_tree] => [position] => 17 ) [2] => Array ( [expr_type] => const [base_expr] => 2 [sub_tree] => [position] => 18 ) ) [position] => 16 ) [1] => Array ( [expr_type] => expression [alias] => Array ( [as] => 1 [name] => c2 [base_expr] => as `c2` [position] => 31 ) [base_expr] => 1+ 2 [sub_tree] => Array ( [0] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 26 ) [1] => Array ( [expr_type] => operator [base_expr] => + [sub_tree] => [position] => 27 ) [2] => Array ( [expr_type] => const [base_expr] => 2 [sub_tree] => [position] => 29 ) ) [position] => 26 ) [2] => Array ( [expr_type] => aggregate_function [alias] => [base_expr] => sum [sub_tree] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => c2 [sub_tree] => [position] => 44 ) ) [position] => 40 ) [3] => Array ( [expr_type] => aggregate_function [alias] => Array ( [as] => 1 [name] => sum_c3 [base_expr] => as sum_c3 [position] => 56 ) [base_expr] => sum [sub_tree] => Array ( [0] => Array ( [expr_type] => colref [base_expr] => c3 [sub_tree] => [position] => 52 ) ) [position] => 48 ) [4] => Array ( [expr_type] => expression [alias] => Array ( [as] => [name] => case_statement [base_expr] => case_statement [position] => 164 ) ``` -------------------------------- ### Complex SQL Query Parse Output Source: https://github.com/greenlion/php-sql-parser/wiki/Complex-Example This array represents the structured output of a complex SQL query parsed by the PHP SQL Parser. It details each component of the query, including expression types, base expressions, and their positions within the original SQL string. Useful for understanding the parser's internal representation of SQL. ```php ( [0] => Array ( [expr_type] => colref [base_expr] => c1 [sub_tree] => [position] => 369 ) [1] => Array ( [expr_type] => operator [base_expr] => = [sub_tree] => [position] => 372 ) [2] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 374 ) [3] => Array ( [expr_type] => operator [base_expr] => and [sub_tree] => [position] => 376 ) [4] => Array ( [expr_type] => colref [base_expr] => c2 [sub_tree] => [position] => 380 ) [5] => Array ( [expr_type] => operator [base_expr] => in [sub_tree] => [position] => 383 ) [6] => Array ( [expr_type] => in-list [base_expr] => (1,2,3, "apple") [sub_tree] => Array ( [0] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 387 ) [1] => Array ( [expr_type] => const [base_expr] => 2 [sub_tree] => [position] => 389 ) [2] => Array ( [expr_type] => const [base_expr] => 3 [sub_tree] => [position] => 391 ) [3] => Array ( [expr_type] => const [base_expr] => "apple" [sub_tree] => [position] => 394 ) ) [position] => 386 ) [7] => Array ( [expr_type] => operator [base_expr] => and [sub_tree] => [position] => 403 ) [8] => Array ( [expr_type] => reserved [base_expr] => exists [sub_tree] => [position] => 407 ) [9] => Array ( [expr_type] => subquery [base_expr] => ( select 1 from some_other_table another_table where x > 1) [sub_tree] => Array ( [SELECT] => Array ( [0] => Array ( [expr_type] => const [alias] => [base_expr] => 1 [sub_tree] => [position] => 423 ) ) [FROM] => Array ( [0] => Array ( [expr_type] => table [table] => some_other_table [alias] => Array ( [as] => [name] => another_table [base_expr] => another_table [position] => 447 ) [join_type] => JOIN [position] => 434 ) ) [WHERE] => Array ( [0] => Array ( [expr_type] => operator [base_expr] => > [sub_tree] => [position] => 464 ) [1] => Array ( [expr_type] => colref [base_expr] => x [sub_tree] => [position] => 462 ) [2] => Array ( [expr_type] => const [base_expr] => 1 [sub_tree] => [position] => 466 ) ) ) [position] => 407 ) ) ``` -------------------------------- ### Complex SQL Query Structure Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This array represents the parsed structure of a complex SQL query. It details table joins, aliases, and conditions used in the query. ```json { "OPTIONS": [ "DISTINCT", "FOR UPDATE", "LOCK IN SHARE MODE" ], "INTO": [ "@a1", "@a2", "@a3", "outfile", "\"/xyz\"" ], "FROM": [ { "expr_type": "table", "table": "t1", "alias": { "as": "", "name": "the_t1", "base_expr": "the_t1", "position": 267 }, "join_type": "JOIN", "ref_type": "", "ref_clause": "", "base_expr": "t1 the_t1", "sub_tree": null, "position": 264 }, { "expr_type": "table", "table": "t2", "alias": null, "join_type": "LEFT", "ref_type": "USING", "ref_clause": [ { "expr_type": "colref", "base_expr": "c1", "sub_tree": null, "position": 299 }, { "expr_type": "colref", "base_expr": "c2", "sub_tree": null, "position": 302 } ], "base_expr": "t2 using(c1,c2)", "sub_tree": null, "position": 290 }, { "expr_type": "table", "table": "t3", "alias": { "as": "1", "name": "tX", "base_expr": "as tX", "position": 314 }, "join_type": "JOIN", "ref_type": "ON", "ref_clause": [ { "expr_type": "colref", "base_expr": "tX.c1", "sub_tree": null, "position": 323 }, { "expr_type": "operator", "base_expr": "=", "sub_tree": null, "position": 329 }, { "expr_type": "colref", "base_expr": "the_t1.c1", "sub_tree": null, "position": 331 } ], "base_expr": "t3 as tX ON tX.c1 = the_t1.c1", "sub_tree": null, "position": 311 }, { "expr_type": "table", "table": "t4", "alias": { "as": "", "name": "t4_x", "base_expr": "t4_x", "position": 349 }, "join_type": "JOIN", "ref_type": "USING", "ref_clause": [ { "expr_type": "colref", "base_expr": "x", "sub_tree": null, "position": 360 } ], "base_expr": "t4 t4_x using(x)", "sub_tree": null, "position": 346 } ], "WHERE": [ ] } ``` -------------------------------- ### Parsed Complex SQL Expression Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet shows the detailed, tree-like structure generated by the PHP SQL Parser for a complex SQL expression. It includes a CASE statement with WHEN and ELSE clauses, and a column reference. ```json { "base_expr": "Status" = CASE WHEN quantity > 0 THEN 'in stock' ELSE 'out of stock' END, "sub_tree": [ { "expr_type": "const", "base_expr": "Status", "sub_tree": null, "position": 66 }, { "expr_type": "operator", "base_expr": "=", "sub_tree": null, "position": 75 }, { "expr_type": "reserved", "base_expr": "CASE", "sub_tree": null, "position": 77 }, { "expr_type": "reserved", "base_expr": "WHEN", "sub_tree": null, "position": 90 }, { "expr_type": "colref", "base_expr": "quantity", "sub_tree": null, "position": 95 }, { "expr_type": "operator", "base_expr": ">", "sub_tree": null, "position": 104 }, { "expr_type": "const", "base_expr": "0", "sub_tree": null, "position": 106 }, { "expr_type": "reserved", "base_expr": "THEN", "sub_tree": null, "position": 108 }, { "expr_type": "const", "base_expr": "'in stock'", "sub_tree": null, "position": 113 }, { "expr_type": "reserved", "base_expr": "ELSE", "sub_tree": null, "position": 132 }, { "expr_type": "const", "base_expr": "'out of stock'", "sub_tree": null, "position": 137 }, { "expr_type": "reserved", "base_expr": "END", "sub_tree": null, "position": 160 } ], "position": 66 } ``` -------------------------------- ### Parsed Subquery Expression Source: https://github.com/greenlion/php-sql-parser/blob/master/wiki/Complex-Example.md This snippet shows the parsed representation of a subquery used as an expression. It includes the alias assigned to the subquery and its base expression. ```json { "expr_type": "expression", "alias": { "as": 1, "name": "subquery", "base_expr": "as subquery", "position": 228 }, "base_expr": "(select c1+c2 from t1 inner_t1 limit 1)", "sub_tree": [] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.