Skip to content

Commit

Permalink
EmptyResult WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 4, 2018
1 parent 67a82b1 commit b49800b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ final public function getDriver(): Driver
* @param mixed ...$args
* @throws Exception
*/
final public function query(...$args): ?Result
final public function query(...$args): Result
{
return $this->nativeQuery($this->translateArgs($args));
}
Expand Down Expand Up @@ -258,7 +258,7 @@ protected function translateArgs(array $args): string
* Executes the SQL query.
* @throws Exception
*/
final public function nativeQuery(string $sql): ?Result
final public function nativeQuery(string $sql): Result
{
if (!$this->driver) {
$this->connect();
Expand All @@ -276,7 +276,7 @@ final public function nativeQuery(string $sql): ?Result
throw $e;
}

$res = $res ? $this->createResultSet($res) : null;
$res = $this->createResultSet($res ?: new Drivers\EmptyResult(max(0, $this->driver->getAffectedRows())));
if ($event) {
$this->onEvent($event->done($res));
}
Expand Down
74 changes: 74 additions & 0 deletions src/Dibi/Drivers/EmptyResult.php
Original file line number Diff line number Diff line change
@@ -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 empty result set.
*/
class EmptyResult 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;
}
}
2 changes: 1 addition & 1 deletion src/Dibi/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public function count(): int
}


private function query($args): ?Result
private function query($args): Result
{
$res = $this->connection->query($args);
foreach ($this->setups as $setup) {
Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/dibi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* store connections info.
*
* @method void disconnect()
* @method Dibi\Result|null query(...$args)
* @method Dibi\Result|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)
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/interfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function getDriver(): Driver;
* Generates (translates) and executes SQL query.
* @throws Exception
*/
function query(...$args): ?Result;
function query(...$args): Result;

/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
Expand Down
3 changes: 2 additions & 1 deletion tests/dibi/Fluent.fetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) {

// affected rows
$res = $conn->update('products', ['title' => 'new'])->execute();
Assert::null($res);
Assert::same(3, $res->getRowCount());
Assert::same(0, $res->getColumnCount());
Assert::same(3, $conn->getAffectedRows());

0 comments on commit b49800b

Please sign in to comment.