generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2da98c
commit c98c431
Showing
29 changed files
with
1,317 additions
and
339 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
parameters: | ||
treatPhpDocTypesAsCertain: false | ||
ignoreErrors: | ||
- '#Variable method call on RichanFongdasen\\Turso\\TursoHttpClient.#' | ||
- '#Variable method call on RichanFongdasen\\Turso\\TursoClient.#' |
This file contains 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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichanFongdasen\Turso\Contracts; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
use Stringable; | ||
|
||
interface TursoQuery extends Arrayable, Stringable | ||
{ | ||
public function getIndex(): int; | ||
|
||
public function getType(): string; | ||
|
||
public function setIndex(int $index): self; | ||
} |
This file contains 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 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 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 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,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichanFongdasen\Turso\Http; | ||
|
||
use Illuminate\Support\Collection; | ||
use RichanFongdasen\Turso\Contracts\TursoQuery; | ||
use RichanFongdasen\Turso\Exceptions\TursoQueryException; | ||
use RichanFongdasen\Turso\Queries\ExecuteQuery; | ||
|
||
class QueryResponse | ||
{ | ||
protected int $affectedRows = 0; | ||
|
||
protected Collection $columns; | ||
|
||
protected ?string $lastInsertId = null; | ||
|
||
protected TursoQuery $query; | ||
|
||
protected array $rawResponse; | ||
|
||
protected int $replicationIndex = 0; | ||
|
||
protected string $responseType = ''; | ||
|
||
protected Collection $rows; | ||
|
||
public function __construct(TursoQuery $query, array $response = []) | ||
{ | ||
$this->query = $query; | ||
$this->rawResponse = $response; | ||
$this->responseType = data_get($response, 'type', 'error'); | ||
|
||
$this->affectedRows = (int) data_get($response, 'response.result.affected_row_count', 0); | ||
$this->lastInsertId = data_get($response, 'response.result.last_insert_rowid'); | ||
$this->replicationIndex = (int) data_get($response, 'response.result.replication_index'); | ||
|
||
$this->columns = $this->extractColumns($response); | ||
$this->rows = $this->extractRows($response); | ||
|
||
$this->checkIfResponseHasError(); | ||
} | ||
|
||
protected function checkIfResponseHasError(): void | ||
{ | ||
if ($this->responseType !== 'error') { | ||
return; | ||
} | ||
|
||
$errorCode = (string) data_get($this->rawResponse, 'error.code', 'UNKNOWN_ERROR'); | ||
$errorMessage = (string) data_get($this->rawResponse, 'error.message', 'Error: An unknown error has occurred'); | ||
|
||
$statement = ($this->query instanceof ExecuteQuery) | ||
? $this->query->getStatement() | ||
: $this->query->getType(); | ||
|
||
throw new TursoQueryException($errorCode, $errorMessage, $statement); | ||
} | ||
|
||
protected function extractColumns(array $response): Collection | ||
{ | ||
return collect((array) data_get($response, 'response.result.cols', [])) | ||
->keyBy('name') | ||
->keys(); | ||
} | ||
|
||
protected function extractRows(array $response): Collection | ||
{ | ||
$rows = new Collection(); | ||
|
||
collect((array) data_get($response, 'response.result.rows', [])) | ||
->each(function (array $item) use (&$rows) { | ||
$row = new Collection(); | ||
|
||
collect($item) | ||
->each(function (array $column, int $index) use (&$row) { | ||
$value = match ($column['type']) { | ||
'blob' => base64_decode((string) $column['value'], true), | ||
'integer' => (int) $column['value'], | ||
'float' => (float) $column['value'], | ||
'null' => null, | ||
default => (string) $column['value'], | ||
}; | ||
|
||
$row->put($this->columns->get($index), $value); | ||
}); | ||
|
||
$rows->push($row); | ||
}); | ||
|
||
return $rows; | ||
} | ||
|
||
public function getAffectedRows(): int | ||
{ | ||
return $this->affectedRows; | ||
} | ||
|
||
public function getColumns(): Collection | ||
{ | ||
return $this->columns; | ||
} | ||
|
||
public function getLastInsertId(): ?string | ||
{ | ||
return $this->lastInsertId; | ||
} | ||
|
||
public function getQuery(): TursoQuery | ||
{ | ||
return $this->query; | ||
} | ||
|
||
public function getRawResponse(): array | ||
{ | ||
return $this->rawResponse; | ||
} | ||
|
||
public function getReplicationIndex(): int | ||
{ | ||
return $this->replicationIndex; | ||
} | ||
|
||
public function getResponseType(): string | ||
{ | ||
return $this->responseType; | ||
} | ||
|
||
public function getRows(): Collection | ||
{ | ||
return $this->rows; | ||
} | ||
} |
Oops, something went wrong.