diff --git a/CHANGELOG.md b/CHANGELOG.md index 267fc1d..3f54222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Enh #276: Implement `ColumnFactory` class (@Tigrov) - Enh #279: Separate column type constants (@Tigrov) - Enh #280: Realize `ColumnBuilder` class (@Tigrov) +- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 30a0407..0d188e0 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -63,7 +63,7 @@ protected function getType(string $dbType, array $info = []): string $dbType = preg_replace('/\([^)]+\)/', '', $dbType); - if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) { + if ($dbType === 'interval day to second' && isset($info['size']) && $info['size'] > 0) { return ColumnType::STRING; } diff --git a/src/Command.php b/src/Command.php index 15386b3..820d197 100644 --- a/src/Command.php +++ b/src/Command.php @@ -54,7 +54,7 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra $returnParams[$phName]['dataType'] = PDO::PARAM_INT; } - $returnParams[$phName]['size'] = isset($columnSchemas[$name]) ? $columnSchemas[$name]->getSize() : -1; + $returnParams[$phName]['size'] = ($columnSchemas[$name]?->getSize() ?? 3998) + 2; $returning[] = $this->db->getQuoter()->quoteColumnName($name); } diff --git a/src/Schema.php b/src/Schema.php index fdd0df7..b5d1f71 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -39,9 +39,8 @@ * @psalm-type ColumnInfoArray = array{ * column_name: string, * data_type: string, - * data_precision: string|null, * data_scale: string|null, - * data_length: string, + * size: string, * nullable: string, * data_default: string|null, * is_pk: string|null, @@ -331,14 +330,9 @@ protected function findColumns(TableSchemaInterface $table): bool SELECT A.COLUMN_NAME, A.DATA_TYPE, - A.DATA_PRECISION, A.DATA_SCALE, A.IDENTITY_COLUMN, - ( - CASE A.CHAR_USED WHEN 'C' THEN A.CHAR_LENGTH - ELSE A.DATA_LENGTH - END - ) AS DATA_LENGTH, + (CASE WHEN A.CHAR_LENGTH > 0 THEN A.CHAR_LENGTH ELSE A.DATA_PRECISION END) AS "size", A.NULLABLE, A.DATA_DEFAULT, ( @@ -429,14 +423,14 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $dbType = $info['data_type']; $column = $columnFactory->fromDbType($dbType, [ 'scale' => $info['data_scale'], - 'precision' => $info['data_precision'], + 'size' => $info['size'], ]); + /** @psalm-suppress DeprecatedMethod */ $column->name($info['column_name']); - $column->allowNull($info['nullable'] === 'Y'); + $column->notNull($info['nullable'] !== 'Y'); $column->comment($info['column_comment']); $column->primaryKey((bool) $info['is_pk']); $column->autoIncrement($info['identity_column'] === 'YES'); - $column->size((int) $info['data_length']); $column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column)); return $column; diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 8bdc7b1..7062677 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -359,22 +359,45 @@ public function testInsertWithReturningPksWithPrimaryKeyString(): void $command = $db->createCommand(); $schema = $db->getSchema(); - if ($schema->getTableSchema('{{test_insert_ex_string}}') !== null) { - $command->dropTable('{{test_insert_ex_string}}')->execute(); + if ($schema->getTableSchema('{{test_insert_pk}}') !== null) { + $command->dropTable('{{test_insert_pk}}')->execute(); } $command->createTable( - '{{test_insert_ex_string}}', + '{{test_insert_pk}}', ['id' => 'varchar(10) primary key', 'name' => 'varchar(10)'], )->execute(); - $result = $command->insertWithReturningPks('{{test_insert_ex_string}}', ['id' => '1', 'name' => 'test']); + $result = $command->insertWithReturningPks('{{test_insert_pk}}', ['id' => '1', 'name' => 'test']); $this->assertSame(['id' => '1'], $result); $db->close(); } + public function testInsertWithReturningPksWithPrimaryKeySignedDecimal(): void + { + $db = $this->getConnection(); + + $command = $db->createCommand(); + $schema = $db->getSchema(); + + if ($schema->getTableSchema('{{test_insert_pk}}') !== null) { + $command->dropTable('{{test_insert_pk}}')->execute(); + } + + $command->createTable( + '{{test_insert_pk}}', + ['id' => 'number(5,2) primary key', 'name' => 'varchar(10)'], + )->execute(); + + $result = $command->insertWithReturningPks('{{test_insert_pk}}', ['id' => '-123.45', 'name' => 'test']); + + $this->assertSame(['id' => '-123.45'], $result); + + $db->close(); + } + /** * @throws Exception * @throws InvalidConfigException diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index fcdb634..ab426a0 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -20,11 +20,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => null, + 'size' => null, 'scale' => 0, 'defaultValue' => null, ], @@ -33,11 +32,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => null, + 'size' => null, 'scale' => 0, 'defaultValue' => 1, ], @@ -46,11 +44,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => 3, + 'size' => 3, 'scale' => 0, 'defaultValue' => 1, ], @@ -59,11 +56,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => null, + 'size' => null, 'scale' => 0, 'defaultValue' => 1, ], @@ -72,11 +68,10 @@ public static function columns(): array 'dbType' => 'CHAR', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 100, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -85,11 +80,10 @@ public static function columns(): array 'dbType' => 'VARCHAR2', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 100, - 'precision' => null, 'scale' => null, 'defaultValue' => 'some\'thing', ], @@ -98,11 +92,10 @@ public static function columns(): array 'dbType' => 'VARCHAR2', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 4000, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -111,11 +104,10 @@ public static function columns(): array 'dbType' => 'NVARCHAR2', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 100, - 'precision' => null, 'scale' => null, 'defaultValue' => '', ], @@ -124,11 +116,10 @@ public static function columns(): array 'dbType' => 'FLOAT', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => 126, + 'size' => 126, 'scale' => null, 'defaultValue' => null, ], @@ -137,11 +128,10 @@ public static function columns(): array 'dbType' => 'FLOAT', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => 126, + 'size' => 126, 'scale' => null, 'defaultValue' => 1.23, ], @@ -150,11 +140,10 @@ public static function columns(): array 'dbType' => 'BLOB', 'phpType' => 'mixed', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 4000, - 'precision' => null, + 'size' => null, 'scale' => null, 'defaultValue' => null, ], @@ -163,11 +152,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => 5, + 'size' => 5, 'scale' => 2, 'defaultValue' => 33.22, ], @@ -176,11 +164,10 @@ public static function columns(): array 'dbType' => 'TIMESTAMP(6)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 11, - 'precision' => null, + 'size' => null, 'scale' => 6, 'defaultValue' => "to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')", ], @@ -189,11 +176,10 @@ public static function columns(): array 'dbType' => 'INTERVAL DAY(0) TO SECOND(0)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 11, - 'precision' => 0, + 'size' => 0, 'scale' => 0, 'defaultValue' => "INTERVAL '0 10:33:21' DAY(0) TO SECOND(0)", ], @@ -202,11 +188,10 @@ public static function columns(): array 'dbType' => 'INTERVAL DAY(1) TO SECOND(0)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 11, - 'precision' => 1, + 'size' => 1, 'scale' => 0, 'defaultValue' => "INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)", ], @@ -215,11 +200,10 @@ public static function columns(): array 'dbType' => 'CHAR', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -228,11 +212,10 @@ public static function columns(): array 'dbType' => 'CHAR', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => null, 'scale' => null, 'defaultValue' => '1', ], @@ -241,11 +224,10 @@ public static function columns(): array 'dbType' => 'TIMESTAMP(6)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 11, - 'precision' => null, + 'size' => null, 'scale' => 6, 'defaultValue' => new Expression('CURRENT_TIMESTAMP'), ], @@ -254,11 +236,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, - 'size' => 22, - 'precision' => 3, + 'size' => 3, 'scale' => 0, 'defaultValue' => 130, // b'10000010' ], @@ -272,11 +253,10 @@ public static function columns(): array 'dbType' => 'NUMBER', 'phpType' => 'int', 'primaryKey' => true, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => true, 'enumValues' => null, - 'size' => 22, - 'precision' => null, + 'size' => null, 'scale' => 0, 'defaultValue' => null, ], @@ -285,11 +265,10 @@ public static function columns(): array 'dbType' => 'VARCHAR2', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 255, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ],