Passed
Pull Request — main (#122)
by Andreas
02:05
created

PDOConnection::prepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 4
cp 0.5
rs 10
cc 2
nc 2
nop 2
crap 2.5
1
<?php
2
3
/**
4
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
5
 * license agreements.  See the NOTICE file distributed with this work for
6
 * additional information regarding copyright ownership.  Crate licenses
7
 * this file to you under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.  You may
9
 * obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16
 * License for the specific language governing permissions and limitations
17
 * under the License.
18
 *
19
 * However, if you have executed another commercial license agreement
20
 * with Crate these terms will supersede the license and you may use the
21
 * software solely pursuant to the terms of the relevant commercial agreement.
22
 */
23
24
namespace Crate\DBAL\Driver\PDOCrate;
25
26
use Crate\PDO\PDOCrateDB;
27
use Crate\PDO\PDOStatement;
28
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
29
use Doctrine\DBAL\Driver\PDO\Exception;
30
use Doctrine\DBAL\Driver\Result as ResultInterface;
31
use Doctrine\DBAL\ParameterType;
32
use PDO;
33
use PDOException;
34
35
class PDOConnection implements ConnectionInterface
36
{
37
    private PDOCrateDB $connection;
38
39
    /**
40
     * @param string $dsn
41
     * @param string $user
42
     * @param string $password
43
     * @param array|null $options
44
     */
45 12
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
46
    {
47 12
        $this->connection = new PDOCrateDB($dsn, $user, $password, $options);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type string; however, parameter $passwd of Crate\PDO\PDOCrateDB::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $this->connection = new PDOCrateDB($dsn, $user, /** @scrutinizer ignore-type */ $password, $options);
Loading history...
Bug introduced by
It seems like $user can also be of type string; however, parameter $username of Crate\PDO\PDOCrateDB::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $this->connection = new PDOCrateDB($dsn, /** @scrutinizer ignore-type */ $user, $password, $options);
Loading history...
48
        // FIXME: `[PDOStatement::class, []]` might be correct, but fails in `crate-pdo`.
49 12
        $this->connection->setAttribute(PDO::ATTR_STATEMENT_CLASS, PDOStatement::class);
50 12
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
51
    }
52
53
    public function getServerVersion(): string
54
    {
55
        // Unable to detect platform version.
56
        // TODO: Need to retrieve and propagate CrateDB server version here?
57
        return "5.0.0";
58
    }
59
60 2
    public function getNativeConnection(): PDOCrateDB
61
    {
62 2
        return $this->connection;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     *
68
     * References:
69
     * - https://github.com/doctrine/dbal/issues/2025
70
     * - https://github.com/doctrine/dbal/pull/517
71
     * - https://github.com/doctrine/dbal/pull/373
72
     */
73 108
    public function prepare($sql, $options = []): CrateStatement
74
    {
75
        try {
76 108
            return new CrateStatement($this->connection, $sql, $options);
77
        } catch (PDOException $exception) {
78
            throw Exception::new($exception);
79
        }
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 90
    public function exec($sql): int
86
    {
87
        try {
88 90
            $result = $this->connection->exec($sql);
89
            assert($result !== false);
90 90
            return $result;
91 8
        } catch (PDOException $exception) {
92 8
            throw Exception::new($exception);
93
        }
94
    }
95
96 68
    public function query(string $sql): ResultInterface
97
    {
98
        try {
99 68
            $stmt = $this->prepare($sql);
100 68
            $stmt->execute();
101 66
            return new Result($stmt);
102 2
        } catch (PDOException $exception) {
103
            throw Exception::new($exception);
104
        }
105
    }
106
107 4
    public function quote($value, $type = ParameterType::STRING): string
108
    {
109
        try {
110 4
            return $this->connection->quote($value, $type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection->quote($value, $type) could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
111
        } catch (PDOException $exception) {
112
            throw Exception::new($exception);
113
        }
114
    }
115
116
    public function lastInsertId($name = null): string
117
    {
118
        try {
119
            return $this->connection->lastInsertId($name);
120
        } catch (PDOException $exception) {
121
            throw Exception::new($exception);
122
        }
123
    }
124
125
    public function beginTransaction(): void
126
    {
127
    }
128
129
    public function commit(): void
130
    {
131
    }
132
133
    public function rollBack(): void
134
    {
135
    }
136
}
137