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