|
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 MySQLiMultipleExecution |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
protected $mySQLconnection = null; |
|
40
|
|
|
|
|
41
|
|
|
protected function executeMultipleRepetitiveValues($qry, $prmtrs) |
|
42
|
|
|
{ |
|
43
|
|
|
$stmt = $this->mySQLconnection->stmt_init(); |
|
44
|
|
|
if ($stmt->prepare($qry)) { |
|
45
|
|
|
foreach ($prmtrs as $vParams) { |
|
46
|
|
|
$paramType = $this->setVariableTypeForMySqlStatementsMany($vParams); |
|
47
|
|
|
$aParams = []; |
|
48
|
|
|
$aParams[] = &$paramType; |
|
49
|
|
|
for ($counter = 0; $counter < $stmt->param_count; $counter++) { |
|
50
|
|
|
$aParams[] = &$vParams[$counter]; |
|
51
|
|
|
} |
|
52
|
|
|
call_user_func_array([$stmt, 'bind_param'], $aParams); |
|
53
|
|
|
$stmt->execute(); |
|
54
|
|
|
} |
|
55
|
|
|
$stmt->close(); |
|
56
|
|
|
return ''; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Establishes the defaults for ENUM or SET field |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $fldType |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function establishDefaultEnumSet($fldType) |
|
67
|
|
|
{ |
|
68
|
|
|
$dfltArray = [ |
|
69
|
|
|
'enum' => ['additional' => ['size' => 1], 'suffix' => ''], |
|
70
|
|
|
'set' => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'], |
|
71
|
|
|
]; |
|
72
|
|
|
return $dfltArray[$fldType]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Adjust table name with proper sufix and prefix |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $refTbl |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function fixTableSource($refTbl) |
|
82
|
|
|
{ |
|
83
|
|
|
$outS = []; |
|
84
|
|
|
if (substr($refTbl, 0, 1) !== '`') { |
|
85
|
|
|
$outS[] = '`'; |
|
86
|
|
|
} |
|
87
|
|
|
$psT = strpos($refTbl, '.`'); |
|
88
|
|
|
if ($psT !== false) { |
|
89
|
|
|
$refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT); |
|
90
|
|
|
} |
|
91
|
|
|
$outS[] = $refTbl; |
|
92
|
|
|
if (substr($refTbl, -1) !== '`') { |
|
93
|
|
|
$outS[] = '`'; |
|
94
|
|
|
} |
|
95
|
|
|
return implode('', $outS); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* returns the list of all MySQL generic information |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getMySQLgenericInformations() |
|
104
|
|
|
{ |
|
105
|
|
|
if (is_null($this->mySQLconnection)) { |
|
106
|
|
|
return []; |
|
107
|
|
|
} |
|
108
|
|
|
return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->handleMySqlVersionConsistenly()]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function getMySqlCurrentDatabase() |
|
112
|
|
|
{ |
|
113
|
|
|
$result = $this->mySQLconnection->query('SELECT DATABASE();'); |
|
114
|
|
|
return $result->fetch_row()[0]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Glues Database and Table into 1 single string |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $dbName |
|
121
|
|
|
* @param string $tbName |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function glueDbTb($dbName, $tbName) |
|
125
|
|
|
{ |
|
126
|
|
|
return '`' . $dbName . '`.`' . $tbName . '`'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Manages features flag |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $fieldName |
|
133
|
|
|
* @param array $features |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function handleFeatures($fieldName, $features) |
|
137
|
|
|
{ |
|
138
|
|
|
$rOly = $this->handleFeaturesSingle($fieldName, $features, 'readonly'); |
|
139
|
|
|
$rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled'); |
|
140
|
|
|
$rNl = []; |
|
141
|
|
|
if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) { |
|
142
|
|
|
$rNl = ['include_null']; |
|
143
|
|
|
} |
|
144
|
|
|
return array_merge([], $rOly, $rDbld, $rNl); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Handles the features |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $fieldName |
|
151
|
|
|
* @param array $features |
|
152
|
|
|
* @param string $featureKey |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
private function handleFeaturesSingle($fieldName, $features, $featureKey) |
|
156
|
|
|
{ |
|
157
|
|
|
$fMap = [ |
|
158
|
|
|
'readonly' => ['readonly', 'class', 'input_readonly'], |
|
159
|
|
|
'disabled' => ['disabled'] |
|
160
|
|
|
]; |
|
161
|
|
|
$aReturn = []; |
|
162
|
|
|
if (array_key_exists($featureKey, $features)) { |
|
163
|
|
|
if (array_key_exists($fieldName, $features[$featureKey])) { |
|
164
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0]; |
|
165
|
|
|
if (count($fMap[$featureKey]) > 1) { |
|
166
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2]; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
return $aReturn; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Ensures a consistent output of version for MySQL as well as MariaDB |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
private function handleMySqlVersionConsistenly() |
|
179
|
|
|
{ |
|
180
|
|
|
if (substr($this->mySQLconnection->server_info, -7) === 'MariaDB') { |
|
181
|
|
|
$strVersionParts = explode('.', explode('-', $this->mySQLconnection->server_info)[1]); |
|
182
|
|
|
return $this->getTwoDecimalsNumber($strVersionParts[0]) . $this->getTwoDecimalsNumber($strVersionParts[1]) |
|
|
|
|
|
|
183
|
|
|
. $this->getTwoDecimalsNumber($strVersionParts[2]); |
|
184
|
|
|
} |
|
185
|
|
|
return $this->mySQLconnection->server_version; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* |
|
190
|
|
|
* @param type $inNumber |
|
|
|
|
|
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
private function getTwoDecimalsNumber($inNumber) |
|
194
|
|
|
{ |
|
195
|
|
|
if ($inNumber < 10) { |
|
196
|
|
|
return '0' . $inNumber; |
|
197
|
|
|
} |
|
198
|
|
|
return $inNumber; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Detects what kind of variable has been transmitted |
|
203
|
|
|
* to return the identifier needed by MySQL statement preparing |
|
204
|
|
|
* |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function setVariableTypeForMySqlStatements($variabaleValue) |
|
208
|
|
|
{ |
|
209
|
|
|
$sReturn = 'b'; |
|
210
|
|
|
if (is_int($variabaleValue)) { |
|
211
|
|
|
$sReturn = 'i'; |
|
212
|
|
|
} elseif (is_double($variabaleValue)) { |
|
213
|
|
|
$sReturn = 'd'; |
|
214
|
|
|
} elseif (is_string($variabaleValue)) { |
|
215
|
|
|
$sReturn = 's'; |
|
216
|
|
|
} |
|
217
|
|
|
return $sReturn; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
protected function setVariableTypeForMySqlStatementsMany($variabales) |
|
221
|
|
|
{ |
|
222
|
|
|
$types = []; |
|
223
|
|
|
foreach ($variabales as $value2) { |
|
224
|
|
|
$types[] = $this->setVariableTypeForMySqlStatements($value2); |
|
225
|
|
|
} |
|
226
|
|
|
return implode('', $types); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|