Completed
Push — master ( 9b2539...51b667 )
by Daniel
02:30
created

MySQLiMultipleExecution::establishDefaultEnumSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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
     * Establishes the defaults for ENUM or SET field
62
     *
63
     * @param string $fldType
64
     * @return array
65
     */
66
    protected function establishDefaultEnumSet($fldType)
67
    {
68
        $dfltArray = [
69
            'enum' => ['additional' => ['size' => 1], 'suffix' => ''],
70
            'set'  => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'],
71
        ];
72
        return $dfltArray[$fldType];
73
    }
74
75
    /**
76
     * Adjust table name with proper sufix and prefix
77
     *
78
     * @param string $refTbl
79
     * @return string
80
     */
81
    protected function fixTableSource($refTbl)
82
    {
83
        $outS = [];
84
        if (substr($refTbl, 0, 1) !== '`') {
85
            $outS[] = '`';
86
        }
87
        $psT = strpos($refTbl, '.`');
88
        if ($psT !== false) {
89
            $refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT);
90
        }
91
        $outS[] = $refTbl;
92
        if (substr($refTbl, -1) !== '`') {
93
            $outS[] = '`';
94
        }
95
        return implode('', $outS);
96
    }
97
98
    /**
99
     * Creates a mask to differentiate between Mandatory and Optional fields
100
     *
101
     * @param array $details
102
     * @return string
103
     */
104
    protected function getFieldCompletionType($details)
105
    {
106
        $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']];
107
        if ($details['IS_NULLABLE'] == 'YES') {
108
            $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']];
109
        }
110
        return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']);
0 ignored issues
show
Bug introduced by
It seems like setStringIntoTag() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111
    }
112
113
    /**
114
     * returns the list of all MySQL generic informations
115
     *
116
     * @return array
117
     */
118
    protected function getMySQLgenericInformations()
119
    {
120
        if (is_null($this->mySQLconnection)) {
121
            return [];
122
        }
123
        return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->mySQLconnection->server_version];
124
    }
125
126
    protected function getMySqlCurrentDatabase()
127
    {
128
        $result = $this->mySQLconnection->query('SELECT DATABASE();');
129
        return $result->fetch_row()[0];
130
    }
131
132
    /**
133
     * Glues Database and Table into 1 single string
134
     *
135
     * @param string $dbName
136
     * @param string $tbName
137
     * @return string
138
     */
139
    protected function glueDbTb($dbName, $tbName)
140
    {
141
        return '`' . $dbName . '`.`' . $tbName . '`';
142
    }
143
144
    /**
145
     * Manages features flag
146
     *
147
     * @param string $fieldName
148
     * @param array $features
149
     * @return string
150
     */
151
    protected function handleFeatures($fieldName, $features)
152
    {
153
        $rOly  = $this->handleFeaturesSingle($fieldName, $features, 'readonly');
154
        $rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled');
155
        $rNl   = [];
156
        if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) {
157
            $rNl = ['include_null'];
158
        }
159
        return array_merge([], $rOly, $rDbld, $rNl);
160
    }
161
162
    /**
163
     * Handles the features
164
     *
165
     * @param string $fieldName
166
     * @param array $features
167
     * @param string $featureKey
168
     * @return array
169
     */
170
    private function handleFeaturesSingle($fieldName, $features, $featureKey)
171
    {
172
        $fMap    = [
173
            'readonly' => ['readonly', 'class', 'input_readonly'],
174
            'disabled' => ['disabled']
175
        ];
176
        $aReturn = [];
177
        if (array_key_exists($featureKey, $features)) {
178
            if (array_key_exists($fieldName, $features[$featureKey])) {
179
                $aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0];
180
                if (count($fMap[$featureKey]) > 1) {
181
                    $aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2];
182
                }
183
            }
184
        }
185
        return $aReturn;
186
    }
187
188
    /**
189
     * Detects what kind of variable has been transmited
190
     * to return the identifier needed by MySQL statement preparing
191
     *
192
     * @param type $variabaleValue
193
     * @return string
194
     */
195
    protected function setVariableTypeForMySqlStatements($variabaleValue)
196
    {
197
        $sReturn = 'b';
198
        if (is_int($variabaleValue)) {
199
            $sReturn = 'i';
200
        } elseif (is_double($variabaleValue)) {
201
            $sReturn = 'd';
202
        } elseif (is_string($variabaleValue)) {
203
            $sReturn = 's';
204
        }
205
        return $sReturn;
206
    }
207
208
    protected function setVariableTypeForMySqlStatementsMany($variabales)
209
    {
210
        $types = [];
211
        foreach ($variabales as $value2) {
212
            $types[] = $this->setVariableTypeForMySqlStatements($value2);
213
        }
214
        return implode('', $types);
215
    }
216
}
217