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 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
|
|
|
* Adjust table name with proper sufix and prefix |
62
|
|
|
* |
63
|
|
|
* @param string $refTbl |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function fixTableSource($refTbl) |
67
|
|
|
{ |
68
|
|
|
$outS = []; |
69
|
|
|
if (substr($refTbl, 0, 1) !== '`') { |
70
|
|
|
$outS[] = '`'; |
71
|
|
|
} |
72
|
|
|
$psT = strpos($refTbl, '.`'); |
73
|
|
|
if ($psT !== false) { |
74
|
|
|
$refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT); |
75
|
|
|
} |
76
|
|
|
$outS[] = $refTbl; |
77
|
|
|
if (substr($refTbl, -1) !== '`') { |
78
|
|
|
$outS[] = '`'; |
79
|
|
|
} |
80
|
|
|
return implode('', $outS); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* returns the list of all MySQL generic informations |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function getMySQLgenericInformations() |
89
|
|
|
{ |
90
|
|
|
if (is_null($this->mySQLconnection)) { |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->mySQLconnection->server_version]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function getMySqlCurrentDatabase() |
97
|
|
|
{ |
98
|
|
|
$result = $this->mySQLconnection->query('SELECT DATABASE();'); |
99
|
|
|
return $result->fetch_row()[0]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Manages features flag |
104
|
|
|
* |
105
|
|
|
* @param string $fieldName |
106
|
|
|
* @param array $features |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function handleFeatures($fieldName, $features) |
110
|
|
|
{ |
111
|
|
|
$rOly = $this->handleFeaturesSingle($fieldName, $features, 'readonly'); |
112
|
|
|
$rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled'); |
113
|
|
|
$rNl = []; |
114
|
|
|
if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) { |
115
|
|
|
$rNl = ['include_null']; |
116
|
|
|
} |
117
|
|
|
return array_merge([], $rOly, $rDbld, $rNl); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handles the features |
122
|
|
|
* |
123
|
|
|
* @param string $fieldName |
124
|
|
|
* @param array $features |
125
|
|
|
* @param string $featureKey |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
private function handleFeaturesSingle($fieldName, $features, $featureKey) |
129
|
|
|
{ |
130
|
|
|
$fMap = [ |
131
|
|
|
'readonly' => ['readonly', 'class', 'input_readonly'], |
132
|
|
|
'disabled' => ['disabled'] |
133
|
|
|
]; |
134
|
|
|
$aReturn = []; |
135
|
|
|
if (array_key_exists($featureKey, $features)) { |
136
|
|
|
if (array_key_exists($fieldName, $features[$featureKey])) { |
137
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0]; |
138
|
|
|
if (count($fMap[$featureKey]) > 1) { |
139
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
return $aReturn; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Detects what kind of variable has been transmited |
148
|
|
|
* to return the identifier needed by MySQL statement preparing |
149
|
|
|
* |
150
|
|
|
* @param type $variabaleValue |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function setVariableTypeForMySqlStatements($variabaleValue) |
154
|
|
|
{ |
155
|
|
|
$sReturn = 'b'; |
156
|
|
|
if (is_int($variabaleValue)) { |
157
|
|
|
$sReturn = 'i'; |
158
|
|
|
} elseif (is_double($variabaleValue)) { |
159
|
|
|
$sReturn = 'd'; |
160
|
|
|
} elseif (is_string($variabaleValue)) { |
161
|
|
|
$sReturn = 's'; |
162
|
|
|
} |
163
|
|
|
return $sReturn; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function setVariableTypeForMySqlStatementsMany($variabales) |
167
|
|
|
{ |
168
|
|
|
$types = []; |
169
|
|
|
foreach ($variabales as $value2) { |
170
|
|
|
$types[] = $this->setVariableTypeForMySqlStatements($value2); |
171
|
|
|
} |
172
|
|
|
return implode('', $types); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|