1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Useful functions to get quick MySQL content |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiByDanielGPstructures |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use MySQLiByDanielGP, |
40
|
|
|
MySQLiByDanielGPqueries; |
41
|
|
|
|
42
|
|
|
protected $advCache = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Ensures table has special quotes and DOT as final char |
46
|
|
|
* (if not empty, of course) |
47
|
|
|
* |
48
|
|
|
* @param string $referenceTable |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
private function correctTableWithQuotesAsFieldPrefix($referenceTable) |
52
|
|
|
{ |
53
|
|
|
if ($referenceTable != '') { |
54
|
|
|
return '`' . str_replace('`', '', $referenceTable) . '`.'; |
55
|
|
|
} |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Establish Database and Table intended to work with |
61
|
|
|
* (in case the DB is omitted get the default one) |
62
|
|
|
* |
63
|
|
|
* @param string $tblSrc |
64
|
|
|
*/ |
65
|
|
|
private function establishDatabaseAndTable($tblSrc) |
66
|
|
|
{ |
67
|
|
|
if (strpos($tblSrc, '.') === false) { |
68
|
|
|
if (!array_key_exists('workingDatabase', $this->advCache)) { |
69
|
|
|
$this->advCache['workingDatabase'] = $this->getMySqlCurrentDatabase(); |
70
|
|
|
} |
71
|
|
|
return [$this->advCache['workingDatabase'], $tblSrc]; |
72
|
|
|
} |
73
|
|
|
return explode('.', str_replace('`', '', $tblSrc)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the name of a field for displaying |
78
|
|
|
* |
79
|
|
|
* @param array $details |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getFieldNameForDisplay($details) |
83
|
|
|
{ |
84
|
|
|
$tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
85
|
|
|
if ($details['COLUMN_COMMENT'] != '') { |
86
|
|
|
return $details['COLUMN_COMMENT']; |
87
|
|
|
} elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
88
|
|
|
return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
89
|
|
|
} |
90
|
|
|
return $details['COLUMN_NAME']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Prepares the output of text fields defined w. FKs |
95
|
|
|
* |
96
|
|
|
* @param array $foreignKeysArray |
97
|
|
|
* @param array $value |
98
|
|
|
* @param array $iar |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
102
|
|
|
{ |
103
|
|
|
$query = $this->sQueryGenericSelectKeyValue([ |
104
|
|
|
'`' . $value['COLUMN_NAME'] . '`', |
105
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][2], |
106
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][0] |
107
|
|
|
]); |
108
|
|
|
$inAdtnl = ['size' => 1]; |
109
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
110
|
|
|
$inAdtnl = array_merge($inAdtnl, ['include_null']); |
111
|
|
|
} |
112
|
|
|
if ($iar !== []) { |
113
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
114
|
|
|
} |
115
|
|
|
$slct = [ |
116
|
|
|
'Options' => $this->setMySQLquery2Server($query, 'array_key_value')['result'], |
117
|
|
|
'Value' => $this->getFieldValue($value), |
118
|
|
|
]; |
119
|
|
|
return $this->setArrayToSelect($slct['Options'], $slct['Value'], $value['COLUMN_NAME'], $inAdtnl); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Prepares the output of text fields w/o FKs |
124
|
|
|
* |
125
|
|
|
* @param array $value |
126
|
|
|
* @param array $iar |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function getFieldOutputTextNonFK($value, $iar) |
130
|
|
|
{ |
131
|
|
|
$fldNos = $this->setFieldNumbers($value); |
132
|
|
|
$inAdtnl = [ |
133
|
|
|
'type' => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'), |
134
|
|
|
'name' => $value['COLUMN_NAME'], |
135
|
|
|
'id' => $value['COLUMN_NAME'], |
136
|
|
|
'size' => min(30, $fldNos['M']), |
137
|
|
|
'maxlength' => min(255, $fldNos['M']), |
138
|
|
|
'value' => $this->getFieldValue($value), |
139
|
|
|
]; |
140
|
|
|
if ($iar !== []) { |
141
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
142
|
|
|
} |
143
|
|
|
return $this->setStringIntoShortTag('input', $inAdtnl); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Return the list of Tables from the MySQL server |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
protected function getMySQLStatistics($filterArray = null) |
152
|
|
|
{ |
153
|
|
|
return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* returns a list of MySQL databases |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function getMySQLactiveDatabases() |
162
|
|
|
{ |
163
|
|
|
return $this->getMySQLlistDatabases(true); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* returns a list of active MySQL engines |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
protected function getMySQLactiveEngines() |
172
|
|
|
{ |
173
|
|
|
return $this->getMySQLlistEngines(true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* returns the list of all MySQL global variables |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
protected function getMySQLglobalVariables() |
182
|
|
|
{ |
183
|
|
|
return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function getMySQLlistColumns($filterArray = null) |
192
|
|
|
{ |
193
|
|
|
return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* returns a list of MySQL databases (w. choice of exclude/include the system ones) |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
protected function getMySQLlistDatabases($excludeSystemDbs = true) |
202
|
|
|
{ |
203
|
|
|
return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* returns a list of MySQL engines (w. choice of return only the active ones) |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
protected function getMySQLlistEngines($onlyActiveOnes = true) |
212
|
|
|
{ |
213
|
|
|
return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function getMySQLlistIndexes($filterArray = null) |
222
|
|
|
{ |
223
|
|
|
return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Return various information (from predefined list) from the MySQL server |
228
|
|
|
* |
229
|
|
|
* @return int|null|array |
230
|
|
|
*/ |
231
|
|
|
private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null) |
232
|
|
|
{ |
233
|
|
|
if (is_null($this->mySQLconnection)) { |
234
|
|
|
if ($returnType == 'value') { |
235
|
|
|
return null; |
236
|
|
|
} |
237
|
|
|
return []; |
238
|
|
|
} |
239
|
|
|
return $this->getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return various information (from predefined list) from the MySQL server |
244
|
|
|
* |
245
|
|
|
* @param string $rChoice |
246
|
|
|
* @param string $returnType |
247
|
|
|
* @param array $additionalFeatures |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
private function getMySQLlistMultipleFinal($rChoice, $returnType, $additionalFeatures = null) |
251
|
|
|
{ |
252
|
|
|
$qByChoice = [ |
253
|
|
|
'Columns' => ['sQueryMySqlColumns', $additionalFeatures], |
254
|
|
|
'Databases' => ['sQueryMySqlActiveDatabases', $additionalFeatures], |
255
|
|
|
'Engines' => ['sQueryMySqlActiveEngines', $additionalFeatures], |
256
|
|
|
'Indexes' => ['sQueryMySqlIndexes', $additionalFeatures], |
257
|
|
|
'ServerTime' => ['sQueryMySqlServerTime'], |
258
|
|
|
'Statistics' => ['sQueryMySqlStatistics', $additionalFeatures], |
259
|
|
|
'Tables' => ['sQueryMySqlTables', $additionalFeatures], |
260
|
|
|
'VariablesGlobal' => ['sQueryMySqlGlobalVariables'], |
261
|
|
|
]; |
262
|
|
|
if (array_key_exists($rChoice, $qByChoice)) { |
263
|
|
|
return $this->setMySQLquery2Server($this->transformStrIntoFn($qByChoice, $rChoice), $returnType)['result']; |
264
|
|
|
} |
265
|
|
|
return []; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Return the list of Tables from the MySQL server |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
protected function getMySQLlistTables($filterArray = null) |
274
|
|
|
{ |
275
|
|
|
return $this->getMySQLlistMultiple('Tables', 'full_array_key_numbered', $filterArray); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return the time from the MySQL server |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
|
|
protected function getMySQLserverTime() |
284
|
|
|
{ |
285
|
|
|
return $this->getMySQLlistMultiple('ServerTime', 'value'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Reads data from table into REQUEST super global |
290
|
|
|
* |
291
|
|
|
* @param string $tableName |
292
|
|
|
* @param array $filtersArray |
293
|
|
|
*/ |
294
|
|
|
protected function getRowDataFromTable($tableName, $filtersArray) |
295
|
|
|
{ |
296
|
|
|
$query = $this->sQueryRowsFromTable([$tableName, $this->setArrayToFilterValues($filtersArray)]); |
297
|
|
|
$rawData = $this->setMySQLquery2Server($query, 'array_pairs_key_value')['result']; |
298
|
|
|
if (!is_null($rawData)) { |
299
|
|
|
$this->initializeSprGlbAndSession(); |
300
|
|
|
foreach ($rawData as $key => $value) { |
301
|
|
|
$vToSet = str_replace(['\\\\"', '\\"', "\\\\'", "\\'"], ['"', '"', "'", "'"], $value); |
302
|
|
|
$this->tCmnRequest->request->set($key, $vToSet); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Builds an filter string from pair of key and value, where value is array |
309
|
|
|
* |
310
|
|
|
* @param string $key |
311
|
|
|
* @param array $value |
312
|
|
|
* @param string $referenceTable |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
|
|
private function setArrayLineArrayToFilter($key, $value, $referenceTable) |
316
|
|
|
{ |
317
|
|
|
$filters2 = implode(', ', array_diff($value, [''])); |
318
|
|
|
if ($filters2 != '') { |
319
|
|
|
return '(' . $referenceTable . '`' . $key . '` IN ("' |
320
|
|
|
. str_replace(',', '","', str_replace(["'", '"'], '', $filters2)) . '"))'; |
321
|
|
|
} |
322
|
|
|
return ''; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Builds an filter string from pair of key and value, none array |
327
|
|
|
* |
328
|
|
|
* @param string $key |
329
|
|
|
* @param int|float|string $value |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
private function setArrayLineToFilter($key, $value) |
333
|
|
|
{ |
334
|
|
|
$fTemp = '='; |
335
|
|
|
if ((substr($value, 0, 1) == '%') && (substr($value, -1) == '%')) { |
336
|
|
|
$fTemp = 'LIKE'; |
337
|
|
|
} |
338
|
|
|
return '(`' . $key . '` ' . $fTemp . '"' . $value . '")'; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Transforms an array into usable filters |
343
|
|
|
* |
344
|
|
|
* @param array $entryArray |
345
|
|
|
* @param string $referenceTable |
346
|
|
|
* @return string |
347
|
|
|
*/ |
348
|
|
|
private function setArrayToFilterValues($entryArray, $referenceTable = '') |
349
|
|
|
{ |
350
|
|
|
$filters = []; |
351
|
|
|
$refTable = $this->correctTableWithQuotesAsFieldPrefix($referenceTable); |
352
|
|
|
foreach ($entryArray as $key => $value) { |
353
|
|
|
if (is_array($value)) { |
354
|
|
|
$filters[] = $this->setArrayLineArrayToFilter($key, $value, $refTable); |
355
|
|
|
} elseif (!in_array($value, ['', '%%'])) { |
356
|
|
|
$filters[] = $this->setArrayLineToFilter($key, $value); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
return implode(' AND ', array_diff($filters, [''])); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
private function transformStrIntoFn($queryByChoice, $rChoice) |
363
|
|
|
{ |
364
|
|
|
$query = null; |
365
|
|
|
switch (count($queryByChoice[$rChoice])) { |
366
|
|
|
case 1: |
367
|
|
|
$query = call_user_func([$this, $queryByChoice[$rChoice][0]]); |
368
|
|
|
break; |
369
|
|
|
case 2: |
370
|
|
|
$query = call_user_func([$this, $queryByChoice[$rChoice][0]], $queryByChoice[$rChoice][1]); |
371
|
|
|
break; |
372
|
|
|
} |
373
|
|
|
return $query; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
} |
377
|
|
|
|