Skip to content

Commit

Permalink
Update alias table support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 27, 2023
1 parent 19904bf commit b76794a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
{
Expand All @@ -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);
}
Expand Down
18 changes: 10 additions & 8 deletions src/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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())) {
Expand All @@ -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;
Expand All @@ -523,6 +524,7 @@ public function render(): string
/**
* Render the SELECT statement
*
* @throws Exception
* @return string
*/
public function __toString(): string
Expand Down
7 changes: 7 additions & 0 deletions tests/Sql/JoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b76794a

Please sign in to comment.