Skip to content

Commit

Permalink
querybuilder bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed Mar 6, 2018
1 parent e10dae0 commit a8c9764
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/DataObjects/Result/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct( array $total )
'pages' => 0,
], $total));

$this->setEntries( $this->total->founds );
$this->setEntries( $this->entries );
}

// ------------------------------------------------------------------------
Expand All @@ -57,7 +57,8 @@ public function __construct( array $total )
public function setEntries( $entries )
{
$this->entries = (int) $entries;
$this->total->pages = ceil($this->total->rows / $this->entries );

$this->total->pages = @ceil($this->total->rows / $this->entries );

$activePage = $this->getActivePage();

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Abstracts/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ abstract protected function platformTransactionCommitHandler();
*
* @return string
*/
protected function compileSqlBinds( $sqlStatement, array $binds = [] )
public function compileSqlBinds( $sqlStatement, array $binds = [] )
{
$hasSqlBinders = strpos( $sqlStatement, ':' ) !== false;

Expand Down
42 changes: 25 additions & 17 deletions src/Sql/Abstracts/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use O2System\Database\DataObjects\Result;
use O2System\Database\Sql\Datastructures\QueryBuilderCache;
use O2System\Database\Sql\Datastructures\QueryStatement;
use O2System\Spl\Exceptions\RuntimeException;

/**
Expand Down Expand Up @@ -1945,7 +1946,7 @@ public function get($limit = null, $offset = null)
: $this->conn->query($this->getSqlStatement(false), $this->builderCache->binds);

if ($result) {
$result->setTotalRows($this->countAllResults(true));
$result->setTotalRows($this->countAllResults());
}

$this->cacheMode = $this->conn->getConfig('cacheEnable');
Expand Down Expand Up @@ -1984,7 +1985,7 @@ public function getWhere(array $where = [], $limit = null, $offset = null)
: $this->conn->query($this->getSqlStatement(false), $this->builderCache->binds);

if ($result) {
$result->setTotalRows($this->countAllResults(true));
$result->setTotalRows($this->countAllResults());
}

$this->cacheMode = $this->conn->getConfig('cacheEnable');
Expand All @@ -2006,17 +2007,21 @@ public function getWhere(array $where = [], $limit = null, $offset = null)
public function countAll()
{
$this->count('*', 'numrows');
$sqlStatement = $this->getSqlStatement();

if ($this->isSubQuery) {
return '( ' . $sqlStatement . ' )';
}

if ($this->testMode) {
return $sqlStatement;
return $this->getSqlStatement(false);
} elseif ($this->isSubQuery) {
$queryStatement = new QueryStatement();
$queryStatement->setSqlStatement( $sqlStatement = $this->getSqlStatement(), $this->builderCache->binds );
$queryStatement->setSqlFinalStatement( $this->conn->compileSqlBinds( $sqlStatement, $this->builderCache->binds ) );

$sqlStatement = $queryStatement->getSqlFinalStatement();

return '( ' . $sqlStatement . ' )';
}

$result = $this->conn->query($sqlStatement);
$result = $this->conn->query($this->getSqlStatement(false), $this->builderCache->binds);
$this->builderCache->reset();

if ($result->count() == 0) {
return 0;
Expand All @@ -2033,13 +2038,11 @@ public function countAll()
* Perform execution of count all result from Query Builder along with WHERE, LIKE, HAVING, GROUP BY, and LIMIT Sql
* statement.
*
* @param bool $reset Whether perform reset Query Builder or not
*
* @return int
* @throws \O2System\Spl\Exceptions\RuntimeException
* @access public
*/
public function countAllResults($reset = true)
public function countAllResults()
{
// save previous
$previousSelect = $this->builderCache->select;
Expand All @@ -2058,15 +2061,20 @@ public function countAllResults($reset = true)
$this->builderCache->offset = $previousOffset;

if ($this->testMode) {
return $sqlStatement;
}
return $this->getSqlStatement(false);
} elseif ($this->isSubQuery) {
$queryStatement = new QueryStatement();
$queryStatement->setSqlStatement( $sqlStatement = $this->getSqlStatement(), $this->builderCache->binds );
$queryStatement->setSqlFinalStatement( $this->conn->compileSqlBinds( $sqlStatement, $this->builderCache->binds ) );

$result = $this->conn->query($sqlStatement, $this->builderCache->binds);
$sqlStatement = $queryStatement->getSqlFinalStatement();

if ($reset === true) {
$this->builderCache->reset();
return '( ' . $sqlStatement . ' )';
}

$result = $this->conn->query($this->getSqlStatement(false), $this->builderCache->binds);
$this->builderCache->reset();

if ($result->count() == 0) {
return 0;
}
Expand Down

0 comments on commit a8c9764

Please sign in to comment.