1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Db\StatementInterface; |
7
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
8
|
|
|
|
9
|
|
|
trait ConnectionTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ConnectionInterface |
13
|
|
|
*/ |
14
|
|
|
protected $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ConnectionInterface |
18
|
|
|
*/ |
19
|
|
|
protected $mainConnection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set driver config |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
abstract protected function beforeConnection(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set driver config |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
abstract protected function afterConnection(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a connection to the server, based on the config and available packages |
37
|
|
|
* |
38
|
|
|
* @return ConnectionInterface|null |
39
|
|
|
*/ |
40
|
|
|
abstract protected function createConnection(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ConnectionInterface $connection |
44
|
|
|
* |
45
|
|
|
* @return Driver |
46
|
|
|
*/ |
47
|
|
|
public function useConnection(ConnectionInterface $connection) |
48
|
|
|
{ |
49
|
|
|
$this->connection = $connection; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Driver |
55
|
|
|
*/ |
56
|
|
|
public function useMainConnection() |
57
|
|
|
{ |
58
|
|
|
$this->connection = $this->mainConnection; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the server description |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function serverInfo() |
68
|
|
|
{ |
69
|
|
|
return $this->connection->serverInfo(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the driver extension |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function extension() |
78
|
|
|
{ |
79
|
|
|
return $this->connection->extension(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the client character set |
84
|
|
|
* |
85
|
|
|
* @param string $charset |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function setCharset(string $charset) |
90
|
|
|
{ |
91
|
|
|
$this->connection->setCharset($charset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return a quoted string |
96
|
|
|
* |
97
|
|
|
* @param string $string |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function quote(string $string) |
102
|
|
|
{ |
103
|
|
|
return $this->connection->quote($string); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return a quoted string |
108
|
|
|
* |
109
|
|
|
* @param string $string |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function quoteBinary(string $string) |
114
|
|
|
{ |
115
|
|
|
return $this->connection->quoteBinary($string); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Execute a query on the current database |
120
|
|
|
* |
121
|
|
|
* @param string $query |
122
|
|
|
* @param bool $unbuffered |
123
|
|
|
* |
124
|
|
|
* @return StatementInterface|bool |
125
|
|
|
*/ |
126
|
|
|
public function query(string $query, bool $unbuffered = false) |
127
|
|
|
{ |
128
|
|
|
return $this->connection->query($query, $unbuffered); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the number of rows affected by the last query |
133
|
|
|
* |
134
|
|
|
* @return integer |
135
|
|
|
*/ |
136
|
|
|
public function affectedRows() |
137
|
|
|
{ |
138
|
|
|
return $this->connection->affectedRows(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Execute a query on the current database and fetch the specified field |
143
|
|
|
* |
144
|
|
|
* @param string $query |
145
|
|
|
* @param int $field |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public function result(string $query, int $field = -1) |
150
|
|
|
{ |
151
|
|
|
return $this->connection->result($query, $field); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Execute a query on the current database and store the result |
156
|
|
|
* |
157
|
|
|
* @param string $query |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function multiQuery(string $query) |
162
|
|
|
{ |
163
|
|
|
return $this->connection->multiQuery($query); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the result saved by the multiQuery() method |
168
|
|
|
* |
169
|
|
|
* @return StatementInterface|bool |
170
|
|
|
*/ |
171
|
|
|
public function storedResult() |
172
|
|
|
{ |
173
|
|
|
return $this->connection->storedResult(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the next row set of the last query |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function nextResult() |
182
|
|
|
{ |
183
|
|
|
return $this->connection->nextResult(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Convert value returned by database to actual value |
188
|
|
|
* |
189
|
|
|
* @param string|resource|null $value |
190
|
|
|
* @param TableFieldEntity $field |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function value($value, TableFieldEntity $field) |
195
|
|
|
{ |
196
|
|
|
return $this->connection->value($value, $field); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritDoc |
201
|
|
|
*/ |
202
|
|
|
public function begin() |
203
|
|
|
{ |
204
|
|
|
$result = $this->connection->query("BEGIN"); |
205
|
|
|
return $result !== false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritDoc |
210
|
|
|
*/ |
211
|
|
|
public function commit() |
212
|
|
|
{ |
213
|
|
|
$result = $this->connection->query("COMMIT"); |
214
|
|
|
return $result !== false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritDoc |
219
|
|
|
*/ |
220
|
|
|
public function rollback() |
221
|
|
|
{ |
222
|
|
|
$result = $this->connection->query("ROLLBACK"); |
223
|
|
|
return $result !== false; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|