Completed
Push — master ( c52182...0c0ab1 )
by Hong
02:32
created

Statement   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
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
20
/**
21
 * Statement
22
 *
23
 * PDO driver statement
24
 *
25
 * @package Phossa2\Db
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     StatementAbstract
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
class Statement extends StatementAbstract
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected function realPrepare($link, /*# string */ $sql)
37
    {
38
        /* @var $link \PDO */
39
        return $link->prepare($sql);
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    protected function realExecute(array $parameters)/*# : bool */
46
    {
47
        /* @var $stmt \PDOStatement */
48
        $stmt = $this->prepared;
49
50
        // bind parameters
51
        if (!empty($parameters) &&
52
            !$this->bindParameters($stmt, $parameters)
53
        ) {
54
            return false;
55
        }
56
57
        // execute
58
        return $stmt->execute();
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    protected function realClose($stmt)
65
    {
66
        /* @var $stmt \PDOStatement */
67
        $stmt->closeCursor();
68
    }
69
70
    /**
71
     * bind parameters
72
     *
73
     * @param  \PDOStatement $stmt
74
     * @param  array $parameters
75
     * @return bool
76
     * @access protected
77
     */
78
    protected function bindParameters(
79
        \PDOStatement $stmt,
80
        array $parameters
81
    )/*# : bool */ {
82
        foreach ($parameters as $name => &$value) {
83
            $type  = Types::guessType($value);
84
            $param = $this->fixParam($name);
85
            if (false === $stmt->bindParam($param, $value, $type)) {
86
                return false;
87
            }
88
        }
89
        return true;
90
    }
91
92
    /**
93
     * Fix param name
94
     *
95
     * @param  mixed $name
96
     * @return string
97
     * @access protected
98
     */
99
    protected function fixParam($name)/*# : string */
100
    {
101
        return is_int($name) ?
102
            ($name + 1) :
103
            ($name[0] === ':' ? $name : (':' . $name));
104
    }
105
}
106