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

PDOConnection   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 65.12%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 19
eloc 43
c 5
b 0
f 1
dl 0
loc 113
ccs 28
cts 43
cp 0.6512
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getNativeConnection() 0 3 1
A exec() 0 8 2
A prepare() 0 6 2
A query() 0 8 2
A getServerVersion() 0 5 1
A beginTransaction() 0 6 2
A __construct() 0 5 1
A lastInsertId() 0 6 2
A commit() 0 6 2
A rollBack() 0 6 2
A quote() 0 6 2
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 18
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
46
    {
47 18
        $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 18
        $this->connection->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
49 18
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
50
    }
51
52
    public function getServerVersion(): string
53
    {
54
        // Unable to detect platform version.
55
        // TODO: Need to retrieve and propagate CrateDB server version here?
56
        return "5.0.0";
57
    }
58
59 2
    public function getNativeConnection(): PDOCrateDB
60
    {
61 2
        return $this->connection;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     *
67
     * References:
68
     * - https://github.com/doctrine/dbal/issues/2025
69
     * - https://github.com/doctrine/dbal/pull/517
70
     * - https://github.com/doctrine/dbal/pull/373
71
     */
72 108
    public function prepare($sql, $options = []): CrateStatement
73
    {
74
        try {
75 108
            return new CrateStatement($this->connection, $sql, $options);
76
        } catch (PDOException $exception) {
77
            throw Exception::new($exception);
78
        }
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 90
    public function exec($sql): int
85
    {
86
        try {
87 90
            $result = $this->connection->exec($sql);
88
            assert($result !== false);
89 90
            return $result;
90 8
        } catch (PDOException $exception) {
91 8
            throw Exception::new($exception);
92
        }
93
    }
94
95 68
    public function query(string $sql): ResultInterface
96
    {
97
        try {
98 68
            $stmt = $this->prepare($sql);
99 68
            $stmt->execute();
100 66
            return new Result($stmt);
101 2
        } catch (PDOException $exception) {
102
            throw Exception::new($exception);
103
        }
104
    }
105
106 4
    public function quote($value, $type = ParameterType::STRING): string
107
    {
108
        try {
109 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...
110
        } catch (PDOException $exception) {
111
            throw Exception::new($exception);
112
        }
113
    }
114
115
    public function lastInsertId($name = null): string
116
    {
117
        try {
118
            return $this->connection->lastInsertId($name);
119
        } catch (PDOException $exception) {
120
            throw Exception::new($exception);
121
        }
122
    }
123
124 6
    public function beginTransaction()
125
    {
126
        try {
127 6
            return $this->connection->beginTransaction();
128
        } catch (PDOException $exception) {
129
            throw Exception::new($exception);
130
        }
131
    }
132
133 2
    public function commit()
134
    {
135
        try {
136 2
            return $this->connection->commit();
137
        } catch (PDOException $exception) {
138
            throw Exception::new($exception);
139
        }
140
    }
141
142 2
    public function rollBack()
143
    {
144
        try {
145 2
            return $this->connection->rollBack();
146 2
        } catch (PDOException $exception) {
147 2
            throw Exception::new($exception);
148
        }
149
    }
150
}
151