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
|
|
|
* Queries for the MySQL module |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiByDanielGPqueriesBasic |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
private function sCleanParameters(&$parameters) |
40
|
|
|
{ |
41
|
|
|
if (is_array($parameters)) { |
42
|
|
|
$tmpArray = []; |
43
|
|
|
foreach ($parameters as &$value) { |
44
|
|
|
$tmpArray[] = filter_var($value, FILTER_SANITIZE_STRING); |
45
|
|
|
} |
46
|
|
|
$parameters = $tmpArray; |
47
|
|
|
} else { |
48
|
|
|
$parameters = filter_var($parameters, FILTER_SANITIZE_STRING); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function sQueryGenericSelectKeyValue($parameters) |
53
|
|
|
{ |
54
|
|
|
$this->sCleanParameters($parameters); |
55
|
|
|
return implode(' ', [ |
56
|
|
|
'SELECT', |
57
|
|
|
$parameters[0] . ', ' . $parameters[1], |
58
|
|
|
'FROM', |
59
|
|
|
$parameters[2], |
60
|
|
|
'GROUP BY', |
61
|
|
|
$parameters[0], |
62
|
|
|
'ORDER BY', |
63
|
|
|
$parameters[1] . ';' |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Query to list Databases |
69
|
|
|
* |
70
|
|
|
* @param boolean $excludeSystemDbs |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function sQueryMySqlActiveDatabases($excludeSystemDbs = true) |
74
|
|
|
{ |
75
|
|
|
$sDBs = 'WHERE ' |
76
|
|
|
. '`SCHEMA_NAME` NOT IN ("' |
77
|
|
|
. implode('", "', ['information_schema', 'mysql', 'performance_schema', 'sys']) . '") '; |
78
|
|
|
return 'SELECT ' |
79
|
|
|
. '`SCHEMA_NAME` As `Db`, `DEFAULT_CHARACTER_SET_NAME` AS `DbCharset`, ' |
80
|
|
|
. '`DEFAULT_COLLATION_NAME` AS `DbCollation` ' |
81
|
|
|
. 'FROM `information_schema`.`SCHEMATA` ' |
82
|
|
|
. ($excludeSystemDbs ? $sDBs : '') |
83
|
|
|
. 'GROUP BY `SCHEMA_NAME`;'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Query to list MySQL engines |
88
|
|
|
* |
89
|
|
|
* @param boolean $onlyActiveOnes |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function sQueryMySqlActiveEngines($onlyActiveOnes = true) |
93
|
|
|
{ |
94
|
|
|
return 'SELECT ' |
95
|
|
|
. '`ENGINE` AS `Engine`, `SUPPORT` AS `Support`, `COMMENT` AS `Comment` ' |
96
|
|
|
. 'FROM `information_schema`.`ENGINES` ' |
97
|
|
|
. ($onlyActiveOnes ? 'WHERE (`SUPPORT` IN ("DEFAULT", "YES")) ' : '') |
98
|
|
|
. 'GROUP BY `ENGINE`;'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Query to list Global Variables |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function sQueryMySqlGlobalVariables() |
107
|
|
|
{ |
108
|
|
|
return 'SHOW GLOBAL VARIABLES;'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The MySQL server time |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function sQueryMySqlServerTime() |
117
|
|
|
{ |
118
|
|
|
return 'SELECT NOW();'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get all the row details from a table based on given filter |
123
|
|
|
* |
124
|
|
|
* @param array $parameters |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function sQueryRowsFromTable($parameters) |
128
|
|
|
{ |
129
|
|
|
$this->sCleanParameters($parameters); |
130
|
|
|
return 'SELECT * ' |
131
|
|
|
. 'FROM `' . $parameters[0] . '` ' |
132
|
|
|
. 'WHERE ' . $parameters[1] . ';'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function sQueryToDeleteSingleIdentifier($parameters) |
136
|
|
|
{ |
137
|
|
|
$this->sCleanParameters($parameters); |
138
|
|
|
return 'DELETE ' |
139
|
|
|
. 'FROM `' . $parameters[0] . '` ' |
140
|
|
|
. 'WHERE `' . $parameters[1] . '` = "' . $parameters[2] . '";'; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|