diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 3b4c2915b..db04451b8 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -21,6 +21,18 @@ */ class Connection { + private const Drivers = [ + 'firebird' => Drivers\Firebird\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; @@ -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'."); } diff --git a/src/Dibi/Drivers/DummyDriver.php b/src/Dibi/Drivers/Dummy/Connection.php similarity index 93% rename from src/Dibi/Drivers/DummyDriver.php rename to src/Dibi/Drivers/Dummy/Connection.php index 74501d3ad..0ede9d2ef 100644 --- a/src/Dibi/Drivers/DummyDriver.php +++ b/src/Dibi/Drivers/Dummy/Connection.php @@ -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; } @@ -64,7 +65,7 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { return $this; } diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/Firebird/Connection.php similarity index 94% rename from src/Dibi/Drivers/FirebirdDriver.php rename to src/Dibi/Drivers/Firebird/Connection.php index 96e5f1515..0604458e5 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/Firebird/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\Firebird; use Dibi; -use Dibi\Drivers\Engines\FirebirdEngine; -use Dibi\Drivers\Firebird\Firebird; +use Dibi\Drivers; use Dibi\Helpers; @@ -26,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; @@ -87,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 @@ -201,9 +200,9 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new FirebirdEngine($this); + return new Drivers\Engines\FirebirdEngine($this); } @@ -211,9 +210,9 @@ public function getReflector(): Engine * Result set driver factory. * @param resource $resource */ - public function createResultDriver($resource): Firebird + public function createResultDriver($resource): Result { - return new Firebird($resource); + return new Result($resource); } diff --git a/src/Dibi/Drivers/Firebird/Result.php b/src/Dibi/Drivers/Firebird/Result.php index b1450467b..f971fc2ce 100644 --- a/src/Dibi/Drivers/Firebird/Result.php +++ b/src/Dibi/Drivers/Firebird/Result.php @@ -11,7 +11,6 @@ use Dibi; use Dibi\Drivers; -use Dibi\Drivers\FirebirdDriver; use Dibi\Helpers; @@ -53,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]); diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySQLi/Connection.php similarity index 97% rename from src/Dibi/Drivers/MySqliDriver.php rename to src/Dibi/Drivers/MySQLi/Connection.php index e2d7a14b0..35afac930 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySQLi/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\MySQLi; use Dibi; -use Dibi\Drivers\Engines\MySQLEngine; -use Dibi\Drivers\MySQLi\Result; +use Dibi\Drivers; /** @@ -32,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; @@ -265,9 +264,9 @@ public function getResource(): ?\mysqli /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new MySQLEngine($this); + return new Drivers\Engines\MySQLEngine($this); } diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OCI8/Connection.php similarity index 97% rename from src/Dibi/Drivers/OracleDriver.php rename to src/Dibi/Drivers/OCI8/Connection.php index ea013e92d..3422d4ac8 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OCI8/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\OCI8; use Dibi; -use Dibi\Drivers\Engines\OracleEngine; -use Dibi\Drivers\OCI8\Result; +use Dibi\Drivers; /** @@ -27,7 +26,7 @@ * - resource (resource) => existing connection resource * - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect */ -class OracleDriver implements Connection +class Connection implements Drivers\Connection { /** @var resource */ private $connection; @@ -191,9 +190,9 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new OracleEngine($this); + return new Drivers\Engines\OracleEngine($this); } diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/ODBC/Connection.php similarity index 96% rename from src/Dibi/Drivers/OdbcDriver.php rename to src/Dibi/Drivers/ODBC/Connection.php index a51a3e6b9..65e3c5acd 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/ODBC/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\ODBC; use Dibi; -use Dibi\Drivers\Engines\ODBCEngine; -use Dibi\Drivers\ODBC\Result; +use Dibi\Drivers; /** @@ -25,7 +24,7 @@ * - resource (resource) => existing connection resource * - microseconds (bool) => use microseconds in datetime format? */ -class OdbcDriver implements Connection +class Connection implements Drivers\Connection { /** @var resource */ private $connection; @@ -177,9 +176,9 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new ODBCEngine($this); + return new Drivers\Engines\ODBCEngine($this); } diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PDO/Connection.php similarity index 91% rename from src/Dibi/Drivers/PdoDriver.php rename to src/Dibi/Drivers/PDO/Connection.php index f9e158f92..55a790978 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PDO/Connection.php @@ -7,15 +7,11 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\PDO; use Dibi; -use Dibi\Drivers\Engines\MySQLEngine; -use Dibi\Drivers\Engines\OracleEngine; -use Dibi\Drivers\Engines\PostgreSQLEngine; -use Dibi\Drivers\Engines\SQLiteEngine; -use Dibi\Drivers\Engines\SQLServerEngine; -use Dibi\Drivers\PDO\Result; +use Dibi\Drivers; +use Dibi\Drivers\Engines; use Dibi\Helpers; use PDO; @@ -31,7 +27,7 @@ * - resource (PDO) => existing connection * - version */ -class PdoDriver implements Connection +class Connection implements Drivers\Connection { private ?PDO $connection; private ?int $affectedRows; @@ -102,10 +98,10 @@ public function query(string $sql): ?Result $code ??= 0; $message = "SQLSTATE[$sqlState]: $message"; throw match ($this->driverName) { - 'mysql' => MySqliDriver::createException($message, $code, $sql), - 'oci' => OracleDriver::createException($message, $code, $sql), - 'pgsql' => PostgreDriver::createException($message, $sqlState, $sql), - 'sqlite' => SqliteDriver::createException($message, $code, $sql), + 'mysql' => Drivers\MySQLi\Connection::createException($message, $code, $sql), + 'oci' => Drivers\OCI8\Connection::createException($message, $code, $sql), + 'pgsql' => Drivers\PgSQL\Connection::createException($message, $sqlState, $sql), + 'sqlite' => Drivers\SQLite3\Connection::createException($message, $code, $sql), default => new Dibi\DriverException($message, $code, $sql), }; } @@ -180,14 +176,14 @@ public function getResource(): ?PDO /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { return match ($this->driverName) { - 'mysql' => new MySQLEngine($this), - 'oci' => new OracleEngine($this), - 'pgsql' => new PostgreSQLEngine($this, $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION)), - 'sqlite' => new SQLiteEngine($this), - 'mssql', 'dblib', 'sqlsrv' => new SQLServerEngine($this), + 'mysql' => new Engines\MySQLEngine($this), + 'oci' => new Engines\OracleEngine($this), + 'pgsql' => new Engines\PostgreSQLEngine($this, $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION)), + 'sqlite' => new Engines\SQLiteEngine($this), + 'mssql', 'dblib', 'sqlsrv' => new Engines\SQLServerEngine($this), default => throw new Dibi\NotSupportedException, }; } diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PgSQL/Connection.php similarity index 96% rename from src/Dibi/Drivers/PostgreDriver.php rename to src/Dibi/Drivers/PgSQL/Connection.php index 2081ee8e5..f6ec6bbfc 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PgSQL/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\PgSQL; use Dibi; -use Dibi\Drivers\Engines\PostgreSQLEngine; -use Dibi\Drivers\PgSQL\Result; +use Dibi\Drivers; use Dibi\Helpers; use PgSql; @@ -28,7 +27,7 @@ * - resource (resource) => existing connection resource * - connect_type (int) => see pg_connect() */ -class PostgreDriver implements Connection +class Connection implements Drivers\Connection { /** @var resource|PgSql\Connection */ private $connection; @@ -237,9 +236,9 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new PostgreSQLEngine($this, pg_parameter_status($this->connection, 'server_version')); + return new Drivers\Engines\PostgreSQLEngine($this, pg_parameter_status($this->connection, 'server_version')); } diff --git a/src/Dibi/Drivers/SqlsrvDriver.php b/src/Dibi/Drivers/SQLSrv/Connection.php similarity index 96% rename from src/Dibi/Drivers/SqlsrvDriver.php rename to src/Dibi/Drivers/SQLSrv/Connection.php index 3fad8d1b8..1bbb6e534 100644 --- a/src/Dibi/Drivers/SqlsrvDriver.php +++ b/src/Dibi/Drivers/SQLSrv/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\SQLSrv; use Dibi; -use Dibi\Drivers\Engines\SQLServerEngine; -use Dibi\Drivers\SQLSrv\Result; +use Dibi\Drivers; use Dibi\Helpers; @@ -27,7 +26,7 @@ * - charset => character encoding to set (default is UTF-8) * - resource (resource) => existing connection resource */ -class SqlsrvDriver implements Connection +class Connection implements Drivers\Connection { /** @var resource */ private $connection; @@ -175,9 +174,9 @@ public function getResource(): mixed /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new SQLServerEngine($this); + return new Drivers\Engines\SQLServerEngine($this); } diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SQLite3/Connection.php similarity index 96% rename from src/Dibi/Drivers/SqliteDriver.php rename to src/Dibi/Drivers/SQLite3/Connection.php index 51779ab04..617564f50 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SQLite3/Connection.php @@ -7,11 +7,10 @@ declare(strict_types=1); -namespace Dibi\Drivers; +namespace Dibi\Drivers\SQLite3; use Dibi; -use Dibi\Drivers\Engines\SQLiteEngine; -use Dibi\Drivers\SQLite3\Result; +use Dibi\Drivers; use Dibi\Helpers; use SQLite3; @@ -25,7 +24,7 @@ * - formatDateTime => how to format datetime in SQL (@see date) * - resource (SQLite3) => existing connection resource */ -class SqliteDriver implements Connection +class Connection implements Drivers\Connection { private SQLite3 $connection; private string $fmtDate; @@ -179,9 +178,9 @@ public function getResource(): ?SQLite3 /** * Returns the connection reflector. */ - public function getReflector(): Engine + public function getReflector(): Drivers\Engine { - return new SQLiteEngine($this); + return new Drivers\Engines\SQLiteEngine($this); } diff --git a/src/Dibi/Drivers/Sqlite3Driver.php b/src/Dibi/Drivers/Sqlite3Driver.php deleted file mode 100644 index f3dfa9ede..000000000 --- a/src/Dibi/Drivers/Sqlite3Driver.php +++ /dev/null @@ -1,18 +0,0 @@ -setAttribute(PDO::ATTR_ERRMODE, $errorMode); } - new Dibi\Drivers\PdoDriver(['resource' => $pdo]); + new Dibi\Drivers\PDO\Connection(['resource' => $pdo]); }