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