From 3b3c0b7b5f775363622bb78445869df7d4589151 Mon Sep 17 00:00:00 2001 From: Martin Daskalov Date: Tue, 23 Jul 2019 13:40:48 +0100 Subject: [PATCH 1/2] Implement table aliasing and extent the builder to have multiple joins with the same table name but different aliases --- src/Manipulation/AbstractBaseQuery.php | 12 +++++--- src/Manipulation/ColumnQuery.php | 8 ++++-- src/Manipulation/JoinQuery.php | 39 ++++++++++++++++++++------ src/Manipulation/Select.php | 8 ++++-- src/Syntax/Column.php | 3 +- src/Syntax/SyntaxFactory.php | 2 +- 6 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/Manipulation/AbstractBaseQuery.php b/src/Manipulation/AbstractBaseQuery.php index 940bf3c..6b40366 100644 --- a/src/Manipulation/AbstractBaseQuery.php +++ b/src/Manipulation/AbstractBaseQuery.php @@ -157,9 +157,7 @@ public function getWhere() */ public function getTable() { - $newTable = array($this->table); - - return \is_null($this->table) ? null : SyntaxFactory::createTable($newTable); + return $this->table; } /** @@ -169,7 +167,13 @@ public function getTable() */ public function setTable($table) { - $this->table = (string) $table; + if ( ! (is_object($table) && is_a($table, Table::class))) { + $table = new Table( + (string) $table + ); + } + + $this->table = $table; return $this; } diff --git a/src/Manipulation/ColumnQuery.php b/src/Manipulation/ColumnQuery.php index 8a1ff6d..e0e1f66 100644 --- a/src/Manipulation/ColumnQuery.php +++ b/src/Manipulation/ColumnQuery.php @@ -224,9 +224,11 @@ public function getAllColumns() { $columns = $this->getColumns(); - foreach ($this->joinQuery->getJoins() as $join) { - $joinCols = $join->getAllColumns(); - $columns = \array_merge($columns, $joinCols); + foreach ($this->joinQuery->getJoins() as $joins) { + foreach ($joins as $join) { + $joinCols = $join->getAllColumns(); + $columns = \array_merge($columns, $joinCols); + } } return $columns; diff --git a/src/Manipulation/JoinQuery.php b/src/Manipulation/JoinQuery.php index 8ddbbd2..3965bec 100644 --- a/src/Manipulation/JoinQuery.php +++ b/src/Manipulation/JoinQuery.php @@ -10,6 +10,7 @@ namespace NilPortugues\Sql\QueryBuilder\Manipulation; +use NilPortugues\Sql\QueryBuilder\Syntax\Table; use NilPortugues\Sql\QueryBuilder\Syntax\Where; use NilPortugues\Sql\QueryBuilder\Syntax\Column; use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory; @@ -83,7 +84,7 @@ public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns } /** - * @param string $table + * @param Table $table * @param mixed $selfColumn * @param mixed $refColumn * @param string[] $columns @@ -98,7 +99,13 @@ public function join( $columns = [], $joinType = null ) { - if (!isset($this->joins[$table])) { + if ( !(is_object($table) && is_a($table, Table::class))) { + $table = new Table( + (string) $table + ); + } + + if (! $this->joinExists($table)) { $select = QueryFactory::createSelect($table); $select->setColumns($columns); $select->setJoinType($joinType); @@ -106,7 +113,7 @@ public function join( $this->addJoin($select, $selfColumn, $refColumn); } - return $this->joins[$table]; + return $this->joins[$table->getName()][$table->getAlias()]; } /** @@ -119,9 +126,11 @@ public function join( public function addJoin(Select $select, $selfColumn, $refColumn) { $select->isJoin(true); - $table = $select->getTable()->getName(); - if (!isset($this->joins[$table])) { + $tableName = $select->getTable()->getName(); + $tableAlias = $select->getTable()->getAlias(); + + if (! $this->joinExists($select->getTable())) { if (!$selfColumn instanceof Column) { $newColumn = array($selfColumn); $selfColumn = SyntaxFactory::createColumn( @@ -131,10 +140,20 @@ public function addJoin(Select $select, $selfColumn, $refColumn) } $select->joinCondition()->equals($refColumn, $selfColumn); - $this->joins[$table] = $select; + $this->joins[$tableName][$tableAlias] = $select; } - return $this->joins[$table]; + return $this->joins[$tableName][$tableAlias]; + } + + /** + * @param Table $table + * + * @return bool + */ + private function joinExists(Table $table) + { + return isset($this->joins[$table->getName()][$table->getAlias()]); } /** @@ -299,8 +318,10 @@ public function getAllJoins() { $joins = $this->joins; - foreach ($this->joins as $join) { - $joins = \array_merge($joins, $join->getAllJoins()); + foreach ($this->joins as $joins) { + foreach ($joins as $join) { + $joins = \array_merge($joins, $join->getAllJoins()); + } } return $joins; diff --git a/src/Manipulation/Select.php b/src/Manipulation/Select.php index 1580df4..bca3c2a 100644 --- a/src/Manipulation/Select.php +++ b/src/Manipulation/Select.php @@ -117,7 +117,7 @@ public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns } /** - * @param string $table + * @param Table $table * @param string $selfColumn * @param string $refColumn * @param string[] $columns @@ -346,8 +346,10 @@ protected function getAllOperation($data, $operation) $collection[] = $data; } - foreach ($this->joinQuery->getJoins() as $join) { - $collection = \array_merge($collection, $join->$operation()); + foreach ($this->joinQuery->getJoins() as $joins) { + foreach ($joins as $join) { + $collection = \array_merge($collection, $join->$operation()); + } } return $collection; diff --git a/src/Syntax/Column.php b/src/Syntax/Column.php index aa244f9..b461e7b 100644 --- a/src/Syntax/Column.php +++ b/src/Syntax/Column.php @@ -89,8 +89,7 @@ public function getTable() */ public function setTable($table) { - $newTable = array($table); - $this->table = SyntaxFactory::createTable($newTable); + $this->table = $table; return $this; } diff --git a/src/Syntax/SyntaxFactory.php b/src/Syntax/SyntaxFactory.php index 21032dc..ab3aec9 100644 --- a/src/Syntax/SyntaxFactory.php +++ b/src/Syntax/SyntaxFactory.php @@ -64,7 +64,7 @@ public static function createColumn(array &$argument, $table = null) $columnAlias = null; } - return new Column($columnName, (string) $table, $columnAlias); + return new Column($columnName, $table, $columnAlias); } /** From 3e19540e85845aa76808df68ad771bded4ffcbb0 Mon Sep 17 00:00:00 2001 From: Martin Daskalov Date: Tue, 23 Jul 2019 14:09:27 +0100 Subject: [PATCH 2/2] Refactor the code --- src/Manipulation/AbstractBaseQuery.php | 2 +- src/Manipulation/JoinQuery.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Manipulation/AbstractBaseQuery.php b/src/Manipulation/AbstractBaseQuery.php index 6b40366..5fbf251 100644 --- a/src/Manipulation/AbstractBaseQuery.php +++ b/src/Manipulation/AbstractBaseQuery.php @@ -167,7 +167,7 @@ public function getTable() */ public function setTable($table) { - if ( ! (is_object($table) && is_a($table, Table::class))) { + if (!(is_object($table) && is_a($table, Table::class))) { $table = new Table( (string) $table ); diff --git a/src/Manipulation/JoinQuery.php b/src/Manipulation/JoinQuery.php index 3965bec..ed45110 100644 --- a/src/Manipulation/JoinQuery.php +++ b/src/Manipulation/JoinQuery.php @@ -99,7 +99,7 @@ public function join( $columns = [], $joinType = null ) { - if ( !(is_object($table) && is_a($table, Table::class))) { + if (!(is_object($table) && is_a($table, Table::class))) { $table = new Table( (string) $table );