1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-19 12:21:46 +02:00

Connection::query() and Fluent::execute() always return Result, not the number of affected rows (BC break)

This commit is contained in:
David Grudl
2018-05-02 18:39:25 +02:00
parent e291d4d825
commit 8dff5b5b3c
7 changed files with 92 additions and 23 deletions

View File

@@ -187,10 +187,9 @@ class Connection implements IConnection
/**
* Generates (translates) and executes SQL query.
* @param mixed ...$args
* @return Result|int|null result set or number of affected rows
* @throws Exception
*/
final public function query(...$args)
final public function query(...$args): Result
{
return $this->nativeQuery($this->translateArgs($args));
}
@@ -257,10 +256,9 @@ class Connection implements IConnection
/**
* Executes the SQL query.
* @return Result|int|null result set or number of affected rows
* @throws Exception
*/
final public function nativeQuery(string $sql)
final public function nativeQuery(string $sql): Result
{
if (!$this->driver) {
$this->connect();
@@ -278,12 +276,7 @@ class Connection implements IConnection
throw $e;
}
if ($res) {
$res = $this->createResultSet($res);
} else {
$res = $this->driver->getAffectedRows();
}
$res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows())));
if ($event) {
$this->onEvent($event->done($res));
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Dibi\Drivers;
use Dibi;
/**
* The dibi driver for no result set.
*/
class NoDataResult implements Dibi\ResultDriver
{
use Dibi\Strict;
/** @var int */
private $rows;
public function __construct(int $rows)
{
$this->rows = $rows;
}
/**
* Returns the number of affected rows.
*/
public function getRowCount(): int
{
return $this->rows;
}
public function fetch(bool $assoc): ?array
{
return null;
}
public function seek(int $row): bool
{
return false;
}
public function free(): void
{
}
public function getResultColumns(): array
{
return [];
}
public function getResultResource()
{
return null;
}
public function unescapeBinary(string $value): string
{
return $value;
}
}

View File

@@ -380,10 +380,7 @@ class Fluent implements IDataSource
}
/**
* @return Result|int
*/
private function query($args)
private function query($args): Result
{
$res = $this->connection->query($args);
foreach ($this->setups as $setup) {

View File

@@ -13,8 +13,8 @@ declare(strict_types=1);
* store connections info.
*
* @method void disconnect()
* @method Dibi\Result|int|null query(...$args)
* @method Dibi\Result|int|null nativeQuery(...$args)
* @method Dibi\Result query(...$args)
* @method Dibi\Result nativeQuery(...$args)
* @method bool test(...$args)
* @method Dibi\DataSource dataSource(...$args)
* @method Dibi\Row|null fetch(...$args)

View File

@@ -214,10 +214,9 @@ interface IConnection
/**
* Generates (translates) and executes SQL query.
* @return Result|int|null result set or number of affected rows
* @throws Exception
*/
function query(...$args);
function query(...$args): Result;
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.

View File

@@ -20,18 +20,22 @@ $conn->query('INSERT INTO products', [
Assert::same(1, $conn->getAffectedRows());
$conn->query('UPDATE products SET title="xxx" WHERE product_id > 100');
$res = $conn->query('UPDATE products SET title="xxx" WHERE product_id > 100');
Assert::same(0, $conn->getAffectedRows());
Assert::same(0, $res->getRowCount());
$conn->query('UPDATE products SET title="xxx"');
$res = $conn->query('UPDATE products SET title="xxx"');
Assert::same(4, $conn->getAffectedRows());
Assert::same(4, $res->getRowCount());
$conn->query('DELETE FROM orders');
$conn->query('DELETE FROM products WHERE product_id > 100');
$res = $conn->query('DELETE FROM products WHERE product_id > 100');
Assert::same(0, $conn->getAffectedRows());
Assert::same(0, $res->getRowCount());
$conn->query('DELETE FROM products WHERE product_id < 3');
$res = $conn->query('DELETE FROM products WHERE product_id < 3');
Assert::same(2, $conn->getAffectedRows());
Assert::same(2, $res->getRowCount());

View File

@@ -54,4 +54,6 @@ if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) {
// affected rows
$res = $conn->update('products', ['title' => 'new'])->execute();
Assert::same(3, $res);
Assert::same(3, $res->getRowCount());
Assert::same(0, $res->getColumnCount());
Assert::same(3, $conn->getAffectedRows());