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 MySQLiByDanielGP |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use MySQLiMultiple; |
40
|
|
|
|
41
|
|
|
protected $mySQLconnection = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Intiates connection to MySQL |
45
|
|
|
* |
46
|
|
|
* @param array $mySQLconfig |
47
|
|
|
* |
48
|
|
|
* $mySQLconfig = [ |
49
|
|
|
* 'host' => MYSQL_HOST, |
50
|
|
|
* 'port' => MYSQL_PORT, |
51
|
|
|
* 'username' => MYSQL_USERNAME, |
52
|
|
|
* 'password' => MYSQL_PASSWORD, |
53
|
|
|
* 'database' => MYSQL_DATABASE, |
54
|
|
|
* ]; |
55
|
|
|
*/ |
56
|
|
|
protected function connectToMySql($mySQLconfig) |
57
|
|
|
{ |
58
|
|
|
if (is_null($this->mySQLconnection)) { |
59
|
|
|
extract($mySQLconfig); |
60
|
|
|
$this->mySQLconnection = new \mysqli($host, $username, $password, $database, $port); |
61
|
|
|
if (is_null($this->mySQLconnection->connect_error)) { |
62
|
|
|
$sReturn = ''; |
63
|
|
|
} else { |
64
|
|
|
$erNo = $this->mySQLconnection->connect_errno; |
65
|
|
|
$erMsg = $this->mySQLconnection->connect_error; |
66
|
|
|
$this->mySQLconnection = null; |
67
|
|
|
$msg = $this->lclMsgCmn('i18n_Feedback_ConnectionError'); |
|
|
|
|
68
|
|
|
$sReturn = sprintf($msg, $erNo, $erMsg, $host, $port, $username, $database); |
69
|
|
|
} |
70
|
|
|
return $sReturn; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* returns a list of MySQL databases |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function getMySQLactiveDatabases() |
80
|
|
|
{ |
81
|
|
|
return $this->getMySQLlistDatabases(true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* returns a list of active MySQL engines |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function getMySQLactiveEngines() |
90
|
|
|
{ |
91
|
|
|
return $this->getMySQLlistEngines(true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* returns the list of all MySQL generic informations |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function getMySQLgenericInformations() |
100
|
|
|
{ |
101
|
|
|
if (is_null($this->mySQLconnection)) { |
102
|
|
|
$line = []; |
103
|
|
|
} else { |
104
|
|
|
$line = [ |
105
|
|
|
'Info' => $this->mySQLconnection->server_info, |
106
|
|
|
'Version' => $this->mySQLconnection->server_version |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
return $line; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* returns the list of all MySQL global variables |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function getMySQLglobalVariables() |
118
|
|
|
{ |
119
|
|
|
return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function getMySQLlistColumns($filterArray = null) |
128
|
|
|
{ |
129
|
|
|
return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* returns a list of MySQL databases (w. choice of exclude/include the system ones) |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function getMySQLlistDatabases($excludeSystemDbs = true) |
138
|
|
|
{ |
139
|
|
|
return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* returns a list of MySQL engines (w. choice of return only the active ones) |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
protected function getMySQLlistEngines($onlyActiveOnes = true) |
148
|
|
|
{ |
149
|
|
|
return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
protected function getMySQLlistIndexes($filterArray = null) |
158
|
|
|
{ |
159
|
|
|
return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return various informations (from predefined list) from the MySQL server |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null) |
168
|
|
|
{ |
169
|
|
|
if (is_null($this->mySQLconnection)) { |
170
|
|
|
switch ($returnType) { |
171
|
|
|
case 'value': |
172
|
|
|
$line = null; |
173
|
|
|
break; |
174
|
|
|
default: |
175
|
|
|
$line = []; |
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
switch ($returnChoice) { |
180
|
|
|
case 'Columns': |
181
|
|
|
$query = $this->sQueryMySqlColumns($additionalFeatures); |
|
|
|
|
182
|
|
|
break; |
183
|
|
|
case 'Databases': |
184
|
|
|
$query = $this->sQueryMySqlActiveDatabases($additionalFeatures); |
|
|
|
|
185
|
|
|
break; |
186
|
|
|
case 'Engines': |
187
|
|
|
$query = $this->sQueryMySqlActiveEngines($additionalFeatures); |
|
|
|
|
188
|
|
|
break; |
189
|
|
|
case 'Indexes': |
190
|
|
|
$query = $this->sQueryMySqlIndexes($additionalFeatures); |
|
|
|
|
191
|
|
|
break; |
192
|
|
|
case 'ServerTime': |
193
|
|
|
$query = $this->sQueryMySqlServerTime(); |
|
|
|
|
194
|
|
|
break; |
195
|
|
|
case 'Statistics': |
196
|
|
|
$query = $this->sQueryMySqlStatistics($additionalFeatures); |
|
|
|
|
197
|
|
|
break; |
198
|
|
|
case 'Tables': |
199
|
|
|
$query = $this->sQueryMySqlTables($additionalFeatures); |
|
|
|
|
200
|
|
|
break; |
201
|
|
|
case 'VariablesGlobal': |
202
|
|
|
$query = $this->sQueryMySqlGlobalVariables(); |
|
|
|
|
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
$line = $this->setMySQLquery2Server($query, $returnType)[ |
|
|
|
|
206
|
|
|
'result' |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
return $line; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return the list of Tables from the MySQL server |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
protected function getMySQLStatistics($filterArray = null) |
218
|
|
|
{ |
219
|
|
|
return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Return the list of Tables from the MySQL server |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
protected function getMySQLlistTables($filterArray = null) |
228
|
|
|
{ |
229
|
|
|
return $this->getMySQLlistMultiple('Tables', 'full_array_key_numbered', $filterArray); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns the Query language type by scanning the 1st keyword from a given query |
234
|
|
|
* |
235
|
|
|
* @param input $sQuery |
236
|
|
|
*/ |
237
|
|
|
protected function getMySQLqueryType($sQuery) |
238
|
|
|
{ |
239
|
|
|
$queryPieces = explode(' ', $sQuery); |
240
|
|
|
$statementTypes = $this->getMySQLqueryStatementType(); |
|
|
|
|
241
|
|
|
if (in_array($queryPieces[0], array_keys($statementTypes))) { |
242
|
|
|
$type = $statementTypes[$queryPieces[0]]['Type']; |
243
|
|
|
$sReturn = array_merge([ |
244
|
|
|
'detected1stKeywordWithinQuery' => $queryPieces[0], |
245
|
|
|
$type => $this->getMySQLqueryLanguageType()[$type], |
|
|
|
|
246
|
|
|
], $statementTypes[$queryPieces[0]]); |
247
|
|
|
} else { |
248
|
|
|
$sReturn = [ |
249
|
|
|
'detected1stKeywordWithinQuery' => $queryPieces[0], |
250
|
|
|
'unknown' => [ |
251
|
|
|
'standsFor' => 'unknown', |
252
|
|
|
'description' => 'unknown', |
253
|
|
|
], |
254
|
|
|
'Type' => 'unknown', |
255
|
|
|
'Description' => 'unknown', |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
return $sReturn; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Provides a detection if given Query does contain a Parameter |
263
|
|
|
* that may require statement processing later on |
264
|
|
|
* |
265
|
|
|
* @param string $sQuery |
266
|
|
|
* @param string $paramIdentifier |
267
|
|
|
* @return boolean |
268
|
|
|
*/ |
269
|
|
|
protected function getMySQLqueryWithParameterIdentifier($sQuery, $paramIdentifier) |
270
|
|
|
{ |
271
|
|
|
$sReturn = true; |
272
|
|
|
if (strpos($sQuery, $paramIdentifier) === false) { |
273
|
|
|
$sReturn = false; |
274
|
|
|
} |
275
|
|
|
return $sReturn; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return the time from the MySQL server |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
protected function getMySQLserverTime() |
284
|
|
|
{ |
285
|
|
|
return $this->getMySQLlistMultiple('ServerTime', 'value'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Reads data from table into $_REQUEST |
290
|
|
|
* |
291
|
|
|
* @param string $tableName |
292
|
|
|
* @param array $filtersArray |
293
|
|
|
*/ |
294
|
|
|
protected function getRowDataFromTable($tableName, $filtersArray) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$query = $this->sQueryRowsFromTable([ |
|
|
|
|
297
|
|
|
$tableName, |
298
|
|
|
$this->setArrayToFilterValues($filtersArray), |
299
|
|
|
]); |
300
|
|
|
$rawData = $this->setMySQLquery2Server($query, 'array_pairs_key_value')['result']; |
301
|
|
|
if (!is_null($rawData)) { |
302
|
|
|
foreach ($rawData as $key => $value) { |
303
|
|
|
$_REQUEST[$key] = str_replace(['\\\\"', '\\"', "\\\\'", "\\'"], ['"', '"', "'", "'"], $value); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Just to keep a list of type of language as array |
310
|
|
|
* |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
private static function listOfMySQLqueryLanguageType() |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
return [ |
316
|
|
|
'DCL' => [ |
317
|
|
|
'standsFor' => 'Data Control Language', |
318
|
|
|
'description' => implode(', ', [ |
319
|
|
|
'includes commands such as GRANT', |
320
|
|
|
'and mostly concerned with rights', |
321
|
|
|
'permissions and other controls of the database system', |
322
|
|
|
]), |
323
|
|
|
], |
324
|
|
|
'DDL' => [ |
325
|
|
|
'standsFor' => 'Data Definition Language', |
326
|
|
|
'description' => implode(', ', [ |
327
|
|
|
'deals with database schemas and descriptions', |
328
|
|
|
'of how the data should reside in the database', |
329
|
|
|
]), |
330
|
|
|
], |
331
|
|
|
'DML' => [ |
332
|
|
|
'standsFor' => 'Data Manipulation Language', |
333
|
|
|
'description' => implode(', ', [ |
334
|
|
|
'deals with data manipulation', |
335
|
|
|
'and includes most common SQL statements such as SELECT, INSERT, UPDATE, DELETE etc', |
336
|
|
|
'and it is used to store, modify, retrieve, delete and update data in database', |
337
|
|
|
]), |
338
|
|
|
], |
339
|
|
|
'DQL' => [ |
340
|
|
|
'standsFor' => 'Data Query Language', |
341
|
|
|
'description' => 'deals with data/structure retrieval', |
342
|
|
|
], |
343
|
|
|
'DTL' => [ |
344
|
|
|
'standsFor' => 'Data Transaction Language', |
345
|
|
|
'description' => implode('. ', [ |
346
|
|
|
'statements are used to manage changes made by DML statements', |
347
|
|
|
'It allows statements to be grouped together into logical transactions', |
348
|
|
|
]), |
349
|
|
|
], |
350
|
|
|
]; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Just to keep a list of statement types as array |
355
|
|
|
* |
356
|
|
|
* @return array |
357
|
|
|
*/ |
358
|
|
|
private static function listOfMySQLqueryStatementType() |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
return [ |
361
|
|
|
'ALTER' => [ |
362
|
|
|
'Type' => 'DDL', |
363
|
|
|
'Description' => 'create objects in the database', |
364
|
|
|
], |
365
|
|
|
'CALL' => [ |
366
|
|
|
'Type' => 'DML', |
367
|
|
|
'Description' => 'call a stored procedure', |
368
|
|
|
], |
369
|
|
|
'COMMENT' => [ |
370
|
|
|
'Type' => 'DDL', |
371
|
|
|
'Description' => 'add comments to the data dictionary', |
372
|
|
|
], |
373
|
|
|
'COMMIT' => [ |
374
|
|
|
'Type' => 'DTL', |
375
|
|
|
'Description' => 'sends a signal to MySQL to save all un-commited statements', |
376
|
|
|
], |
377
|
|
|
'CREATE' => [ |
378
|
|
|
'Type' => 'DDL', |
379
|
|
|
'Description' => 'create objects within a database', |
380
|
|
|
], |
381
|
|
|
'DELETE' => [ |
382
|
|
|
'Type' => 'DML', |
383
|
|
|
'Description' => 'deletes records from a table (all or partial depending on potential conditions)', |
384
|
|
|
], |
385
|
|
|
'DESC' => [ |
386
|
|
|
'Type' => 'DML', |
387
|
|
|
'Description' => 'interpretation of the data access path (synonym of EXPLAIN)', |
388
|
|
|
], |
389
|
|
|
'DESCRIBE' => [ |
390
|
|
|
'type' => 'DML', |
391
|
|
|
'Description' => 'interpretation of the data access path (synonym of EXPLAIN)', |
392
|
|
|
], |
393
|
|
|
'DO' => [ |
394
|
|
|
'Type' => 'DML', |
395
|
|
|
'Description' => 'executes an expression without returning any result', |
396
|
|
|
], |
397
|
|
|
'DROP' => [ |
398
|
|
|
'Type' => 'DDL', |
399
|
|
|
'Description' => 'delete objects from a database', |
400
|
|
|
], |
401
|
|
|
'EXPLAIN' => [ |
402
|
|
|
'Type' => 'DML', |
403
|
|
|
'Description' => 'interpretation of the data access path', |
404
|
|
|
], |
405
|
|
|
'GRANT' => [ |
406
|
|
|
'Type' => 'DCL', |
407
|
|
|
'Description' => 'allow users access privileges to database', |
408
|
|
|
], |
409
|
|
|
'HANDLER' => [ |
410
|
|
|
'Type' => 'DML', |
411
|
|
|
'Description' => 'statement provides direct access to table storage engine interfaces', |
412
|
|
|
], |
413
|
|
|
'HELP' => [ |
414
|
|
|
'Type' => 'DQL', |
415
|
|
|
'Description' => implode(' ', [ |
416
|
|
|
'The HELP statement returns online information from the MySQL Reference manual.', |
417
|
|
|
'Its proper operation requires that the help tables in the mysql database', |
418
|
|
|
'be initialized with help topic information', |
419
|
|
|
]), |
420
|
|
|
], |
421
|
|
|
'INSERT' => [ |
422
|
|
|
'Type' => 'DML', |
423
|
|
|
'Description' => 'insert data into a table', |
424
|
|
|
], |
425
|
|
|
'LOAD' => [ |
426
|
|
|
'Type' => 'DML', |
427
|
|
|
'Description' => implode(' ', [ |
428
|
|
|
'The LOAD DATA INFILE statement reads rows from a text file', |
429
|
|
|
'into a table at a very high speed', |
430
|
|
|
'or LOAD XML statement reads data from an XML file into a table', |
431
|
|
|
]), |
432
|
|
|
], |
433
|
|
|
'LOCK' => [ |
434
|
|
|
'Type' => 'DML', |
435
|
|
|
'Description' => 'concurrency control', |
436
|
|
|
], |
437
|
|
|
'MERGE' => [ |
438
|
|
|
'Type' => 'DML', |
439
|
|
|
'Description' => 'UPSERT operation (insert or update)', |
440
|
|
|
], |
441
|
|
|
'RELEASE' => [ |
442
|
|
|
'Type' => 'DTL', |
443
|
|
|
'Description' => implode(' ', [ |
444
|
|
|
'The RELEASE SAVEPOINT statement removes the named savepoint', |
445
|
|
|
'from the set of savepoints of the current transaction.', |
446
|
|
|
'No commit or rollback occurs. It is an error if the savepoint does not exist.', |
447
|
|
|
]), |
448
|
|
|
], |
449
|
|
|
'RENAME' => [ |
450
|
|
|
'Type' => 'DDL', |
451
|
|
|
'Description' => 'rename objects from a database', |
452
|
|
|
], |
453
|
|
|
'REPLACE' => [ |
454
|
|
|
'Type' => 'DML', |
455
|
|
|
'Description' => implode(' ', [ |
456
|
|
|
'REPLACE works exactly like INSERT, except that if an old row in the table', |
457
|
|
|
'has the same value as a new row for a PRIMARY KEY or a UNIQUE index,', |
458
|
|
|
'the old row is deleted before the new row is inserted', |
459
|
|
|
]), |
460
|
|
|
], |
461
|
|
|
'REVOKE' => [ |
462
|
|
|
'Type' => 'DCL', |
463
|
|
|
'description' => 'withdraw users access privileges given by using the GRANT command', |
464
|
|
|
], |
465
|
|
|
'ROLLBACK' => [ |
466
|
|
|
'Type' => 'DTL', |
467
|
|
|
'Description' => 'restore database to original since the last COMMIT', |
468
|
|
|
], |
469
|
|
|
'SELECT' => [ |
470
|
|
|
'Type' => 'DQL', |
471
|
|
|
'Description' => 'retrieve data from the a database', |
472
|
|
|
], |
473
|
|
|
'SAVEPOINT' => [ |
474
|
|
|
'Type' => 'DTL', |
475
|
|
|
'Description' => 'identify a point in a transaction to which you can later roll back', |
476
|
|
|
], |
477
|
|
|
'SET' => [ |
478
|
|
|
'Type' => 'DTL', |
479
|
|
|
'Description' => 'change values of global/session variables or transaction characteristics', |
480
|
|
|
], |
481
|
|
|
'SHOW' => [ |
482
|
|
|
'Type' => 'DQL', |
483
|
|
|
'Description' => implode(' ', [ |
484
|
|
|
'has many forms that provide information about databases, tables, columns,', |
485
|
|
|
'or status information about the server', |
486
|
|
|
]), |
487
|
|
|
], |
488
|
|
|
'START' => [ |
489
|
|
|
'Type' => 'DTL', |
490
|
|
|
'Description' => 'marks the starting point for a transaction', |
491
|
|
|
], |
492
|
|
|
'TRUNCATE' => [ |
493
|
|
|
'Type' => 'DDL', |
494
|
|
|
'Description' => implode(', ', [ |
495
|
|
|
'remove all records from a table', |
496
|
|
|
'including all spaces allocated for the records are removed' |
497
|
|
|
]), |
498
|
|
|
], |
499
|
|
|
'UPDATE' => [ |
500
|
|
|
'Type' => 'DML', |
501
|
|
|
'Description' => 'updates existing data within a table', |
502
|
|
|
], |
503
|
|
|
'USE' => [ |
504
|
|
|
'Type' => 'DML', |
505
|
|
|
'Description' => implode(' ', [ |
506
|
|
|
'The USE db_name statement tells MySQL to use the db_name database', |
507
|
|
|
'as the default (current) database for subsequent statements.', |
508
|
|
|
]), |
509
|
|
|
], |
510
|
|
|
]; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Transforms an array into usable filters |
515
|
|
|
* |
516
|
|
|
* @param array $entryArray |
517
|
|
|
* @param string $referenceTable |
518
|
|
|
* @return array |
519
|
|
|
*/ |
520
|
|
|
private function setArrayToFilterValues($entryArray, $referenceTable = '') |
521
|
|
|
{ |
522
|
|
|
$filters = ''; |
523
|
|
|
if ($referenceTable != '') { |
524
|
|
|
$referenceTable = '`' . $referenceTable . '`.'; |
525
|
|
|
} |
526
|
|
|
foreach ($entryArray as $key => $value) { |
527
|
|
|
if (is_array($value)) { |
528
|
|
|
$filters2 = ''; |
529
|
|
|
foreach ($value as $value2) { |
530
|
|
|
if ($value2 != '') { |
531
|
|
|
if ($filters2 != '') { |
532
|
|
|
$filters2 .= ','; |
533
|
|
|
} |
534
|
|
|
$filters2 .= '"' . $value2 . '"'; |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
if ($filters2 != '') { |
538
|
|
|
if ($filters != '') { |
539
|
|
|
$filters .= ' AND '; |
540
|
|
|
} |
541
|
|
|
$filters .= ' ' . $referenceTable . '`' . $key |
542
|
|
|
. '` IN ("' . str_replace(',', '","', str_replace(["'", '"'], '', $filters2)) |
543
|
|
|
. '")'; |
544
|
|
|
} |
545
|
|
|
} else { |
546
|
|
|
if (($filters != '') && (!in_array($value, ['', '%%']))) { |
547
|
|
|
$filters .= ' AND '; |
548
|
|
|
} |
549
|
|
|
if (!in_array($value, ['', '%%'])) { |
550
|
|
|
if ((substr($value, 0, 1) == '%') && (substr($value, -1) == '%')) { |
551
|
|
|
$filters .= ' ' . $key . ' LIKE "' . $value . '"'; |
552
|
|
|
} else { |
553
|
|
|
$filters .= ' ' . $key . ' = "' . $value . '"'; |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
return $filters; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Returns maximum length for a given MySQL field |
563
|
|
|
* |
564
|
|
|
* @param string $field_full_type |
|
|
|
|
565
|
|
|
* @return array |
566
|
|
|
*/ |
567
|
|
|
protected function setFieldNumbers($fieldDetails, $outputFormated = false) |
568
|
|
|
{ |
569
|
|
|
switch ($fieldDetails['DATA_TYPE']) { |
570
|
|
View Code Duplication |
case 'bigint': |
|
|
|
|
571
|
|
|
if (strpos($fieldDetails['COLUMN_TYPE'], 'unsigned') !== false) { |
572
|
|
|
$aReturn = ['m' => 0, 'M' => 18446744072705500000, 'l' => 20]; |
573
|
|
|
} else { |
574
|
|
|
$aReturn = ['m' => -9223372036854770000, 'M' => 9223372036854770000, 'l' => 20]; |
575
|
|
|
} |
576
|
|
|
break; |
577
|
|
|
case 'char': |
578
|
|
|
case 'varchar': |
579
|
|
|
case 'tinytext': |
580
|
|
|
$lgth = str_replace([$fieldDetails['DATA_TYPE'], '(', ')'], '', $fieldDetails['COLUMN_TYPE']); |
581
|
|
|
$aReturn = ['l' => $lgth]; |
582
|
|
|
break; |
583
|
|
|
case 'decimal': |
584
|
|
|
case 'numeric': |
585
|
|
|
$aReturn = ['l' => $fieldDetails['NUMERIC_PRECISION'], 'd' => $fieldDetails['NUMERIC_SCALE']]; |
586
|
|
|
break; |
587
|
|
View Code Duplication |
case 'int': |
|
|
|
|
588
|
|
|
if (strpos($fieldDetails['COLUMN_TYPE'], 'unsigned') !== false) { |
589
|
|
|
$aReturn = ['m' => 0, 'M' => 4294967295, 'l' => 10]; |
590
|
|
|
} else { |
591
|
|
|
$aReturn = ['m' => -2147483648, 'M' => 2147483647, 'l' => 11]; |
592
|
|
|
} |
593
|
|
|
break; |
594
|
|
View Code Duplication |
case 'mediumint': |
|
|
|
|
595
|
|
|
if (strpos($fieldDetails['COLUMN_TYPE'], 'unsigned') !== false) { |
596
|
|
|
$aReturn = ['m' => 0, 'M' => 16777215, 'l' => 8]; |
597
|
|
|
} else { |
598
|
|
|
$aReturn = ['m' => -8388608, 'M' => 8388607, 'l' => 8]; |
599
|
|
|
} |
600
|
|
|
break; |
601
|
|
View Code Duplication |
case 'smallint': |
|
|
|
|
602
|
|
|
if (strpos($fieldDetails['COLUMN_TYPE'], 'unsigned') !== false) { |
603
|
|
|
$aReturn = ['m' => 0, 'M' => 65535, 'l' => 5]; |
604
|
|
|
} else { |
605
|
|
|
$aReturn = ['m' => -32768, 'M' => 32767, 'l' => 6]; |
606
|
|
|
} |
607
|
|
|
break; |
608
|
|
View Code Duplication |
case 'tinyint': |
|
|
|
|
609
|
|
|
if (strpos($fieldDetails['COLUMN_TYPE'], 'unsigned') !== false) { |
610
|
|
|
$aReturn = ['m' => 0, 'M' => 255, 'l' => 3]; |
611
|
|
|
} else { |
612
|
|
|
$aReturn = ['m' => -128, 'M' => 127, 'l' => 4]; |
613
|
|
|
} |
614
|
|
|
break; |
615
|
|
|
default: |
616
|
|
|
$aReturn = null; |
617
|
|
|
break; |
618
|
|
|
} |
619
|
|
|
if ($outputFormated) { |
620
|
|
|
if (is_array($aReturn)) { |
621
|
|
|
foreach ($aReturn as $key => $value) { |
622
|
|
|
$aReturn[$key] = $this->setNumberFormat($value); |
|
|
|
|
623
|
|
|
} |
624
|
|
|
} |
625
|
|
|
} |
626
|
|
|
return $aReturn; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Transmit Query to MySQL server and get results back |
631
|
|
|
* |
632
|
|
|
* @param string $sQuery |
633
|
|
|
* @param string $sReturnType |
634
|
|
|
* @param array $ftrs |
635
|
|
|
* @return boolean|array|string |
636
|
|
|
*/ |
637
|
|
|
protected function setMySQLquery2Server($sQuery, $sReturnType = null, $ftrs = null) |
638
|
|
|
{ |
639
|
|
|
$aReturn = [ |
640
|
|
|
'customError' => '', |
641
|
|
|
'result' => null |
642
|
|
|
]; |
643
|
|
|
if (is_null($sReturnType)) { |
644
|
|
|
return $this->mySQLconnection->query(html_entity_decode($sQuery)); |
645
|
|
|
} elseif (is_null($this->mySQLconnection)) { |
646
|
|
|
$aReturn['customError'] = $this->lclMsgCmn('i18n_MySQL_ConnectionNotExisting'); |
|
|
|
|
647
|
|
|
} else { |
648
|
|
|
$result = $this->mySQLconnection->query(html_entity_decode($sQuery)); |
649
|
|
|
if ($result) { |
650
|
|
|
switch (strtolower($sReturnType)) { |
651
|
|
|
case 'array_first_key_rest_values': |
652
|
|
|
case 'array_key_value': |
653
|
|
|
case 'array_key_value2': |
654
|
|
|
case 'array_key2_value': |
655
|
|
|
case 'array_numbered': |
656
|
|
|
case 'array_pairs_key_value': |
657
|
|
View Code Duplication |
case 'full_array_key_numbered': |
|
|
|
|
658
|
|
|
$aReturn = $this->setMySQLquery2ServerByPattern([ |
659
|
|
|
'NoOfColumns' => $result->field_count, |
660
|
|
|
'NoOfRows' => $result->num_rows, |
661
|
|
|
'QueryResult' => $result, |
662
|
|
|
'returnType' => $sReturnType, |
663
|
|
|
'return' => $aReturn |
664
|
|
|
]); |
665
|
|
|
break; |
666
|
|
|
case 'full_array_key_numbered_with_record_number_prefix': |
667
|
|
View Code Duplication |
case 'full_array_key_numbered_with_prefix': |
|
|
|
|
668
|
|
|
$aReturn = $this->setMySQLquery2ServerByPattern([ |
669
|
|
|
'NoOfColumns' => $result->field_count, |
670
|
|
|
'NoOfRows' => $result->num_rows, |
671
|
|
|
'QueryResult' => $result, |
672
|
|
|
'returnType' => $sReturnType, |
673
|
|
|
'prefix' => $ftrs['prefix'], |
674
|
|
|
'return' => $aReturn |
675
|
|
|
]); |
676
|
|
|
break; |
677
|
|
|
case 'id': |
678
|
|
|
$aReturn['result'] = $this->mySQLconnection->insert_id; |
679
|
|
|
break; |
680
|
|
|
case 'lines': |
681
|
|
|
$aReturn['result'] = $result->num_rows; |
682
|
|
|
break; |
683
|
|
|
case 'value': |
684
|
|
|
if (($result->num_rows == 1) && ($result->field_count == 1)) { |
685
|
|
|
$aReturn['result'] = $result->fetch_row()[0]; |
686
|
|
|
} else { |
687
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected1ResultedOther'); |
|
|
|
|
688
|
|
|
$aReturn['customError'] = sprintf($msg, $result->num_rows); |
689
|
|
|
} |
690
|
|
|
break; |
691
|
|
|
default: |
692
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryInvalidReturnTypeSpecified'); |
|
|
|
|
693
|
|
|
$aReturn['customError'] = sprintf($msg, $sReturnType, __FUNCTION__); |
694
|
|
|
break; |
695
|
|
|
} |
696
|
|
|
if (is_object($result)) { |
697
|
|
|
$result->close(); |
698
|
|
|
} |
699
|
|
|
} else { |
700
|
|
|
$erNo = $this->mySQLconnection->errno; |
701
|
|
|
$erMsg = $this->mySQLconnection->error; |
702
|
|
|
$aReturn['customError'] = sprintf($this->lclMsgCmn('i18n_MySQL_QueryError'), $erNo, $erMsg); |
|
|
|
|
703
|
|
|
} |
704
|
|
|
} |
705
|
|
|
return $aReturn; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Turns a raw query result into various structures |
710
|
|
|
* based on different predefined $parameters['returnType'] value |
711
|
|
|
* |
712
|
|
|
* @param array $parameters |
713
|
|
|
* @return array as ['customError' => '...', 'result' => '...'] |
714
|
|
|
*/ |
715
|
|
|
protected function setMySQLquery2ServerByPattern($parameters) |
716
|
|
|
{ |
717
|
|
|
$aReturn = $parameters['return']; |
718
|
|
|
$buildArray = false; |
719
|
|
|
switch ($parameters['returnType']) { |
720
|
|
View Code Duplication |
case 'array_first_key_rest_values': |
|
|
|
|
721
|
|
|
if ($parameters['NoOfColumns'] >= 2) { |
722
|
|
|
$buildArray = true; |
723
|
|
|
} else { |
724
|
|
|
$msg = $this->lclMsgCmn('QueryResultExpectedAtLeast2ColsResultedOther'); |
|
|
|
|
725
|
|
|
$aReturn['customError'] = sprintf($msg, $parameters['NoOfColumns']); |
726
|
|
|
} |
727
|
|
|
break; |
728
|
|
|
case 'array_key_value': |
729
|
|
|
case 'array_key_value2': |
730
|
|
View Code Duplication |
case 'array_key2_value': |
|
|
|
|
731
|
|
|
if ($parameters['NoOfColumns'] == 2) { |
732
|
|
|
$buildArray = true; |
733
|
|
|
} else { |
734
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected2ColumnsResultedOther'); |
|
|
|
|
735
|
|
|
$aReturn['customError'] = sprintf($msg, $parameters['NoOfColumns']); |
736
|
|
|
} |
737
|
|
|
break; |
738
|
|
View Code Duplication |
case 'array_numbered': |
|
|
|
|
739
|
|
|
if ($parameters['NoOfColumns'] == 1) { |
740
|
|
|
$buildArray = true; |
741
|
|
|
} else { |
742
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected1ColumnResultedOther'); |
|
|
|
|
743
|
|
|
$aReturn['customError'] = sprintf($msg, $parameters['NoOfColumns']); |
744
|
|
|
} |
745
|
|
|
break; |
746
|
|
|
case 'array_pairs_key_value': |
747
|
|
|
if (($parameters['NoOfRows'] == 1) && ($parameters['NoOfColumns'] > 1)) { |
748
|
|
|
$buildArray = true; |
749
|
|
|
} else { |
750
|
|
|
$shorterLclString = 'i18n_MySQL_QueryResultExpected1RowManyColumnsResultedOther'; |
751
|
|
|
$msg = $this->lclMsgCmn($shorterLclString); |
|
|
|
|
752
|
|
|
$aReturn['customError'] = sprintf($msg, $parameters['NoOfRows'], $parameters['NoOfColumns']); |
753
|
|
|
} |
754
|
|
|
break; |
755
|
|
|
case 'full_array_key_numbered': |
756
|
|
|
case 'full_array_key_numbered_with_prefix': |
757
|
|
|
case 'full_array_key_numbered_with_record_number_prefix': |
758
|
|
|
if ($parameters['NoOfColumns'] == 0) { |
759
|
|
|
$aReturn['customError'] = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected1OrMoreRows0Resulted'); |
|
|
|
|
760
|
|
|
if (in_array($parameters['returnType'], [ |
761
|
|
|
'full_array_key_numbered_with_prefix', |
762
|
|
|
'full_array_key_numbered_with_record_number_prefix', |
763
|
|
|
])) { |
764
|
|
|
$aReturn['result'][$parameters['prefix']] = null; |
765
|
|
|
} |
766
|
|
|
} else { |
767
|
|
|
$buildArray = true; |
768
|
|
|
$counter2 = 0; |
769
|
|
|
} |
770
|
|
|
break; |
771
|
|
|
default: |
772
|
|
|
$aReturn['customError'] = $parameters['returnType'] . ' is not defined!'; |
773
|
|
|
break; |
774
|
|
|
} |
775
|
|
|
if ($buildArray) { |
776
|
|
|
for ($counter = 0; $counter < $parameters['NoOfRows']; $counter++) { |
777
|
|
|
$line = $parameters['QueryResult']->fetch_row(); |
778
|
|
|
switch ($parameters['returnType']) { |
779
|
|
View Code Duplication |
case 'array_first_key_rest_values': |
|
|
|
|
780
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
781
|
|
|
$columnCounter = 0; |
782
|
|
|
foreach ($finfo as $value) { |
783
|
|
|
if ($columnCounter != 0) { |
784
|
|
|
$aReturn['result'][$line[0]][$value->name] = $line[$columnCounter]; |
785
|
|
|
} |
786
|
|
|
$columnCounter++; |
787
|
|
|
} |
788
|
|
|
break; |
789
|
|
|
case 'array_key_value': |
790
|
|
|
$aReturn['result'][$line[0]] = $line[1]; |
791
|
|
|
break; |
792
|
|
|
case 'array_key_value2': |
793
|
|
|
$aReturn['result'][$line[0]][] = $line[1]; |
794
|
|
|
break; |
795
|
|
|
case 'array_key2_value': |
796
|
|
|
$aReturn['result'][$line[0] . '@' . $line[1]] = $line[1]; |
797
|
|
|
break; |
798
|
|
|
case 'array_numbered': |
799
|
|
|
$aReturn['result'][] = $line[0]; |
800
|
|
|
break; |
801
|
|
|
case 'array_pairs_key_value': |
802
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
803
|
|
|
$columnCounter = 0; |
804
|
|
|
foreach ($finfo as $value) { |
805
|
|
|
$aReturn['result'][$value->name] = $line[$columnCounter]; |
806
|
|
|
$columnCounter++; |
807
|
|
|
} |
808
|
|
|
break; |
809
|
|
View Code Duplication |
case 'full_array_key_numbered': |
|
|
|
|
810
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
811
|
|
|
$columnCounter = 0; |
812
|
|
|
foreach ($finfo as $value) { |
813
|
|
|
$aReturn['result'][$counter2][$value->name] = $line[$columnCounter]; |
|
|
|
|
814
|
|
|
$columnCounter++; |
815
|
|
|
} |
816
|
|
|
$counter2++; |
817
|
|
|
break; |
818
|
|
|
case 'full_array_key_numbered_with_record_number_prefix': |
819
|
|
|
$parameters['prefix'] = 'RecordNo'; |
820
|
|
|
// intentionally left open |
821
|
|
View Code Duplication |
case 'full_array_key_numbered_with_prefix': |
|
|
|
|
822
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
823
|
|
|
$columnCounter = 0; |
824
|
|
|
foreach ($finfo as $value) { |
825
|
|
|
$aReturn['result'][$parameters['prefix']][$counter2][$value->name] = $line[$columnCounter]; |
826
|
|
|
$columnCounter++; |
827
|
|
|
} |
828
|
|
|
$counter2++; |
829
|
|
|
break; |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
} |
833
|
|
|
return $aReturn; |
834
|
|
|
} |
835
|
|
|
} |
836
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.