diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-10-10 15:37:25 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-10-10 15:50:32 +0300 |
commit | 42c34ddd1fee261da43331e855c80bc432d66247 (patch) | |
tree | f7ce555367d895540489756cae5a016d846ae01c | |
parent | cd62cf6524cbd8764655364ea8a243f69b6020b0 (diff) | |
download | sql-parser-42c34ddd1fee261da43331e855c80bc432d66247.zip sql-parser-42c34ddd1fee261da43331e855c80bc432d66247.tar.gz sql-parser-42c34ddd1fee261da43331e855c80bc432d66247.tar.bz2 |
Avoid building a field multiple times if clause has synonyms.v3.0.3
-rw-r--r-- | src/Statement.php | 23 | ||||
-rw-r--r-- | tests/Builder/SelectStatementTest.php | 26 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/Statement.php b/src/Statement.php index 3fe3f42..b15dc3d 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -108,6 +108,20 @@ abstract class Statement */ $query = ''; + /** + * Clauses which were built already. + * + * It is required to keep track of built clauses because some fields, + * for example `join` is used by multiple clauses (`JOIN`, `LEFT JOIN`, + * `LEFT OUTER JOIN`, etc.). The same happens for `VALUE` and `VALUES`. + * + * A clause is considered built just after fields' value + * (`$this->field`) was used in building. + * + * @var array + */ + $built = array(); + foreach (static::$CLAUSES as $clause) { /** * The name of the clause. @@ -144,6 +158,15 @@ abstract class Statement continue; } + // Checking if this field was already built. + if ($type & 1) { + if (!empty($built[$field])) { + continue; + } + + $built[$field] = true; + } + // Checking if the name of the clause should be added. if ($type & 2) { $query .= $name . ' '; diff --git a/tests/Builder/SelectStatementTest.php b/tests/Builder/SelectStatementTest.php new file mode 100644 index 0000000..58e8dc6 --- /dev/null +++ b/tests/Builder/SelectStatementTest.php @@ -0,0 +1,26 @@ +<?php + +namespace SqlParser\Tests\Builder; + +use SqlParser\Parser; + +use SqlParser\Tests\TestCase; + +class SelectStatementTest extends TestCase +{ + + public function testBuilder() + { + $query = 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ' + . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)'; + + $parser = new Parser($query); + $stmt = $parser->statements[0]; + + $this->assertEquals( + 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ' + . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) ', + $stmt->build() + ); + } +} |