|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rcalicdan\FiberAsync\QueryBuilder; |
| 4 | + |
| 5 | +use Rcalicdan\FiberAsync\Api\Async; |
| 6 | +use Rcalicdan\FiberAsync\Api\AsyncPostgreSQL; |
| 7 | +use Rcalicdan\FiberAsync\Api\Promise; |
| 8 | +use Rcalicdan\FiberAsync\Promise\Interfaces\PromiseInterface; |
| 9 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryBuilderCoreTrait; |
| 10 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryConditionsTrait; |
| 11 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryJoinTrait; |
| 12 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryGroupingTrait; |
| 13 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryAdvancedConditionsTrait; |
| 14 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\QueryDebugTrait; |
| 15 | +use Rcalicdan\FiberAsync\QueryBuilder\Traits\SqlBuilderTrait; |
| 16 | + |
| 17 | + |
| 18 | +class PostgresQueryBuilder |
| 19 | +{ |
| 20 | + use QueryBuilderCoreTrait, |
| 21 | + QueryConditionsTrait, |
| 22 | + QueryJoinTrait, |
| 23 | + QueryGroupingTrait, |
| 24 | + QueryAdvancedConditionsTrait, |
| 25 | + SqlBuilderTrait, |
| 26 | + QueryDebugTrait; |
| 27 | + |
| 28 | + /** |
| 29 | + * Create a new AsyncQueryBuilder instance. |
| 30 | + * |
| 31 | + * @param string $table The table name to query. |
| 32 | + */ |
| 33 | + final public function __construct(string $table = '') |
| 34 | + { |
| 35 | + if ($table !== '') { |
| 36 | + $this->table = $table; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Execute the query and return all results. |
| 42 | + * |
| 43 | + * @return PromiseInterface<array<int, array<string, mixed>>> A promise that resolves to the query results. |
| 44 | + */ |
| 45 | + public function get(): PromiseInterface |
| 46 | + { |
| 47 | + $sql = $this->buildSelectQuery(); |
| 48 | + |
| 49 | + return AsyncPostgreSQL::query($sql, $this->getCompiledBindings()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the first result from the query. |
| 54 | + * |
| 55 | + * @return PromiseInterface<array<string, mixed>|false> A promise that resolves to the first result or false. |
| 56 | + */ |
| 57 | + public function first(): PromiseInterface |
| 58 | + { |
| 59 | + // A new instance with a limit is created for this specific query execution |
| 60 | + $instanceWithLimit = $this->limit(1); |
| 61 | + $sql = $instanceWithLimit->buildSelectQuery(); |
| 62 | + |
| 63 | + return AsyncPostgreSQL::fetchOne($sql, $instanceWithLimit->getCompiledBindings()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Find a record by ID. |
| 68 | + * |
| 69 | + * @param mixed $id The ID value to search for. |
| 70 | + * @param string $column The column name to search in. |
| 71 | + * @return PromiseInterface<array<string, mixed>|false> A promise that resolves to the found record or false. |
| 72 | + */ |
| 73 | + public function find(mixed $id, string $column = 'id'): PromiseInterface |
| 74 | + { |
| 75 | + return $this->where($column, $id)->first(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Find a record by ID or throw an exception if not found. |
| 80 | + * |
| 81 | + * @param mixed $id The ID value to search for. |
| 82 | + * @param string $column The column name to search in. |
| 83 | + * @return PromiseInterface<array<string, mixed>> A promise that resolves to the found record. |
| 84 | + * |
| 85 | + * @throws \RuntimeException When no record is found. |
| 86 | + */ |
| 87 | + public function findOrFail(mixed $id, string $column = 'id'): PromiseInterface |
| 88 | + { |
| 89 | + // @phpstan-ignore-next-line |
| 90 | + return Async::async(function () use ($id, $column): array { |
| 91 | + $result = await($this->find($id, $column)); |
| 92 | + if ($result === null || $result === false) { |
| 93 | + $idString = is_scalar($id) ? (string) $id : 'complex_type'; |
| 94 | + |
| 95 | + throw new \RuntimeException("Record not found with {$column} = {$idString}"); |
| 96 | + } |
| 97 | + |
| 98 | + return $result; |
| 99 | + })(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get a single value from the first result. |
| 104 | + * |
| 105 | + * @param string $column The column name to retrieve. |
| 106 | + * @return PromiseInterface<mixed> A promise that resolves to the column value or null. |
| 107 | + */ |
| 108 | + public function value(string $column): PromiseInterface |
| 109 | + { |
| 110 | + // @phpstan-ignore-next-line |
| 111 | + return Async::async(function () use ($column): mixed { |
| 112 | + $result = await($this->select($column)->first()); |
| 113 | + |
| 114 | + return ($result !== false && isset($result[$column])) ? $result[$column] : null; |
| 115 | + })(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Count the number of records. |
| 120 | + * |
| 121 | + * @param string $column The column to count. |
| 122 | + * @return PromiseInterface<int> A promise that resolves to the record count. |
| 123 | + */ |
| 124 | + public function count(string $column = '*'): PromiseInterface |
| 125 | + { |
| 126 | + $sql = $this->buildCountQuery($column); |
| 127 | + /** @var PromiseInterface<int> */ |
| 128 | + $promise = AsyncPostgreSQL::fetchValue($sql, $this->getCompiledBindings()); |
| 129 | + |
| 130 | + return $promise; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Check if any records exist. |
| 135 | + * |
| 136 | + * @return PromiseInterface<bool> A promise that resolves to true if records exist, false otherwise. |
| 137 | + */ |
| 138 | + public function exists(): PromiseInterface |
| 139 | + { |
| 140 | + // @phpstan-ignore-next-line |
| 141 | + return Async::async(function (): bool { |
| 142 | + $count = await($this->count()); |
| 143 | + |
| 144 | + return $count > 0; |
| 145 | + })(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Insert a single record. |
| 150 | + * |
| 151 | + * @param array<string, mixed> $data The data to insert as column => value pairs. |
| 152 | + * @return PromiseInterface<int> A promise that resolves to the number of affected rows. |
| 153 | + */ |
| 154 | + public function insert(array $data): PromiseInterface |
| 155 | + { |
| 156 | + if ($data === []) { |
| 157 | + return Promise::resolve(0); |
| 158 | + } |
| 159 | + $sql = $this->buildInsertQuery($data); |
| 160 | + |
| 161 | + return AsyncPostgreSQL::execute($sql, array_values($data)); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Insert multiple records in a batch operation. |
| 166 | + * |
| 167 | + * @param array<array<string, mixed>> $data An array of records to insert. |
| 168 | + * @return PromiseInterface<int> A promise that resolves to the number of affected rows. |
| 169 | + */ |
| 170 | + public function insertBatch(array $data): PromiseInterface |
| 171 | + { |
| 172 | + if ($data === []) { |
| 173 | + return Promise::resolve(0); |
| 174 | + } |
| 175 | + |
| 176 | + $sql = $this->buildInsertBatchQuery($data); |
| 177 | + $bindings = []; |
| 178 | + foreach ($data as $row) { |
| 179 | + if (is_array($row)) { |
| 180 | + $bindings = array_merge($bindings, array_values($row)); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return AsyncPostgreSQL::execute($sql, $bindings); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Create a new record (alias for insert). |
| 189 | + * |
| 190 | + * @param array<string, mixed> $data The data to insert as column => value pairs. |
| 191 | + * @return PromiseInterface<int> A promise that resolves to the number of affected rows. |
| 192 | + */ |
| 193 | + public function create(array $data): PromiseInterface |
| 194 | + { |
| 195 | + return $this->insert($data); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Update records matching the query conditions. |
| 200 | + * |
| 201 | + * @param array<string, mixed> $data The data to update as column => value pairs. |
| 202 | + * @return PromiseInterface<int> A promise that resolves to the number of affected rows. |
| 203 | + */ |
| 204 | + public function update(array $data): PromiseInterface |
| 205 | + { |
| 206 | + if ($data === []) { |
| 207 | + return Promise::resolve(0); |
| 208 | + } |
| 209 | + $sql = $this->buildUpdateQuery($data); |
| 210 | + $whereBindings = $this->getCompiledBindings(); |
| 211 | + $bindings = array_merge(array_values($data), $whereBindings); |
| 212 | + |
| 213 | + return AsyncPostgreSQL::execute($sql, $bindings); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Delete records matching the query conditions. |
| 218 | + * |
| 219 | + * @return PromiseInterface<int> A promise that resolves to the number of affected rows. |
| 220 | + */ |
| 221 | + public function delete(): PromiseInterface |
| 222 | + { |
| 223 | + $sql = $this->buildDeleteQuery(); |
| 224 | + |
| 225 | + return AsyncPostgreSQL::execute($sql, $this->getCompiledBindings()); |
| 226 | + } |
| 227 | +} |
0 commit comments