sulheru / php-sql-parser

Automatically exported from code.google.com/p/php-sql-parser
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Patch for /trunk/php-sql-parser.php #49

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
fixes issue 45.

Here is a test :

testsuite/tests/parser/issue45.php :

//////////////////////////////////////
<?php
require_once(dirname(__FILE__) . '/../../../php-sql-parser.php');
require_once(dirname(__FILE__) . '/../../test-more.php');

$parser = new PHPSQLParser();

$sql = "SELECT a from b left join c on c.a = b.a and (c.b. = b.b) where b.a > 
1";
$parser->parse($sql, true);
$p = $parser->parsed;
$expected = getExpectedValue(dirname(__FILE__), 'issue45.serialized');
eq_array($p, $expected, 'does not die if subtree in join condition');

//////////////////////////////////////

and file with the serialized array :

testsuite/expected/parser/issue45.serialized :

a:3:{s:6:"SELECT";a:1:{i:0;a:5:{s:9:"expr_type";s:6:"colref";s:5:"alias";b:0;s:9
:"base_expr";s:1:"a";s:8:"sub_tree";b:0;s:8:"position";i:7;}}s:4:"FROM";a:2:{i:0
;a:9:{s:9:"expr_type";s:5:"table";s:5:"table";s:1:"b";s:5:"alias";b:0;s:9:"join_
type";s:4:"JOIN";s:8:"ref_type";b:0;s:10:"ref_clause";b:0;s:9:"base_expr";s:1:"b
";s:8:"sub_tree";b:0;s:8:"position";i:14;}i:1;a:9:{s:9:"expr_type";s:5:"table";s
:5:"table";s:1:"c";s:5:"alias";b:0;s:9:"join_type";s:4:"LEFT";s:8:"ref_type";s:2
:"ON";s:10:"ref_clause";a:5:{i:0;a:4:{s:9:"expr_type";s:6:"colref";s:9:"base_exp
r";s:3:"c.a";s:8:"sub_tree";b:0;s:8:"position";i:31;}i:1;a:4:{s:9:"expr_type";s:
8:"operator";s:9:"base_expr";s:1:"=";s:8:"sub_tree";b:0;s:8:"position";i:35;}i:2
;a:4:{s:9:"expr_type";s:6:"colref";s:9:"base_expr";s:3:"b.a";s:8:"sub_tree";b:0;
s:8:"position";i:37;}i:3;a:4:{s:9:"expr_type";s:8:"operator";s:9:"base_expr";s:3
:"and";s:8:"sub_tree";b:0;s:8:"position";i:41;}i:4;a:4:{s:9:"expr_type";s:10:"ex
pression";s:9:"base_expr";s:12:"(c.b. = 
b.b)";s:8:"sub_tree";a:3:{i:0;a:4:{s:9:"expr_type";s:6:"colref";s:9:"base_expr";
s:4:"c.b.";s:8:"sub_tree";b:0;s:8:"position";i:46;}i:1;a:4:{s:9:"expr_type";s:8:
"operator";s:9:"base_expr";s:1:"=";s:8:"sub_tree";b:0;s:8:"position";i:51;}i:2;a
:4:{s:9:"expr_type";s:6:"colref";s:9:"base_expr";s:3:"b.b";s:8:"sub_tree";b:0;s:
8:"position";i:53;}}s:8:"position";i:45;}}s:9:"base_expr";s:31:"c on c.a = b.a 
and (c.b. = 
b.b)";s:8:"sub_tree";b:0;s:8:"position";i:26;}}s:5:"WHERE";a:3:{i:0;a:4:{s:9:"ex
pr_type";s:6:"colref";s:9:"base_expr";s:3:"b.a";s:8:"sub_tree";b:0;s:8:"position
";i:64;}i:1;a:4:{s:9:"expr_type";s:8:"operator";s:9:"base_expr";s:1:">";s:8:"sub
_tree";b:0;s:8:"position";i:68;}i:2;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_e
xpr";s:1:"1";s:8:"sub_tree";b:0;s:8:"position";i:70;}}}

Original issue reported on code.google.com by nderm...@adequasys.com on 23 Apr 2012 at 6:52

Attachments:

GoogleCodeExporter commented 9 years ago
This patch seems to work. It has only one little problem. In some cases the 
internal position flag is not correct. This can create errors, if the sql 
string has similiar parts on multiple positions. The parser looks for the 
position with a simple string compare, so it is necessary to point to the 
correct internal position to find the right sql part. It occurs on sub_trees.

Original comment by pho...@gmx.de on 26 Apr 2012 at 3:57

GoogleCodeExporter commented 9 years ago
I have checked the patch again and I couldn't create a wrong test. So it is now 
integrated into the codebase, thank you.

Original comment by pho...@gmx.de on 2 May 2012 at 12:35