1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 22:26:43 +02:00

fetch(), fetchSingle(), getAffectedRows(), getInsertId() return NULL instead of FALSE on error (BC break)

This commit is contained in:
David Grudl
2017-06-09 18:11:51 +02:00
parent 5c514f6721
commit 7d42317279
18 changed files with 109 additions and 94 deletions

View File

@@ -317,7 +317,7 @@ class Connection
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
if (!is_int($rows) || $rows < 0) { if ($rows === NULL || $rows < 0) {
throw new Exception('Cannot retrieve number of affected rows.'); throw new Exception('Cannot retrieve number of affected rows.');
} }
return $rows; return $rows;
@@ -531,7 +531,7 @@ class Connection
/** /**
* Executes SQL query and fetch result - shortcut for query() & fetch(). * Executes SQL query and fetch result - shortcut for query() & fetch().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return Row|FALSE * @return Row|NULL
* @throws Exception * @throws Exception
*/ */
public function fetch(...$args) public function fetch(...$args)

View File

@@ -168,7 +168,7 @@ class DataSource implements IDataSource
/** /**
* Generates, executes SQL query and fetches the single row. * Generates, executes SQL query and fetches the single row.
* @return Row|FALSE * @return Row|NULL
*/ */
public function fetch() public function fetch()
{ {
@@ -178,7 +178,7 @@ class DataSource implements IDataSource
/** /**
* Like fetch(), but returns only first field. * Like fetch(), but returns only first field.
* @return mixed value on success, FALSE if no next record * @return mixed value on success, NULL if no next record
*/ */
public function fetchSingle() public function fetchSingle()
{ {

View File

@@ -8,6 +8,7 @@
namespace Dibi\Drivers; namespace Dibi\Drivers;
use Dibi; use Dibi;
use Dibi\Helpers;
/** /**
@@ -62,7 +63,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
Dibi\Helpers::alias($config, 'database', 'db'); Helpers::alias($config, 'database', 'db');
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
@@ -129,22 +130,22 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
return ibase_affected_rows($this->connection); return Helpers::false2Null(ibase_affected_rows($this->connection));
} }
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @param string generator name * @param string generator name
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
return ibase_gen_id($sequence, 0, $this->connection); return Helpers::false2Null(ibase_gen_id($sequence, 0, $this->connection));
} }
@@ -382,7 +383,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
@@ -398,7 +399,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
} }
return $result; return Helpers::false2Null($result);
} }

View File

@@ -104,17 +104,17 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
return mssql_rows_affected($this->connection); return Dibi\Helpers::false2Null(mssql_rows_affected($this->connection));
} }
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -123,7 +123,7 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
$row = mssql_fetch_row($res); $row = mssql_fetch_row($res);
return $row[0]; return $row[0];
} }
return FALSE; return NULL;
} }
@@ -338,18 +338,18 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
return mssql_fetch_array($this->resultSet, $assoc ? MSSQL_ASSOC : MSSQL_NUM); return Dibi\Helpers::false2Null(mssql_fetch_array($this->resultSet, $assoc ? MSSQL_ASSOC : MSSQL_NUM));
} }
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek($row)
{ {

View File

@@ -57,7 +57,7 @@ class MsSqlReflector implements Dibi\Reflector
public function getTableCount($table, $fallback = TRUE) public function getTableCount($table, $fallback = TRUE)
{ {
if (empty($table)) { if (empty($table)) {
return FALSE; return NULL;
} }
$result = $this->driver->query(" $result = $this->driver->query("
SELECT MAX(rowcnt) SELECT MAX(rowcnt)
@@ -71,7 +71,7 @@ class MsSqlReflector implements Dibi\Reflector
$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 = intval($row[0]); $count = intval($row[0]);
} else { } else {
$count = FALSE; $count = NULL;
} }
} else { } else {
$count = intval($row[0]); $count = intval($row[0]);

View File

@@ -203,17 +203,17 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
return mysqli_affected_rows($this->connection) === -1 ? FALSE : mysqli_affected_rows($this->connection); return mysqli_affected_rows($this->connection) === -1 ? NULL : mysqli_affected_rows($this->connection);
} }
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -432,7 +432,7 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {

View File

@@ -34,8 +34,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** @var bool */ /** @var bool */
private $autoFree = TRUE; private $autoFree = TRUE;
/** @var int|FALSE Affected rows */ /** @var int|NULL Affected rows */
private $affectedRows = FALSE; private $affectedRows;
/** @var int Cursor */ /** @var int Cursor */
private $row = 0; private $row = 0;
@@ -100,14 +100,14 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function query($sql) public function query($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = NULL;
$res = @odbc_exec($this->connection, $sql); // intentionally @ $res = @odbc_exec($this->connection, $sql); // intentionally @
if ($res === FALSE) { if ($res === FALSE) {
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = odbc_num_rows($res); $this->affectedRows = Dibi\Helpers::false2Null(odbc_num_rows($res));
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
return NULL; return NULL;
@@ -116,7 +116,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -126,7 +126,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -363,16 +363,16 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
if ($assoc) { if ($assoc) {
return odbc_fetch_array($this->resultSet, ++$this->row); return Dibi\Helpers::false2Null(odbc_fetch_array($this->resultSet, ++$this->row));
} else { } else {
$set = $this->resultSet; $set = $this->resultSet;
if (!odbc_fetch_row($set, ++$this->row)) { if (!odbc_fetch_row($set, ++$this->row)) {
return FALSE; return NULL;
} }
$count = odbc_num_fields($set); $count = odbc_num_fields($set);
$cols = []; $cols = [];

View File

@@ -43,8 +43,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** @var string Date and datetime format */ /** @var string Date and datetime format */
private $fmtDate, $fmtDateTime; private $fmtDate, $fmtDateTime;
/** @var int|FALSE Number of affected rows */ /** @var int|NULL Number of affected rows */
private $affectedRows = FALSE; private $affectedRows;
/** /**
@@ -109,7 +109,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function query($sql) public function query($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = NULL;
$res = oci_parse($this->connection, $sql); $res = oci_parse($this->connection, $sql);
if ($res) { if ($res) {
@oci_execute($res, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT); @oci_execute($res, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
@@ -118,7 +118,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
throw self::createException($err['message'], $err['code'], $sql); throw self::createException($err['message'], $err['code'], $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = oci_num_rows($res); $this->affectedRows = Dibi\Helpers::false2Null(oci_num_rows($res));
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
} else { } else {
@@ -151,7 +151,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -161,12 +161,12 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
$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'] : FALSE; return isset($row['ID']) ? (int) $row['ID'] : NULL;
} }
@@ -396,11 +396,11 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
return oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS); return Dibi\Helpers::false2Null(oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS));
} }

View File

@@ -8,6 +8,7 @@
namespace Dibi\Drivers; namespace Dibi\Drivers;
use Dibi; use Dibi;
use Dibi\Helpers;
use PDO; use PDO;
@@ -33,8 +34,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** @var \PDOStatement|NULL Resultset resource */ /** @var \PDOStatement|NULL Resultset resource */
private $resultSet; private $resultSet;
/** @var int|FALSE Affected rows */ /** @var int|NULL Affected rows */
private $affectedRows = FALSE; private $affectedRows;
/** @var string */ /** @var string */
private $driverName; private $driverName;
@@ -63,7 +64,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
$foo = &$config['dsn']; $foo = &$config['dsn'];
$foo = &$config['options']; $foo = &$config['options'];
Dibi\Helpers::alias($config, 'resource', 'pdo'); Helpers::alias($config, 'resource', 'pdo');
if ($config['resource'] instanceof PDO) { if ($config['resource'] instanceof PDO) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
@@ -106,11 +107,11 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
// must detect if SQL returns result set or num of affected rows // must detect if SQL returns result set or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6)); $cmd = strtoupper(substr(ltrim($sql), 0, 6));
static $list = ['UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1]; static $list = ['UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1];
$this->affectedRows = FALSE; $this->affectedRows = NULL;
if (isset($list[$cmd])) { if (isset($list[$cmd])) {
$this->affectedRows = $this->connection->exec($sql); $this->affectedRows = Helpers::false2Null($this->connection->exec($sql));
if ($this->affectedRows !== FALSE) { if ($this->affectedRows !== NULL) {
return NULL; return NULL;
} }
} else { } else {
@@ -143,7 +144,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -153,11 +154,11 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
return $this->connection->lastInsertId(); return Helpers::false2Null($this->connection->lastInsertId());
} }
@@ -499,11 +500,11 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
return $this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM); return Helpers::false2Null($this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM));
} }

View File

@@ -8,6 +8,7 @@
namespace Dibi\Drivers; namespace Dibi\Drivers;
use Dibi; use Dibi;
use Dibi\Helpers;
/** /**
@@ -35,8 +36,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** @var bool */ /** @var bool */
private $autoFree = TRUE; private $autoFree = TRUE;
/** @var int|FALSE Affected rows */ /** @var int|NULL Affected rows */
private $affectedRows = FALSE; private $affectedRows;
/** /**
@@ -69,8 +70,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
$string = $config['string']; $string = $config['string'];
} else { } else {
$string = ''; $string = '';
Dibi\Helpers::alias($config, 'user', 'username'); Helpers::alias($config, 'user', 'username');
Dibi\Helpers::alias($config, 'dbname', 'database'); Helpers::alias($config, 'dbname', 'database');
foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) { foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) {
if (isset($config[$key])) { if (isset($config[$key])) {
$string .= $key . '=' . $config[$key] . ' '; $string .= $key . '=' . $config[$key] . ' ';
@@ -133,14 +134,14 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function query($sql) public function query($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = NULL;
$res = @pg_query($this->connection, $sql); // intentionally @ $res = @pg_query($this->connection, $sql); // intentionally @
if ($res === FALSE) { if ($res === FALSE) {
throw self::createException(pg_last_error($this->connection), NULL, $sql); throw self::createException(pg_last_error($this->connection), NULL, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = pg_affected_rows($res); $this->affectedRows = Helpers::false2Null(pg_affected_rows($res));
if (pg_num_fields($res)) { if (pg_num_fields($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
@@ -179,7 +180,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -189,7 +190,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -201,11 +202,11 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
if (!$res) { if (!$res) {
return FALSE; return NULL;
} }
$row = $res->fetch(FALSE); $row = $res->fetch(FALSE);
return is_array($row) ? $row[0] : FALSE; return is_array($row) ? $row[0] : NULL;
} }
@@ -438,11 +439,11 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
return pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM); return Helpers::false2Null(pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM));
} }

View File

@@ -154,7 +154,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -164,7 +164,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -381,11 +381,14 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
$row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM); $row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM);
if (!$row) {
return NULL;
}
$charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT'; $charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT';
if ($row && ($assoc || $charset)) { if ($row && ($assoc || $charset)) {
$tmp = []; $tmp = [];

View File

@@ -38,8 +38,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** @var bool */ /** @var bool */
private $autoFree = TRUE; private $autoFree = TRUE;
/** @var int|FALSE Affected rows */ /** @var int|NULL Affected rows */
private $affectedRows = FALSE; private $affectedRows;
/** @var string */ /** @var string */
private $version = ''; private $version = '';
@@ -109,7 +109,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
*/ */
public function query($sql) public function query($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = NULL;
$res = sqlsrv_query($this->connection, $sql); $res = sqlsrv_query($this->connection, $sql);
if ($res === FALSE) { if ($res === FALSE) {
@@ -117,7 +117,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
throw new Dibi\DriverException($info[0]['message'], $info[0]['code'], $sql); throw new Dibi\DriverException($info[0]['message'], $info[0]['code'], $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = sqlsrv_rows_affected($res); $this->affectedRows = Helpers::false2Null(sqlsrv_rows_affected($res));
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
return NULL; return NULL;
@@ -126,7 +126,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
@@ -136,7 +136,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
@@ -145,7 +145,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC); $row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
return $row[0]; return $row[0];
} }
return FALSE; return NULL;
} }
@@ -369,7 +369,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {

View File

@@ -319,7 +319,7 @@ class Fluent implements IDataSource
/** /**
* Generates, executes SQL query and fetches the single row. * Generates, executes SQL query and fetches the single row.
* @return Row|FALSE * @return Row|NULL
*/ */
public function fetch() public function fetch()
{ {
@@ -333,7 +333,7 @@ class Fluent implements IDataSource
/** /**
* Like fetch(), but returns only first field. * Like fetch(), but returns only first field.
* @return mixed value on success, FALSE if no next record * @return mixed value on success, NULL if no next record
*/ */
public function fetchSingle() public function fetchSingle()
{ {

View File

@@ -282,4 +282,13 @@ class Helpers
return $count; return $count;
} }
/**
* @internal
*/
public static function false2Null($val)
{
return $val === FALSE ? NULL : $val;
}
} }

View File

@@ -174,20 +174,20 @@ class Result implements IDataSource
/** /**
* Fetches the row at current position, process optional type conversion. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
* @return Row|FALSE * @return Row|NULL
*/ */
final public function fetch() final public function fetch()
{ {
$row = $this->getResultDriver()->fetch(TRUE); $row = $this->getResultDriver()->fetch(TRUE);
if (!is_array($row)) { if ($row === NULL) {
return FALSE; return NULL;
} }
$this->fetched = TRUE; $this->fetched = TRUE;
$this->normalize($row); $this->normalize($row);
if ($this->rowFactory) { if ($this->rowFactory) {
return ($this->rowFactory)($row); return ($this->rowFactory)($row);
} elseif ($this->rowClass) { } elseif ($this->rowClass) {
$row = new $this->rowClass($row); return new $this->rowClass($row);
} }
return $row; return $row;
} }
@@ -195,13 +195,13 @@ class Result implements IDataSource
/** /**
* Like fetch(), but returns only first field. * Like fetch(), but returns only first field.
* @return mixed value on success, FALSE if no next record * @return mixed value on success, NULL if no next record
*/ */
final public function fetchSingle() final public function fetchSingle()
{ {
$row = $this->getResultDriver()->fetch(TRUE); $row = $this->getResultDriver()->fetch(TRUE);
if (!is_array($row)) { if ($row === NULL) {
return FALSE; return NULL;
} }
$this->fetched = TRUE; $this->fetched = TRUE;
$this->normalize($row); $this->normalize($row);

View File

@@ -182,7 +182,7 @@ class dibi
/** /**
* Executes SQL query and fetch result - Monostate for Dibi\Connection::query() & fetch(). * Executes SQL query and fetch result - Monostate for Dibi\Connection::query() & fetch().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return Dibi\Row * @return Dibi\Row|NULL
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function fetch(...$args) public static function fetch(...$args)

View File

@@ -49,13 +49,13 @@ interface Driver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|NULL number of rows or NULL on error
*/ */
function getAffectedRows(); function getAffectedRows();
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure * @return int|NULL int on success or NULL on failure
*/ */
function getInsertId($sequence); function getInsertId($sequence);
@@ -166,16 +166,16 @@ interface ResultDriver
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Exception * @throws Exception
*/ */
function seek($row); function seek($row);
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @return array|NULL array on success, NULL if no next record
* @internal * @internal
*/ */
function fetch($type); function fetch($type);

View File

@@ -25,7 +25,7 @@ class MockDriver extends Dibi\Drivers\SqlsrvDriver
function fetch($assoc) function fetch($assoc)
{ {
return FALSE; return NULL;
} }
} }