Completed
Push — master ( 0c0ab1...4bb828 )
by Hong
02:29
created

Statement   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A realPrepare() 0 5 1
A realExecute() 0 15 3
A realClose() 0 5 1
A bindParameters() 0 13 3
A fixParam() 0 6 3
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\Pdo;
16
17
use Phossa2\Db\Types;
18
use Phossa2\Db\Driver\StatementAbstract;
19
use Phossa2\Db\Interfaces\ResultInterface;
20
21
/**
22
 * Statement
23
 *
24
 * PDO 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)
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 \PDO */
51
        return $link->prepare($sql);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    protected function realExecute(array $parameters)/*# : bool */
58
    {
59
        /* @var $stmt \PDOStatement */
60
        $stmt = $this->prepared;
61
62
        // bind parameters
63
        if (!empty($parameters) &&
64
            !$this->bindParameters($stmt, $parameters)
65
        ) {
66
            return false;
67
        }
68
69
        // execute
70
        return $stmt->execute();
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function realClose($stmt)
77
    {
78
        /* @var $stmt \PDOStatement */
79
        $stmt->closeCursor();
80
    }
81
82
    /**
83
     * bind parameters
84
     *
85
     * @param  \PDOStatement $stmt
86
     * @param  array $parameters
87
     * @return bool
88
     * @access protected
89
     */
90
    protected function bindParameters(
91
        \PDOStatement $stmt,
92
        array $parameters
93
    )/*# : bool */ {
94
        foreach ($parameters as $name => &$value) {
95
            $type  = Types::guessType($value);
96
            $param = $this->fixParam($name);
97
            if (false === $stmt->bindParam($param, $value, $type)) {
98
                return false;
99
            }
100
        }
101
        return true;
102
    }
103
104
    /**
105
     * Fix param name
106
     *
107
     * @param  mixed $name
108
     * @return string
109
     * @access protected
110
     */
111
    protected function fixParam($name)/*# : string */
112
    {
113
        return is_int($name) ?
114
            ($name + 1) :
115
            ($name[0] === ':' ? $name : (':' . $name));
116
    }
117
}
118