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