Passed
Push — travis-php74 ( e33f92...3a6a6e )
by Sam
07:33
created

PDOStatementHandle::__construct()   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 1
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
        'bool' => 'int', // Bools should be ints
51
52
        // MySQL
53
        'NEWDECIMAL' => 'float',
54
55
        // SQlite
56
        'integer' => 'int',
57
        'double' => 'float',
58
    ];
59
60
    /**
61
     * Fetch a record form the statement with its type data corrected
62
     * Returns data as an array of maps
63
     * @return array
64
     */
65
    public function typeCorrectedFetchAll()
66
    {
67
        if ($this->columnMeta === null) {
68
            $columnCount = $this->statement->columnCount();
69
            $this->columnMeta = [];
70
            for ($i = 0; $i<$columnCount; $i++) {
71
                $this->columnMeta[$i] = $this->statement->getColumnMeta($i);
72
            }
73
        }
74
75
        // Re-map fetched data using columnMeta
76
        return array_map(
77
            function ($rowArray) {
78
                $row = [];
79
                foreach ($this->columnMeta as $i => $meta) {
80
                    // Coerce any column types that aren't correctly retrieved from the database
81
                    if (isset($meta['native_type']) && isset(self::$type_mapping[$meta['native_type']])) {
82
                        settype($rowArray[$i], self::$type_mapping[$meta['native_type']]);
83
                    }
84
                    $row[$meta['name']] = $rowArray[$i];
85
                }
86
                return $row;
87
            },
88
            $this->statement->fetchAll(PDO::FETCH_NUM)
89
        );
90
    }
91
92
    /**
93
     * Closes the cursor, enabling the statement to be executed again (PDOStatement::closeCursor)
94
     *
95
     * @return bool Returns true on success
96
     */
97
    public function closeCursor()
98
    {
99
        return $this->statement->closeCursor();
100
    }
101
102
    /**
103
     * Fetch the SQLSTATE associated with the last operation on the statement handle
104
     * (PDOStatement::errorCode)
105
     *
106
     * @return string
107
     */
108
    public function errorCode()
109
    {
110
        return $this->statement->errorCode();
111
    }
112
113
    /**
114
     * Fetch extended error information associated with the last operation on the statement handle
115
     * (PDOStatement::errorInfo)
116
     *
117
     * @return array
118
     */
119
    public function errorInfo()
120
    {
121
        return $this->statement->errorInfo();
122
    }
123
124
    /**
125
     * Returns the number of rows affected by the last SQL statement (PDOStatement::rowCount)
126
     *
127
     * @return int
128
     */
129
    public function rowCount()
130
    {
131
        return $this->statement->rowCount();
132
    }
133
134
    /**
135
     * Executes a prepared statement (PDOStatement::execute)
136
     *
137
     * @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...
138
     *                    being executed
139
     * @return bool Returns true on success
140
     */
141
    public function execute(array $parameters)
142
    {
143
        return $this->statement->execute($parameters);
144
    }
145
146
    /**
147
     * Return the PDOStatement that this object provides a handle to
148
     *
149
     * @return PDOStatement
150
     */
151
    public function getPDOStatement()
152
    {
153
        return $this->statement;
154
    }
155
}
156