adding counting methods
This commit is contained in:
parent
d5ed2af885
commit
94a87f91fa
6 changed files with 58 additions and 0 deletions
|
@ -17,4 +17,5 @@ interface DSOFactoryInterface
|
||||||
|
|
||||||
public function search() : Search;
|
public function search() : Search;
|
||||||
public function executeSearch(Search $search, array $params = array(), $deleted = false) : array;
|
public function executeSearch(Search $search, array $params = array(), $deleted = false) : array;
|
||||||
|
public function executeCount(Search $search, array $params = array(), $deleted = false) : ?int;
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,18 @@ abstract class AbstractDriver implements DSODriverInterface
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function count(string $table, Search $search, array $params)
|
||||||
|
{
|
||||||
|
$s = $this->getStatement(
|
||||||
|
'count',
|
||||||
|
['table'=>$table,'search'=>$search]
|
||||||
|
);
|
||||||
|
if (!$s->execute($params)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return intval($s->fetchAll(\PDO::FETCH_COLUMN)[0]);
|
||||||
|
}
|
||||||
|
|
||||||
public function select(string $table, Search $search, array $params)
|
public function select(string $table, Search $search, array $params)
|
||||||
{
|
{
|
||||||
$s = $this->getStatement(
|
$s = $this->getStatement(
|
||||||
|
|
|
@ -45,6 +45,20 @@ class MySQLDriver extends AbstractDriver
|
||||||
return implode(PHP_EOL, $out).';';
|
return implode(PHP_EOL, $out).';';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function sql_count($args)
|
||||||
|
{
|
||||||
|
//extract query parts from Search and expand paths
|
||||||
|
$where = $this->expandPaths($args['search']->where());
|
||||||
|
//select from
|
||||||
|
$out = ["SELECT count(dso_id) FROM `{$args['table']}`"];
|
||||||
|
//where statement
|
||||||
|
if ($where !== null) {
|
||||||
|
$out[] = "WHERE ".$where;
|
||||||
|
}
|
||||||
|
//return
|
||||||
|
return implode(PHP_EOL, $out).';';
|
||||||
|
}
|
||||||
|
|
||||||
protected function sql_ddl($args=array())
|
protected function sql_ddl($args=array())
|
||||||
{
|
{
|
||||||
$out = [];
|
$out = [];
|
||||||
|
|
|
@ -181,6 +181,18 @@ class Factory implements DSOFactoryInterface
|
||||||
return $arr;
|
return $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function executeCount(Search $search, array $params = array(), $deleted = false) : ?int
|
||||||
|
{
|
||||||
|
//add deletion clause and expand column names
|
||||||
|
$search = $this->preprocessSearch($search, $deleted);
|
||||||
|
//run select
|
||||||
|
return $this->driver->count(
|
||||||
|
$this->table,
|
||||||
|
$search,
|
||||||
|
$params
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function executeSearch(Search $search, array $params = array(), $deleted = false) : array
|
public function executeSearch(Search $search, array $params = array(), $deleted = false) : array
|
||||||
{
|
{
|
||||||
//add deletion clause and expand column names
|
//add deletion clause and expand column names
|
||||||
|
|
|
@ -34,6 +34,20 @@ class AbstractLegacyDriver extends AbstractDriver
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function sql_count($args)
|
||||||
|
{
|
||||||
|
//extract query parts from Search and expand paths
|
||||||
|
$where = $this->expandPaths($args['search']->where());
|
||||||
|
//select from
|
||||||
|
$out = ["SELECT count(dso_id) FROM `{$args['table']}`"];
|
||||||
|
//where statement
|
||||||
|
if ($where !== null) {
|
||||||
|
$out[] = "WHERE ".$where;
|
||||||
|
}
|
||||||
|
//return
|
||||||
|
return implode(PHP_EOL, $out).';';
|
||||||
|
}
|
||||||
|
|
||||||
protected function sql_select($args)
|
protected function sql_select($args)
|
||||||
{
|
{
|
||||||
//extract query parts from Search and expand paths
|
//extract query parts from Search and expand paths
|
||||||
|
|
|
@ -18,6 +18,11 @@ class Search implements \Serializable
|
||||||
$this->factory = $factory;
|
$this->factory = $factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function count(array $params = array(), $deleted = false)
|
||||||
|
{
|
||||||
|
return $this->factory->executeCount($this, $params, $deleted);
|
||||||
|
}
|
||||||
|
|
||||||
public function execute(array $params = array(), $deleted = false)
|
public function execute(array $params = array(), $deleted = false)
|
||||||
{
|
{
|
||||||
return $this->factory->executeSearch($this, $params, $deleted);
|
return $this->factory->executeSearch($this, $params, $deleted);
|
||||||
|
|
Loading…
Reference in a new issue