diff options
-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() + ); + } +} |