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 MySQLiByDanielGPnumbers, |
40
|
|
|
MySQLiMultipleExecution; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Intiates connection to MySQL |
44
|
|
|
* |
45
|
|
|
* @param array $mySQLconfig |
46
|
|
|
* |
47
|
|
|
* $mySQLconfig = [ |
48
|
|
|
* 'host' => MYSQL_HOST, |
49
|
|
|
* 'port' => MYSQL_PORT, |
50
|
|
|
* 'username' => MYSQL_USERNAME, |
51
|
|
|
* 'password' => MYSQL_PASSWORD, |
52
|
|
|
* 'database' => MYSQL_DATABASE, |
53
|
|
|
* ]; |
54
|
|
|
*/ |
55
|
|
|
protected function connectToMySql($mySQLconfig) |
56
|
|
|
{ |
57
|
|
|
if (is_null($this->mySQLconnection)) { |
58
|
|
|
extract($mySQLconfig); |
59
|
|
|
$this->mySQLconnection = new \mysqli($host, $username, $password, $database, $port); |
60
|
|
|
if (is_null($this->mySQLconnection->connect_error)) { |
61
|
|
|
return ''; |
62
|
|
|
} |
63
|
|
|
$this->mySQLconnection = null; |
64
|
|
|
$erNo = $this->mySQLconnection->connect_errno; |
65
|
|
|
$erMsg = $this->mySQLconnection->connect_error; |
66
|
|
|
$msg = $this->lclMsgCmn('i18n_Feedback_ConnectionError'); |
67
|
|
|
return sprintf($msg, $erNo, $erMsg, $host, $port, $username, $database); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Provides a detection if given Query does contain a Parameter |
73
|
|
|
* that may require statement processing later on |
74
|
|
|
* |
75
|
|
|
* @param string $sQuery |
76
|
|
|
* @param string $paramIdentifier |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
|
|
protected function getMySQLqueryWithParameterIdentifier($sQuery, $paramIdentifier) |
80
|
|
|
{ |
81
|
|
|
$sReturn = true; |
82
|
|
|
if (strpos($sQuery, $paramIdentifier) === false) { |
83
|
|
|
$sReturn = false; |
84
|
|
|
} |
85
|
|
|
return $sReturn; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Transmit Query to MySQL server and get results back |
90
|
|
|
* |
91
|
|
|
* @param string $sQuery |
92
|
|
|
* @param string $sReturnType |
93
|
|
|
* @param array $ftrs |
94
|
|
|
* @return boolean|array|string |
95
|
|
|
*/ |
96
|
|
|
protected function setMySQLquery2Server($sQuery, $sReturnType = null, $ftrs = null) |
97
|
|
|
{ |
98
|
|
|
if (is_null($sReturnType)) { |
99
|
|
|
$this->mySQLconnection->query(html_entity_decode($sQuery)); |
100
|
|
|
return ''; |
101
|
|
|
} elseif (is_null($this->mySQLconnection)) { |
102
|
|
|
return ['customError' => $this->lclMsgCmn('i18n_MySQL_ConnectionNotExisting'), 'result' => null]; |
103
|
|
|
} |
104
|
|
|
$result = $this->mySQLconnection->query(html_entity_decode($sQuery)); |
105
|
|
|
if ($result) { |
106
|
|
|
return $this->setMySQLquery2ServerConnected(['Result' => $result, 'RType' => $sReturnType, 'F' => $ftrs]); |
107
|
|
|
} |
108
|
|
|
$erM = [$this->mySQLconnection->errno, $this->mySQLconnection->error]; |
109
|
|
|
$cErr = sprintf($this->lclMsgCmn('i18n_MySQL_QueryError'), $erM[0], $erM[1]); |
110
|
|
|
return ['customError' => $cErr, 'result' => null]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Turns a raw query result into various structures |
115
|
|
|
* based on different predefined $parameters['returnType'] value |
116
|
|
|
* |
117
|
|
|
* @param array $parameters |
118
|
|
|
* @return array as ['customError' => '...', 'result' => '...'] |
119
|
|
|
*/ |
120
|
|
|
private function setMySQLquery2ServerByPattern($parameters) |
121
|
|
|
{ |
122
|
|
|
$aReturn = $parameters['return']; |
123
|
|
|
$vld = $this->setMySQLqueryValidateInputs($parameters); |
124
|
|
|
if ($vld[1] !== '') { |
125
|
|
|
return ['customError' => $vld[1], 'result' => '']; |
126
|
|
|
} elseif ($parameters['returnType'] == 'value') { |
127
|
|
|
return ['customError' => $vld[1], 'result' => $parameters['QueryResult']->fetch_row()[0]]; |
128
|
|
|
} |
129
|
|
|
$counter2 = 0; |
130
|
|
|
for ($counter = 0; $counter < $parameters['NoOfRows']; $counter++) { |
131
|
|
|
$line = $parameters['QueryResult']->fetch_row(); |
132
|
|
|
$this->setMySQLquery2ServerByPatternLine($parameters, $line, $counter, $counter2, $aReturn); |
133
|
|
|
$counter2++; |
134
|
|
|
} |
135
|
|
|
return ['customError' => '', 'result' => $aReturn['result']]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function setMySQLquery2ServerByPatternKey($parameters, $line, $counter) |
139
|
|
|
{ |
140
|
|
|
switch ($parameters['returnType']) { |
141
|
|
|
case 'array_key_value': |
142
|
|
|
return [$line[0], $line[1]]; |
143
|
|
|
// intentionally left open |
144
|
|
|
case 'array_key2_value': |
145
|
|
|
return [$line[0] . '@' . $line[1], $line[1]]; |
146
|
|
|
// intentionally left open |
147
|
|
|
case 'array_numbered': |
148
|
|
|
return [$counter, $line[0]]; |
149
|
|
|
// intentionally left open |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function setMySQLquery2ServerByPatternLine($parameters, $line, $counter, $counter2, &$aReturn) |
154
|
|
|
{ |
155
|
|
|
if (in_array($parameters['returnType'], ['array_key_value', 'array_key2_value', 'array_numbered'])) { |
156
|
|
|
$rslt = $this->setMySQLquery2ServerByPatternKey($parameters, $line, $counter); |
157
|
|
|
$aReturn['result'][$rslt[0]] = $rslt[1]; |
158
|
|
|
} elseif ($parameters['returnType'] == 'array_key_value2') { |
159
|
|
|
$aReturn['result'][$line[0]][] = $line[1]; |
160
|
|
|
} else { |
161
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
162
|
|
|
$this->setMySQLquery2ServerByPatternLineAdvanced($parameters, $finfo, $line, $counter2, $aReturn); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function setMySQLquery2ServerByPatternLineAdvanced($parameters, $finfo, $line, $counter2, &$aReturn) |
167
|
|
|
{ |
168
|
|
|
foreach ($finfo as $columnCounter => $value) { |
169
|
|
|
switch ($parameters['returnType']) { |
170
|
|
|
case 'array_first_key_rest_values': |
171
|
|
|
if ($columnCounter !== 0) { |
172
|
|
|
$aReturn['result'][$line[0]][$value->name] = $line[$columnCounter]; |
173
|
|
|
} |
174
|
|
|
break; |
175
|
|
|
case 'array_pairs_key_value': |
176
|
|
|
$aReturn['result'][$value->name] = $line[$columnCounter]; |
177
|
|
|
break; |
178
|
|
|
case 'full_array_key_numbered': |
179
|
|
|
$aReturn['result'][$counter2][$value->name] = $line[$columnCounter]; |
180
|
|
|
break; |
181
|
|
|
case 'full_array_key_numbered_with_record_number_prefix': |
182
|
|
|
$parameters['prefix'] = 'RecordNo'; |
183
|
|
|
// intentionally left open |
184
|
|
|
case 'full_array_key_numbered_with_prefix': |
185
|
|
|
$aReturn['result'][$parameters['prefix']][$counter2][$value->name] = $line[$columnCounter]; |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function setMySQLquery2ServerConnected($inArray) |
192
|
|
|
{ |
193
|
|
|
if ($inArray['RType'] == 'id') { |
194
|
|
|
return ['customError' => '', 'result' => $this->mySQLconnection->insert_id]; |
195
|
|
|
} elseif ($inArray['RType'] == 'lines') { |
196
|
|
|
return ['result' => $inArray['Result']->num_rows, 'customError' => '']; |
197
|
|
|
} |
198
|
|
|
$parameters = [ |
199
|
|
|
'NoOfColumns' => $inArray['Result']->field_count, |
200
|
|
|
'NoOfRows' => $inArray['Result']->num_rows, |
201
|
|
|
'QueryResult' => $inArray['Result'], |
202
|
|
|
'returnType' => $inArray['RType'], |
203
|
|
|
'return' => ['customError' => '', 'result' => null] |
204
|
|
|
]; |
205
|
|
|
if (substr($inArray['RType'], -6) == 'prefix') { |
206
|
|
|
$parameters['prefix'] = $inArray['F']['prefix']; |
207
|
|
|
} |
208
|
|
|
return $this->setMySQLquery2ServerByPattern($parameters); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|