Completed
Push — master ( 6a811e...c52182 )
by Hong
02:35
created

Statement::realExecute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 6
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\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 = is_int($name) ? ($name + 1) :
85
            ($name[0] === ':' ? $name : (':' . $name));
86
            if (false === $stmt->bindParam($param, $value, $type)) {
87
                return false;
88
            }
89
        }
90
        return true;
91
    }
92
}
93