3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Adapter\Driver\Pdo\Feature
;
12 use Zend\Db\Adapter\Driver\Feature\AbstractFeature
;
13 use Zend\Db\Adapter\Driver\Pdo
;
18 class SqliteRowCounter
extends AbstractFeature
24 public function getName()
26 return 'SqliteRowCounter';
30 * @param \Zend\Db\Adapter\Driver\Pdo\Statement $statement
33 public function getCountForStatement(Pdo\Statement
$statement)
35 $countStmt = clone $statement;
36 $sql = $statement->getSql();
37 if ($sql == '' ||
stripos($sql, 'select') === false) {
40 $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')';
41 $countStmt->prepare($countSql);
42 $result = $countStmt->execute();
43 $countRow = $result->getResource()->fetch(\PDO
::FETCH_ASSOC
);
44 unset($statement, $result);
45 return $countRow['count'];
52 public function getCountForSql($sql)
54 if (!stripos($sql, 'select')) {
57 $countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')';
59 $pdo = $this->pdoDriver
->getConnection()->getResource();
60 $result = $pdo->query($countSql);
61 $countRow = $result->fetch(\PDO
::FETCH_ASSOC
);
62 return $countRow['count'];
69 public function getRowCountClosure($context)
71 $sqliteRowCounter = $this;
72 return function () use ($sqliteRowCounter, $context) {
73 /** @var $sqliteRowCounter SqliteRowCounter */
74 return ($context instanceof Pdo\Statement
)
75 ?
$sqliteRowCounter->getCountForStatement($context)
76 : $sqliteRowCounter->getCountForSql($context);