-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add a Surreal Eloquent query foundation #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f85f08a
feat: add a surreal eloquent query foundation
ibourgeois a482f5a
fix: allow surreal string literals with slashes
ibourgeois c90e064
fix: encode surreal null values as none
ibourgeois 50300c9
fix: support surreal nested where groups
ibourgeois 5eb17cc
fix: auto-generate surreal ids for inserts
ibourgeois 5216b5b
fix: harden surreal query driver edges
ibourgeois 821ab95
fix: polish surreal query compilation
ibourgeois ac67d4e
fix: align surreal probe with http result shape
ibourgeois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
|
|
||
| namespace App\Services\Surreal\Query; | ||
|
|
||
| use App\Services\Surreal\Schema\SurrealSchemaConnection; | ||
| use Illuminate\Contracts\Database\Query\Expression; | ||
| use Illuminate\Database\Query\Builder; | ||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Collection; | ||
| use RuntimeException; | ||
| use stdClass; | ||
|
|
||
| class SurrealQueryBuilder extends Builder | ||
| { | ||
| /** | ||
| * @param string|Expression|array<string|Expression> $columns | ||
| * @return Collection<int, stdClass> | ||
| */ | ||
| public function get($columns = ['*']): Collection | ||
| { | ||
| $original = $this->columns; | ||
|
|
||
| $this->columns ??= Arr::wrap($columns); | ||
|
|
||
| $rows = $this->surrealConnection()->selectRecords( | ||
| table: (string) $this->from, | ||
| columns: $this->resolveColumns($this->columns), | ||
| wheres: $this->wheres ?? [], | ||
| orders: $this->orders ?? [], | ||
| limit: $this->limit, | ||
| offset: $this->offset, | ||
| ); | ||
|
|
||
| $this->columns = $original; | ||
|
|
||
| return $this->applyAfterQueryCallbacks(new Collection(array_map( | ||
| static fn (array $row): stdClass => (object) $row, | ||
| $rows, | ||
| ))); | ||
| } | ||
|
|
||
| public function insert(array $values): bool | ||
| { | ||
| $records = $this->prepareInsertValues($values); | ||
|
|
||
| foreach ($records as $record) { | ||
| $this->surrealConnection()->insertRecord( | ||
| table: (string) $this->from, | ||
| values: $record, | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public function insertGetId(array $values, $sequence = null): int|string | ||
| { | ||
| return $this->surrealConnection()->insertRecordAndReturnId( | ||
| table: (string) $this->from, | ||
| values: $values, | ||
| keyName: is_string($sequence) && $sequence !== '' ? $sequence : 'id', | ||
| ); | ||
| } | ||
|
|
||
| public function update(array $values): int | ||
| { | ||
| if ($values === []) { | ||
| return 0; | ||
| } | ||
|
|
||
| return $this->surrealConnection()->updateRecords( | ||
| table: (string) $this->from, | ||
| values: $values, | ||
| wheres: $this->wheres ?? [], | ||
| limit: $this->limit, | ||
| ); | ||
| } | ||
|
|
||
| public function delete($id = null): int | ||
| { | ||
| $query = $this; | ||
|
|
||
| if ($id !== null) { | ||
| $query = clone $this; | ||
| $query->where('id', '=', $id); | ||
| } | ||
|
|
||
| return $this->surrealConnection()->deleteRecords( | ||
| table: (string) $query->from, | ||
| wheres: $query->wheres ?? [], | ||
| limit: $query->limit, | ||
| ); | ||
| } | ||
|
|
||
| public function exists(): bool | ||
| { | ||
| $query = clone $this; | ||
|
|
||
| return $query->limit(1)->get(['id'])->isNotEmpty(); | ||
| } | ||
|
|
||
| public function count($columns = '*'): int | ||
| { | ||
| return $this->get(Arr::wrap($columns))->count(); | ||
ibourgeois marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @param array<int|string, mixed> $values | ||
| * @return list<array<string, mixed>> | ||
| */ | ||
| private function prepareInsertValues(array $values): array | ||
| { | ||
| if ($values === []) { | ||
| return []; | ||
| } | ||
|
|
||
| if (array_is_list($values) && isset($values[0]) && is_array($values[0])) { | ||
| return array_values(array_map( | ||
| static function (mixed $record): array { | ||
| if (! is_array($record)) { | ||
| throw new RuntimeException('Surreal bulk inserts expect each record payload to be an array.'); | ||
| } | ||
|
|
||
| /** @var array<string, mixed> $record */ | ||
| return $record; | ||
| }, | ||
| $values, | ||
| )); | ||
| } | ||
ibourgeois marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** @var array<string, mixed> $values */ | ||
| return [$values]; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string|Expression> $columns | ||
| * @return list<string> | ||
| */ | ||
| private function resolveColumns(array $columns): array | ||
| { | ||
| return array_values(array_map(function (mixed $column): string { | ||
| if ($column instanceof Expression) { | ||
| return (string) $column->getValue($this->grammar); | ||
| } | ||
|
|
||
| return (string) $column; | ||
| }, $columns)); | ||
| } | ||
|
|
||
| private function surrealConnection(): SurrealSchemaConnection | ||
| { | ||
| $connection = $this->getConnection(); | ||
|
|
||
| if (! $connection instanceof SurrealSchemaConnection) { | ||
| throw new RuntimeException('SurrealQueryBuilder requires a SurrealSchemaConnection instance.'); | ||
| } | ||
|
|
||
| return $connection; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.