1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Phalcon Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Implementation of this file has been influenced by AtlasPHP |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/atlasphp/Atlas.Pdo |
14
|
|
|
* @license https://github.com/atlasphp/Atlas.Pdo/blob/1.x/LICENSE.md |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phalcon\DataMapper\Pdo; |
20
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use PDO; |
23
|
|
|
use Phalcon\DataMapper\Pdo\Connection\AbstractConnection; |
24
|
|
|
use Phalcon\DataMapper\Pdo\Profiler\Profiler; |
25
|
|
|
use Phalcon\DataMapper\Pdo\Profiler\ProfilerInterface; |
26
|
|
|
|
27
|
|
|
use function explode; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Provides array quoting, profiling, a new `perform()` method, new `fetch*()` |
31
|
|
|
* methods |
32
|
|
|
* |
33
|
|
|
* @property array $arguments |
34
|
|
|
* @property PDO $pdo |
35
|
|
|
* @property ProfilerInterface $profiler |
36
|
|
|
*/ |
37
|
|
|
class Connection extends AbstractConnection |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $arguments = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* This overrides the parent so that it can take connection attributes as a |
48
|
|
|
* constructor parameter, and set them after connection. |
49
|
|
|
* |
50
|
|
|
* @param string $dsn |
51
|
|
|
* @param string $username |
52
|
|
|
* @param string $password |
53
|
|
|
* @param array $options |
54
|
|
|
* @param array $queries |
55
|
|
|
* @param ProfilerInterface $profiler |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
string $dsn, |
59
|
|
|
string $username = null, |
60
|
|
|
string $password = null, |
61
|
|
|
array $options = [], |
62
|
|
|
array $queries = [], |
63
|
|
|
ProfilerInterface $profiler = null |
64
|
|
|
) { |
65
|
|
|
$parts = explode(':', $dsn); |
66
|
|
|
$available = [ |
67
|
|
|
"mysql" => true, |
68
|
|
|
"pgsql" => true, |
69
|
|
|
"sqlite" => true, |
70
|
|
|
"mssql" => true, |
71
|
|
|
]; |
72
|
|
|
if (!isset($available[$parts[0]])) { |
73
|
|
|
throw new InvalidArgumentException( |
74
|
|
|
"Driver not supported [" . $parts[0] . "]" |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
// if no error mode is specified, use exceptions |
80
|
|
|
if (!isset($options[PDO::ATTR_ERRMODE])) { |
81
|
|
|
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Arguments store |
85
|
|
|
$this->arguments = [ |
86
|
|
|
$dsn, |
87
|
|
|
$username, |
88
|
|
|
$password, |
89
|
|
|
$options, |
90
|
|
|
$queries, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
// Create a new profiler if none has been passed |
94
|
|
|
if ($profiler === null) { |
95
|
|
|
$profiler = new Profiler(); |
96
|
|
|
} |
97
|
|
|
$this->setProfiler($profiler); |
98
|
|
|
|
99
|
|
|
// Quotes |
100
|
|
|
$this->quote = $this->getQuoteNames($parts[0]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* The purpose of this method is to hide sensitive data from stack traces. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function __debugInfo() |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
'arguments' => [ |
112
|
|
|
$this->arguments[0], |
113
|
|
|
'****', |
114
|
|
|
'****', |
115
|
|
|
$this->arguments[3], |
116
|
|
|
$this->arguments[4], |
117
|
|
|
], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Connects to the database. |
123
|
|
|
*/ |
124
|
|
|
public function connect(): void |
125
|
|
|
{ |
126
|
|
|
if (!$this->pdo) { |
127
|
|
|
// connect |
128
|
|
|
$this->profiler->start(__FUNCTION__); |
129
|
|
|
[$dsn, $username, $password, $options, $queries] = $this->arguments; |
130
|
|
|
$this->pdo = new PDO($dsn, $username, $password, $options); |
131
|
|
|
$this->profiler->finish(); |
132
|
|
|
|
133
|
|
|
foreach ($queries as $query) { |
134
|
|
|
$this->exec($query); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Disconnects from the database. |
141
|
|
|
*/ |
142
|
|
|
public function disconnect(): void |
143
|
|
|
{ |
144
|
|
|
$this->profiler->start(__FUNCTION__); |
145
|
|
|
$this->pdo = null; |
146
|
|
|
$this->profiler->finish(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|