Skip to content

Commit 9886bdd

Browse files
authored
Support StringableStream instances (#349)
1 parent 386ef1d commit 9886bdd

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from `$table, $columns, $rows` to `$table, $rows, $columns = []` (@Tigrov)
88
- Enh #260: Support `Traversable` values for `DMLQueryBuilder::batchInsert()` method with empty columns (@Tigrov)
99
- Enh #255, #321: Implement and use `SqlParser` class (@Tigrov)
10-
- New #236: Implement `ColumnSchemaInterface` classes according to the data type of database table columns
10+
- New #236, #349: Implement `ColumnInterface` classes according to the data type of database table columns
1111
for type casting performance. Related with yiisoft/db#752 (@Tigrov)
1212
- Chg #272: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov)
1313
- Enh #275: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)

src/Column/BinaryColumn.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Yiisoft\Db\Command\Param;
88
use Yiisoft\Db\Expression\Expression;
99
use Yiisoft\Db\Schema\Column\BinaryColumn as BaseBinaryColumn;
10+
use Yiisoft\Db\Schema\Data\StringableStream;
1011

1112
use function is_string;
1213

@@ -15,8 +16,10 @@ final class BinaryColumn extends BaseBinaryColumn
1516
public function dbTypecast(mixed $value): mixed
1617
{
1718
if ($this->getDbType() === 'blob') {
18-
if ($value instanceof Param && is_string($value->value)) {
19+
if ($value instanceof Param) {
1920
$value = $value->value;
21+
} elseif ($value instanceof StringableStream) {
22+
$value = $value->getValue();
2023
}
2124

2225
if (is_string($value)) {

tests/ColumnTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
use Yiisoft\Db\Schema\Column\DoubleColumn;
2222
use Yiisoft\Db\Schema\Column\IntegerColumn;
2323
use Yiisoft\Db\Schema\Column\StringColumn;
24+
use Yiisoft\Db\Schema\Data\StringableStream;
2425
use Yiisoft\Db\Tests\Common\CommonColumnTest;
2526

2627
use function iterator_to_array;
2728
use function str_repeat;
28-
use function stream_get_contents;
2929
use function version_compare;
3030

3131
/**
@@ -65,7 +65,7 @@ protected function assertTypecastedValues(array $result, bool $allTypecasted = f
6565
$this->assertSame(str_repeat('x', 100), $result['char_col']);
6666
$this->assertNull($result['char_col3']);
6767
$this->assertSame(1.234, $result['float_col']);
68-
$this->assertSame("\x10\x11\x12", stream_get_contents($result['blob_col']));
68+
$this->assertSame("\x10\x11\x12", (string) $result['blob_col']);
6969
$this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_col']);
7070
$this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_local']);
7171
$this->assertEquals(new DateTimeImmutable('14:50:23'), $result['time_col']);
@@ -75,7 +75,7 @@ protected function assertTypecastedValues(array $result, bool $allTypecasted = f
7575
if ($allTypecasted) {
7676
$this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $result['json_col']);
7777
} else {
78-
$this->assertSame('[{"a":1,"b":null,"c":[1,3,5]}]', stream_get_contents($result['json_col']));
78+
$this->assertSame('[{"a":1,"b":null,"c":[1,3,5]}]', (string) $result['json_col']);
7979
}
8080
}
8181

@@ -240,11 +240,20 @@ public function testBinaryColumn(): void
240240
$binaryCol = new BinaryColumn();
241241
$binaryCol->dbType('blob');
242242

243-
$this->assertInstanceOf(Expression::class, $binaryCol->dbTypecast("\x10\x11\x12"));
244-
$this->assertInstanceOf(
245-
Expression::class,
243+
$expected = new Expression('TO_BLOB(UTL_RAW.CAST_TO_RAW(:value))', ['value' => "\x10\x11\x12"]);
244+
245+
$this->assertEquals(
246+
$expected,
247+
$binaryCol->dbTypecast("\x10\x11\x12"),
248+
);
249+
$this->assertEquals(
250+
$expected,
246251
$binaryCol->dbTypecast(new Param("\x10\x11\x12", PDO::PARAM_LOB)),
247252
);
253+
$this->assertEquals(
254+
$expected,
255+
$binaryCol->dbTypecast(new StringableStream("\x10\x11\x12")),
256+
);
248257
}
249258

250259
public function testJsonColumn(): void

tests/Provider/QueryBuilderProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ public static function prepareValue(): array
421421
$values['binary'][0] = "HEXTORAW('737472696e67')";
422422
$values['paramBinary'][0] = "HEXTORAW('737472696e67')";
423423
$values['paramResource'][0] = "HEXTORAW('737472696e67')";
424+
$values['ResourceStream'][0] = "HEXTORAW('737472696e67')";
424425

425426
return $values;
426427
}

0 commit comments

Comments
 (0)