From ec160735393c1ac65b364b3183c54af447ddbf6c Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 24 Feb 2014 16:20:25 +0100 Subject: [PATCH] Fixes to SqlSelectStatement --- src/Models/SearchParsers/TagSearchParser.php | 2 +- src/Sql/Statements/SqlSelectStatement.php | 30 +++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index 6b5098e9..34b984e0 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -8,7 +8,7 @@ class TagSearchParser extends AbstractSearchParser ->addInnerJoin('post_tag', new SqlEqualsOperator('tag.id', 'post_tag.tag_id')) ->addInnerJoin('post', new SqlEqualsOperator('post.id', 'post_tag.post_id')) ->setCriterion((new SqlConjunction)->add(SqlInOperator::fromArray('safety', SqlBinding::fromArray($allowedSafety)))) - ->groupBy('tag.id'); + ->setGroupBy('tag.id'); } protected function processSimpleToken($value, $neg) diff --git a/src/Sql/Statements/SqlSelectStatement.php b/src/Sql/Statements/SqlSelectStatement.php index 1b1944f1..96b68ba7 100644 --- a/src/Sql/Statements/SqlSelectStatement.php +++ b/src/Sql/Statements/SqlSelectStatement.php @@ -7,7 +7,7 @@ class SqlSelectStatement extends SqlStatement protected $columns = null; protected $source = null; protected $innerJoins = []; - protected $outerJoins = []; + protected $leftOuterJoins = []; protected $criterion = null; protected $orderBy = []; protected $limit = null; @@ -68,12 +68,28 @@ class SqlSelectStatement extends SqlStatement return $this; } - public function addOuterJoin($table, $expression) + public function addLeftOuterJoin($table, $expression) { - $this->innerJoins []= [$table, $this->attachExpression($expression)]; + $this->leftOuterJoins []= [$table, $this->attachExpression($expression)]; return $this; } + public function getJoinedTables() + { + $tables = []; + foreach (array_merge($this->innerJoins, $this->leftOuterJoins) as $join) + { + list ($table, $joinExpression) = $join; + $tables []= $table; + } + return array_unique($tables); + } + + public function isTableJoined($table) + { + return in_array($table, $this->getJoinedTables()); + } + public function getCriterion() { return $this->criterion; @@ -123,7 +139,7 @@ class SqlSelectStatement extends SqlStatement return $this; } - public function groupBy($groupBy) + public function setGroupBy($groupBy) { $this->groupBy = $this->attachExpression($groupBy); } @@ -146,10 +162,10 @@ class SqlSelectStatement extends SqlStatement $sql .= ' INNER JOIN ' . $table . ' ON ' . $criterion->getAsString(); } - foreach ($this->outerJoins as $outerJoin) + foreach ($this->leftOuterJoins as $outerJoin) { - list ($table, $criterion) = $join; - $sql .= ' OUTER JOIN ' . $table . ' ON ' . $criterion->getAsString(); + list ($table, $criterion) = $outerJoin; + $sql .= ' LEFT OUTER JOIN ' . $table . ' ON ' . $criterion->getAsString(); } if (!empty($this->criterion) and !empty($this->criterion->getAsString()))