Skip to content

Commit

Permalink
renamed driver classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 3, 2024
1 parent 9f46a29 commit 9bde005
Show file tree
Hide file tree
Showing 30 changed files with 147 additions and 137 deletions.
17 changes: 14 additions & 3 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
*/
class Connection
{
private const Drivers = [
'firebird' => Drivers\Ibase\Connection::class,
'mysqli' => Drivers\MySQLi\Connection::class,
'odbc' => Drivers\ODBC\Connection::class,
'oracle' => Drivers\OCI8\Connection::class,
'pdo' => Drivers\PDO\Connection::class,
'postgre' => Drivers\PgSQL\Connection::class,
'sqlite3' => Drivers\SQLite3\Connection::class,
'sqlite' => Drivers\SQLite3\Connection::class,
'sqlsrv' => Drivers\SQLSrv\Connection::class,
];

/** function (Event $event); Occurs after query is executed */
public ?array $onEvent = [];
private array $config;
Expand Down Expand Up @@ -129,8 +141,7 @@ final public function connect(): void
$class = $this->config['driver'];

} else {
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($this->config['driver'])));
$class = "Dibi\\Drivers\\{$class}Driver";
$class = self::Drivers[strtolower($this->config['driver'])] ?? throw new Exception("Unknown driver '{$this->config['driver']}'.");
if (!class_exists($class)) {
throw new Exception("Unable to create instance of Dibi driver '$class'.");
}
Expand Down Expand Up @@ -284,7 +295,7 @@ final public function nativeQuery(#[Language('SQL')] string $sql): Result
throw $e;
}

$res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows())));
$res = $this->createResultSet($res ?: new Drivers\Dummy\Result(max(0, $this->driver->getAffectedRows())));
if ($event) {
$this->onEvent($event->done($res));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi;
use Dibi\Drivers;


/**
* The dummy driver for testing purposes.
*/
class DummyDriver implements Connection, Result, Engine
class Connection implements Drivers\Connection, Drivers\Result, Drivers\Engine
{
public function disconnect(): void
{
}


public function query(string $sql): ?Result
public function query(string $sql): ?Drivers\Result
{
return null;
}
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Engine
public function getReflector(): Drivers\Engine
{
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi\Drivers;


/**
* The driver for no result set.
*/
class NoDataResult implements Result
class Result implements Drivers\Result
{
private int $rows;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for Firebird/InterBase database.
*/
class FirebirdReflector implements Engine
class FirebirdEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi;
use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for MySQL databases.
* @internal
*/
class MySqlReflector implements Engine
class MySQLEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi;
use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for ODBC connections.
*/
class OdbcReflector implements Engine
class ODBCEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi;
use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for Oracle database.
*/
class OracleReflector implements Engine
class OracleEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for PostgreSQL database.
*/
class PostgreReflector implements Engine
class PostgreSQLEngine implements Engine
{
private Connection $driver;
private string $version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi;
use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for Microsoft SQL Server and SQL Azure databases.
*/
class SqlsrvReflector implements Engine
class SQLServerEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Engines;

use Dibi\Drivers\Connection;
use Dibi\Drivers\Engine;


/**
* The reflector for SQLite database.
*/
class SqliteReflector implements Engine
class SQLiteEngine implements Engine
{
private Connection $driver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Ibase;

use Dibi;
use Dibi\Drivers;
use Dibi\Helpers;


Expand All @@ -24,7 +25,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 Connection
class Connection implements Drivers\Connection
{
public const ErrorExceptionThrown = -836;

Expand Down Expand Up @@ -85,7 +86,7 @@ public function disconnect(): void
* Executes the SQL query.
* @throws Dibi\DriverException|Dibi\Exception
*/
public function query(string $sql): ?Result
public function query(string $sql): ?Drivers\Result
{
$resource = $this->inTransaction
? $this->transaction
Expand Down Expand Up @@ -199,19 +200,19 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Engine
public function getReflector(): Drivers\Engine
{
return new FirebirdReflector($this);
return new Drivers\Engines\FirebirdEngine($this);
}


/**
* Result set driver factory.
* @param resource $resource
*/
public function createResultDriver($resource): FirebirdResult
public function createResultDriver($resource): Result
{
return new FirebirdResult($resource);
return new Result($resource);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Ibase;

use Dibi;
use Dibi\Drivers;
use Dibi\Helpers;


/**
* The driver for Firebird/InterBase result set.
*/
class FirebirdResult implements Result
class Result implements Drivers\Result
{
/** @var resource */
private $resultSet;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function fetch(bool $assoc): ?array
: @ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @

if (ibase_errcode()) {
if (ibase_errcode() === FirebirdDriver::ERROR_EXCEPTION_THROWN) {
if (ibase_errcode() === Connection::ERROR_EXCEPTION_THROWN) {
preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match);
throw new Dibi\ProcedureException($match[3], $match[1], $match[2]);

Check failure on line 57 in src/Dibi/Drivers/Firebird/Result.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #2 $code of class Dibi\ProcedureException constructor expects int, string given.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\MySQLi;

use Dibi;
use Dibi\Drivers;


/**
Expand All @@ -30,7 +31,7 @@
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
* - resource (mysqli) => existing connection resource
*/
class MySqliDriver implements Connection
class Connection implements Drivers\Connection
{
public const ErrorAccessDenied = 1045;
public const ErrorDuplicateEntry = 1062;
Expand Down Expand Up @@ -263,18 +264,18 @@ public function getResource(): ?\mysqli
/**
* Returns the connection reflector.
*/
public function getReflector(): Engine
public function getReflector(): Drivers\Engine
{
return new MySqlReflector($this);
return new Drivers\Engines\MySQLEngine($this);
}


/**
* Result set driver factory.
*/
public function createResultDriver(\mysqli_result $result): MySqliResult
public function createResultDriver(\mysqli_result $result): Result
{
return new MySqliResult($result, $this->buffered);
return new Result($result, $this->buffered);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\MySQLi;

use Dibi;
use Dibi\Drivers;


/**
* The driver for MySQL result set.
*/
class MySqliResult implements Result
class Result implements Drivers\Result
{
private \mysqli_result $resultSet;
private bool $buffered;
Expand Down
Loading

0 comments on commit 9bde005

Please sign in to comment.