|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage\Driver\Pdo; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Storage\Driver\Connection as ConnectionInterface; |
|
6
|
|
|
use Igni\Storage\Exception\ConnectionException; |
|
7
|
|
|
use PDO; |
|
8
|
|
|
|
|
9
|
|
|
final class Connection implements ConnectionInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var PDO|null */ |
|
12
|
|
|
private $handler; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $dsn; |
|
16
|
|
|
|
|
17
|
|
|
/** @var ConnectionOptions */ |
|
18
|
|
|
private $options; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $queryLog = []; |
|
22
|
|
|
|
|
23
|
49 |
|
public function __construct(string $dsn, ConnectionOptions $options = null) |
|
24
|
|
|
{ |
|
25
|
49 |
|
$this->dsn = $dsn; |
|
26
|
49 |
|
$this->options = $options ?? new ConnectionOptions(); |
|
27
|
49 |
|
} |
|
28
|
|
|
|
|
29
|
48 |
|
public function close(): void |
|
30
|
|
|
{ |
|
31
|
48 |
|
$this->handler = null; |
|
32
|
48 |
|
} |
|
33
|
|
|
|
|
34
|
25 |
|
public function connect(): void |
|
35
|
|
|
{ |
|
36
|
25 |
|
if ($this->isConnected()) { |
|
37
|
22 |
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
25 |
|
$this->handler = new PDO( |
|
41
|
25 |
|
$this->dsn, |
|
42
|
25 |
|
$this->options->getUsername(), |
|
43
|
25 |
|
$this->options->getPassword(), |
|
44
|
25 |
|
$this->options->getAttributes() |
|
45
|
|
|
); |
|
46
|
25 |
|
} |
|
47
|
|
|
|
|
48
|
25 |
|
public function isConnected(): bool |
|
49
|
|
|
{ |
|
50
|
25 |
|
return $this->handler !== null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $query |
|
55
|
|
|
* @param array $parameters |
|
56
|
|
|
* @return Cursor |
|
57
|
|
|
* @throws ConnectionException |
|
58
|
|
|
*/ |
|
59
|
24 |
|
public function execute(...$parameters): Cursor |
|
60
|
|
|
{ |
|
61
|
24 |
|
if (!$this->isConnected()) { |
|
62
|
21 |
|
$this->connect(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
24 |
|
$query = $parameters[0]; |
|
66
|
|
|
|
|
67
|
24 |
|
if (isset($parameters[1]) && is_array($parameters[1])) { |
|
68
|
13 |
|
return new Cursor($this, $query, $parameters[1]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
return new Cursor($this, $query); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function beginTransaction(): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->handler->beginTransaction(); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function inTransaction(): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->handler->inTransaction(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function commit(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->handler->commit(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function rollBack(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->handler->rollBack(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function quote($string): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->handler->quote((string) $string); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
23 |
|
public function getBaseConnection(): PDO |
|
100
|
|
|
{ |
|
101
|
23 |
|
$this->connect(); |
|
102
|
23 |
|
return $this->handler; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function log(string $query) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->queryLog[] = $query; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.