diff --git a/src/Sql/Join.php b/src/Sql/Join.php index 4b40aca..2acf286 100644 --- a/src/Sql/Join.php +++ b/src/Sql/Join.php @@ -69,6 +69,7 @@ class Join * @param mixed $foreignTable * @param array $columns * @param string $join + * @throws Exception */ public function __construct(AbstractSql $sql, mixed $foreignTable, array $columns, string $join = 'JOIN') { @@ -78,10 +79,12 @@ public function __construct(AbstractSql $sql, mixed $foreignTable, array $column if (($foreignTable instanceof Select) || ($foreignTable instanceof \Pop\Db\Sql)) { $this->foreignTable = (string)$foreignTable; } else if (is_array($foreignTable)) { - foreach ($foreignTable as $alias => $table) { - $this->foreignTable = $this->sql->quoteId($table) . ' AS ' . $this->sql->quoteId($alias); - break; + if (count($foreignTable) !== 1) { + throw new Exception('Error: Only one table can be used in JOIN clause.'); } + $alias = array_key_first($foreignTable); + $table = $foreignTable[$alias]; + $this->foreignTable = $this->sql->quoteId($table) . ' AS ' . $this->sql->quoteId($alias); } else { $this->foreignTable = $this->sql->quoteId($foreignTable); } diff --git a/src/Sql/Select.php b/src/Sql/Select.php index 739b3a8..25e398a 100644 --- a/src/Sql/Select.php +++ b/src/Sql/Select.php @@ -457,11 +457,12 @@ public function render(): string $sql .= (string)$this->table; // Else, if there is an aliased table } else if (is_array($this->table)) { - if (count($this->table) !== 1) + if (count($this->table) !== 1) { throw new Exception('Error: Only one table can be used in FROM clause.'); + } $alias = array_key_first($this->table); $table = $this->table[$alias]; - $sql .= $this->quoteId($table) . ' AS ' . $this->quoteId($alias); + $sql .= $this->quoteId($table) . ' AS ' . $this->quoteId($alias); // Else, just select from the table } else { $sql .= $this->quoteId($this->table); @@ -474,27 +475,27 @@ public function render(): string } } - // Build any WHERE clauses + // Build WHERE clause if ($this->where !== null) { $sql .= ' WHERE ' . $this->where; } - // Build any HAVING clause + // Build HAVING clause if ($this->having !== null) { $sql .= ' HAVING ' . $this->having; } - // Build any GROUP BY clause + // Build GROUP BY clause if ($this->groupBy !== null) { $sql .= ' GROUP BY ' . $this->groupBy; } - // Build any ORDER BY clause + // Build ORDER BY clause if ($this->orderBy !== null) { $sql .= ' ORDER BY ' . $this->orderBy; } - // Build any LIMIT clause for all other database types. + // Build LIMIT clause for all other database types. if (!$this->isSqlsrv()) { if ($this->limit !== null) { if ((str_contains($this->limit, ',')) && ($this->isPgsql())) { @@ -506,7 +507,7 @@ public function render(): string } } - // Build any OFFSET clause for all other database types. + // Build OFFSET clause for all other database types. if (!$this->isSqlsrv()) { if ($this->offset !== null) { $sql .= ' OFFSET ' . $this->offset; @@ -523,6 +524,7 @@ public function render(): string /** * Render the SELECT statement * + * @throws Exception * @return string */ public function __toString(): string diff --git a/tests/Sql/JoinTest.php b/tests/Sql/JoinTest.php index a81e82d..c9ae5cc 100644 --- a/tests/Sql/JoinTest.php +++ b/tests/Sql/JoinTest.php @@ -36,6 +36,13 @@ public function testForeignTableAlias() $this->assertEquals('SELECT * FROM `users` LEFT JOIN `user_info` AS `userinfo` ON (`userinfo`.`user_id` = `users`.`id`)', $sql->render()); } + public function testForeignTableAliasException() + { + $this->expectException('Pop\Db\Sql\Exception'); + $sql = $this->db->createSql(); + $sql->select()->from('users')->leftJoin(['userinfo' => 'user_info', 'another_table' => 'another_info'], ['userinfo.user_id' => 'users.id']); + } + public function testForeignTableSql() { $sql1 = $this->db->createSql();