Completed
Push — master ( f7f475...9b2539 )
by Daniel
02:18
created

MySQLiMultipleExecution::glueDbTb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
     * Glues Database and Table into 1 single string
104
     *
105
     * @param string $dbName
106
     * @param string $tbName
107
     * @return string
108
     */
109
    protected function glueDbTb($dbName, $tbName)
110
    {
111
        return '`' . $dbName . '`.`' . $tbName . '`';
112
    }
113
114
    /**
115
     * Manages features flag
116
     *
117
     * @param string $fieldName
118
     * @param array $features
119
     * @return string
120
     */
121
    protected function handleFeatures($fieldName, $features)
122
    {
123
        $rOly  = $this->handleFeaturesSingle($fieldName, $features, 'readonly');
124
        $rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled');
125
        $rNl   = [];
126
        if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) {
127
            $rNl = ['include_null'];
128
        }
129
        return array_merge([], $rOly, $rDbld, $rNl);
130
    }
131
132
    /**
133
     * Handles the features
134
     *
135
     * @param string $fieldName
136
     * @param array $features
137
     * @param string $featureKey
138
     * @return array
139
     */
140
    private function handleFeaturesSingle($fieldName, $features, $featureKey)
141
    {
142
        $fMap    = [
143
            'readonly' => ['readonly', 'class', 'input_readonly'],
144
            'disabled' => ['disabled']
145
        ];
146
        $aReturn = [];
147
        if (array_key_exists($featureKey, $features)) {
148
            if (array_key_exists($fieldName, $features[$featureKey])) {
149
                $aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0];
150
                if (count($fMap[$featureKey]) > 1) {
151
                    $aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2];
152
                }
153
            }
154
        }
155
        return $aReturn;
156
    }
157
158
    /**
159
     * Detects what kind of variable has been transmited
160
     * to return the identifier needed by MySQL statement preparing
161
     *
162
     * @param type $variabaleValue
163
     * @return string
164
     */
165
    protected function setVariableTypeForMySqlStatements($variabaleValue)
166
    {
167
        $sReturn = 'b';
168
        if (is_int($variabaleValue)) {
169
            $sReturn = 'i';
170
        } elseif (is_double($variabaleValue)) {
171
            $sReturn = 'd';
172
        } elseif (is_string($variabaleValue)) {
173
            $sReturn = 's';
174
        }
175
        return $sReturn;
176
    }
177
178
    protected function setVariableTypeForMySqlStatementsMany($variabales)
179
    {
180
        $types = [];
181
        foreach ($variabales as $value2) {
182
            $types[] = $this->setVariableTypeForMySqlStatements($value2);
183
        }
184
        return implode('', $types);
185
    }
186
}
187