|
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); |
|
|
|
|
|
|
48
|
12 |
|
$this->connection->setAttribute(PDO::ATTR_STATEMENT_CLASS, PDOStatement::class); |
|
49
|
12 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getServerVersion() |
|
53
|
|
|
{ |
|
54
|
|
|
// Unable to detect platform version. |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function getNativeConnection(): PDOCrateDB |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->connection; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritDoc} |
|
65
|
|
|
* |
|
66
|
|
|
* References: |
|
67
|
|
|
* - https://github.com/doctrine/dbal/issues/2025 |
|
68
|
|
|
* - https://github.com/doctrine/dbal/pull/517 |
|
69
|
|
|
* - https://github.com/doctrine/dbal/pull/373 |
|
70
|
|
|
*/ |
|
71
|
110 |
|
public function prepare($sql, $options = []): CrateStatement |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
110 |
|
return new CrateStatement($this->connection, $sql, $options); |
|
75
|
|
|
} catch (PDOException $exception) { |
|
76
|
|
|
throw Exception::new($exception); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
92 |
|
public function exec($sql): int |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
92 |
|
$result = $this->connection->exec($sql); |
|
87
|
|
|
assert($result !== false); |
|
88
|
92 |
|
return $result; |
|
89
|
8 |
|
} catch (PDOException $exception) { |
|
90
|
8 |
|
throw Exception::new($exception); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
66 |
|
public function query(string $sql): ResultInterface |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
66 |
|
$stmt = $this->prepare($sql); |
|
98
|
66 |
|
$stmt->execute(); |
|
99
|
64 |
|
return new Result($stmt); |
|
100
|
2 |
|
} catch (PDOException $exception) { |
|
101
|
2 |
|
throw Exception::new($exception); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
public function quote($value, $type = ParameterType::STRING): string |
|
106
|
|
|
{ |
|
107
|
4 |
|
return $this->connection->quote($value, $type); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function lastInsertId($name = null): string|false |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->connection->lastInsertId($name); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function beginTransaction(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function commit(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function rollBack(): bool |
|
126
|
|
|
{ |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: