Skip to content

Commit

Permalink
renamed interfaces (BC break)
Browse files Browse the repository at this point in the history
Dibi\Driver => Dibi\Drivers\Connection
Dibi\ResultDriver => Dibi\Drivers\Result
Dibi\Reflector => Dibi\Drivers\Engine
  • Loading branch information
dg committed Sep 3, 2024
1 parent 833477d commit 114122c
Show file tree
Hide file tree
Showing 40 changed files with 284 additions and 254 deletions.
10 changes: 5 additions & 5 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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> */
Expand Down Expand Up @@ -120,12 +120,12 @@ public function __destruct()
*/
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 {
Expand Down Expand Up @@ -196,7 +196,7 @@ final public function getConfig(?string $key = null, $default = null): mixed
/**
* 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();
Expand Down Expand Up @@ -448,7 +448,7 @@ public function transaction(callable $callback): mixed
/**
* 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);
Expand Down
97 changes: 97 additions & 0 deletions src/Dibi/Drivers/Connection.php
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions src/Dibi/Drivers/DummyDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* 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;
}
Expand Down Expand Up @@ -64,7 +64,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Engine
{
return $this;
}
Expand Down
40 changes: 40 additions & 0 deletions src/Dibi/Drivers/Engine.php
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* - 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;

Expand Down Expand Up @@ -85,7 +85,7 @@ public function disconnect(): void
* 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
Expand Down Expand Up @@ -199,7 +199,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Engine
{
return new FirebirdReflector($this);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Dibi/Drivers/FirebirdReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@

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,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/FirebirdResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* The driver for Firebird/InterBase result set.
*/
class FirebirdResult implements Dibi\ResultDriver
class FirebirdResult implements Result
{
public function __construct(
/** @var resource */
Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/Drivers/MySqlReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
* 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,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions src/Dibi/Drivers/MySqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* - 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;
Expand Down Expand Up @@ -146,7 +146,7 @@ public function ping(): bool
* 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 @

Expand Down Expand Up @@ -263,7 +263,7 @@ public function getResource(): ?\mysqli
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Engine
{
return new MySqlReflector($this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/MySqliResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* The driver for MySQL result set.
*/
class MySqliResult implements Dibi\ResultDriver
class MySqliResult implements Result
{
public function __construct(
private readonly \mysqli_result $resultSet,
Expand Down
3 changes: 1 addition & 2 deletions src/Dibi/Drivers/NoDataResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

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,
Expand Down
6 changes: 3 additions & 3 deletions src/Dibi/Drivers/OdbcDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* - 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;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function disconnect(): void
* 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 @
Expand Down Expand Up @@ -175,7 +175,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Engine
{
return new OdbcReflector($this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/Drivers/OdbcReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
/**
* 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,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OdbcResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* The driver interacting with result set via ODBC connections.
*/
class OdbcResult implements Dibi\ResultDriver
class OdbcResult implements Result
{
private int $row = 0;

Expand Down
Loading

0 comments on commit 114122c

Please sign in to comment.