diff --git a/src/Definition/Exasol.php b/src/Definition/Exasol.php index 653f165..9406e71 100644 --- a/src/Definition/Exasol.php +++ b/src/Definition/Exasol.php @@ -182,7 +182,7 @@ public function __construct(string $type, array $options = []) public function getSQLDefinition(): string { - $definition = $this->buildDefinitionString(); + $definition = $this->getTypeOnlySQLDefinition(); if ($this->getDefault() !== null) { $definition .= ' DEFAULT ' . $this->getDefault(); @@ -199,7 +199,7 @@ public function getSQLDefinition(): string * most of the types just append it, but some of them are complex and some have no length... * used here and in i/e lib */ - public function buildDefinitionString(): string + public function getTypeOnlySQLDefinition(): string { $type = $this->getType(); $definition = strtoupper($type); diff --git a/src/Definition/Snowflake.php b/src/Definition/Snowflake.php index 9d6af4a..eff0b8d 100644 --- a/src/Definition/Snowflake.php +++ b/src/Definition/Snowflake.php @@ -102,16 +102,25 @@ public function __construct(string $type, array $options = []) parent::__construct($type, $options); } - public function getSQLDefinition(): string + + public function getTypeOnlySQLDefinition(): string { - $definition = $this->getType(); - if ($this->getLength() !== null && $this->getLength() !== '') { - $definition .= '(' . $this->getLength() . ')'; + $out = $this->getType(); + $length = $this->getLength(); + if ($length !== null && $length !== '') { + $out .= ' (' . $length . ')'; } + return $out; + } + + public function getSQLDefinition(): string + { + $definition = $this->getTypeOnlySQLDefinition(); if (!$this->isNullable()) { $definition .= ' NOT NULL'; } return $definition; + // TODO default value by basetype } /** diff --git a/tests/ExasolDatatypeTest.php b/tests/ExasolDatatypeTest.php index fc4b061..4d29aeb 100644 --- a/tests/ExasolDatatypeTest.php +++ b/tests/ExasolDatatypeTest.php @@ -119,6 +119,15 @@ public function testInvalidType(): void new Exasol('UNKNOWN'); } + /** + * @dataProvider expectedSqlDefinitionsTypesOnly + * @param mixed[] $options + */ + public function testTypeOnlySqlDefinition(string $type, array $options, string $expectedDefinition): void + { + $definition = new Exasol($type, $options); + self::assertEquals($expectedDefinition, $definition->getTypeOnlySQLDefinition()); + } /** * @dataProvider expectedSqlDefinitions * @param mixed[] $options @@ -132,7 +141,7 @@ public function testSqlDefinition(string $type, array $options, string $expected /** * @return array */ - public function expectedSqlDefinitions(): array + public function expectedSqlDefinitionsTypesOnly(): array { return [ 'DECIMAL' => ['DECIMAL', [], 'DECIMAL (36,18)'], @@ -175,6 +184,28 @@ public function expectedSqlDefinitions(): array ]; } + /** + * @return array + */ + public function expectedSqlDefinitions(): array + { + return array_merge( + $this->expectedSqlDefinitionsTypesOnly(), + [ + 'NUMBER WITH LENGTH AND DEFAULT' => [ + 'NUMBER', + ['length' => '20,20', 'default' => 10], + 'NUMBER (20,20) DEFAULT 10', + ], + 'NUMBER WITH LENGTH AND DEFAULT NULLABLE' => [ + 'NUMBER', + ['length' => '20,20', 'default' => 10, 'nullable' => true], + 'NUMBER (20,20) DEFAULT 10', + ], + ] + ); + } + /** * @dataProvider validLengths * @param mixed[] $extraOptions diff --git a/tests/SnowflakeDatatypeTest.php b/tests/SnowflakeDatatypeTest.php index ffd77e7..904f4cd 100644 --- a/tests/SnowflakeDatatypeTest.php +++ b/tests/SnowflakeDatatypeTest.php @@ -109,20 +109,53 @@ public function testValidBinaryLengths(): void public function testSqlDefinition(): void { - $definition = new Snowflake('NUMERIC', ['length' => '']); - $this->assertTrue($definition->getSQLDefinition() === 'NUMERIC'); + $definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC NOT NULL'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true]); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10)'); + + $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']); + $this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (0)'); + + $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']); + $this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (9)'); + + $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']); + $this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ'); + + $definition = new Snowflake('TIMESTAMP_TZ'); + $this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ'); + } + + public function testTypeOnlySqlDefinition(): void + { + $definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false, 'default' => '10']); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ(0)'); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (0)'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ(9)'); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (9)'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ'); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ'); $definition = new Snowflake('TIMESTAMP_TZ'); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ'); + $this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ'); } /**