Skip to content

Commit

Permalink
added whereSprintf and orWhereSprintf funtions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 8, 2020
1 parent bb2b66c commit 69c9abe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [7.4.0] - Unreleased
### Added
- `SelectAggregate` query allows to set not only fields but anything (math operations, for instance) and save the result as a column

## [7.3.6] - 2019-12-25
### Added
- New function `Database::clearCache()` to clear the cache of all tables
Expand Down
2 changes: 2 additions & 0 deletions src/Queries/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class Select extends Query
'orderBy',
'catHaving',
'where',
'whereSprintf',
'whereEquals',
'orWhere',
'orWhereSprinf',
'catWhere',
'limit',
'offset',
Expand Down
15 changes: 11 additions & 4 deletions src/Queries/SelectAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class SelectAggregate extends Select
'SUM',
];

public function __construct(Table $table, string $function, string $field = 'id')
public function __construct(Table $table, string $function, string $field = 'id', string $as = null)
{
$this->table = $table;
$this->field = $field;
Expand All @@ -31,19 +31,26 @@ public function __construct(Table $table, string $function, string $field = 'id'
);
}

if ($as) {
$columns = sprintf('%s(%s) AS `%s`', $function, $field, $as);
} else {
$columns = sprintf('%s(%s)', $function, $field);
}

$this->query = $table->getDatabase()
->select()
->from((string) $table)
->columns(sprintf('%s(%s)', $function, $field));
->columns($columns);
}

public function run()
{
$statement = $this->__invoke();
$statement->setFetchMode(PDO::FETCH_NUM);
$result = $statement->fetchColumn();

$field = $this->table->{$this->field};
$field = $this->table->{$this->field} ?? null;

return $field->format($statement->fetchColumn());
return $field ? $field->format($result) : $result;
}
}
4 changes: 2 additions & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ public function select(): Select
return $query;
}

public function selectAggregate(string $function, string $field = 'id'): SelectAggregate
public function selectAggregate(string $function, string $field = 'id', string $as = null): SelectAggregate
{
$query = new SelectAggregate($this, $function, $field);
$query = new SelectAggregate($this, $function, $field, $as);

if ($eventDispatcher = $this->getEventDispatcher()) {
$eventDispatcher->dispatch(new CreateSelectQuery($query));
Expand Down
18 changes: 18 additions & 0 deletions tests/QueryMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ public function testCount(Database $db)
foo
LIMIT 5 OFFSET 10
FOR UPDATE
SQL
);
}

/**
* @depends testDatabase
*/
public function testSum(Database $db)
{
$query = $db->post->selectAggregate('sum', 'qty * sale_price', 'total_stock');

$this->assertEquals(
(string) $query,
<<<'SQL'
SELECT
SUM(qty * sale_price) AS `total_stock`
FROM
`post`
SQL
);
}
Expand Down

0 comments on commit 69c9abe

Please sign in to comment.