Skip to content

Commit

Permalink
Update according changes in ColumnSchemaInterface (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 1, 2024
1 parent 1b2b7ba commit 55bbd32
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 5 additions & 11 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
(
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 27 additions & 4 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 55bbd32

Please sign in to comment.