1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface; |
7
|
|
|
use Lagdo\DbAdmin\Driver\Db\QueryInterface; |
8
|
|
|
use Lagdo\DbAdmin\Driver\Db\StatementInterface; |
9
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
10
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
11
|
|
|
|
12
|
|
|
trait QueryTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QueryInterface |
16
|
|
|
*/ |
17
|
|
|
protected $query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get logged user |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function user() |
25
|
|
|
{ |
26
|
|
|
return $this->query->user(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get current schema from the database |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
// public function schema() |
35
|
|
|
// { |
36
|
|
|
// return $this->query->schema(); |
37
|
|
|
// } |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Select data from table |
41
|
|
|
* |
42
|
|
|
* @param string $table |
43
|
|
|
* @param array $select Result of processSelectColumns()[0] |
44
|
|
|
* @param array $where Result of processSelectWhere() |
45
|
|
|
* @param array $group Result of processSelectColumns()[1] |
46
|
|
|
* @param array $order Result of processSelectOrder() |
47
|
|
|
* @param int $limit Result of processSelectLimit() |
48
|
|
|
* @param int $page Index of page starting at zero |
49
|
|
|
* |
50
|
|
|
* @return StatementInterface|bool |
51
|
|
|
*/ |
52
|
|
|
public function select(string $table, array $select, array $where, |
53
|
|
|
array $group, array $order = [], int $limit = 1, int $page = 0) |
54
|
|
|
{ |
55
|
|
|
return $this->query->select($table, $select, $where, $group, $order, $limit, $page); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Insert data into table |
60
|
|
|
* |
61
|
|
|
* @param string $table |
62
|
|
|
* @param array $values Escaped columns in keys, quoted data in values |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function insert(string $table, array $values) |
67
|
|
|
{ |
68
|
|
|
return $this->query->insert($table, $values); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Update data in table |
73
|
|
|
* |
74
|
|
|
* @param string $table |
75
|
|
|
* @param array $values Escaped columns in keys, quoted data in values |
76
|
|
|
* @param string $queryWhere " WHERE ..." |
77
|
|
|
* @param int $limit 0 or 1 |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function update(string $table, array $values, string $queryWhere, int $limit = 0) |
82
|
|
|
{ |
83
|
|
|
return $this->query->update($table, $values, $queryWhere, $limit); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Delete data from table |
88
|
|
|
* |
89
|
|
|
* @param string $table |
90
|
|
|
* @param string $queryWhere " WHERE ..." |
91
|
|
|
* @param int $limit 0 or 1 |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function delete(string $table, string $queryWhere, int $limit = 0) |
96
|
|
|
{ |
97
|
|
|
return $this->query->delete($table, $queryWhere, $limit); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Insert or update data in table |
102
|
|
|
* |
103
|
|
|
* @param string $table |
104
|
|
|
* @param array $rows |
105
|
|
|
* @param array $primary of arrays with escaped columns in keys and quoted data in values |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function insertOrUpdate(string $table, array $rows, array $primary) |
110
|
|
|
{ |
111
|
|
|
return $this->query->insertOrUpdate($table, $rows, $primary); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get last auto increment ID |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function lastAutoIncrementId() |
120
|
|
|
{ |
121
|
|
|
return $this->query->lastAutoIncrementId(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return query with a timeout |
126
|
|
|
* |
127
|
|
|
* @param string $query |
128
|
|
|
* @param int $timeout In seconds |
129
|
|
|
* |
130
|
|
|
* @return string or null if the driver doesn't support query timeouts |
131
|
|
|
*/ |
132
|
|
|
public function slowQuery(string $query, int $timeout) |
133
|
|
|
{ |
134
|
|
|
return $this->query->slowQuery($query, $timeout); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove current user definer from SQL command |
139
|
|
|
* |
140
|
|
|
* @param string $query |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function removeDefiner(string $query): string |
145
|
|
|
{ |
146
|
|
|
return $this->query->removeDefiner($query); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Execute query |
151
|
|
|
* |
152
|
|
|
* @param string $query |
153
|
|
|
* @param bool $execute |
154
|
|
|
* @param bool $failed |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
* @throws Exception |
158
|
|
|
*/ |
159
|
|
|
public function executeQuery(string $query, bool $execute = true, |
160
|
|
|
bool $failed = false/*, string $time = ''*/): bool |
161
|
|
|
{ |
162
|
|
|
return $this->query->executeQuery($query, $execute, $failed/*, $time*/); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Create SQL condition from parsed query string |
167
|
|
|
* |
168
|
|
|
* @param array $where Parsed query string |
169
|
|
|
* @param array $fields |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function where(array $where, array $fields = []): string |
174
|
|
|
{ |
175
|
|
|
return $this->query->where($where, $fields); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Explain select |
180
|
|
|
* |
181
|
|
|
* @param ConnectionInterface $connection |
182
|
|
|
* @param string $query |
183
|
|
|
* |
184
|
|
|
* @return StatementInterface|bool |
185
|
|
|
*/ |
186
|
|
|
public function explain(ConnectionInterface $connection, string $query) |
187
|
|
|
{ |
188
|
|
|
return $this->query->explain($connection, $query); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get approximate number of rows |
193
|
|
|
* |
194
|
|
|
* @param TableEntity $tableStatus |
195
|
|
|
* @param array $where |
196
|
|
|
* |
197
|
|
|
* @return int|null |
198
|
|
|
*/ |
199
|
|
|
public function countRows(TableEntity $tableStatus, array $where) |
200
|
|
|
{ |
201
|
|
|
return $this->query->countRows($tableStatus, $where); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Convert column to be searchable |
206
|
|
|
* |
207
|
|
|
* @param string $idf Escaped column name |
208
|
|
|
* @param array $value ["op" => , "val" => ] |
209
|
|
|
* @param TableFieldEntity $field |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public function convertSearch(string $idf, array $value, TableFieldEntity $field) |
214
|
|
|
{ |
215
|
|
|
return $this->query->convertSearch($idf, $value, $field); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get view SELECT |
220
|
|
|
* |
221
|
|
|
* @param string $name |
222
|
|
|
* |
223
|
|
|
* @return array array("select" => ) |
224
|
|
|
*/ |
225
|
|
|
public function view(string $name) |
226
|
|
|
{ |
227
|
|
|
return $this->query->view($name); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|