Skip to content

Commit

Permalink
Merge pull request #4 from richan-fongdasen/issue-3
Browse files Browse the repository at this point in the history
Fixes #3 - Issues related to Blob/Binary data type
  • Loading branch information
richan-fongdasen authored Apr 23, 2024
2 parents f6501e7 + 9c28876 commit 420bf40
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
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)),
],
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');

0 comments on commit 420bf40

Please sign in to comment.