Skip to content
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

Fixes #3 - Issues related to Blob/Binary data type #4

Merged
merged 1 commit into from
Apr 23, 2024
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
8 changes: 6 additions & 2 deletions src/Database/TursoPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public function bindValue(string|int $param, mixed $value, $type = PDO::PARAM_ST
$type = PDO::PARAM_NULL;
}

if ($type === PDO::PARAM_STR && (! ctype_print($value) || ! mb_check_encoding($value, 'UTF-8'))) {
$type = PDO::PARAM_LOB;
}

$this->bindings[$param] = match ($type) {
PDO::PARAM_LOB => [
'type' => 'blob',
'value' => base64_encode($value),
'type' => 'blob',
'base64' => base64_encode(base64_encode($value)),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the binary/blob data with a single base64 encoding produces an intermittent error. I've reported this issue to the Turso development team. As a quick fix to avoid the error, we will send the binary/blob data by encoding it in base64 format twice.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's kinda wild

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is only a quick fix. I will update the approach as soon as the Turso dev team fixes the issue. There's also a workaround to store binary data as a base64-encoded string in the database using Laravel's custom attribute casting.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway.. Do you have any suggestions about this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works, it works. Interested to hear what Turso have to say .

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're currently investigating the issue. You can find the issue report I've submitted on Turso's Discord server in the help channel.

],
PDO::PARAM_BOOL => [
'type' => 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion src/Http/QueryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function extractRows(array $response): Collection
collect($item)
->each(function (array $column, int $index) use (&$row) {
$value = match ($column['type']) {
'blob' => base64_decode((string) $column['value'], true),
'blob' => base64_decode((string) base64_decode((string) $column['base64'], true), true),
'integer' => (int) $column['value'],
'float' => (float) $column['value'],
'null' => null,
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/DataTypes/BlobDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

beforeEach(function () {
Schema::create('blob_table', function ($table) {
$table->id();
$table->binary('blob');
});
});

afterEach(function () {
Schema::dropIfExists('blob_table');
});

test('it can insert a new blob data', function () {
$data = random_bytes(50);

$result = DB::table('blob_table')->insert([
'blob' => $data,
]);

$newData = DB::table('blob_table')->first();

expect($result)->toBeTrue()
->and(DB::table('blob_table')->count())->toBe(1)
->and($newData->blob)->toBe($data);
})->group('BlobDataTest', 'DataTypes', 'FeatureTest');

test('it can update an existing blob data', function () {
$data = random_bytes(50);

DB::table('blob_table')->insert([
'blob' => $data,
]);

$newData = random_bytes(50);

$result = DB::table('blob_table')->update([
'blob' => $newData,
]);

$updatedData = DB::table('blob_table')->first();

expect($result)->toBe(1)
->and($updatedData->blob)->toBe($newData);
})->group('BlobDataTest', 'DataTypes', 'FeatureTest');