Completed
Push — master ( 37f54e...a8ce98 )
by Hong
02:32
created

Statement::__construct()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Mysqli;
16
17
use Phossa2\Db\Types;
18
use Phossa2\Db\Driver\StatementAbstract;
19
use Phossa2\Db\Interfaces\ResultInterface;
20
21
/**
22
 * Statement
23
 *
24
 * Mysqli driver statement
25
 *
26
 * @package Phossa2\Db
27
 * @author  Hong Zhang <[email protected]>
28
 * @see     StatementAbstract
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
class Statement extends StatementAbstract
33
{
34
    /**
35
     * Constructor
36
     *
37
     * @param  ResultInterface $resultPrototype
38
     * @access public
39
     */
40
    public function __construct(ResultInterface $resultPrototype = null)
41
    {
42
        $this->result_prototype = $resultPrototype ?: new Result();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    protected function realPrepare($link, /*# string */ $sql)
49
    {
50
        /* @var $link \mysqli */
51
        return $link->prepare($sql);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    protected function realExecute(array $parameters)/*# : bool */
58
    {
59
        /** @var $stmt \mysqli_stmt */
60
        $stmt = $this->prepared;
61
62
        // bind parameters
63
        if (!empty($parameters) &&
64
            !$this->bindParameters($stmt, $parameters)
65
        ) {
66
            // bind failure
67
            return false;
68
        }
69
70
        $res = $stmt->execute();
71
72
        if ($stmt->result_metadata()) {
73
            $stmt->store_result();
74
        }
75
76
        return $res;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    protected function realClose($stmt)
83
    {
84
        /* @var $stmt \mysqli_stmt */
85
        //$stmt->close();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
    }
87
88
    /**
89
     * bind parameters
90
     *
91
     * @param  \mysqli_stmt $stmt
92
     * @param  array $parameters
93
     * @return bool
94
     * @access protected
95
     */
96
    protected function bindParameters(
97
        \mysqli_stmt $stmt,
98
        array $parameters
99
    )/*# : bool */ {
100
        $types = '';
101
        $args  = [];
102
        foreach ($parameters as $name => &$value) {
103
            $type = Types::guessType($value);
104
            switch ($type) {
105
                case Types::PARAM_INT:
106
                case Types::PARAM_BOOL:
107
                    $types .= 'i';
108
                    break;
109
                default:
110
                    $types .= 's';
111
                    break;
112
            }
113
            $args[] = &$value;
114
        }
115
        if (count($args)) {
116
            array_unshift($args, $types);
117
            return call_user_func_array([$stmt, 'bind_param'], $args);
118
        }
119
        return true;
120
    }
121
}
122