mirror of
https://github.com/dg/dibi.git
synced 2025-08-03 12:47:33 +02:00
used safe casting to int (BC break)
This commit is contained in:
@@ -260,9 +260,9 @@ FROM %SQL', $this->sql, '
|
|||||||
{
|
{
|
||||||
if ($this->count === null) {
|
if ($this->count === null) {
|
||||||
$this->count = $this->conds || $this->offset || $this->limit
|
$this->count = $this->conds || $this->offset || $this->limit
|
||||||
? (int) $this->connection->nativeQuery(
|
? Helpers::intVal($this->connection->nativeQuery(
|
||||||
'SELECT COUNT(*) FROM (' . $this->__toString() . ') t'
|
'SELECT COUNT(*) FROM (' . $this->__toString() . ') t'
|
||||||
)->fetchSingle()
|
)->fetchSingle())
|
||||||
: $this->getTotalCount();
|
: $this->getTotalCount();
|
||||||
}
|
}
|
||||||
return $this->count;
|
return $this->count;
|
||||||
@@ -275,9 +275,9 @@ FROM %SQL', $this->sql, '
|
|||||||
public function getTotalCount(): int
|
public function getTotalCount(): int
|
||||||
{
|
{
|
||||||
if ($this->totalCount === null) {
|
if ($this->totalCount === null) {
|
||||||
$this->totalCount = (int) $this->connection->nativeQuery(
|
$this->totalCount = Helpers::intVal($this->connection->nativeQuery(
|
||||||
'SELECT COUNT(*) FROM ' . $this->sql
|
'SELECT COUNT(*) FROM ' . $this->sql
|
||||||
)->fetchSingle();
|
)->fetchSingle());
|
||||||
}
|
}
|
||||||
return $this->totalCount;
|
return $this->totalCount;
|
||||||
}
|
}
|
||||||
|
@@ -68,12 +68,12 @@ class MsSqlReflector implements Dibi\Reflector
|
|||||||
if (!is_array($row) || count($row) < 1) {
|
if (!is_array($row) || count($row) < 1) {
|
||||||
if ($fallback) {
|
if ($fallback) {
|
||||||
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escapeIdentifier($table)}")->fetch(false);
|
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escapeIdentifier($table)}")->fetch(false);
|
||||||
$count = (int) ($row[0]);
|
$count = Dibi\Helpers::intVal($row[0]);
|
||||||
} else {
|
} else {
|
||||||
$count = null;
|
$count = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$count = (int) ($row[0]);
|
$count = Dibi\Helpers::intVal($row[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $count;
|
return $count;
|
||||||
|
@@ -159,7 +159,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
|||||||
public function getInsertId(?string $sequence): ?int
|
public function getInsertId(?string $sequence): ?int
|
||||||
{
|
{
|
||||||
$row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(true);
|
$row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(true);
|
||||||
return isset($row['ID']) ? (int) $row['ID'] : null;
|
return isset($row['ID']) ? Dibi\Helpers::intVal($row['ID']) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -139,7 +139,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
|
|||||||
$res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()');
|
$res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()');
|
||||||
if (is_resource($res)) {
|
if (is_resource($res)) {
|
||||||
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
|
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
|
||||||
return (int) $row[0];
|
return Dibi\Helpers::intVal($row[0]);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -378,9 +378,9 @@ class Fluent implements IDataSource
|
|||||||
|
|
||||||
public function count(): int
|
public function count(): int
|
||||||
{
|
{
|
||||||
return (int) $this->query([
|
return Helpers::intVal($this->query([
|
||||||
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
|
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
|
||||||
])->fetchSingle();
|
])->fetchSingle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -285,4 +285,23 @@ class Helpers
|
|||||||
{
|
{
|
||||||
return $val === false ? null : $val;
|
return $val === false ? null : $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @return string|int
|
||||||
|
*/
|
||||||
|
public static function intVal($value): int
|
||||||
|
{
|
||||||
|
if (is_int($value)) {
|
||||||
|
return $value;
|
||||||
|
} elseif (is_string($value) && preg_match('#-?\d++\z#A', $value)) {
|
||||||
|
if (is_float($value * 1)) {
|
||||||
|
throw new Exception("Number $value is greater than integer.");
|
||||||
|
}
|
||||||
|
return (int) $value;
|
||||||
|
} else {
|
||||||
|
throw new Exception("Expected number, '$value' given.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -536,7 +536,7 @@ final class Translator
|
|||||||
} elseif ($this->comment) {
|
} elseif ($this->comment) {
|
||||||
return "(limit $arg)";
|
return "(limit $arg)";
|
||||||
} else {
|
} else {
|
||||||
$this->limit = (int) $arg;
|
$this->limit = Helpers::intVal($arg);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
@@ -546,7 +546,7 @@ final class Translator
|
|||||||
} elseif ($this->comment) {
|
} elseif ($this->comment) {
|
||||||
return "(offset $arg)";
|
return "(offset $arg)";
|
||||||
} else {
|
} else {
|
||||||
$this->offset = (int) $arg;
|
$this->offset = Helpers::intVal($arg);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
|
@@ -144,10 +144,9 @@ if ($config['system'] === 'mysql') {
|
|||||||
->limit(' 1; DROP TABLE users')
|
->limit(' 1; DROP TABLE users')
|
||||||
->offset(' 1; DROP TABLE users');
|
->offset(' 1; DROP TABLE users');
|
||||||
|
|
||||||
Assert::same(
|
Assert::error(function () use ($fluent) {
|
||||||
reformat(' SELECT * LIMIT 1 OFFSET 1'),
|
(string) $fluent;
|
||||||
(string) $fluent
|
}, E_USER_ERROR, "Expected number, ' 1; DROP TABLE users' given.");
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
32
tests/dibi/Helpers.intVal().phpt
Normal file
32
tests/dibi/Helpers.intVal().phpt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Dibi\Helpers;
|
||||||
|
use Tester\Assert;
|
||||||
|
|
||||||
|
require __DIR__ . '/bootstrap.php';
|
||||||
|
|
||||||
|
|
||||||
|
Assert::same(0, Helpers::intVal(0));
|
||||||
|
Assert::same(0, Helpers::intVal('0'));
|
||||||
|
Assert::same(-10, Helpers::intVal('-10'));
|
||||||
|
|
||||||
|
Assert::exception(function () {
|
||||||
|
Helpers::intVal('12345678901234567890123456879');
|
||||||
|
}, Dibi\Exception::class, 'Number 12345678901234567890123456879 is greater than integer.');
|
||||||
|
|
||||||
|
Assert::exception(function () {
|
||||||
|
Helpers::intVal('-12345678901234567890123456879');
|
||||||
|
}, Dibi\Exception::class, 'Number -12345678901234567890123456879 is greater than integer.');
|
||||||
|
|
||||||
|
Assert::exception(function () {
|
||||||
|
Helpers::intVal('');
|
||||||
|
}, Dibi\Exception::class, "Expected number, '' given.");
|
||||||
|
|
||||||
|
Assert::exception(function () {
|
||||||
|
Helpers::intVal('not number');
|
||||||
|
}, Dibi\Exception::class, "Expected number, 'not number' given.");
|
||||||
|
|
||||||
|
Assert::exception(function () {
|
||||||
|
Helpers::intVal(null);
|
||||||
|
}, Dibi\Exception::class, "Expected number, '' given.");
|
Reference in New Issue
Block a user