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
|
|
|
/** |
42
|
|
|
* returns the list of all MySQL generic informations |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
protected function getMySQLgenericInformations() |
47
|
|
|
{ |
48
|
|
|
if (is_null($this->mySQLconnection)) { |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->mySQLconnection->server_version]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function executeMultipleRepetitiveValues($qry, $prmtrs) |
55
|
|
|
{ |
56
|
|
|
$stmt = $this->mySQLconnection->stmt_init(); |
57
|
|
|
if ($stmt->prepare($qry)) { |
58
|
|
|
foreach ($prmtrs as $vParams) { |
59
|
|
|
$paramType = $this->setVariableTypeForMySqlStatementsMany($vParams); |
60
|
|
|
$aParams = []; |
61
|
|
|
$aParams[] = &$paramType; |
62
|
|
|
for ($counter = 0; $counter < $stmt->param_count; $counter++) { |
63
|
|
|
$aParams[] = &$vParams[$counter]; |
64
|
|
|
} |
65
|
|
|
call_user_func_array([$stmt, 'bind_param'], $aParams); |
66
|
|
|
$stmt->execute(); |
67
|
|
|
} |
68
|
|
|
$stmt->close(); |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getMySqlCurrentDatabase() |
74
|
|
|
{ |
75
|
|
|
$result = $this->mySQLconnection->query('SELECT DATABASE();'); |
76
|
|
|
return $result->fetch_row()[0]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Detects what kind of variable has been transmited |
81
|
|
|
* to return the identifier needed by MySQL statement preparing |
82
|
|
|
* |
83
|
|
|
* @param type $variabaleValue |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function setVariableTypeForMySqlStatements($variabaleValue) |
87
|
|
|
{ |
88
|
|
|
$sReturn = 'b'; |
89
|
|
|
if (is_int($variabaleValue)) { |
90
|
|
|
$sReturn = 'i'; |
91
|
|
|
} elseif (is_double($variabaleValue)) { |
92
|
|
|
$sReturn = 'd'; |
93
|
|
|
} elseif (is_string($variabaleValue)) { |
94
|
|
|
$sReturn = 's'; |
95
|
|
|
} |
96
|
|
|
return $sReturn; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function setVariableTypeForMySqlStatementsMany($variabales) |
100
|
|
|
{ |
101
|
|
|
$types = []; |
102
|
|
|
foreach ($variabales as $value2) { |
103
|
|
|
$types[] = $this->setVariableTypeForMySqlStatements($value2); |
104
|
|
|
} |
105
|
|
|
return implode('', $types); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|