Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
carbon: ^2.63
- laravel: 12.*
testbench: 10.*
carbon: ^3.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
1,543 changes: 762 additions & 781 deletions composer.lock

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions src/Database/LibsqlConnection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Libsql\Laravel\Database;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function getPdo(): LibsqlDatabase

/**
* Set the active PDO connection used for reads.
*
*
* @param LibsqlDatabase|\Closure $pdo
* @return \Libsql\Laravel\Database\LibsqlConnection
*/
Expand Down Expand Up @@ -113,11 +114,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
$statement = $this->getPdo()->prepare($query);
$results = (array) $statement->query($bindings);

$decodedResults = array_map(function ($row) {
return decodeBlobs($row);
}, $results);

return $decodedResults;
return $results;
});

$rowValues = array_values($data);
Expand Down Expand Up @@ -175,8 +172,7 @@ public function affectingStatement($query, $bindings = [])
#[\ReturnTypeWillChange]
protected function getDefaultSchemaGrammar(): LibsqlSchemaGrammar
{
($grammar = new LibsqlSchemaGrammar)->setConnection($this);
return $this->withTablePrefix($grammar);
return new LibsqlSchemaGrammar($this);
}

public function getSchemaBuilder(): LibsqlSchemaBuilder
Expand All @@ -190,7 +186,7 @@ public function getSchemaBuilder(): LibsqlSchemaBuilder

public function getDefaultPostProcessor(): LibsqlQueryProcessor
{
return new LibsqlQueryProcessor;
return new LibsqlQueryProcessor();
}

public function useDefaultPostProcessor()
Expand All @@ -200,10 +196,7 @@ public function useDefaultPostProcessor()

protected function getDefaultQueryGrammar()
{
($grammar = new LibsqlQueryGrammar)->setConnection($this);
$this->withTablePrefix($grammar);

return $grammar;
return new LibsqlQueryGrammar($this);
}

public function useDefaultQueryGrammar()
Expand Down Expand Up @@ -233,6 +226,7 @@ public function isUniqueConstraintError(\Exception $exception): bool
return (bool) preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage());
}


public function escapeString($input)
{
if ($input === null) {
Expand All @@ -258,5 +252,4 @@ public function quote($input)

return $this->escapeBinary($input);
}

}
2 changes: 1 addition & 1 deletion src/Database/LibsqlConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ protected function createConnection($driver, $connection, $database, $prefix = '

public function createConnector(array $config)
{
//
//
}
}
18 changes: 5 additions & 13 deletions src/Database/LibsqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,16 @@ private function detectConnectionMode(array $config): string
{
$database = $config['path'];
$url = $config['url'];
$authToken = $config['authToken'];

$mode = 'unknown';

if ($database === ':memory:') {
if ($database === ':memory:' || empty($url)) {
$mode = 'memory';
}

if (empty($database) && !empty($url) && !empty($authToken)) {
} elseif (empty($database) && !empty($url)) {
$mode = 'remote';
}

if (!empty($database) && $database !== ':memory:' && empty($url) && empty($authToken) && empty($url)) {
} elseif (!empty($database) && empty($url)) {
$mode = 'local';
}

if (!empty($database) && $database !== ':memory:' && !empty($authToken) && !empty($url)) {
} elseif (!empty($database) && !empty($url)) {
$mode = 'remote_replica';
}

Expand All @@ -95,8 +88,7 @@ private function detectConnectionMode(array $config): string

public function version(): string
{
// TODO: Need to return an actual version from libSQL binary
return '0.0.1';
return '3.45.1';
}

public function inTransaction(): bool
Expand Down
39 changes: 0 additions & 39 deletions src/Database/LibsqlQueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,8 @@

namespace Libsql\Laravel\Database;

use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Query\Processors\SQLiteProcessor;

class LibsqlQueryProcessor extends SQLiteProcessor
{
/**
* Process the list of tables.
*
* @param mixed $results
*/
public function processTables($results): array
{
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
'size' => isset($result->size) ? (int) $result->size : null,
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
'collation' => $result->collation ?? null, // MySQL only
'engine' => $result->engine ?? null, // MySQL only
];
}, $results);
}

public function processViews($results)
{
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
'definition' => $result->definition,
];
}, $results);
}

public function processSelect(Builder $query, $results)
{
return $results;
}
}
6 changes: 3 additions & 3 deletions src/Database/LibsqlSchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Libsql\Laravel\Database;

use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\SQLiteBuilder;
Expand Down Expand Up @@ -127,8 +129,6 @@ public function getColumns($table)

protected function grammar(): LibsqlSchemaGrammar
{
$grammar = new LibsqlSchemaGrammar;

return $grammar;
return new LibsqlSchemaGrammar($this->connection);
}
}
5 changes: 3 additions & 2 deletions src/Database/LibsqlSchemaGrammar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Libsql\Laravel\Database;
Expand All @@ -13,7 +14,7 @@ public function compileDropAllIndexes(): string
return "SELECT 'DROP INDEX IF EXISTS \"' || name || '\";' FROM sqlite_schema WHERE type = 'index' AND name NOT LIKE 'sqlite_%'";
}

public function compileDropAllTables(): string
public function compileDropAllTables($schema = null): string
{
return "SELECT 'DROP TABLE IF EXISTS \"' || name || '\";' FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%'";
}
Expand All @@ -23,7 +24,7 @@ public function compileDropAllTriggers(): string
return "SELECT 'DROP TRIGGER IF EXISTS \"' || name || '\";' FROM sqlite_schema WHERE type = 'trigger' AND name NOT LIKE 'sqlite_%'";
}

public function compileDropAllViews(): string
public function compileDropAllViews($schema = null): string
{
return "SELECT 'DROP VIEW IF EXISTS \"' || name || '\";' FROM sqlite_schema WHERE type = 'view'";
}
Expand Down
36 changes: 15 additions & 21 deletions src/Database/LibsqlStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Carbon;
use Libsql\Statement;
use Libsql\Blob;

class LibsqlStatement
{
Expand Down Expand Up @@ -62,7 +63,7 @@ public function query(array $parameters = []): mixed
}

$results = $this->statement->query()->fetchArray();
$rows = decodeDoubleBase64($results);
$rows = decode($results);
$rowValues = array_values($rows);

return match ($this->mode) {
Expand All @@ -79,7 +80,7 @@ public function query(array $parameters = []): mixed
$this->statement->bind([$key => $value]);
}
$result = $this->statement->query()->fetchArray();
$rows = decodeDoubleBase64($result);
$rows = decode($result);

return match ($this->mode) {
\PDO::FETCH_ASSOC => collect($rows),
Expand All @@ -91,27 +92,20 @@ public function query(array $parameters = []): mixed

public function execute(array $parameters = []): bool
{
try {

if (empty($parameters)) {
$parameters = $this->bindings;
}

foreach ($parameters as $key => $value) {
$this->statement->bind([$key => $value]);
}
if (empty($parameters)) {
$parameters = $this->parameterCasting($this->bindings);
}

if (str_starts_with(strtolower($this->query), 'select')) {
$queryRows = $this->statement->query()->fetchArray();
$this->affectedRows = count($queryRows);
} else {
$this->affectedRows = $this->statement->execute();
}
$this->statement->bind($parameters);

return true;
} catch (\Exception $e) {
return false;
if (str_starts_with(strtolower($this->query), 'select')) {
$queryRows = $this->statement->query()->fetchArray();
$this->affectedRows = count($queryRows);
} else {
$this->affectedRows = $this->statement->execute();
}

return true;
}

#[\ReturnTypeWillChange]
Expand Down Expand Up @@ -217,7 +211,7 @@ private function parameterCasting(array $parameters): array
};

if ($type === 'blob') {
$value = base64_encode(base64_encode($value));
$value = new Blob($value);
}

if ($type === 'boolean') {
Expand Down
3 changes: 1 addition & 2 deletions src/LibsqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class LibsqlConnection extends SQLiteConnection
#[\ReturnTypeWillChange]
protected function getDefaultSchemaGrammar(): LibsqlSchemaGrammar
{
($grammar = new LibsqlSchemaGrammar())->setConnection($this);
return $this->withTablePrefix($grammar);
return new LibsqlSchemaGrammar($this);
}
}
1 change: 1 addition & 0 deletions src/LibsqlServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

declare(strict_types=1);
Expand Down
1 change: 1 addition & 0 deletions src/Vector/VectorMacro.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Libsql\Laravel\Vector;
Expand Down
1 change: 0 additions & 1 deletion src/VectorCast.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

Expand Down
48 changes: 20 additions & 28 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Libsql\Blob;

function arrayToStdClass(array $array): array
{
$result = [];
Expand Down Expand Up @@ -68,39 +70,29 @@ function is_vector($value): bool
return array_keys($value) === range(0, count($value) - 1);
}

function decodeBlobs(array $row): array
function decode(array $result): array
{
return array_map(function ($value) {
return is_resource($value) ? stream_get_contents($value) : $value;
}, $row);
}
return array_map(function ($row) {
return array_map(function ($value) {
if ($value instanceof Blob) {
return $value->blob;
}

function decodeDoubleBase64(array $result): array
{
if (isset($result) && is_array($result)) {
foreach ($result as &$row) {
foreach ($row as $key => &$value) {
if (is_string($value) && isValidDateOrTimestamp($value)) {
continue;
}

if (is_string($value) && $decoded = json_decode($value, true)) {
$value = $decoded;
}

if (is_string($value) && isValidBlob($value)) {
$value = base64_decode(base64_decode($value));
}
if (!is_string($value)) {
return $value;
}
}
}

return $result;
}
if (isValidDateOrTimestamp($value)) {
return $value;
}

function isValidBlob(mixed $value): bool
{
return (bool) preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $value);
if ($decoded = json_decode($value, true)) {
return $decoded;
}

return $value;
}, $row);
}, $result);
}

function isValidDateOrTimestamp($string, $format = null): bool
Expand Down
Loading