|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace Hyperf\Database\PgSQL\DBAL; |
| 13 | + |
| 14 | +use Doctrine\DBAL\Driver\Result as ResultInterface; |
| 15 | +use Doctrine\DBAL\Driver\Statement as StatementInterface; |
| 16 | +use Doctrine\DBAL\ParameterType; |
| 17 | +use Swoole\Coroutine\PostgreSQL; |
| 18 | +use Swoole\Coroutine\PostgreSQLStatement; |
| 19 | + |
| 20 | +class Connection implements \Doctrine\DBAL\Driver\Connection |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Create a new PDO connection instance. |
| 24 | + */ |
| 25 | + public function __construct(private PostgreSQL $connection) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Execute an SQL statement. |
| 31 | + */ |
| 32 | + public function exec(string $sql): int |
| 33 | + { |
| 34 | + $stmt = $this->connection->query($sql); |
| 35 | + |
| 36 | + \assert($stmt instanceof PostgreSQLStatement); |
| 37 | + |
| 38 | + return $stmt->affectedRows(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Prepare a new SQL statement. |
| 43 | + */ |
| 44 | + public function prepare(string $sql): StatementInterface |
| 45 | + { |
| 46 | + $stmt = $this->connection->prepare($sql); |
| 47 | + |
| 48 | + \assert($stmt instanceof PostgreSQLStatement); |
| 49 | + |
| 50 | + return new Statement($stmt); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Execute a new query against the connection. |
| 55 | + */ |
| 56 | + public function query(string $sql): ResultInterface |
| 57 | + { |
| 58 | + $stmt = $this->connection->query($sql); |
| 59 | + |
| 60 | + \assert($stmt instanceof PostgreSQLStatement); |
| 61 | + |
| 62 | + return new Result($stmt); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the last insert ID. |
| 67 | + * |
| 68 | + * @param null|string $name |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + public function lastInsertId($name = null) |
| 72 | + { |
| 73 | + if ($name !== null) { |
| 74 | + return $this->query(sprintf('SELECT CURRVAL(%s)', $this->quote($name)))->fetchOne(); |
| 75 | + } |
| 76 | + |
| 77 | + return $this->query('SELECT LASTVAL()')->fetchOne(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Begin a new database transaction. |
| 82 | + */ |
| 83 | + public function beginTransaction(): bool |
| 84 | + { |
| 85 | + $this->exec('BEGIN'); |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Commit a database transaction. |
| 92 | + */ |
| 93 | + public function commit(): bool |
| 94 | + { |
| 95 | + $this->exec('COMMIT'); |
| 96 | + |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Roll back a database transaction. |
| 102 | + */ |
| 103 | + public function rollBack(): bool |
| 104 | + { |
| 105 | + $this->exec('ROLLBACK'); |
| 106 | + |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Wrap quotes around the given input. |
| 112 | + * |
| 113 | + * @param string $input |
| 114 | + * @param string $type |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + public function quote($input, $type = ParameterType::STRING) |
| 118 | + { |
| 119 | + return $this->connection->escapeLiteral($input); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the server version for the connection. |
| 124 | + */ |
| 125 | + public function getServerVersion(): string |
| 126 | + { |
| 127 | + $result = $this->query('SHOW server_version'); |
| 128 | + |
| 129 | + $serverVersion = $result->fetchOne(); |
| 130 | + if ($version = strstr($serverVersion, ' ', true)) { |
| 131 | + return $version; |
| 132 | + } |
| 133 | + |
| 134 | + return $serverVersion; |
| 135 | + } |
| 136 | + |
| 137 | + public function getNativeConnection(): PostgreSQL |
| 138 | + { |
| 139 | + return $this->connection; |
| 140 | + } |
| 141 | +} |
0 commit comments