From f77984479a96d3ef56cd144b70a11213b4bfe5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Semmler?= Date: Tue, 12 Jul 2022 13:38:06 +0200 Subject: [PATCH 1/4] Add buildDefinitionString as public method --- src/Definition/Snowflake.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Definition/Snowflake.php b/src/Definition/Snowflake.php index 9d6af4a..be1ba5c 100644 --- a/src/Definition/Snowflake.php +++ b/src/Definition/Snowflake.php @@ -102,12 +102,20 @@ public function __construct(string $type, array $options = []) parent::__construct($type, $options); } - public function getSQLDefinition(): string + + public function buildDefinitionString(): 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->buildDefinitionString(); if (!$this->isNullable()) { $definition .= ' NOT NULL'; } From e3b5717e4aa5db24034039733ddc8dee750b21c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Semmler?= Date: Tue, 12 Jul 2022 13:46:46 +0200 Subject: [PATCH 2/4] Fix tests --- tests/SnowflakeDatatypeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SnowflakeDatatypeTest.php b/tests/SnowflakeDatatypeTest.php index ffd77e7..7046186 100644 --- a/tests/SnowflakeDatatypeTest.php +++ b/tests/SnowflakeDatatypeTest.php @@ -113,10 +113,10 @@ public function testSqlDefinition(): void $this->assertTrue($definition->getSQLDefinition() === 'NUMERIC'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ(0)'); + $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ (0)'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']); - $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ(9)'); + $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ (9)'); $definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']); $this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ'); From 8c356314aa4f020fe3b751165fe0f17545c06c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Semmler?= Date: Wed, 13 Jul 2022 14:37:41 +0200 Subject: [PATCH 3/4] Add default value to snflk and add tests --- src/Definition/Exasol.php | 4 +-- src/Definition/Snowflake.php | 9 +++++-- tests/ExasolDatatypeTest.php | 33 +++++++++++++++++++++++- tests/SnowflakeDatatypeTest.php | 45 ++++++++++++++++++++++++++++----- 4 files changed, 80 insertions(+), 11 deletions(-) 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 be1ba5c..7e648c3 100644 --- a/src/Definition/Snowflake.php +++ b/src/Definition/Snowflake.php @@ -103,7 +103,7 @@ public function __construct(string $type, array $options = []) } - public function buildDefinitionString(): string + public function getTypeOnlySQLDefinition(): string { $out = $this->getType(); $length = $this->getLength(); @@ -115,10 +115,15 @@ public function buildDefinitionString(): string public function getSQLDefinition(): string { - $definition = $this->buildDefinitionString(); + $definition = $this->getTypeOnlySQLDefinition(); if (!$this->isNullable()) { $definition .= ' NOT NULL'; } + + if ($this->getDefault() !== null) { + $definition .= ' DEFAULT ' . $this->getDefault(); + } + return $definition; } 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 7046186..b97a23d 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, 'default' => '10']); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL DEFAULT 10'); + + $definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true, 'default' => '10']); + $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) DEFAULT 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'); } /** From a2dbaf729d00d474d75c1e67aa8e8923fda6caf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Semmler?= Date: Wed, 13 Jul 2022 15:45:38 +0200 Subject: [PATCH 4/4] Drop default value for snflk --- src/Definition/Snowflake.php | 6 +----- tests/SnowflakeDatatypeTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Definition/Snowflake.php b/src/Definition/Snowflake.php index 7e648c3..eff0b8d 100644 --- a/src/Definition/Snowflake.php +++ b/src/Definition/Snowflake.php @@ -119,12 +119,8 @@ public function getSQLDefinition(): string if (!$this->isNullable()) { $definition .= ' NOT NULL'; } - - if ($this->getDefault() !== null) { - $definition .= ' DEFAULT ' . $this->getDefault(); - } - return $definition; + // TODO default value by basetype } /** diff --git a/tests/SnowflakeDatatypeTest.php b/tests/SnowflakeDatatypeTest.php index b97a23d..904f4cd 100644 --- a/tests/SnowflakeDatatypeTest.php +++ b/tests/SnowflakeDatatypeTest.php @@ -115,11 +115,11 @@ public function testSqlDefinition(): void $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, 'default' => '10']); - $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL DEFAULT 10'); + $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, 'default' => '10']); - $this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) DEFAULT 10'); + $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)');