Failed Conditions
Push — master ( d9aaf5...e5fe8c )
by Marco
12s
created

PDOStatement::convertParamType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use Doctrine\DBAL\FetchMode;
23
use Doctrine\DBAL\ParameterType;
24
use PDO;
25
26
/**
27
 * The PDO implementation of the Statement interface.
28
 * Used by all PDO-based drivers.
29
 *
30
 * @since 2.0
31
 */
32
class PDOStatement extends \PDOStatement implements Statement
33
{
34
    /**
35
     * @var int[]
36
     */
37
    private const PARAM_TYPE_MAP = [
38
        ParameterType::NULL         => PDO::PARAM_NULL,
39
        ParameterType::INTEGER      => PDO::PARAM_INT,
40
        ParameterType::STRING       => PDO::PARAM_STR,
41
        ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
42
        ParameterType::BOOLEAN      => PDO::PARAM_BOOL,
43
    ];
44
45
    /**
46
     * @var int[]
47
     */
48
    private const FETCH_MODE_MAP = [
49
        FetchMode::ASSOCIATIVE     => PDO::FETCH_ASSOC,
50
        FetchMode::NUMERIC         => PDO::FETCH_NUM,
51
        FetchMode::MIXED           => PDO::FETCH_BOTH,
52
        FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
53
        FetchMode::COLUMN          => PDO::FETCH_COLUMN,
54
        FetchMode::CUSTOM_OBJECT   => PDO::FETCH_CLASS,
55
    ];
56
57
    /**
58
     * Protected constructor.
59
     */
60 233
    protected function __construct()
61
    {
62 233
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 221
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
68
    {
69 221
        $fetchMode = $this->convertFetchMode($fetchMode);
70
71
        // This thin wrapper is necessary to shield against the weird signature
72
        // of PDOStatement::setFetchMode(): even if the second and third
73
        // parameters are optional, PHP will not let us remove it from this
74
        // declaration.
75
        try {
76 221
            if ($arg2 === null && $arg3 === null) {
77 221
                return parent::setFetchMode($fetchMode);
78
            }
79
80 2
            if ($arg3 === null) {
81 2
                return parent::setFetchMode($fetchMode, $arg2);
82
            }
83
84
            return parent::setFetchMode($fetchMode, $arg2, $arg3);
85
        } catch (\PDOException $exception) {
86
            throw new PDOException($exception);
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 33
    public function bindValue($param, $value, $type = ParameterType::STRING)
94
    {
95 33
        $type = $this->convertParamType($type);
96
97
        try {
98 33
            return parent::bindValue($param, $value, $type);
99
        } catch (\PDOException $exception) {
100
            throw new PDOException($exception);
101
        }
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 7
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
108
    {
109 7
        $type = $this->convertParamType($type);
110
111
        try {
112 7
            return parent::bindParam($column, $variable, $type, $length, $driverOptions);
113
        } catch (\PDOException $exception) {
114
            throw new PDOException($exception);
115
        }
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 20
    public function closeCursor()
122
    {
123
        try {
124 20
            return parent::closeCursor();
125
        } catch (\PDOException $exception) {
126
            // Exceptions not allowed by the interface.
127
            // In case driver implementations do not adhere to the interface, silence exceptions here.
128
            return true;
129
        }
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 119
    public function execute($params = null)
136
    {
137
        try {
138 119
            return parent::execute($params);
139 7
        } catch (\PDOException $exception) {
140 3
            throw new PDOException($exception);
141
        }
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 68
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
148
    {
149 68
        $fetchMode = $this->convertFetchMode($fetchMode);
150
151
        try {
152 68
            if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
153 9
                return parent::fetch();
154
            }
155
156 59
            if (\PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
157 59
                return parent::fetch($fetchMode);
158
            }
159
160
            if (0 === $cursorOffset) {
161
                return parent::fetch($fetchMode, $cursorOrientation);
162
            }
163
164
            return parent::fetch($fetchMode, $cursorOrientation, $cursorOffset);
165
        } catch (\PDOException $exception) {
166
            throw new PDOException($exception);
167
        }
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 101
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
174
    {
175 101
        $fetchMode = $this->convertFetchMode($fetchMode);
176
177
        try {
178 101
            if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
179 88
                return parent::fetchAll();
180
            }
181
182 54
            if (null === $fetchArgument && null === $ctorArgs) {
183 53
                return parent::fetchAll($fetchMode);
184
            }
185
186 1
            if (null === $ctorArgs) {
187 1
                return parent::fetchAll($fetchMode, $fetchArgument);
188
            }
189
190
            return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
191
        } catch (\PDOException $exception) {
192
            throw new PDOException($exception);
193
        }
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 42
    public function fetchColumn($columnIndex = 0)
200
    {
201
        try {
202 42
            return parent::fetchColumn($columnIndex);
203
        } catch (\PDOException $exception) {
204
            throw new PDOException($exception);
205
        }
206
    }
207
208
    /**
209
     * Converts DBAL parameter type to PDO parameter type
210
     *
211
     * @param int $type Parameter type
212
     */
213 40
    private function convertParamType(int $type) : int
214
    {
215 40
        if (! isset(self::PARAM_TYPE_MAP[$type])) {
216
            throw new \InvalidArgumentException('Invalid parameter type: ' . $type);
217
        }
218
219 40
        return self::PARAM_TYPE_MAP[$type];
220
    }
221
222
    /**
223
     * Converts DBAL fetch mode to PDO fetch mode
224
     *
225
     * @param int|null $fetchMode Fetch mode
226
     */
227 221
    private function convertFetchMode(?int $fetchMode) : ?int
228
    {
229 221
        if ($fetchMode === null) {
230 97
            return null;
231
        }
232
233 221
        if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
234
            throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
235
        }
236
237 221
        return self::FETCH_MODE_MAP[$fetchMode];
238
    }
239
}
240