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
|
|
|
* Returns a Date field 2 use in a form |
73
|
|
|
* |
74
|
|
|
* @param array $value |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getFieldOutputDate($value) |
78
|
|
|
{ |
79
|
|
|
$defaultValue = $this->getFieldValue($value); |
80
|
|
|
if (is_null($defaultValue)) { |
81
|
|
|
$defaultValue = date('Y-m-d'); |
82
|
|
|
} |
83
|
|
|
$inA = [ |
84
|
|
|
'type' => 'text', |
85
|
|
|
'name' => $value['Field'], |
86
|
|
|
'id' => $value['Field'], |
87
|
|
|
'value' => $defaultValue, |
88
|
|
|
'size' => 10, |
89
|
|
|
'maxlength' => 10, |
90
|
|
|
'onfocus' => implode('', [ |
91
|
|
|
'javascript:NewCssCal(\'' . $value['Field'], |
92
|
|
|
'\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);', |
93
|
|
|
]), |
94
|
|
|
]; |
95
|
|
|
return $this->setStringIntoShortTag('input', $inA) . $this->setCalendarControl($value['Field']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Provides a detection if given Query does contain a Parameter |
100
|
|
|
* that may require statement processing later on |
101
|
|
|
* |
102
|
|
|
* @param string $sQuery |
103
|
|
|
* @param string $paramIdentifier |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
protected function getMySQLqueryWithParameterIdentifier($sQuery, $paramIdentifier) |
107
|
|
|
{ |
108
|
|
|
$sReturn = true; |
109
|
|
|
if (strpos($sQuery, $paramIdentifier) === false) { |
110
|
|
|
$sReturn = false; |
111
|
|
|
} |
112
|
|
|
return $sReturn; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Transmit Query to MySQL server and get results back |
117
|
|
|
* |
118
|
|
|
* @param string $sQuery |
119
|
|
|
* @param string $sReturnType |
120
|
|
|
* @param array $ftrs |
121
|
|
|
* @return boolean|array|string |
122
|
|
|
*/ |
123
|
|
|
protected function setMySQLquery2Server($sQuery, $sReturnType = null, $ftrs = null) |
124
|
|
|
{ |
125
|
|
|
if (is_null($sReturnType)) { |
126
|
|
|
$this->mySQLconnection->query(html_entity_decode($sQuery)); |
127
|
|
|
return ''; |
128
|
|
|
} elseif (is_null($this->mySQLconnection)) { |
129
|
|
|
return ['customError' => $this->lclMsgCmn('i18n_MySQL_ConnectionNotExisting'), 'result' => null]; |
130
|
|
|
} |
131
|
|
|
$result = $this->mySQLconnection->query(html_entity_decode($sQuery)); |
132
|
|
|
if ($result) { |
133
|
|
|
return $this->setMySQLquery2ServerConnected(['Result' => $result, 'RType' => $sReturnType, 'F' => $ftrs]); |
134
|
|
|
} |
135
|
|
|
$erM = [$this->mySQLconnection->errno, $this->mySQLconnection->error]; |
136
|
|
|
$cErr = sprintf($this->lclMsgCmn('i18n_MySQL_QueryError'), $erM[0], $erM[1]); |
137
|
|
|
return ['customError' => $cErr, 'result' => null]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Turns a raw query result into various structures |
142
|
|
|
* based on different predefined $parameters['returnType'] value |
143
|
|
|
* |
144
|
|
|
* @param array $parameters |
145
|
|
|
* @return array as ['customError' => '...', 'result' => '...'] |
146
|
|
|
*/ |
147
|
|
|
private function setMySQLquery2ServerByPattern($parameters) |
148
|
|
|
{ |
149
|
|
|
$aReturn = $parameters['return']; |
150
|
|
|
$vld = $this->setMySQLqueryValidateInputs($parameters); |
151
|
|
|
if ($vld[1] !== '') { |
152
|
|
|
return ['customError' => $vld[1], 'result' => '']; |
153
|
|
|
} elseif ($parameters['returnType'] == 'value') { |
154
|
|
|
return ['customError' => $vld[1], 'result' => $parameters['QueryResult']->fetch_row()[0]]; |
155
|
|
|
} |
156
|
|
|
$counter2 = 0; |
157
|
|
|
for ($counter = 0; $counter < $parameters['NoOfRows']; $counter++) { |
158
|
|
|
$line = $parameters['QueryResult']->fetch_row(); |
159
|
|
|
$this->setMySQLquery2ServerByPatternLine($parameters, $line, $counter, $counter2, $aReturn); |
160
|
|
|
$counter2++; |
161
|
|
|
} |
162
|
|
|
return ['customError' => '', 'result' => $aReturn['result']]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function setMySQLquery2ServerByPatternKey($parameters, $line, $counter) |
166
|
|
|
{ |
167
|
|
|
switch ($parameters['returnType']) { |
168
|
|
|
case 'array_key_value': |
169
|
|
|
return [$line[0], $line[1]]; |
170
|
|
|
// intentionally left open |
171
|
|
|
case 'array_key2_value': |
172
|
|
|
return [$line[0] . '@' . $line[1], $line[1]]; |
173
|
|
|
// intentionally left open |
174
|
|
|
case 'array_numbered': |
175
|
|
|
return [$counter, $line[0]]; |
176
|
|
|
// intentionally left open |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function setMySQLquery2ServerByPatternLine($parameters, $line, $counter, $counter2, &$aReturn) |
181
|
|
|
{ |
182
|
|
|
if (in_array($parameters['returnType'], ['array_key_value', 'array_key2_value', 'array_numbered'])) { |
183
|
|
|
$rslt = $this->setMySQLquery2ServerByPatternKey($parameters, $line, $counter); |
184
|
|
|
$aReturn['result'][$rslt[0]] = $rslt[1]; |
185
|
|
|
} elseif ($parameters['returnType'] == 'array_key_value2') { |
186
|
|
|
$aReturn['result'][$line[0]][] = $line[1]; |
187
|
|
|
} else { |
188
|
|
|
$finfo = $parameters['QueryResult']->fetch_fields(); |
189
|
|
|
$this->setMySQLquery2ServerByPatternLineAdvanced($parameters, $finfo, $line, $counter2, $aReturn); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function setMySQLquery2ServerByPatternLineAdvanced($parameters, $finfo, $line, $counter2, &$aReturn) |
194
|
|
|
{ |
195
|
|
|
foreach ($finfo as $columnCounter => $value) { |
196
|
|
|
switch ($parameters['returnType']) { |
197
|
|
|
case 'array_first_key_rest_values': |
198
|
|
|
if ($columnCounter !== 0) { |
199
|
|
|
$aReturn['result'][$line[0]][$value->name] = $line[$columnCounter]; |
200
|
|
|
} |
201
|
|
|
break; |
202
|
|
|
case 'array_pairs_key_value': |
203
|
|
|
$aReturn['result'][$value->name] = $line[$columnCounter]; |
204
|
|
|
break; |
205
|
|
|
case 'full_array_key_numbered': |
206
|
|
|
$aReturn['result'][$counter2][$value->name] = $line[$columnCounter]; |
207
|
|
|
break; |
208
|
|
|
case 'full_array_key_numbered_with_record_number_prefix': |
209
|
|
|
$parameters['prefix'] = 'RecordNo'; |
210
|
|
|
// intentionally left open |
211
|
|
|
case 'full_array_key_numbered_with_prefix': |
212
|
|
|
$aReturn['result'][$parameters['prefix']][$counter2][$value->name] = $line[$columnCounter]; |
213
|
|
|
break; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function setMySQLquery2ServerConnected($inArray) |
219
|
|
|
{ |
220
|
|
|
if ($inArray['RType'] == 'id') { |
221
|
|
|
return ['customError' => '', 'result' => $this->mySQLconnection->insert_id]; |
222
|
|
|
} elseif ($inArray['RType'] == 'lines') { |
223
|
|
|
return ['result' => $inArray['Result']->num_rows, 'customError' => '']; |
224
|
|
|
} |
225
|
|
|
$parameters = [ |
226
|
|
|
'NoOfColumns' => $inArray['Result']->field_count, |
227
|
|
|
'NoOfRows' => $inArray['Result']->num_rows, |
228
|
|
|
'QueryResult' => $inArray['Result'], |
229
|
|
|
'returnType' => $inArray['RType'], |
230
|
|
|
'return' => ['customError' => '', 'result' => null] |
231
|
|
|
]; |
232
|
|
|
if (substr($inArray['RType'], -6) == 'prefix') { |
233
|
|
|
$parameters['prefix'] = $inArray['F']['prefix']; |
234
|
|
|
} |
235
|
|
|
return $this->setMySQLquery2ServerByPattern($parameters); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|