diff --git a/src/DSOFactoryInterface.php b/src/DSOFactoryInterface.php index c633826..c27dac5 100644 --- a/src/DSOFactoryInterface.php +++ b/src/DSOFactoryInterface.php @@ -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; } diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 4c1a3c7..1603f81 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -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( diff --git a/src/Drivers/MySQLDriver.php b/src/Drivers/MySQLDriver.php index a7174aa..0be826d 100644 --- a/src/Drivers/MySQLDriver.php +++ b/src/Drivers/MySQLDriver.php @@ -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 = []; diff --git a/src/Factory.php b/src/Factory.php index 41d8c8b..13e58dc 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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 diff --git a/src/LegacyDrivers/AbstractLegacyDriver.php b/src/LegacyDrivers/AbstractLegacyDriver.php index e468eb1..4543bbf 100644 --- a/src/LegacyDrivers/AbstractLegacyDriver.php +++ b/src/LegacyDrivers/AbstractLegacyDriver.php @@ -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 diff --git a/src/Search.php b/src/Search.php index da72140..b3b4fb9 100644 --- a/src/Search.php +++ b/src/Search.php @@ -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);