-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.php
67 lines (57 loc) · 1.7 KB
/
Driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* This file is part of Helix package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Helix\Database\PDO;
use Helix\Contracts\Database\QueryInterface;
use Helix\Database\Driver as BaseDriver;
use Helix\Database\Exception\DatabaseException;
use Helix\Database\PDO\Exception\DatabaseException as PDODatabaseException;
/**
* @psalm-type PDOAttr = \PDO::ATTR_*
*/
abstract class Driver extends BaseDriver
{
/**
* @param ConnectionInfo $info
*/
public function __construct(ConnectionInfo $info)
{
parent::__construct($info);
}
/**
* @param array<PDOAttr, scalar> $options
* @return array<PDOAttr, scalar>
*/
protected function getOptions(array $options = []): array
{
$default = \iterator_to_array($this->getDefaultOptions());
return \array_merge($options, $default);
}
/**
* @return iterable<PDOAttr, scalar>
*/
protected function getDefaultOptions(): iterable
{
yield \PDO::ATTR_CASE => \PDO::CASE_UPPER;
yield \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION;
yield \PDO::ATTR_EMULATE_PREPARES => false;
yield \PDO::ATTR_STRINGIFY_FETCHES => false;
}
/**
* @param \Throwable $e
* @param QueryInterface|null $query
* @return DatabaseException
*/
public function exception(\Throwable $e, QueryInterface $query = null): DatabaseException
{
if ($e instanceof \PDOException) {
return PDODatabaseException::fromPDO($e);
}
return new DatabaseException($e->getMessage(), (int)$e->getCode());
}
}