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