Completed
Push — 4 ( afb3c8...0b5f5e )
by Loz
43s queued 20s
created

PDOStatementHandle::errorInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use PDO;
6
use PDOStatement;
7
8
/**
9
 * A handle to a PDOStatement, with cached column metadata, and type conversion
10
 *
11
 * Column metadata can't be fetched from a native PDOStatement after multiple calls in some DB backends,
12
 * so we wrap in this handle object, which also takes care of tidying up content types to keep in line
13
 * with the SilverStripe 4.4+ type expectations.
14
 */
15
class PDOStatementHandle
16
{
17
18
    /**
19
     * The statement to provide a handle to
20
     *
21
     * @var PDOStatement
22
     */
23
    private $statement;
24
25
    /**
26
     * Cached column metadata
27
     *
28
     * @var array
29
     */
30
    private $columnMeta = null;
31
32
    /**
33
     * Create a new handle.
34
     *
35
     * @param $statement The statement to provide a handle to
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\Connect\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37
    public function __construct(PDOStatement $statement)
38
    {
39
        $this->statement = $statement;
40
    }
41
42
    /**
43
     * Mapping of PDO-reported "native types" to PHP types
44
     */
45
    protected static $type_mapping = [
46
        // PGSQL
47
        'float8' => 'float',
48
        'float16' => 'float',
49
        'numeric' => 'float',
50
51
        // MySQL
52
        'NEWDECIMAL' => 'float',
53
54
        // SQlite
55
        'integer' => 'int',
56
        'double' => 'float',
57
    ];
58
59
    /**
60
     * Fetch a record form the statement with its type data corrected
61
     * Returns data as an array of maps
62
     * @return array
63
     */
64
    public function typeCorrectedFetchAll()
65
    {
66
        if ($this->columnMeta === null) {
67
            $columnCount = $this->statement->columnCount();
68
            $this->columnMeta = [];
69
            for ($i = 0; $i<$columnCount; $i++) {
70
                $this->columnMeta[$i] = $this->statement->getColumnMeta($i);
71
            }
72
        }
73
74
        // Re-map fetched data using columnMeta
75
        return array_map(
76
            function ($rowArray) {
77
                $row = [];
78
                foreach ($this->columnMeta as $i => $meta) {
79
                    // Coerce any column types that aren't correctly retrieved from the database
80
                    if (isset($meta['native_type']) && isset(self::$type_mapping[$meta['native_type']])) {
81
                        settype($rowArray[$i], self::$type_mapping[$meta['native_type']]);
82
                    }
83
                    $row[$meta['name']] = $rowArray[$i];
84
                }
85
                return $row;
86
            },
87
            $this->statement->fetchAll(PDO::FETCH_NUM)
88
        );
89
    }
90
91
    /**
92
     * Closes the cursor, enabling the statement to be executed again (PDOStatement::closeCursor)
93
     *
94
     * @return bool Returns true on success
95
     */
96
    public function closeCursor()
97
    {
98
        return $this->statement->closeCursor();
99
    }
100
101
    /**
102
     * Fetch the SQLSTATE associated with the last operation on the statement handle
103
     * (PDOStatement::errorCode)
104
     *
105
     * @return string
106
     */
107
    public function errorCode()
108
    {
109
        return $this->statement->errorCode();
110
    }
111
112
    /**
113
     * Fetch extended error information associated with the last operation on the statement handle
114
     * (PDOStatement::errorInfo)
115
     *
116
     * @return array
117
     */
118
    public function errorInfo()
119
    {
120
        return $this->statement->errorInfo();
121
    }
122
123
    /**
124
     * Returns the number of rows affected by the last SQL statement (PDOStatement::rowCount)
125
     *
126
     * @return int
127
     */
128
    public function rowCount()
129
    {
130
        return $this->statement->rowCount();
131
    }
132
133
    /**
134
     * Executes a prepared statement (PDOStatement::execute)
135
     *
136
     * @param $parameters An array of values with as many elements as there are bound parameters in the SQL statement
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\Connect\An was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
137
     *                    being executed
138
     * @return bool Returns true on success
139
     */
140
    public function execute(array $parameters)
141
    {
142
        return $this->statement->execute($parameters);
143
    }
144
145
    /**
146
     * Return the PDOStatement that this object provides a handle to
147
     *
148
     * @return PDOStatement
149
     */
150
    public function getPDOStatement()
151
    {
152
        return $this->statement;
153
    }
154
}
155