Completed
Push — master ( a7e144...4cf57b )
by Daniel
04:19
created

MySQLiMultiple::getMySqlCurrentDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 MySQLiMultiple
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
    protected function getMySqlCurrentDatabase()
61
    {
62
        return $this->mySQLconnection->query('SELECT DATABASE();')[0];
63
    }
64
65
    /**
66
     * Detects what kind of variable has been transmited
67
     * to return the identifier needed by MySQL statement preparing
68
     *
69
     * @param type $variabaleValue
70
     * @return string
71
     */
72
    protected function setVariableTypeForMySqlStatements($variabaleValue)
73
    {
74
        $sReturn = 'b';
75
        if (is_int($variabaleValue)) {
76
            $sReturn = 'i';
77
        } elseif (is_double($variabaleValue)) {
78
            $sReturn = 'd';
79
        } elseif (is_string($variabaleValue)) {
80
            $sReturn = 's';
81
        }
82
        return $sReturn;
83
    }
84
85
    protected function setVariableTypeForMySqlStatementsMany($variabales)
86
    {
87
        $types = [];
88
        foreach ($variabales as $value2) {
89
            $types[] = $this->setVariableTypeForMySqlStatements($value2);
90
        }
91
        return implode('', $types);
92
    }
93
}
94