Completed
Push — master ( 665550...56a36e )
by Daniel
02:22
created

MySQLiMultipleExecution   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMySQLgenericInformations() 0 7 2
A executeMultipleRepetitiveValues() 0 18 4
A getMySqlCurrentDatabase() 0 5 1
A setVariableTypeForMySqlStatements() 0 12 4
A setVariableTypeForMySqlStatementsMany() 0 8 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
    /**
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