Skip to content

Commit

Permalink
Issue 488: Remove usage of deprecated SQL_CALC_FOUND_ROWS
Browse files Browse the repository at this point in the history
#488

Further fixed a few things found by the IDE.
  • Loading branch information
blackcoder87 committed Jun 7, 2021
1 parent 71e1bbf commit b61784e
Show file tree
Hide file tree
Showing 49 changed files with 673 additions and 392 deletions.
4 changes: 2 additions & 2 deletions application/libraries/Ilch/Database/Mysql.php
Expand Up @@ -378,7 +378,7 @@ public function truncate($table)
*/
protected function getFieldsSql($fields)
{
if (!is_array($fields) && ($fields === '*' || strpos($fields, '(') !== false)) {
if (!\is_array($fields) && ($fields === '*' || strpos($fields, '(') !== false)) {
return $fields;
}

Expand Down Expand Up @@ -421,7 +421,7 @@ public function quote($field, $complete = false)
}
}
$parts = explode('.', $field);
if (count($parts) > 2) {
if (\count($parts) > 2) {
throw new \InvalidArgumentException('Invalid field expression: ' . $field);
}

Expand Down
2 changes: 1 addition & 1 deletion application/libraries/Ilch/Database/Mysql/Delete.php
Expand Up @@ -6,7 +6,7 @@

namespace Ilch\Database\Mysql;

use \Ilch\Database\Mysql as DB;
use Ilch\Database\Mysql as DB;

class Delete extends QueryBuilder
{
Expand Down
Expand Up @@ -55,15 +55,15 @@ public function addParts(array $parts)
*/
public function __toString()
{
if (!is_array($this->parts)) {
if (!\is_array($this->parts)) {
return '';
}

if (count($this->parts) === 1) {
if (\count($this->parts) === 1) {
return (string) $this->parts[0];
}

if (count($this->parts) > 0) {
if (\count($this->parts) > 0) {
$parts = [];
foreach ($this->parts as $part) {
$parts[] = (string) $part;
Expand Down
4 changes: 2 additions & 2 deletions application/libraries/Ilch/Database/Mysql/Expression/Join.php
Expand Up @@ -51,7 +51,7 @@ class Join
function __construct($table, $type)
{
$allowedTypes = [self::INNER, self::LEFT, self::RIGHT];
if (!in_array($type, $allowedTypes)) {
if (!\in_array($type, $allowedTypes)) {
throw new \InvalidArgumentException('invalid type, allowed: ' . implode(', ', $allowedTypes));
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public function getFields()
public function setConditions(array $conditions, $type = null)
{
$this->conditions = $conditions;
if (isset($type) && in_array($type, ['and', 'or'])) {
if (isset($type) && \in_array($type, ['and', 'or'])) {
$this->conditionsType = $type;
}
return $this;
Expand Down
5 changes: 3 additions & 2 deletions application/libraries/Ilch/Database/Mysql/Insert.php
Expand Up @@ -6,7 +6,7 @@

namespace Ilch\Database\Mysql;

use \Ilch\Database\Mysql as DB;
use Ilch\Database\Mysql as DB;

class Insert extends QueryBuilder
{
Expand Down Expand Up @@ -85,7 +85,8 @@ public function generateSql()
}

$sql = 'INSERT INTO ' . $this->db->quote('[prefix]_'.$this->table). ' (';
$sqlFields = $sqlValues = [];
$sqlValues = [];
$sqlFields = $sqlValues;

if (!empty($this->values)) {
foreach ($this->values as $key => $value) {
Expand Down
36 changes: 17 additions & 19 deletions application/libraries/Ilch/Database/Mysql/QueryBuilder.php
Expand Up @@ -6,7 +6,7 @@

namespace Ilch\Database\Mysql;

use \Ilch\Database\Mysql as DB;
use Ilch\Database\Mysql as DB;

abstract class QueryBuilder
{
Expand Down Expand Up @@ -45,8 +45,8 @@ public function __construct(DB $db)
*/
public function where($where, $type = 'and')
{
if (is_array($where)) {
if (!in_array($type, ['and', 'or'])) {
if (\is_array($where)) {
if (!\in_array($type, ['and', 'or'])) {
throw new \InvalidArgumentException('Invalid type: "and" or "or" expected');
}
$where = $this->createCompositeExpression($where, $type);
Expand Down Expand Up @@ -176,7 +176,7 @@ protected function addWhere($type, $where)
$oppositeType = $type === 'and' ? 'or' : 'and';
// add to existing or
if (is_a($this->where, __NAMESPACE__ . '\Expression\\' . ucfirst($type) . 'X')) {
if (is_array($where)) {
if (\is_array($where)) {
$this->where->addParts($this->createCompositePartArray($where));
} elseif ($where instanceof Expression\CompositePart) {
$this->where->addPart($where);
Expand All @@ -188,7 +188,7 @@ protected function addWhere($type, $where)

// new or insert existing where into condition
if (is_a($this->where, __NAMESPACE__ . '\Expression\\' . ucfirst($oppositeType) . 'X')) {
if (!is_array($where)) {
if (!\is_array($where)) {
$where = [$where];
}
array_unshift($where, $this->where);
Expand Down Expand Up @@ -240,27 +240,25 @@ protected function createComparisonExpression($key, $value)
$singleComparisonOperators = ['=', '<=', '>=', '<', '>', '!=', '<>', 'LIKE', 'NOT LIKE', 'IS', 'IS NOT'];

// expect comparison of 2 fields -> don't escape (f.e. join conditions)
if (is_int($key)) {
if (\is_int($key)) {
$conditionParts = explode(' ', $value);
if (count($conditionParts) < 3 || ( count($conditionParts) == 3 && !in_array($conditionParts[1], $singleComparisonOperators) ) || ( count($conditionParts) == 4 && !in_array($conditionParts[1].' '.$conditionParts[2], $singleComparisonOperators) ) ) {
if (\count($conditionParts) < 3 || ( \count($conditionParts) == 3 && !\in_array($conditionParts[1], $singleComparisonOperators) ) || ( \count($conditionParts) == 4 && !\in_array($conditionParts[1].' '.$conditionParts[2], $singleComparisonOperators) ) ) {
throw new \InvalidArgumentException('Invalid comparison expression');
}

$left = $this->db->quote($conditionParts[0]);
if (count($conditionParts) == 4) {
if (\count($conditionParts) == 4) {
$operator = $conditionParts[1].' '.$conditionParts[2];
} else {
$operator = $conditionParts[1];
}

if (in_array($operator, ['IS', 'IS NOT'])) {
if (\in_array($operator, ['IS', 'IS NOT'])) {
$right = 'NULL';
} else if (\count($conditionParts) == 4) {
$right = $this->db->quote($conditionParts[3]);
} else {
if (count($conditionParts) == 4) {
$right = $this->db->quote($conditionParts[3]);
} else {
$right = $this->db->quote($conditionParts[2]);
}
$right = $this->db->quote($conditionParts[2]);
}
} else {
// string key -> comparison with value(s)
Expand All @@ -272,21 +270,21 @@ protected function createComparisonExpression($key, $value)
$operator = '=';
}

if (is_array($value)) {
if (\is_array($value)) {
if ($operator === '=') {
$operator = 'IN';
} elseif (in_array($operator, ['!=', '<>'])) {
} elseif (\in_array($operator, ['!=', '<>'])) {
$operator = 'NOT IN';
}
if (!in_array($operator, ['IN', 'NOT IN'])) {
if (!\in_array($operator, ['IN', 'NOT IN'])) {
throw new \InvalidArgumentException('invalid operator for multiple value comparison');
}
$right = '(' . implode(', ', $this->db->escapeArray($value, true)) . ')';
} else {
if (!in_array($operator, $singleComparisonOperators)) {
if (!\in_array($operator, $singleComparisonOperators)) {
throw new \InvalidArgumentException('invalid operator for single value comparison');
}
if (in_array($operator, ['IS', 'IS NOT'])) {
if (\in_array($operator, ['IS', 'IS NOT'])) {
$right = 'NULL';
} else {
$right = $this->db->escape($value, true);
Expand Down
34 changes: 24 additions & 10 deletions application/libraries/Ilch/Database/Mysql/Result.php
@@ -1,6 +1,6 @@
<?php
/**
* @copyright Ilch 2.0
* @copyright Ilch 2
* @package ilch
*/

Expand All @@ -23,6 +23,9 @@ class Result
/** @var \Ilch\Database\Mysql */
protected $db;

/** @var */
protected $foundRows;

/**
* @param \mysqli_result $dbResult
* @param \Ilch\Database\Mysql $db
Expand Down Expand Up @@ -51,7 +54,7 @@ public function getMysqliResult()
public function fetchCell($name = null)
{
if (isset($name)) {
if (is_int($name)) {
if (\is_int($name)) {
$fieldNumber = $name;
} else {
$row = $this->dbResult->fetch_assoc();
Expand Down Expand Up @@ -183,21 +186,32 @@ public function getFieldCount()
}

/**
* Für eine Select Query, die mit useFoundRows aufgerufen wurde, kann so die FOUND_ROWS() aufgerufen werden
* @return integer
* Set the internal pointer to the given position (must be between 0..getNumRow()-1)
* @param int $position
* @return bool
*/
public function setCurrentRow($position)
{
return $this->dbResult->data_seek($position);
}

/**
* Returns the number of found rows if the query was called with useFoundRows.
*
* @return int
*/
public function getFoundRows()
{
return (int) $this->db->queryCell('SELECT FOUND_ROWS()');
return (int) $this->foundRows;
}

/**
* Set the internal pointer to the given position (must be between 0..getNumRow()-1)
* @param int $position
* @return bool
* @param int $foundRows
* @return Result
*/
public function setCurrentRow($position)
public function setFoundRows($foundRows)
{
return $this->dbResult->data_seek($position);
$this->foundRows = $foundRows;
return $this;
}
}

0 comments on commit b61784e

Please sign in to comment.