Skip to content

Commit 55bbd32

Browse files
authored
Update according changes in ColumnSchemaInterface (#281)
1 parent 1b2b7ba commit 55bbd32

File tree

6 files changed

+70
-73
lines changed

6 files changed

+70
-73
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Enh #276: Implement `ColumnFactory` class (@Tigrov)
1515
- Enh #279: Separate column type constants (@Tigrov)
1616
- Enh #280: Realize `ColumnBuilder` class (@Tigrov)
17+
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
1718

1819
## 1.3.0 March 21, 2024
1920

src/Column/ColumnFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function getType(string $dbType, array $info = []): string
6363

6464
$dbType = preg_replace('/\([^)]+\)/', '', $dbType);
6565

66-
if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) {
66+
if ($dbType === 'interval day to second' && isset($info['size']) && $info['size'] > 0) {
6767
return ColumnType::STRING;
6868
}
6969

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra
5454
$returnParams[$phName]['dataType'] = PDO::PARAM_INT;
5555
}
5656

57-
$returnParams[$phName]['size'] = isset($columnSchemas[$name]) ? $columnSchemas[$name]->getSize() : -1;
57+
$returnParams[$phName]['size'] = ($columnSchemas[$name]?->getSize() ?? 3998) + 2;
5858

5959
$returning[] = $this->db->getQuoter()->quoteColumnName($name);
6060
}

src/Schema.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@
3939
* @psalm-type ColumnInfoArray = array{
4040
* column_name: string,
4141
* data_type: string,
42-
* data_precision: string|null,
4342
* data_scale: string|null,
44-
* data_length: string,
43+
* size: string,
4544
* nullable: string,
4645
* data_default: string|null,
4746
* is_pk: string|null,
@@ -331,14 +330,9 @@ protected function findColumns(TableSchemaInterface $table): bool
331330
SELECT
332331
A.COLUMN_NAME,
333332
A.DATA_TYPE,
334-
A.DATA_PRECISION,
335333
A.DATA_SCALE,
336334
A.IDENTITY_COLUMN,
337-
(
338-
CASE A.CHAR_USED WHEN 'C' THEN A.CHAR_LENGTH
339-
ELSE A.DATA_LENGTH
340-
END
341-
) AS DATA_LENGTH,
335+
(CASE WHEN A.CHAR_LENGTH > 0 THEN A.CHAR_LENGTH ELSE A.DATA_PRECISION END) AS "size",
342336
A.NULLABLE,
343337
A.DATA_DEFAULT,
344338
(
@@ -429,14 +423,14 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
429423
$dbType = $info['data_type'];
430424
$column = $columnFactory->fromDbType($dbType, [
431425
'scale' => $info['data_scale'],
432-
'precision' => $info['data_precision'],
426+
'size' => $info['size'],
433427
]);
428+
/** @psalm-suppress DeprecatedMethod */
434429
$column->name($info['column_name']);
435-
$column->allowNull($info['nullable'] === 'Y');
430+
$column->notNull($info['nullable'] !== 'Y');
436431
$column->comment($info['column_comment']);
437432
$column->primaryKey((bool) $info['is_pk']);
438433
$column->autoIncrement($info['identity_column'] === 'YES');
439-
$column->size((int) $info['data_length']);
440434
$column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column));
441435

442436
return $column;

tests/CommandTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,45 @@ public function testInsertWithReturningPksWithPrimaryKeyString(): void
359359
$command = $db->createCommand();
360360
$schema = $db->getSchema();
361361

362-
if ($schema->getTableSchema('{{test_insert_ex_string}}') !== null) {
363-
$command->dropTable('{{test_insert_ex_string}}')->execute();
362+
if ($schema->getTableSchema('{{test_insert_pk}}') !== null) {
363+
$command->dropTable('{{test_insert_pk}}')->execute();
364364
}
365365

366366
$command->createTable(
367-
'{{test_insert_ex_string}}',
367+
'{{test_insert_pk}}',
368368
['id' => 'varchar(10) primary key', 'name' => 'varchar(10)'],
369369
)->execute();
370370

371-
$result = $command->insertWithReturningPks('{{test_insert_ex_string}}', ['id' => '1', 'name' => 'test']);
371+
$result = $command->insertWithReturningPks('{{test_insert_pk}}', ['id' => '1', 'name' => 'test']);
372372

373373
$this->assertSame(['id' => '1'], $result);
374374

375375
$db->close();
376376
}
377377

378+
public function testInsertWithReturningPksWithPrimaryKeySignedDecimal(): void
379+
{
380+
$db = $this->getConnection();
381+
382+
$command = $db->createCommand();
383+
$schema = $db->getSchema();
384+
385+
if ($schema->getTableSchema('{{test_insert_pk}}') !== null) {
386+
$command->dropTable('{{test_insert_pk}}')->execute();
387+
}
388+
389+
$command->createTable(
390+
'{{test_insert_pk}}',
391+
['id' => 'number(5,2) primary key', 'name' => 'varchar(10)'],
392+
)->execute();
393+
394+
$result = $command->insertWithReturningPks('{{test_insert_pk}}', ['id' => '-123.45', 'name' => 'test']);
395+
396+
$this->assertSame(['id' => '-123.45'], $result);
397+
398+
$db->close();
399+
}
400+
378401
/**
379402
* @throws Exception
380403
* @throws InvalidConfigException

0 commit comments

Comments
 (0)