Skip to content

Realize Schema::loadResultColumn() method #316

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

Merged
merged 11 commits into from
Apr 18, 2025
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Enh #315: Remove `getCacheKey()` and `getCacheTag()` methods from `Schema` class (@Tigrov)
- Enh #319: Support `boolean` type (@Tigrov)
- Enh #318, #320: Use `DbArrayHelper::arrange()` instead of `DbArrayHelper::index()` method (@Tigrov)
- New #316: Realize `Schema::loadResultColumn()` method (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
66 changes: 66 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Throwable;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\ReferentialAction;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
Expand All @@ -25,6 +26,7 @@
use function array_map;
use function array_reverse;
use function implode;
use function in_array;
use function is_array;
use function preg_replace;
use function strtolower;
Expand Down Expand Up @@ -175,6 +177,70 @@
return $names;
}

/**
* @param array{
* "oci:decl_type": int|string,
* native_type: string,
* pdo_type: int,
* scale: int,
* table?: string,
* flags: string[],
* name: string,
* len: int,
* precision: int,
* } $metadata
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
protected function loadResultColumn(array $metadata): ColumnInterface|null
{
if (empty($metadata['oci:decl_type'])) {
return null;
}

$dbType = match ($metadata['oci:decl_type']) {
119 => 'json',
'TIMESTAMP WITH TIMEZONE' => 'timestamp with time zone',
'TIMESTAMP WITH LOCAL TIMEZONE' => 'timestamp with local time zone',
default => strtolower((string) $metadata['oci:decl_type']),
};

$columnInfo = ['fromResult' => true];

if (!empty($metadata['table'])) {
$columnInfo['table'] = $metadata['table'];
$columnInfo['name'] = $metadata['name'];

Check warning on line 212 in src/Schema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema.php#L211-L212

Added lines #L211 - L212 were not covered by tests
} elseif (!empty($metadata['name'])) {
$columnInfo['name'] = $metadata['name'];
}

if ($metadata['pdo_type'] === 3) {
$columnInfo['type'] = ColumnType::BINARY;
}

if (!empty($metadata['precision'])) {
$columnInfo['size'] = $metadata['precision'];
}

/** @psalm-suppress PossiblyUndefinedArrayOffset, InvalidArrayOffset */
match ($dbType) {
'timestamp',
'timestamp with time zone',
'timestamp with local time zone' => $columnInfo['size'] = $metadata['scale'],
'interval day to second',
'interval year to month' =>
[$columnInfo['size'], $columnInfo['scale']] = [$metadata['scale'], $metadata['precision']],
'number' => $metadata['scale'] !== -127 ? $columnInfo['scale'] = $metadata['scale'] : null,
'float' => null,
default => $columnInfo['size'] = $metadata['len'],
};

$columnInfo['notNull'] = in_array('not_null', $metadata['flags'], true);

/** @psalm-suppress MixedArgumentTypeCoercion */
return $this->db->getColumnFactory()->fromDbType($dbType, $columnInfo);
}

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down
148 changes: 132 additions & 16 deletions tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;
use Yiisoft\Db\Oracle\Connection;
use Yiisoft\Db\Oracle\Tests\Provider\ColumnProvider;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand All @@ -29,6 +30,135 @@ final class ColumnTest extends AbstractColumnTest
{
use TestTrait;

private function insertTypeValues(Connection $db): void
{
$db->createCommand()->insert(
'type',
[
'int_col' => 1,
'char_col' => str_repeat('x', 100),
'char_col3' => null,
'float_col' => 1.234,
'blob_col' => "\x10\x11\x12",
'timestamp_col' => new Expression("TIMESTAMP '2023-07-11 14:50:23'"),
'bool_col' => false,
'bit_col' => 0b0110_0110, // 102
'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]],
]
)->execute();
}

private function assertResultValues(array $result, string $varsion): void
{
$this->assertSame(1, $result['int_col']);
$this->assertSame(str_repeat('x', 100), $result['char_col']);
$this->assertNull($result['char_col3']);
$this->assertSame(1.234, $result['float_col']);
$this->assertSame("\x10\x11\x12", stream_get_contents($result['blob_col']));
$this->assertEquals(false, $result['bool_col']);
$this->assertSame(0b0110_0110, $result['bit_col']);

if (version_compare($varsion, '21', '>=')) {
$this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $result['json_col']);
} else {
$this->assertSame('[{"a":1,"b":null,"c":[1,3,5]}]', stream_get_contents($result['json_col']));
}
}

public function testQueryWithTypecasting(): void
{
$db = $this->getConnection();
$varsion = $db->getServerInfo()->getVersion();
$db->close();

if (version_compare($varsion, '21', '>=')) {
$this->fixture = 'oci21.sql';
}

$db = $this->getConnection(true);

$this->insertTypeValues($db);

$query = (new Query($db))->from('type')->withTypecasting();

$result = $query->one();

$this->assertResultValues($result, $varsion);

$result = $query->all();

$this->assertResultValues($result[0], $varsion);

$db->close();
}

public function testCommandWithPhpTypecasting(): void
{
$db = $this->getConnection();
$varsion = $db->getServerInfo()->getVersion();
$db->close();

if (version_compare($varsion, '21', '>=')) {
$this->fixture = 'oci21.sql';
}

$db = $this->getConnection(true);

$this->insertTypeValues($db);

$command = $db->createCommand('SELECT * FROM "type"');

$result = $command->withPhpTypecasting()->queryOne();

$this->assertResultValues($result, $varsion);

$result = $command->withPhpTypecasting()->queryAll();

$this->assertResultValues($result[0], $varsion);

$db->close();
}

public function testSelectWithPhpTypecasting(): void
{
$db = $this->getConnection();

$sql = "SELECT null, 1, 2.5, 'string' FROM DUAL";

$expected = [
'NULL' => null,
1 => 1.0,
'2.5' => 2.5,
"'STRING'" => 'string',
];

$result = $db->createCommand($sql)
->withPhpTypecasting()
->queryOne();

$this->assertSame($expected, $result);

$result = $db->createCommand($sql)
->withPhpTypecasting()
->queryAll();

$this->assertSame([$expected], $result);

$result = $db->createCommand('SELECT 2.5 FROM DUAL')
->withPhpTypecasting()
->queryScalar();

$this->assertSame(2.5, $result);

$result = $db->createCommand('SELECT 2.5 FROM DUAL UNION SELECT 3.3 FROM DUAL')
->withPhpTypecasting()
->queryColumn();

$this->assertSame([2.5, 3.3], $result);

$db->close();
}

public function testPhpTypeCast(): void
{
$db = $this->getConnection();
Expand All @@ -39,26 +169,12 @@ public function testPhpTypeCast(): void

$db->close();
$db = $this->getConnection(true);

$command = $db->createCommand();
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('type');

$command->insert('type', [
'int_col' => 1,
'char_col' => str_repeat('x', 100),
'char_col3' => null,
'float_col' => 1.234,
'blob_col' => "\x10\x11\x12",
'timestamp_col' => new Expression("TIMESTAMP '2023-07-11 14:50:23'"),
'bool_col' => false,
'bit_col' => 0b0110_0110, // 102
'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]],
]);
$command->execute();
$query = (new Query($db))->from('type')->one();
$this->insertTypeValues($db);

$this->assertNotNull($tableSchema);
$query = (new Query($db))->from('type')->one();

$intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
$charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
Expand Down
Loading
Loading