Skip to content

Commit

Permalink
Merge branch 'feature/add_aliases_on_select_columns' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreGerault committed Jul 18, 2021
2 parents dceb0ea + 091800f commit 7134ea1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/AGerault/Contracts/Database/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ public function from(string $tableName, ?string $tableAlias = null): self;
*/
public function select(array $columns): self;

/**
* Select the columns to fetch from the joining table
*
* @param array<string> $columns
* @return $this
*/
public function selectOnJoinTable(array $columns): self;

/**
* Specify the amount of results to fetch.
*
* @param int $amount
* @return QueryBuilderInterface
*/
public function limit(int $amount): self;

/**
* Specify the amount of results to skip.
*
* @param int $offset
* @return QueryBuilderInterface
*/
public function offset(int $offset): self;
Expand Down Expand Up @@ -96,4 +106,12 @@ public function innerJoin(string $table, ?string $alias = null): self;
* @return $this
*/
public function on(string $left, string $right): self;

/**
* Aliases if needed the column names
*
* @param bool $enable
* @return $this
*/
public function withAliasPrefixOnColumns(bool $enable = true): self;
}
29 changes: 28 additions & 1 deletion src/AGerault/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class QueryBuilder implements QueryBuilderInterface
* @var string[]
*/
protected array $select = ['*'];
protected array $selectOnJoiningTable = [];

protected string $from;
protected ?string $fromAlias = null;
Expand Down Expand Up @@ -52,6 +53,8 @@ class QueryBuilder implements QueryBuilderInterface

protected ?string $joinCondition = null;

protected bool $aliasPrefixOnColumn = false;

public function from(string $tableName, ?string $tableAlias = null): QueryBuilderInterface
{
$this->from = $tableName;
Expand Down Expand Up @@ -137,7 +140,17 @@ public function delete(): self

private function buildSelectQuery(): string
{
$select = implode(', ', $this->select);
$select = $this->aliasPrefixOnColumn
? implode(', ', array_map(fn (string $a) => ($this->selectOnJoiningTable ? $this->fromAlias . '.' : '') . $a . ' as '. $this->from . '_' . $a, $this->select))
: implode(', ', $this->select);

if ($this->selectOnJoiningTable) {
$select .= ", ";
$select .= $this->aliasPrefixOnColumn
? implode(', ', array_map(fn (string $a) => ($this->selectOnJoiningTable ? $this->joinTableAlias . '.' : '') . $a . ' as '. $this->joinTable . '_' . $a, $this->selectOnJoiningTable))
: implode(', ', $this->select);
}

$query = "SELECT {$select} FROM {$this->from}";

if ($this->fromAlias) {
Expand Down Expand Up @@ -266,4 +279,18 @@ function ($name, $payload) {

return $query;
}

public function withAliasPrefixOnColumns(bool $enable = true): QueryBuilderInterface
{
$this->aliasPrefixOnColumn = $enable;

return $this;
}

public function selectOnJoinTable(array $columns): QueryBuilderInterface
{
$this->selectOnJoiningTable = $columns;

return $this;
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Database/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,39 @@ function () {
->toBe('SELECT title FROM posts p INNER JOIN authors a ON p.author_id = a.id WHERE p.author_name = a.name');
}
);

it(
"should be able to add aliases to each column",
function () {
$query = getQueryBuilder();

$query->select(['title'])
->withAliasPrefixOnColumns(true)
->from('posts', 'p')
->innerJoin('authors', 'a')
->on('p.author_id', 'a.id')
->where('p.author_name', '=', 'a.name');


expect($query->toSQL())
->toBeString()
->toBe('SELECT title as posts_title FROM posts p INNER JOIN authors a ON p.author_id = a.id WHERE p.author_name = a.name');
}
);

it("should be able to add aliases on columns from select and from joined table", function () {
$query = getQueryBuilder();

$query
->select(['title'])
->selectOnJoinTable(['name'])
->withAliasPrefixOnColumns(true)
->from('posts', 'p')
->innerJoin('authors', 'a')
->on('p.author_id', 'a.id')
->where('p.author_name', '=', 'a.name');

expect($query->toSQL())
->toBeString()
->toBe('SELECT p.title as posts_title, a.name as authors_name FROM posts p INNER JOIN authors a ON p.author_id = a.id WHERE p.author_name = a.name');
});

0 comments on commit 7134ea1

Please sign in to comment.