adding counting methods

This commit is contained in:
Joby Elliott 2019-10-17 09:35:31 -06:00
parent d5ed2af885
commit 94a87f91fa
6 changed files with 58 additions and 0 deletions

View file

@ -17,4 +17,5 @@ interface DSOFactoryInterface
public function search() : Search;
public function executeSearch(Search $search, array $params = array(), $deleted = false) : array;
public function executeCount(Search $search, array $params = array(), $deleted = false) : ?int;
}

View file

@ -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)
{
$s = $this->getStatement(

View file

@ -45,6 +45,20 @@ class MySQLDriver extends AbstractDriver
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())
{
$out = [];

View file

@ -181,6 +181,18 @@ class Factory implements DSOFactoryInterface
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
{
//add deletion clause and expand column names

View file

@ -34,6 +34,20 @@ class AbstractLegacyDriver extends AbstractDriver
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)
{
//extract query parts from Search and expand paths

View file

@ -18,6 +18,11 @@ class Search implements \Serializable
$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)
{
return $this->factory->executeSearch($this, $params, $deleted);