Completed
Push — master ( f707f5...a08928 )
by Rasmus
02:27
created

PreparedPDOStatement::getRowsAffected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace mindplay\sql\framework\pdo;
4
5
use InvalidArgumentException;
6
use mindplay\sql\framework\PreparedStatement;
7
use mindplay\sql\model\Driver;
8
use PDO;
9
use PDOStatement;
10
11
/**
12
 * This class implements a Prepared Statement adapter for PDO Statements.
13
 */
14
class PreparedPDOStatement implements PreparedStatement
15
{
16
    /**
17
     * @var PDOStatement
18
     */
19
    private $handle;
20
21
    /**
22
     * @var Driver
23
     */
24
    private $driver;
25
26
    /**
27
     * @var array
28
     */
29
    private $params = [];
30
31
    /**
32
     * @var bool
33
     */
34
    private $executed = false;
35
36
    /**
37
     * @param PDOStatement $handle
38
     * @param Driver       $driver
39
     */
40 1
    public function __construct(PDOStatement $handle, Driver $driver)
41
    {
42 1
        $this->handle = $handle;
43 1
        $this->driver = $driver;
44 1
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 1
    public function bind($name, $value)
50
    {
51 1
        static $PDO_TYPE = [
52
            'integer' => PDO::PARAM_INT,
53
            'double'  => PDO::PARAM_STR, // bind as string, since there's no float type in PDO
54
            'string'  => PDO::PARAM_STR,
55
            'boolean' => PDO::PARAM_BOOL,
56
            'NULL'    => PDO::PARAM_NULL,
57
        ];
58
59 1
        $value_type = gettype($value);
60
61 1
        if (isset($PDO_TYPE[$value_type])) {
62 1
            $this->handle->bindValue($name, $value, $PDO_TYPE[$value_type]);
63
64 1
            $this->params[$name] = $value;
65
        } else {
66 1
            throw new InvalidArgumentException("unexpected value type: {$value_type}");
67
        }
68 1
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 1
    public function execute()
74
    {
75 1
        if (@$this->handle->execute()) {
76 1
            $this->executed = true;
77
        } else {
78
            list($sql_state, $error_code, $error_message) = $this->handle->errorInfo();
79
80
            $exception_type = $this->driver->getExceptionType($sql_state, $error_code, $error_message);
81
82
            throw new $exception_type($this->handle->queryString, $this->params, "{$sql_state}: {$error_message}", $error_code);
83
        }
84 1
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 1
    public function fetch()
90
    {
91 1
        if (! $this->executed) {
92 1
            $this->execute();
93
        }
94
95 1
        $result = $this->handle->fetch(PDO::FETCH_ASSOC);
96
        
97 1
        if ($result === false) {
98 1
            return null;
99
        }
100
        
101 1
        return $result;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function getRowsAffected()
108
    {
109
        return $this->handle->rowCount();
110
    }
111
}
112