Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Refactoring #9

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 58 additions & 17 deletions src/DsnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,52 @@

class DsnBuilder
{
public const PREFIX_MYSQL = "mysql";
public const PREFIX_POSTGRES = "pgsql";
public const PREFIX_CUBRID = "cubrid";
public const PREFIX_ORACLE = "oci";
public const PREFIX_SQLITE = "sqlite";
public const PREFIX_ODBC = "odbc";
public const PREFIX_IBM = "ibm";
public const PREFIX_MS_SQL = "sqlsrv";
public const PREFIX_MS_SQL_LIB = "sybase";
public const PREFIX_INFORMIX = "informix";
public const PREFIX_FIREBIRD = "firebird";
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_MYSQL = DsnPrefix::MYSQL;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_POSTGRES = DsnPrefix::POSTGRES;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_CUBRID = DsnPrefix::CUBRID;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_ORACLE = DsnPrefix::ORACLE;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_SQLITE = DsnPrefix::SQLITE;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_ODBC = DsnPrefix::ODBC;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_IBM = DsnPrefix::IBM;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_MS_SQL = DsnPrefix::MS_SQL;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_MS_SQL_LIB = DsnPrefix::MS_SQL_LIB;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_INFORMIX = DsnPrefix::INFORMIX;
/**
* @deprecated use {@link DsnPrefix} cases instead.
*/
public const PREFIX_FIREBIRD = DsnPrefix::FIREBIRD;

protected string $prefix = self::PREFIX_MYSQL;
protected DsnPrefix $prefix = DsnPrefix::MYSQL;
protected array $config = [];

public static function createMySQLDsn(
Expand All @@ -39,12 +72,16 @@ public static function createPostgresDsn(
): self {
$dsn = new self();
$dsn->config = compact('host', 'port', 'dbname', 'sslMode');
$dsn->prefix = self::PREFIX_POSTGRES;
$dsn->prefix = DsnPrefix::POSTGRES;
return $dsn;
}

public function setPrefix(string $prefix): self
public function setPrefix(string|DsnPrefix $prefix): self
{
if (is_string($prefix)) {
$prefix = DsnPrefix::from($prefix);
}

$this->prefix = $prefix;
return $this;
}
Expand All @@ -60,9 +97,13 @@ public function getDsnProp(string $prop): string|int|null
return $this->config[$prop] ?? null;
}

public static function buildDsn(string $prefix, array $props): string
public static function buildDsn(string|DsnPrefix $prefix, array $props): string
{
$dsn = "{$prefix}:";
if (is_string($prefix)) {
$prefix = DsnPrefix::from($prefix);
}

$dsn = "{$prefix->value}:";
foreach ($props as $name => $value) {
if ($value === null) {
continue;
Expand All @@ -76,7 +117,7 @@ public static function buildDsn(string $prefix, array $props): string

public function __toString(): string
{
return $this->buildDsn($this->prefix, $this->config);
return static::buildDsn($this->prefix->value, $this->config);
}

public static function new(): self
Expand Down
18 changes: 18 additions & 0 deletions src/DsnPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tnapf\Driver;

enum DsnPrefix: string
{
case MYSQL = "mysql";
case POSTGRES = "pgsql";
case CUBRID = "cubrid";
case ORACLE = "oci";
case SQLITE = "sqlite";
case ODBC = "odbc";
case IBM = "ibm";
case MS_SQL = "sqlsrv";
case MS_SQL_LIB = "sybase";
case INFORMIX = "informix";
case FIREBIRD = "firebird";
}
6 changes: 3 additions & 3 deletions src/PDODriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Tnapf\Driver\Interfaces\DriverInterface;
use Tnapf\Driver\Interfaces\PreparedQueryInterface;
use Tnapf\Driver\Interfaces\QueryInterface;
use Tnapf\Driver\PreparedQuery;
use Tnapf\Driver\Query;
use Tnapf\Driver\Interfaces\QueryResponseInterface;

class PDODriver implements DriverInterface
{
Expand Down Expand Up @@ -39,6 +36,9 @@ public function preparedQuery(string $query): PreparedQueryInterface
return new PreparedQuery($query, $this);
}

/**
* @throws DriverException
*/
public function connect(): void
{
if ($this->isConnected()) {
Expand Down
23 changes: 21 additions & 2 deletions src/PreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tnapf\Driver;

use Exception;
use PDOException;
use PDOStatement;
use Tnapf\Driver\Exceptions\QueryException;
use Tnapf\Driver\Interfaces\DriverInterface;
Expand All @@ -19,9 +21,16 @@ public function __construct(
$this->stmt = $this->driver->pdo->prepare($this->query);
}

/**
* @throws QueryException
*/
public function bindValue(string $name, mixed $value): void
{
$result = $this->stmt->bindValue($name, $value);
try {
$result = $this->stmt->bindValue($name, $value);
} catch (PDOException $e) {
throw new QueryException($this, $e->getMessage(), $e->getCode(), $e);
}

if ($result === false) {
[$sqlState, $errorCode, $errorMessage] = $this->stmt->errorInfo();
Expand All @@ -33,16 +42,26 @@ public function bindValue(string $name, mixed $value): void
}
}

/**
* @throws QueryException
*/
public function bindValues(array $values): void
{
foreach ($values as $name => $value) {
$this->bindValue($name, $value);
}
}

/**
* @throws QueryException
*/
public function execute(): QueryResponseInterface
{
$result = $this->stmt->execute();
try {
$result = $this->stmt->execute();
} catch (PDOException $e) {
throw new QueryException($this, $e->getMessage(), $e->getCode(), $e);
}

if ($result === false) {
[$sqlState, $errorCode, $errorMessage] = $this->stmt->errorInfo();
Expand Down
Loading