Skip to content

Commit

Permalink
Merge bed5fb6 into 8fe0909
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed May 7, 2020
2 parents 8fe0909 + bed5fb6 commit 8222f15
Show file tree
Hide file tree
Showing 62 changed files with 533 additions and 216 deletions.
3 changes: 3 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ parameters:
-
message: '~Call to an undefined method .+::children\(\)\.~'
path: "src/Bridges/SymfonyBundle/DependencyInjection/Configuration.php"
-
message: '~.*~'
path: "src/compatibility.php"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"autoload": {
"psr-4": { "Nextras\\Dbal\\": "src/" },
"classmap": ["src/exceptions.php"]
"classmap": ["src/compatibility.php"]
},
"scripts": {
"phpstan": "phpstan analyze -c .phpstan.neon"
Expand Down
11 changes: 9 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@

namespace Nextras\Dbal;

use Exception;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\Utils\LoggerHelper;
use Nextras\Dbal\Utils\MultiLogger;
use Nextras\Dbal\Utils\StrictObjectTrait;
use function array_unshift;
use function assert;
use function call_user_func_array;
Expand All @@ -23,6 +27,9 @@

class Connection implements IConnection
{
use StrictObjectTrait;


/**
* @var array
* @phpstan-var array<string, mixed>
Expand Down Expand Up @@ -205,7 +212,7 @@ public function transactional(callable $callback)
$returnValue = $callback($this);
$this->commitTransaction();
return $returnValue;
} catch (\Exception $e) {
} catch (Exception $e) {
$this->rollbackTransaction();
throw $e;
}
Expand Down Expand Up @@ -348,7 +355,7 @@ private function nativeQuery(string $sql): Result
private function createDriver(): IDriver
{
if (empty($this->config['driver'])) {
throw new InvalidStateException('Undefined driver. Choose from: mysqli, pgsql, sqlsrv.');
throw new InvalidArgumentException('Undefined driver. Choose from: mysqli, pgsql, sqlsrv.');
} elseif ($this->config['driver'] instanceof IDriver) {
return $this->config['driver'];
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/Drivers/Exception/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;


class ConnectionException extends DriverException
{
}
8 changes: 8 additions & 0 deletions src/Drivers/Exception/ConstraintViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;


abstract class ConstraintViolationException extends QueryException
{
}
40 changes: 40 additions & 0 deletions src/Drivers/Exception/DriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;

use Exception;


class DriverException extends Exception
{
/** @var int */
private $errorCode;

/** @var string */
private $errorSqlState;


public function __construct(
string $message,
int $errorCode = 0,
string $errorSqlState = null,
Exception $previousException = null
)
{
parent::__construct($message, 0, $previousException);
$this->errorCode = $errorCode;
$this->errorSqlState = (string) $errorSqlState;
}


public function getErrorCode(): int
{
return $this->errorCode;
}


public function getErrorSqlState(): string
{
return $this->errorSqlState;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;


class ForeignKeyConstraintViolationException extends ConstraintViolationException
{
}
8 changes: 8 additions & 0 deletions src/Drivers/Exception/NotNullConstraintViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;


class NotNullConstraintViolationException extends ConstraintViolationException
{
}
31 changes: 31 additions & 0 deletions src/Drivers/Exception/QueryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;

use Exception;


class QueryException extends DriverException
{
/** @var string */
private $sqlQuery;


public function __construct(
string $message,
int $errorCode = 0,
string $errorSqlState = '',
Exception $previousException = null,
string $sqlQuery = null
)
{
parent::__construct($message, $errorCode, $errorSqlState, $previousException);
$this->sqlQuery = (string) $sqlQuery;
}


public function getSqlQuery(): string
{
return $this->sqlQuery;
}
}
8 changes: 8 additions & 0 deletions src/Drivers/Exception/UniqueConstraintViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Nextras\Dbal\Drivers\Exception;


class UniqueConstraintViolationException extends ConstraintViolationException
{
}
11 changes: 7 additions & 4 deletions src/Drivers/IDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace Nextras\Dbal\Drivers;

use DateInterval;
use DateTimeInterface;
use Nextras\Dbal\Connection;
use Nextras\Dbal\DriverException;
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -60,6 +62,7 @@ public function getResourceHandle();
/**
* Runs query and returns a result. Returns a null if the query does not select any data.
* @internal
* @throws DriverException
*/
public function query(string $query): Result;

Expand Down Expand Up @@ -188,13 +191,13 @@ public function convertBoolToSql(bool $value): string;
public function convertIdentifierToSql(string $value): string;


public function convertDateTimeToSql(\DateTimeInterface $value): string;
public function convertDateTimeToSql(DateTimeInterface $value): string;


public function convertDateTimeSimpleToSql(\DateTimeInterface $value): string;
public function convertDateTimeSimpleToSql(DateTimeInterface $value): string;


public function convertDateIntervalToSql(\DateInterval $value): string;
public function convertDateIntervalToSql(DateInterval $value): string;


public function convertBlobToSql(string $value): string;
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/IResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Nextras\Dbal\Drivers;

use Nextras\Dbal\InvalidStateException;
use Nextras\Dbal\Exception\InvalidArgumentException;


interface IResultAdapter
Expand All @@ -24,7 +24,7 @@ interface IResultAdapter


/**
* @throws InvalidStateException
* @throws InvalidArgumentException
*/
public function seek(int $index): void;

Expand Down
47 changes: 24 additions & 23 deletions src/Drivers/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,36 @@
namespace Nextras\Dbal\Drivers\Mysqli;

use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use mysqli;
use Nextras\Dbal\Connection;
use Nextras\Dbal\ConnectionException;
use Nextras\Dbal\DriverException;
use Nextras\Dbal\Drivers\Exception\ConnectionException;
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\Drivers\Exception\ForeignKeyConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\NotNullConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\QueryException;
use Nextras\Dbal\Drivers\Exception\UniqueConstraintViolationException;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\ForeignKeyConstraintViolationException;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\InvalidArgumentException;
use Nextras\Dbal\NotNullConstraintViolationException;
use Nextras\Dbal\NotSupportedException;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\QueryException;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\UniqueConstraintViolationException;
use Nextras\Dbal\Utils\LoggerHelper;
use Nextras\Dbal\Utils\StrictObjectTrait;
use function assert;


class MysqliDriver implements IDriver
{
use StrictObjectTrait;


/** @var mysqli|null */
private $connection;

Expand Down Expand Up @@ -224,7 +233,7 @@ public function rollbackSavepoint(string $name): void
*/
protected function setupSsl(array $params): void
{
\assert($this->connection !== null);
assert($this->connection !== null);

if (
!isset($params['sslKey']) &&
Expand Down Expand Up @@ -285,20 +294,17 @@ public function convertToPhp(string $value, $nativeType)
{
if ($nativeType === MYSQLI_TYPE_TIMESTAMP) {
return $value . ' ' . $this->connectionTz->getName();

} elseif ($nativeType === MYSQLI_TYPE_LONGLONG) {
// called only on 32bit
// hack for phpstan
/** @var int|float $numeric */
$numeric = $value;
return is_float($tmp = $numeric * 1) ? $numeric : $tmp;

} elseif ($nativeType === MYSQLI_TYPE_TIME) {
preg_match('#^(-?)(\d+):(\d+):(\d+)#', $value, $m);
$value = new DateInterval("PT{$m[2]}H{$m[3]}M{$m[4]}S");
$value->invert = $m[1] ? 1 : 0;
return $value;

} else {
throw new NotSupportedException("MysqliDriver does not support '{$nativeType}' type conversion.");
}
Expand Down Expand Up @@ -342,11 +348,11 @@ public function convertIdentifierToSql(string $value): string
}


public function convertDatetimeToSql(\DateTimeInterface $value): string
public function convertDatetimeToSql(DateTimeInterface $value): string
{
assert($value instanceof \DateTime || $value instanceof \DateTimeImmutable);
assert($value instanceof DateTime || $value instanceof DateTimeImmutable);
if ($value->getTimezone()->getName() !== $this->connectionTz->getName()) {
if ($value instanceof \DateTimeImmutable) {
if ($value instanceof DateTimeImmutable) {
$value = $value->setTimezone($this->connectionTz);
} else {
$value = clone $value;
Expand All @@ -357,13 +363,13 @@ public function convertDatetimeToSql(\DateTimeInterface $value): string
}


public function convertDatetimeSimpleToSql(\DateTimeInterface $value): string
public function convertDatetimeSimpleToSql(DateTimeInterface $value): string
{
return "'" . $value->format('Y-m-d H:i:s.u') . "'";
}


public function convertDateIntervalToSql(\DateInterval $value): string
public function convertDateIntervalToSql(DateInterval $value): string
{
$totalHours = ((int) $value->format('%a')) * 24 + $value->h;
if ($totalHours >= 839) {
Expand Down Expand Up @@ -401,23 +407,18 @@ public function modifyLimitQuery(string $query, ?int $limit, ?int $offset): stri
* This method is based on Doctrine\DBAL project.
* @link www.doctrine-project.org
*/
protected function createException(string $error, int $errorNo, string $sqlState, ?string $query = null): \Exception
protected function createException(string $error, int $errorNo, string $sqlState, ?string $query = null): Exception
{
if (in_array($errorNo, [1216, 1217, 1451, 1452, 1701], true)) {
return new ForeignKeyConstraintViolationException($error, $errorNo, $sqlState, null, $query);

} elseif (in_array($errorNo, [1062, 1557, 1569, 1586], true)) {
return new UniqueConstraintViolationException($error, $errorNo, $sqlState, null, $query);

} elseif (in_array($errorNo, [1044, 1045, 1046, 1049, 1095, 1142, 1143, 1227, 1370, 2002, 2005, 2054], true)) {
return new ConnectionException($error, $errorNo, $sqlState);

} elseif (in_array($errorNo, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) {
return new NotNullConstraintViolationException($error, $errorNo, $sqlState, null, $query);

} elseif ($query !== null) {
return new QueryException($error, $errorNo, $sqlState, null, $query);

} else {
return new DriverException($error, $errorNo, $sqlState);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Drivers/Mysqli/MysqliEmptyResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
namespace Nextras\Dbal\Drivers\Mysqli;

use Nextras\Dbal\Drivers\IResultAdapter;
use Nextras\Dbal\InvalidStateException;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Utils\StrictObjectTrait;


class MysqliEmptyResultAdapter implements IResultAdapter
{
use StrictObjectTrait;


public function seek(int $index): void
{
throw new InvalidStateException("Unable to seek in row set to {$index} index.");
throw new InvalidArgumentException("Unable to seek in row set to {$index} index.");
}


Expand Down
Loading

0 comments on commit 8222f15

Please sign in to comment.