1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MocOrm\Connection; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use phpDocumentor\Reflection\Types\Boolean; |
7
|
|
|
|
8
|
|
|
class Connection |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* This instance object |
12
|
|
|
* @var Connection. |
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
private static $_instance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is instance current connect object connected |
18
|
|
|
* @var PDO |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
private $_connection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @currentConnectionName string of the current connection name |
24
|
|
|
*/ |
25
|
|
|
private $_currentConnectionString; |
26
|
|
|
|
27
|
|
|
private $connectionString; |
28
|
|
|
private $driver; |
29
|
|
|
private $username; |
30
|
|
|
private $password; |
31
|
|
|
private $charset; |
32
|
|
|
private $schema; |
33
|
|
|
private $options; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This save all query orm use. |
|
|
|
|
37
|
|
|
* @var query string |
38
|
|
|
*/ |
39
|
|
|
private $performed_query = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initialize the object or return this object if have value set in attribute $_instance |
43
|
|
|
* @return Connection |
44
|
|
|
*/ |
45
|
|
|
public function __construct($config) |
46
|
|
|
{ |
47
|
|
|
$this->connectionString = $config['connectionString']; |
48
|
|
|
$this->driver = $config['driver']; |
49
|
|
|
$this->username = $config['username']; |
50
|
|
|
$this->password = $config['password']; |
51
|
|
|
$this->charset = $config['charset']; |
52
|
|
|
$this->schema = $config['schema']; |
53
|
|
|
|
54
|
|
|
self::$_instance = $this; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize the connection |
60
|
|
|
* @param string $connectionName name on connection |
61
|
|
|
* @return $this This object from other interator |
62
|
|
|
* @throws \Exception if the connect name haven't set; |
63
|
|
|
*/ |
64
|
|
|
public function setConnection() |
65
|
|
|
{ |
66
|
|
|
$this->_currentConnectionString = $this->connectionString; |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$this->_connection = new \PDO( |
|
|
|
|
70
|
|
|
$this->connectionString, |
71
|
|
|
$this->username, |
72
|
|
|
$this->password |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$charsetQuery = "set names '$this->charset'"; |
76
|
|
|
|
77
|
|
|
$this->_connection->query($charsetQuery); |
78
|
|
|
|
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
throw new \Exception($e->getMessage(), $e->getCode()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get name on current connection |
88
|
|
|
* @return string name on current connection or null if don't have |
89
|
|
|
*/ |
90
|
|
|
final public function getCurrentConnectionString() |
91
|
|
|
{ |
92
|
|
|
return $this->_currentConnectionString; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Initialize the connection |
97
|
|
|
* @return $this |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
public function connection($connectionString, $username, $password) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
if (is_null($this->getCurrentConnectionName())) throw new \Exception('Conexão não setada.'); |
|
|
|
|
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$connectionName = $this->getCurrentConnectionName(); |
106
|
|
|
|
107
|
|
|
$this->_connection = new \PDO( |
|
|
|
|
108
|
|
|
$this->_connectionString[$connectionName], |
|
|
|
|
109
|
|
|
$this->_username[$connectionName], |
|
|
|
|
110
|
|
|
$this->_password[$connectionName] |
|
|
|
|
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
} catch (\Exception $e) { |
114
|
|
|
throw new \Exception($e->getMessage(), $e->getCode()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getConnection() |
121
|
|
|
{ |
122
|
|
|
return $this->_connection; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return query |
127
|
|
|
*/ |
128
|
|
|
public function getPerformedQuery() |
129
|
|
|
{ |
130
|
|
|
return $this->performed_query; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return query |
135
|
|
|
*/ |
136
|
|
|
public function getLastPerformedQuery() |
137
|
|
|
{ |
138
|
|
|
return end($this->performed_query); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param String $query |
143
|
|
|
* @param String $time |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function setPerformedQuery(String $query, String $time) |
147
|
|
|
{ |
148
|
|
|
$this->performed_query[] = ['query' => $query, 'time' => $time]; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function getDriver() |
156
|
|
|
{ |
157
|
|
|
return $this->driver; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getSchema() |
164
|
|
|
{ |
165
|
|
|
return $this->schema; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Open transaction for insert, update, delete. |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
final public function beginTransaction() |
173
|
|
|
{ |
174
|
|
|
$this->_connection->beginTransaction(); |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Using commit to all actions executed after begin transaction |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
final public function commitTransaction() |
183
|
|
|
{ |
184
|
|
|
$this->_connection->commit(); |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Using rollback to all actions executed after begin transaction |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
|
|
final public function rollbackTransaction() |
193
|
|
|
{ |
194
|
|
|
$this->_connection->rollBack(); |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Change schema on postgres |
|
|
|
|
200
|
|
|
* @param $schema schema name |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
final public function changeSchema($schema = null) |
204
|
|
|
{ |
205
|
|
|
if (!is_string($schema)) throw new \InvalidArgumentException('The parameter don\'t is an String.'); |
206
|
|
|
if ($this->driver == 'mysql') throw new \InvalidArgumentException('This driver not supported schemas.'); |
207
|
|
|
|
208
|
|
|
$this->getConnection()->exec("SET search_path TO '$schema';"); |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param bool $logger |
214
|
|
|
* @return Connection |
215
|
|
|
*/ |
216
|
|
|
final public function setAppLogger($logger): Connection { |
217
|
|
|
$this->options['appLogger'] = $logger; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
final public function getAppLogger() { |
224
|
|
|
return $this->options['appLogger']; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|