Skip to content

Commit 9719845

Browse files
committed
fix blob
1 parent 869d6eb commit 9719845

15 files changed

+284
-352
lines changed

composer.lock

Lines changed: 227 additions & 234 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/LibsqlConnection.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getPdo(): LibsqlDatabase
7272

7373
/**
7474
* Set the active PDO connection used for reads.
75-
*
75+
*
7676
* @param LibsqlDatabase|\Closure $pdo
7777
* @return \Libsql\Laravel\Database\LibsqlConnection
7878
*/
@@ -114,11 +114,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
114114
$statement = $this->getPdo()->prepare($query);
115115
$results = (array) $statement->query($bindings);
116116

117-
$decodedResults = array_map(function ($row) {
118-
return decodeBlobs($row);
119-
}, $results);
120-
121-
return $decodedResults;
117+
return $results;
122118
});
123119

124120
$rowValues = array_values($data);
@@ -190,7 +186,7 @@ public function getSchemaBuilder(): LibsqlSchemaBuilder
190186

191187
public function getDefaultPostProcessor(): LibsqlQueryProcessor
192188
{
193-
return new LibsqlQueryProcessor;
189+
return new LibsqlQueryProcessor();
194190
}
195191

196192
public function useDefaultPostProcessor()
@@ -230,6 +226,7 @@ public function isUniqueConstraintError(\Exception $exception): bool
230226
return (bool) preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage());
231227
}
232228

229+
233230
public function escapeString($input)
234231
{
235232
if ($input === null) {

src/Database/LibsqlConnectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ protected function createConnection($driver, $connection, $database, $prefix = '
2929

3030
public function createConnector(array $config)
3131
{
32-
//
32+
//
3333
}
3434
}

src/Database/LibsqlDatabase.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,16 @@ private function detectConnectionMode(array $config): string
6868
{
6969
$database = $config['path'];
7070
$url = $config['url'];
71-
$authToken = $config['authToken'];
7271

7372
$mode = 'unknown';
7473

75-
if ($database === ':memory:') {
74+
if ($database === ':memory:' || empty($url)) {
7675
$mode = 'memory';
77-
}
78-
79-
if (empty($database) && !empty($url) && !empty($authToken)) {
76+
} elseif (empty($database) && !empty($url)) {
8077
$mode = 'remote';
81-
}
82-
83-
if (!empty($database) && $database !== ':memory:' && empty($url) && empty($authToken) && empty($url)) {
78+
} elseif (!empty($database) && empty($url)) {
8479
$mode = 'local';
85-
}
86-
87-
if (!empty($database) && $database !== ':memory:' && !empty($authToken) && !empty($url)) {
80+
} elseif (!empty($database) && !empty($url)) {
8881
$mode = 'remote_replica';
8982
}
9083

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

9689
public function version(): string
9790
{
98-
// TODO: Need to return an actual version from libSQL binary
99-
return '0.0.1';
91+
return '3.45.1';
10092
}
10193

10294
public function inTransaction(): bool

src/Database/LibsqlQueryProcessor.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,8 @@
44

55
namespace Libsql\Laravel\Database;
66

7-
use Illuminate\Contracts\Database\Query\Builder;
87
use Illuminate\Database\Query\Processors\SQLiteProcessor;
98

109
class LibsqlQueryProcessor extends SQLiteProcessor
1110
{
12-
/**
13-
* Process the list of tables.
14-
*
15-
* @param mixed $results
16-
*/
17-
public function processTables($results): array
18-
{
19-
return array_map(function ($result) {
20-
$result = (object) $result;
21-
22-
return [
23-
'name' => $result->name,
24-
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
25-
'size' => isset($result->size) ? (int) $result->size : null,
26-
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
27-
'collation' => $result->collation ?? null, // MySQL only
28-
'engine' => $result->engine ?? null, // MySQL only
29-
];
30-
}, $results);
31-
}
32-
33-
public function processViews($results)
34-
{
35-
return array_map(function ($result) {
36-
$result = (object) $result;
37-
38-
return [
39-
'name' => $result->name,
40-
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
41-
'definition' => $result->definition,
42-
];
43-
}, $results);
44-
}
45-
46-
public function processSelect(Builder $query, $results)
47-
{
48-
return $results;
49-
}
5011
}

src/Database/LibsqlSchemaBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Libsql\Laravel\Database;
66

7+
use Illuminate\Database\QueryException;
8+
use Illuminate\Support\Arr;
79
use Illuminate\Support\Str;
810
use Illuminate\Database\Query\Expression;
911
use Illuminate\Database\Schema\SQLiteBuilder;
@@ -127,8 +129,6 @@ public function getColumns($table)
127129

128130
protected function grammar(): LibsqlSchemaGrammar
129131
{
130-
$grammar = new LibsqlSchemaGrammar($this->connection);
131-
132-
return $grammar;
132+
return new LibsqlSchemaGrammar($this->connection);
133133
}
134134
}

src/Database/LibsqlSchemaGrammar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Libsql\Laravel\Database;

src/Database/LibsqlStatement.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Carbon;
88
use Libsql\Statement;
9+
use Libsql\Blob;
910

1011
class LibsqlStatement
1112
{
@@ -62,7 +63,7 @@ public function query(array $parameters = []): mixed
6263
}
6364

6465
$results = $this->statement->query()->fetchArray();
65-
$rows = decodeDoubleBase64($results);
66+
$rows = decode($results);
6667
$rowValues = array_values($rows);
6768

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

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

9293
public function execute(array $parameters = []): bool
9394
{
94-
try {
95-
96-
if (empty($parameters)) {
97-
$parameters = $this->bindings;
98-
}
99-
100-
foreach ($parameters as $key => $value) {
101-
$this->statement->bind([$key => $value]);
102-
}
95+
if (empty($parameters)) {
96+
$parameters = $this->parameterCasting($this->bindings);
97+
}
10398

104-
if (str_starts_with(strtolower($this->query), 'select')) {
105-
$queryRows = $this->statement->query()->fetchArray();
106-
$this->affectedRows = count($queryRows);
107-
} else {
108-
$this->affectedRows = $this->statement->execute();
109-
}
99+
$this->statement->bind($parameters);
110100

111-
return true;
112-
} catch (\Exception $e) {
113-
return false;
101+
if (str_starts_with(strtolower($this->query), 'select')) {
102+
$queryRows = $this->statement->query()->fetchArray();
103+
$this->affectedRows = count($queryRows);
104+
} else {
105+
$this->affectedRows = $this->statement->execute();
114106
}
107+
108+
return true;
115109
}
116110

117111
#[\ReturnTypeWillChange]
@@ -217,7 +211,7 @@ private function parameterCasting(array $parameters): array
217211
};
218212

219213
if ($type === 'blob') {
220-
$value = base64_encode(base64_encode($value));
214+
$value = new Blob($value);
221215
}
222216

223217
if ($type === 'boolean') {

src/LibsqlConnection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class LibsqlConnection extends SQLiteConnection
1111
#[\ReturnTypeWillChange]
1212
protected function getDefaultSchemaGrammar(): LibsqlSchemaGrammar
1313
{
14-
($grammar = new LibsqlSchemaGrammar())->setConnection($this);
15-
return $this->withTablePrefix($grammar);
14+
return new LibsqlSchemaGrammar($this);
1615
}
1716
}

src/LibsqlServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
declare(strict_types=1);

0 commit comments

Comments
 (0)