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
|
|
|
* Usefull functions to get quick MySQL content |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiByDanielGPstructures |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use MySQLiByDanielGP, |
40
|
|
|
MySQLiByDanielGPqueries; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ensures table has special quoes and DOT as final char |
44
|
|
|
* (if not empty, of course) |
45
|
|
|
* |
46
|
|
|
* @param string $referenceTable |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
private function correctTableWithQuotesAsFieldPrefix($referenceTable) |
50
|
|
|
{ |
51
|
|
|
if ($referenceTable != '') { |
52
|
|
|
return '`' . str_replace('`', '', $referenceTable) . '`.'; |
53
|
|
|
} |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Prepares the output of text fields defined w. FKs |
59
|
|
|
* |
60
|
|
|
* @param array $foreignKeysArray |
61
|
|
|
* @param array $value |
62
|
|
|
* @param array $iar |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
66
|
|
|
{ |
67
|
|
|
$query = $this->sQueryGenericSelectKeyValue([ |
68
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][1], |
69
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][2], |
70
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][0] |
71
|
|
|
]); |
72
|
|
|
$inAdtnl = ['size' => 1]; |
73
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
74
|
|
|
$inAdtnl = array_merge($inAdtnl, ['include_null']); |
75
|
|
|
} |
76
|
|
|
if ($iar !== []) { |
77
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
78
|
|
|
} |
79
|
|
|
$slct = [ |
80
|
|
|
'Options' => $this->setMySQLquery2Server($query, 'array_key_value'), |
81
|
|
|
'Value' => $this->getFieldValue($value), |
82
|
|
|
]; |
83
|
|
|
return $this->setArrayToSelect($slct['Options'], $slct['Value'], $value['COLUMN_NAME'], $inAdtnl); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Prepares the output of text fields w/o FKs |
88
|
|
|
* |
89
|
|
|
* @param array $value |
90
|
|
|
* @param array $iar |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function getFieldOutputTextNonFK($value, $iar) |
94
|
|
|
{ |
95
|
|
|
$fldNos = $this->setFieldNumbers($value); |
96
|
|
|
$inAdtnl = [ |
97
|
|
|
'type' => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'), |
98
|
|
|
'name' => $value['COLUMN_NAME'], |
99
|
|
|
'id' => $value['COLUMN_NAME'], |
100
|
|
|
'size' => min(30, $fldNos['M']), |
101
|
|
|
'maxlength' => min(255, $fldNos['M']), |
102
|
|
|
'value' => $this->getFieldValue($value), |
103
|
|
|
]; |
104
|
|
|
if ($iar !== []) { |
105
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
106
|
|
|
} |
107
|
|
|
return $this->setStringIntoShortTag('input', $inAdtnl); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the list of Tables from the MySQL server |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getMySQLStatistics($filterArray = null) |
116
|
|
|
{ |
117
|
|
|
return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* returns a list of MySQL databases |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getMySQLactiveDatabases() |
126
|
|
|
{ |
127
|
|
|
return $this->getMySQLlistDatabases(true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* returns a list of active MySQL engines |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function getMySQLactiveEngines() |
136
|
|
|
{ |
137
|
|
|
return $this->getMySQLlistEngines(true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* returns the list of all MySQL global variables |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getMySQLglobalVariables() |
146
|
|
|
{ |
147
|
|
|
return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function getMySQLlistColumns($filterArray = null) |
156
|
|
|
{ |
157
|
|
|
return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* returns a list of MySQL databases (w. choice of exclude/include the system ones) |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
protected function getMySQLlistDatabases($excludeSystemDbs = true) |
166
|
|
|
{ |
167
|
|
|
return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* returns a list of MySQL engines (w. choice of return only the active ones) |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
protected function getMySQLlistEngines($onlyActiveOnes = true) |
176
|
|
|
{ |
177
|
|
|
return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
protected function getMySQLlistIndexes($filterArray = null) |
186
|
|
|
{ |
187
|
|
|
return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return various informations (from predefined list) from the MySQL server |
192
|
|
|
* |
193
|
|
|
* @return int|array |
194
|
|
|
*/ |
195
|
|
|
private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null) |
196
|
|
|
{ |
197
|
|
|
if (is_null($this->mySQLconnection)) { |
198
|
|
|
if ($returnType == 'value') { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
return []; |
202
|
|
|
} |
203
|
|
|
return $this->getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Return various informations (from predefined list) from the MySQL server |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
private function getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures = null) |
212
|
|
|
{ |
213
|
|
|
$queryByChoice = [ |
214
|
|
|
'Columns' => $this->sQueryMySqlColumns($additionalFeatures), |
215
|
|
|
'Databases' => $this->sQueryMySqlActiveDatabases($additionalFeatures), |
216
|
|
|
'Engines' => $this->sQueryMySqlActiveEngines($additionalFeatures), |
217
|
|
|
'Indexes' => $this->sQueryMySqlIndexes($additionalFeatures), |
218
|
|
|
'ServerTime' => $this->sQueryMySqlServerTime(), |
219
|
|
|
'Statistics' => $this->sQueryMySqlStatistics($additionalFeatures), |
220
|
|
|
'Tables' => $this->sQueryMySqlTables($additionalFeatures), |
221
|
|
|
'VariablesGlobal' => $this->sQueryMySqlGlobalVariables(), |
222
|
|
|
]; |
223
|
|
|
if (array_key_exists($returnChoice, $queryByChoice)) { |
224
|
|
|
return $this->setMySQLquery2Server($queryByChoice[$returnChoice], $returnType)['result']; |
225
|
|
|
} |
226
|
|
|
return []; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the list of Tables from the MySQL server |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
protected function getMySQLlistTables($filterArray = null) |
235
|
|
|
{ |
236
|
|
|
return $this->getMySQLlistMultiple('Tables', 'full_array_key_numbered', $filterArray); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Return the time from the MySQL server |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
protected function getMySQLserverTime() |
245
|
|
|
{ |
246
|
|
|
return $this->getMySQLlistMultiple('ServerTime', 'value'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Reads data from table into REQUEST super global |
251
|
|
|
* |
252
|
|
|
* @param string $tableName |
253
|
|
|
* @param array $filtersArray |
254
|
|
|
*/ |
255
|
|
|
protected function getRowDataFromTable($tableName, $filtersArray) |
256
|
|
|
{ |
257
|
|
|
$query = $this->sQueryRowsFromTable([$tableName, $this->setArrayToFilterValues($filtersArray)]); |
258
|
|
|
$rawData = $this->setMySQLquery2Server($query, 'array_pairs_key_value')['result']; |
259
|
|
|
if (!is_null($rawData)) { |
260
|
|
|
$this->initializeSprGlbAndSession(); |
261
|
|
|
foreach ($rawData as $key => $value) { |
262
|
|
|
$vToSet = str_replace(['\\\\"', '\\"', "\\\\'", "\\'"], ['"', '"', "'", "'"], $value); |
263
|
|
|
$this->tCmnRequest->request->set($key, $vToSet); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Builds an filter string from pair of key and value, where value is array |
270
|
|
|
* |
271
|
|
|
* @param string $key |
272
|
|
|
* @param array $value |
273
|
|
|
* @param string $referenceTable |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
private function setArrayLineArrayToFilter($key, $value, $referenceTable) |
277
|
|
|
{ |
278
|
|
|
$filters2 = implode(', ', array_diff($value, [''])); |
279
|
|
|
if ($filters2 != '') { |
280
|
|
|
return '(' . $referenceTable . '`' . $key . '` IN ("' |
281
|
|
|
. str_replace(',', '","', str_replace(["'", '"'], '', $filters2)) . '"))'; |
282
|
|
|
} |
283
|
|
|
return ''; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Builds an filter string from pair of key and value, none array |
288
|
|
|
* |
289
|
|
|
* @param string $key |
290
|
|
|
* @param int|float|string $value |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
private function setArrayLineToFilter($key, $value) |
294
|
|
|
{ |
295
|
|
|
$fTemp = '='; |
296
|
|
|
if ((substr($value, 0, 1) == '%') && (substr($value, -1) == '%')) { |
297
|
|
|
$fTemp = 'LIKE'; |
298
|
|
|
} |
299
|
|
|
return '(`' . $key . '` ' . $fTemp . '"' . $value . '")'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Transforms an array into usable filters |
304
|
|
|
* |
305
|
|
|
* @param array $entryArray |
306
|
|
|
* @param string $referenceTable |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
|
|
private function setArrayToFilterValues($entryArray, $referenceTable = '') |
310
|
|
|
{ |
311
|
|
|
$filters = []; |
312
|
|
|
$refTable = $this->correctTableWithQuotesAsFieldPrefix($referenceTable); |
313
|
|
|
foreach ($entryArray as $key => $value) { |
314
|
|
|
if (is_array($value)) { |
315
|
|
|
$filters[] = $this->setArrayLineArrayToFilter($key, $value, $refTable); |
316
|
|
|
} elseif (!in_array($value, ['', '%%'])) { |
317
|
|
|
$filters[] = $this->setArrayLineToFilter($key, $value); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
return implode(' AND ', array_diff($filters, [''])); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.