Completed
Push — master ( 18d42b...fed0be )
by Rasmus
03:18
created

PreparedPDOStatement   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 98
ccs 23
cts 28
cp 0.8214
rs 10

5 Methods

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