Skip to content

Commit

Permalink
drivers WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 3, 2024
1 parent b5718be commit 7a0416e
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 91 deletions.
15 changes: 13 additions & 2 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\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;
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
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,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;


Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -201,19 +200,19 @@ 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);
}


/**
* 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);
}


Expand Down
3 changes: 1 addition & 2 deletions src/Dibi/Drivers/Firebird/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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


Expand Down Expand Up @@ -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]);

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,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;


/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +27,7 @@
* - resource (PDO) => existing connection
* - version
*/
class PdoDriver implements Connection
class Connection implements Drivers\Connection
{
private ?PDO $connection;
private ?int $affectedRows;
Expand Down Expand Up @@ -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),
};
}
Expand Down Expand Up @@ -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,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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'));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
Loading

0 comments on commit 7a0416e

Please sign in to comment.