mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 14:16:39 +02:00
renamed interfaces (BC break)
Dibi\Driver => Dibi\Drivers\Connection Dibi\ResultDriver => Dibi\Drivers\Result Dibi\Reflector => Dibi\Drivers\Engine
This commit is contained in:
@@ -29,7 +29,7 @@ class Connection
|
||||
|
||||
/** @var string[] resultset formats */
|
||||
private array $formats;
|
||||
private ?Driver $driver = null;
|
||||
private ?Drivers\Connection $driver = null;
|
||||
private ?Translator $translator = null;
|
||||
|
||||
/** @var array<string, callable(object): Expression | null> */
|
||||
@@ -122,12 +122,12 @@ class Connection
|
||||
*/
|
||||
final public function connect(): void
|
||||
{
|
||||
if ($this->config['driver'] instanceof Driver) {
|
||||
if ($this->config['driver'] instanceof Drivers\Connection) {
|
||||
$this->driver = $this->config['driver'];
|
||||
$this->translator = new Translator($this);
|
||||
return;
|
||||
|
||||
} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
|
||||
} elseif (is_subclass_of($this->config['driver'], Drivers\Connection::class)) {
|
||||
$class = $this->config['driver'];
|
||||
|
||||
} else {
|
||||
@@ -198,7 +198,7 @@ class Connection
|
||||
/**
|
||||
* Returns the driver and connects to a database in lazy mode.
|
||||
*/
|
||||
final public function getDriver(): Driver
|
||||
final public function getDriver(): Drivers\Connection
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
@@ -450,7 +450,7 @@ class Connection
|
||||
/**
|
||||
* Result set factory.
|
||||
*/
|
||||
public function createResultSet(ResultDriver $resultDriver): Result
|
||||
public function createResultSet(Drivers\Result $resultDriver): Result
|
||||
{
|
||||
return (new Result($resultDriver, $this->config['result']['normalize'] ?? true))
|
||||
->setFormats($this->formats);
|
||||
|
97
src/Dibi/Drivers/Connection.php
Normal file
97
src/Dibi/Drivers/Connection.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi\DriverException;
|
||||
use Dibi\Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Database connection driver.
|
||||
*/
|
||||
interface Connection
|
||||
{
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
* @throws Exception
|
||||
*/
|
||||
function disconnect(): void;
|
||||
|
||||
/**
|
||||
* Internal: Executes the SQL query.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function query(string $sql): ?Result;
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
function getAffectedRows(): ?int;
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
function getInsertId(?string $sequence): ?int;
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws DriverException
|
||||
*/
|
||||
function begin(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function commit(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function rollback(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
*/
|
||||
function getResource(): mixed;
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
function getReflector(): Engine;
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
function escapeText(string $value): string;
|
||||
|
||||
function escapeBinary(string $value): string;
|
||||
|
||||
function escapeIdentifier(string $value): string;
|
||||
|
||||
function escapeBool(bool $value): string;
|
||||
|
||||
function escapeDate(\DateTimeInterface $value): string;
|
||||
|
||||
function escapeDateTime(\DateTimeInterface $value): string;
|
||||
|
||||
function escapeDateInterval(\DateInterval $value): string;
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
function escapeLike(string $value, int $pos): string;
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
|
||||
}
|
@@ -15,14 +15,14 @@ use Dibi;
|
||||
/**
|
||||
* The dummy driver for testing purposes.
|
||||
*/
|
||||
class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
class DummyDriver implements Connection, Result, Engine
|
||||
{
|
||||
public function disconnect(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
40
src/Dibi/Drivers/Engine.php
Normal file
40
src/Dibi/Drivers/Engine.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
|
||||
/**
|
||||
* Engine-specific behaviors.
|
||||
*/
|
||||
interface Engine
|
||||
{
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array of {name [, (bool) view ]}
|
||||
*/
|
||||
function getTables(): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
|
||||
*/
|
||||
function getColumns(string $table): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
* @return array of {name, (array of names) columns [, (bool) unique, (bool) primary ]}
|
||||
*/
|
||||
function getIndexes(string $table): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
function getForeignKeys(string $table): array;
|
||||
}
|
@@ -25,7 +25,7 @@ use function is_resource;
|
||||
* - buffers (int) => buffers is the number of database buffers to allocate for the server-side cache. If 0 or omitted, server chooses its own default.
|
||||
* - resource (resource) => existing connection resource
|
||||
*/
|
||||
class FirebirdDriver implements Dibi\Driver
|
||||
class FirebirdDriver implements Connection
|
||||
{
|
||||
public const ErrorExceptionThrown = -836;
|
||||
|
||||
@@ -86,7 +86,7 @@ class FirebirdDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException|Dibi\Exception
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$resource = $this->inTransaction
|
||||
? $this->transaction
|
||||
@@ -200,7 +200,7 @@ class FirebirdDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new FirebirdReflector($this);
|
||||
}
|
||||
|
@@ -9,16 +9,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for Firebird/InterBase database.
|
||||
*/
|
||||
class FirebirdReflector implements Dibi\Reflector
|
||||
class FirebirdReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -17,7 +17,7 @@ use function is_resource;
|
||||
/**
|
||||
* The driver for Firebird/InterBase result set.
|
||||
*/
|
||||
class FirebirdResult implements Dibi\ResultDriver
|
||||
class FirebirdResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
/** @var resource */
|
||||
|
@@ -16,10 +16,10 @@ use Dibi;
|
||||
* The reflector for MySQL databases.
|
||||
* @internal
|
||||
*/
|
||||
class MySqlReflector implements Dibi\Reflector
|
||||
class MySqlReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -32,7 +32,7 @@ use const MYSQLI_REPORT_OFF, MYSQLI_STORE_RESULT, MYSQLI_USE_RESULT, PREG_SET_OR
|
||||
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
|
||||
* - resource (mysqli) => existing connection resource
|
||||
*/
|
||||
class MySqliDriver implements Dibi\Driver
|
||||
class MySqliDriver implements Connection
|
||||
{
|
||||
public const ErrorAccessDenied = 1045;
|
||||
public const ErrorDuplicateEntry = 1062;
|
||||
@@ -148,7 +148,7 @@ class MySqliDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$res = @$this->connection->query($sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
|
||||
|
||||
@@ -265,7 +265,7 @@ class MySqliDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new MySqlReflector($this);
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ use const MYSQLI_TYPE_LONG, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_TIME, MYSQLI_TYPE_TIN
|
||||
/**
|
||||
* The driver for MySQL result set.
|
||||
*/
|
||||
class MySqliResult implements Dibi\ResultDriver
|
||||
class MySqliResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
private readonly \mysqli_result $resultSet,
|
||||
|
@@ -9,13 +9,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for no result set.
|
||||
*/
|
||||
class NoDataResult implements Dibi\ResultDriver
|
||||
class NoDataResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
private readonly int $rows,
|
||||
|
@@ -24,7 +24,7 @@ use function is_resource;
|
||||
* - resource (resource) => existing connection resource
|
||||
* - microseconds (bool) => use microseconds in datetime format?
|
||||
*/
|
||||
class OdbcDriver implements Dibi\Driver
|
||||
class OdbcDriver implements Connection
|
||||
{
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
@@ -77,7 +77,7 @@ class OdbcDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = @odbc_exec($this->connection, $sql); // intentionally @
|
||||
@@ -176,7 +176,7 @@ class OdbcDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new OdbcReflector($this);
|
||||
}
|
||||
|
@@ -15,10 +15,10 @@ use Dibi;
|
||||
/**
|
||||
* The reflector for ODBC connections.
|
||||
*/
|
||||
class OdbcReflector implements Dibi\Reflector
|
||||
class OdbcReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,7 @@ use function is_resource;
|
||||
/**
|
||||
* The driver interacting with result set via ODBC connections.
|
||||
*/
|
||||
class OdbcResult implements Dibi\ResultDriver
|
||||
class OdbcResult implements Result
|
||||
{
|
||||
private int $row = 0;
|
||||
|
||||
|
@@ -26,7 +26,7 @@ use function in_array, is_resource;
|
||||
* - resource (resource) => existing connection resource
|
||||
* - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect
|
||||
*/
|
||||
class OracleDriver implements Dibi\Driver
|
||||
class OracleDriver implements Connection
|
||||
{
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
@@ -77,7 +77,7 @@ class OracleDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = oci_parse($this->connection, $sql);
|
||||
@@ -190,7 +190,7 @@ class OracleDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new OracleReflector($this);
|
||||
}
|
||||
|
@@ -15,10 +15,10 @@ use Dibi;
|
||||
/**
|
||||
* The reflector for Oracle database.
|
||||
*/
|
||||
class OracleReflector implements Dibi\Reflector
|
||||
class OracleReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,7 @@ use function is_resource;
|
||||
/**
|
||||
* The driver for Oracle result set.
|
||||
*/
|
||||
class OracleResult implements Dibi\ResultDriver
|
||||
class OracleResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
/** @var resource */
|
||||
|
@@ -25,7 +25,7 @@ use function sprintf;
|
||||
* - options (array) => driver specific options {@see PDO::__construct}
|
||||
* - resource (PDO) => existing connection
|
||||
*/
|
||||
class PdoDriver implements Dibi\Driver
|
||||
class PdoDriver implements Connection
|
||||
{
|
||||
private ?PDO $connection;
|
||||
private ?int $affectedRows;
|
||||
@@ -80,7 +80,7 @@ class PdoDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$res = $this->connection->query($sql);
|
||||
if ($res) {
|
||||
@@ -172,7 +172,7 @@ class PdoDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return match ($this->driverName) {
|
||||
'mysql' => new MySqlReflector($this),
|
||||
|
@@ -17,7 +17,7 @@ use PDO;
|
||||
/**
|
||||
* The driver for PDO result set.
|
||||
*/
|
||||
class PdoResult implements Dibi\ResultDriver
|
||||
class PdoResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
private ?\PDOStatement $resultSet,
|
||||
|
@@ -27,7 +27,7 @@ use function in_array, is_array, is_resource, strlen;
|
||||
* - resource (PgSql\Connection) => existing connection resource
|
||||
* - connect_type (int) => see pg_connect()
|
||||
*/
|
||||
class PostgreDriver implements Dibi\Driver
|
||||
class PostgreDriver implements Connection
|
||||
{
|
||||
private PgSql\Connection $connection;
|
||||
private ?int $affectedRows;
|
||||
@@ -110,7 +110,7 @@ class PostgreDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = @pg_query($this->connection, $sql); // intentionally @
|
||||
@@ -232,7 +232,7 @@ class PostgreDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new PostgreReflector($this);
|
||||
}
|
||||
|
@@ -9,16 +9,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for PostgreSQL database.
|
||||
*/
|
||||
class PostgreReflector implements Dibi\Reflector
|
||||
class PostgreReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -9,7 +9,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use PgSql;
|
||||
use function is_resource;
|
||||
@@ -18,7 +17,7 @@ use function is_resource;
|
||||
/**
|
||||
* The driver for PostgreSQL result set.
|
||||
*/
|
||||
class PostgreResult implements Dibi\ResultDriver
|
||||
class PostgreResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PgSql\Result $resultSet,
|
||||
|
58
src/Dibi/Drivers/Result.php
Normal file
58
src/Dibi/Drivers/Result.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi\Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Database result driver.
|
||||
*/
|
||||
interface Result
|
||||
{
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
function getRowCount(): int;
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
* @throws Exception
|
||||
*/
|
||||
function seek(int $row): bool;
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $type true for associative array, false for numeric
|
||||
* @internal
|
||||
*/
|
||||
function fetch(bool $type): ?array;
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
function free(): void;
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
|
||||
*/
|
||||
function getResultColumns(): array;
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
*/
|
||||
function getResultResource(): mixed;
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
function unescapeBinary(string $value): string;
|
||||
}
|
@@ -23,7 +23,7 @@ use SQLite3;
|
||||
* - formatDateTime => how to format datetime in SQL (@see date)
|
||||
* - resource (SQLite3) => existing connection resource
|
||||
*/
|
||||
class SqliteDriver implements Dibi\Driver
|
||||
class SqliteDriver implements Connection
|
||||
{
|
||||
private SQLite3 $connection;
|
||||
private string $fmtDate;
|
||||
@@ -73,7 +73,7 @@ class SqliteDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$res = @$this->connection->query($sql); // intentionally @
|
||||
if ($code = $this->connection->lastErrorCode()) {
|
||||
@@ -174,7 +174,7 @@ class SqliteDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new SqliteReflector($this);
|
||||
}
|
||||
|
@@ -9,16 +9,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for SQLite database.
|
||||
*/
|
||||
class SqliteReflector implements Dibi\Reflector
|
||||
class SqliteReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -17,7 +17,7 @@ use const SQLITE3_ASSOC, SQLITE3_BLOB, SQLITE3_FLOAT, SQLITE3_INTEGER, SQLITE3_N
|
||||
/**
|
||||
* The driver for SQLite result set.
|
||||
*/
|
||||
class SqliteResult implements Dibi\ResultDriver
|
||||
class SqliteResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
private readonly \SQLite3Result $resultSet,
|
||||
|
@@ -26,7 +26,7 @@ use function is_resource, sprintf;
|
||||
* - charset => character encoding to set (default is UTF-8)
|
||||
* - resource (resource) => existing connection resource
|
||||
*/
|
||||
class SqlsrvDriver implements Dibi\Driver
|
||||
class SqlsrvDriver implements Connection
|
||||
{
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
@@ -84,7 +84,7 @@ class SqlsrvDriver implements Dibi\Driver
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Result
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = sqlsrv_query($this->connection, $sql);
|
||||
@@ -171,7 +171,7 @@ class SqlsrvDriver implements Dibi\Driver
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
public function getReflector(): Engine
|
||||
{
|
||||
return new SqlsrvReflector($this);
|
||||
}
|
||||
|
@@ -16,10 +16,10 @@ use function sprintf;
|
||||
/**
|
||||
* The reflector for Microsoft SQL Server and SQL Azure databases.
|
||||
*/
|
||||
class SqlsrvReflector implements Dibi\Reflector
|
||||
class SqlsrvReflector implements Engine
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dibi\Driver $driver,
|
||||
private readonly Connection $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,7 @@ use function is_resource;
|
||||
/**
|
||||
* The driver for Microsoft SQL Server and SQL Azure result set.
|
||||
*/
|
||||
class SqlsrvResult implements Dibi\ResultDriver
|
||||
class SqlsrvResult implements Result
|
||||
{
|
||||
public function __construct(
|
||||
/** @var resource */
|
||||
|
@@ -159,7 +159,7 @@ class Helpers
|
||||
|
||||
|
||||
/** @internal */
|
||||
public static function escape(Driver $driver, $value, string $type): string
|
||||
public static function escape(Drivers\Connection $driver, $value, string $type): string
|
||||
{
|
||||
$types = [
|
||||
Type::Text => 'text',
|
||||
|
20
src/Dibi/IDataSource.php
Normal file
20
src/Dibi/IDataSource.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Provides an interface between a dataset and data-aware components.
|
||||
*/
|
||||
interface IDataSource extends \Countable, \IteratorAggregate
|
||||
{
|
||||
//function \IteratorAggregate::getIterator();
|
||||
//function \Countable::count();
|
||||
}
|
@@ -28,7 +28,7 @@ use Dibi;
|
||||
class Column
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?Dibi\Reflector $reflector,
|
||||
private readonly ?Dibi\Drivers\Engine $reflector,
|
||||
private array $info,
|
||||
) {
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ class Database
|
||||
|
||||
|
||||
public function __construct(
|
||||
private readonly Dibi\Reflector $reflector,
|
||||
private readonly Dibi\Drivers\Engine $reflector,
|
||||
private ?string $name = null,
|
||||
) {
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ class Result
|
||||
|
||||
|
||||
public function __construct(
|
||||
private readonly Dibi\ResultDriver $driver,
|
||||
private readonly Dibi\Drivers\Result $driver,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class Result
|
||||
{
|
||||
if (!isset($this->columns)) {
|
||||
$this->columns = [];
|
||||
$reflector = $this->driver instanceof Dibi\Reflector
|
||||
$reflector = $this->driver instanceof Dibi\Drivers\Engine
|
||||
? $this->driver
|
||||
: null;
|
||||
foreach ($this->driver->getResultColumns() as $info) {
|
||||
|
@@ -26,7 +26,7 @@ use function array_values, strtolower;
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
private readonly Dibi\Reflector $reflector;
|
||||
private readonly Dibi\Drivers\Engine $reflector;
|
||||
private string $name;
|
||||
private bool $view;
|
||||
|
||||
@@ -41,7 +41,7 @@ class Table
|
||||
private ?Index $primaryKey;
|
||||
|
||||
|
||||
public function __construct(Dibi\Reflector $reflector, array $info)
|
||||
public function __construct(Dibi\Drivers\Engine $reflector, array $info)
|
||||
{
|
||||
$this->reflector = $reflector;
|
||||
$this->name = $info['name'];
|
||||
|
@@ -20,7 +20,7 @@ use const PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_NO_EMPTY;
|
||||
*/
|
||||
class Result implements IDataSource
|
||||
{
|
||||
private ?ResultDriver $driver;
|
||||
private ?Drivers\Result $driver;
|
||||
|
||||
/** Translate table */
|
||||
private array $types = [];
|
||||
@@ -37,7 +37,7 @@ class Result implements IDataSource
|
||||
private array $formats = [];
|
||||
|
||||
|
||||
public function __construct(ResultDriver $driver, bool $normalize = true)
|
||||
public function __construct(Drivers\Result $driver, bool $normalize = true)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
if ($normalize) {
|
||||
@@ -62,7 +62,7 @@ class Result implements IDataSource
|
||||
* Safe access to property $driver.
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
final public function getResultDriver(): ResultDriver
|
||||
final public function getResultDriver(): Drivers\Result
|
||||
{
|
||||
if ($this->driver === null) {
|
||||
throw new \RuntimeException('Result-set was released from memory.');
|
||||
|
@@ -17,8 +17,8 @@ use function array_filter, array_keys, array_splice, array_values, count, explod
|
||||
*/
|
||||
final class Translator
|
||||
{
|
||||
private readonly Connection $connection;
|
||||
private readonly Driver $driver;
|
||||
private Connection $connection;
|
||||
private readonly Drivers\Connection $driver;
|
||||
private int $cursor = 0;
|
||||
private array $args;
|
||||
|
||||
|
@@ -1,180 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Provides an interface between a dataset and data-aware components.
|
||||
*/
|
||||
interface IDataSource extends \Countable, \IteratorAggregate
|
||||
{
|
||||
//function \IteratorAggregate::getIterator();
|
||||
//function \Countable::count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Driver interface.
|
||||
*/
|
||||
interface Driver
|
||||
{
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
* @throws Exception
|
||||
*/
|
||||
function disconnect(): void;
|
||||
|
||||
/**
|
||||
* Internal: Executes the SQL query.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function query(string $sql): ?ResultDriver;
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
function getAffectedRows(): ?int;
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
function getInsertId(?string $sequence): ?int;
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws DriverException
|
||||
*/
|
||||
function begin(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function commit(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws DriverException
|
||||
*/
|
||||
function rollback(?string $savepoint = null): void;
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
*/
|
||||
function getResource(): mixed;
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
function getReflector(): Reflector;
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
function escapeText(string $value): string;
|
||||
|
||||
function escapeBinary(string $value): string;
|
||||
|
||||
function escapeIdentifier(string $value): string;
|
||||
|
||||
function escapeBool(bool $value): string;
|
||||
|
||||
function escapeDate(\DateTimeInterface $value): string;
|
||||
|
||||
function escapeDateTime(\DateTimeInterface $value): string;
|
||||
|
||||
function escapeDateInterval(\DateInterval $value): string;
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
function escapeLike(string $value, int $pos): string;
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver interface.
|
||||
*/
|
||||
interface ResultDriver
|
||||
{
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
function getRowCount(): int;
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
* @throws Exception
|
||||
*/
|
||||
function seek(int $row): bool;
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $type true for associative array, false for numeric
|
||||
* @internal
|
||||
*/
|
||||
function fetch(bool $type): ?array;
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
function free(): void;
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
|
||||
*/
|
||||
function getResultColumns(): array;
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
*/
|
||||
function getResultResource(): mixed;
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
function unescapeBinary(string $value): string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reflection driver.
|
||||
*/
|
||||
interface Reflector
|
||||
{
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array of {name [, (bool) view ]}
|
||||
*/
|
||||
function getTables(): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
|
||||
*/
|
||||
function getColumns(string $table): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
* @return array of {name, (array of names) columns [, (bool) unique, (bool) primary ]}
|
||||
*/
|
||||
function getIndexes(string $table): array;
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
function getForeignKeys(string $table): array;
|
||||
}
|
@@ -36,7 +36,7 @@ test('config retrieval and driver instance access', function () use ($config) {
|
||||
|
||||
Assert::null($conn->getConfig('lazy'));
|
||||
Assert::same($config['driver'], $conn->getConfig('driver'));
|
||||
Assert::type(Dibi\Driver::class, $conn->getDriver());
|
||||
Assert::type(Dibi\Drivers\Connection::class, $conn->getDriver());
|
||||
});
|
||||
|
||||
|
||||
|
@@ -19,7 +19,7 @@ class MockDriver extends Dibi\Drivers\SqlsrvDriver
|
||||
}
|
||||
|
||||
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
public function query(string $sql): ?Dibi\Drivers\Result
|
||||
{
|
||||
return new MockResult;
|
||||
}
|
||||
|
Reference in New Issue
Block a user