|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brick\Geo\Engine\Database\Driver; |
| 6 | + |
| 7 | +use Brick\Geo\Engine\Database\Query\BinaryValue; |
| 8 | +use Brick\Geo\Engine\Database\Query\ScalarValue; |
| 9 | +use Brick\Geo\Engine\Database\Result\Row; |
| 10 | +use Brick\Geo\Exception\GeometryEngineException; |
| 11 | +use Override; |
| 12 | +use PgSql\Connection; |
| 13 | + |
| 14 | +/** |
| 15 | + * Database driver using the pgsql extension for PostgreSQL. |
| 16 | + * |
| 17 | + * TODO error handling! |
| 18 | + */ |
| 19 | +final class PgsqlDriver implements DatabaseDriver |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly Connection $connection, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + #[Override] |
| 27 | + public function executeQuery(string|BinaryValue|ScalarValue ...$query) : Row |
| 28 | + { |
| 29 | + $position = 1; |
| 30 | + |
| 31 | + $queryString = ''; |
| 32 | + $queryParams = []; |
| 33 | + |
| 34 | + foreach ($query as $queryPart) { |
| 35 | + if ($queryPart instanceof BinaryValue) { |
| 36 | + $queryString .= '$' . $position++ . '::bytea'; |
| 37 | + $queryParams[] = pg_escape_bytea($this->connection, $queryPart->value); |
| 38 | + } elseif ($queryPart instanceof ScalarValue) { |
| 39 | + $queryString .= '$' . $position++; |
| 40 | + |
| 41 | + if (is_int($queryPart->value)) { |
| 42 | + $queryString .= '::int'; |
| 43 | + $queryParams[] = $queryPart->value; |
| 44 | + } elseif (is_float($queryPart->value)) { |
| 45 | + $queryString .= '::float'; |
| 46 | + $queryParams[] = $queryPart->value; |
| 47 | + } elseif (is_bool($queryPart->value)) { |
| 48 | + $queryString .= '::bool'; |
| 49 | + $queryParams[] = $queryPart->value ? 't' : 'f'; |
| 50 | + } else { |
| 51 | + $queryParams[] = $queryPart->value; |
| 52 | + } |
| 53 | + } else { |
| 54 | + $queryString .= $queryPart; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $value = pg_prepare($this->connection, '', $queryString); |
| 59 | + |
| 60 | + if ($value === false) { |
| 61 | + die(pg_last_error()); |
| 62 | + } |
| 63 | + |
| 64 | + $result = pg_execute($this->connection, '', $queryParams); |
| 65 | + |
| 66 | + if ($result === false) { |
| 67 | + var_dump($queryParams); |
| 68 | + var_dump(pg_last_error($this->connection)); |
| 69 | + die; |
| 70 | + } |
| 71 | + |
| 72 | + /** @var list<list<mixed>> $rows */ |
| 73 | + $rows = pg_fetch_all($result, PGSQL_NUM); |
| 74 | + |
| 75 | + if ($rows === false) { |
| 76 | + die(pg_last_error()); |
| 77 | + } |
| 78 | + |
| 79 | + if (count($rows) !== 1) { |
| 80 | + throw new GeometryEngineException(sprintf('Expected exactly one row, got %d.', count($rows))); |
| 81 | + } |
| 82 | + |
| 83 | + return new Row($this, $rows[0]); |
| 84 | + } |
| 85 | + |
| 86 | + public function convertBinaryResult(mixed $value) : string |
| 87 | + { |
| 88 | + if (is_string($value)) { |
| 89 | + return pg_unescape_bytea($value); |
| 90 | + } |
| 91 | + |
| 92 | + throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value); |
| 93 | + } |
| 94 | + |
| 95 | + public function convertStringResult(mixed $value) : string |
| 96 | + { |
| 97 | + if (is_string($value)) { |
| 98 | + return $value; |
| 99 | + } |
| 100 | + |
| 101 | + throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value); |
| 102 | + } |
| 103 | + |
| 104 | + public function convertIntResult(mixed $value) : int |
| 105 | + { |
| 106 | + // TODO check that actually returned as int; |
| 107 | + // maybe checks for all types sent & received for each driver? |
| 108 | +
|
| 109 | + if (is_int($value)) { |
| 110 | + return $value; |
| 111 | + } |
| 112 | + |
| 113 | + throw GeometryEngineException::unexpectedDatabaseReturnType('int', $value); |
| 114 | + } |
| 115 | + |
| 116 | + public function convertFloatResult(mixed $value) : float |
| 117 | + { |
| 118 | + if (is_numeric($value)) { |
| 119 | + return (float) $value; |
| 120 | + } |
| 121 | + |
| 122 | + throw GeometryEngineException::unexpectedDatabaseReturnType('number or numeric string', $value); |
| 123 | + } |
| 124 | + |
| 125 | + public function convertBoolResult(mixed $value) : bool |
| 126 | + { |
| 127 | + return match ($value) { |
| 128 | + 't' => true, |
| 129 | + 'f' => false, |
| 130 | + default => throw GeometryEngineException::unexpectedDatabaseReturnType('t or f', $value), |
| 131 | + }; |
| 132 | + } |
| 133 | +} |
0 commit comments