Skip to content

Commit 8c35631

Browse files
committed
Add default value to snflk and add tests
1 parent e3b5717 commit 8c35631

4 files changed

Lines changed: 80 additions & 11 deletions

File tree

src/Definition/Exasol.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function __construct(string $type, array $options = [])
182182

183183
public function getSQLDefinition(): string
184184
{
185-
$definition = $this->buildDefinitionString();
185+
$definition = $this->getTypeOnlySQLDefinition();
186186

187187
if ($this->getDefault() !== null) {
188188
$definition .= ' DEFAULT ' . $this->getDefault();
@@ -199,7 +199,7 @@ public function getSQLDefinition(): string
199199
* most of the types just append it, but some of them are complex and some have no length...
200200
* used here and in i/e lib
201201
*/
202-
public function buildDefinitionString(): string
202+
public function getTypeOnlySQLDefinition(): string
203203
{
204204
$type = $this->getType();
205205
$definition = strtoupper($type);

src/Definition/Snowflake.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(string $type, array $options = [])
103103
}
104104

105105

106-
public function buildDefinitionString(): string
106+
public function getTypeOnlySQLDefinition(): string
107107
{
108108
$out = $this->getType();
109109
$length = $this->getLength();
@@ -115,10 +115,15 @@ public function buildDefinitionString(): string
115115

116116
public function getSQLDefinition(): string
117117
{
118-
$definition = $this->buildDefinitionString();
118+
$definition = $this->getTypeOnlySQLDefinition();
119119
if (!$this->isNullable()) {
120120
$definition .= ' NOT NULL';
121121
}
122+
123+
if ($this->getDefault() !== null) {
124+
$definition .= ' DEFAULT ' . $this->getDefault();
125+
}
126+
122127
return $definition;
123128
}
124129

tests/ExasolDatatypeTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public function testInvalidType(): void
119119
new Exasol('UNKNOWN');
120120
}
121121

122+
/**
123+
* @dataProvider expectedSqlDefinitionsTypesOnly
124+
* @param mixed[] $options
125+
*/
126+
public function testTypeOnlySqlDefinition(string $type, array $options, string $expectedDefinition): void
127+
{
128+
$definition = new Exasol($type, $options);
129+
self::assertEquals($expectedDefinition, $definition->getTypeOnlySQLDefinition());
130+
}
122131
/**
123132
* @dataProvider expectedSqlDefinitions
124133
* @param mixed[] $options
@@ -132,7 +141,7 @@ public function testSqlDefinition(string $type, array $options, string $expected
132141
/**
133142
* @return array<string, mixed[]>
134143
*/
135-
public function expectedSqlDefinitions(): array
144+
public function expectedSqlDefinitionsTypesOnly(): array
136145
{
137146
return [
138147
'DECIMAL' => ['DECIMAL', [], 'DECIMAL (36,18)'],
@@ -175,6 +184,28 @@ public function expectedSqlDefinitions(): array
175184
];
176185
}
177186

187+
/**
188+
* @return array<string, mixed[]>
189+
*/
190+
public function expectedSqlDefinitions(): array
191+
{
192+
return array_merge(
193+
$this->expectedSqlDefinitionsTypesOnly(),
194+
[
195+
'NUMBER WITH LENGTH AND DEFAULT' => [
196+
'NUMBER',
197+
['length' => '20,20', 'default' => 10],
198+
'NUMBER (20,20) DEFAULT 10',
199+
],
200+
'NUMBER WITH LENGTH AND DEFAULT NULLABLE' => [
201+
'NUMBER',
202+
['length' => '20,20', 'default' => 10, 'nullable' => true],
203+
'NUMBER (20,20) DEFAULT 10',
204+
],
205+
]
206+
);
207+
}
208+
178209
/**
179210
* @dataProvider validLengths
180211
* @param mixed[] $extraOptions

tests/SnowflakeDatatypeTest.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,53 @@ public function testValidBinaryLengths(): void
109109

110110
public function testSqlDefinition(): void
111111
{
112-
$definition = new Snowflake('NUMERIC', ['length' => '']);
113-
$this->assertTrue($definition->getSQLDefinition() === 'NUMERIC');
112+
$definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]);
113+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC NOT NULL');
114+
115+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]);
116+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL');
117+
118+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false, 'default' => '10']);
119+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL DEFAULT 10');
120+
121+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true, 'default' => '10']);
122+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) DEFAULT 10');
123+
124+
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']);
125+
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (0)');
126+
127+
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']);
128+
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (9)');
129+
130+
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']);
131+
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ');
132+
133+
$definition = new Snowflake('TIMESTAMP_TZ');
134+
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ');
135+
}
136+
137+
public function testTypeOnlySqlDefinition(): void
138+
{
139+
$definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]);
140+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC');
141+
142+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]);
143+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)');
144+
145+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false, 'default' => '10']);
146+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)');
114147

115148
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']);
116-
$this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ (0)');
149+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (0)');
117150

118151
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']);
119-
$this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ (9)');
152+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (9)');
120153

121154
$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']);
122-
$this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ');
155+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ');
123156

124157
$definition = new Snowflake('TIMESTAMP_TZ');
125-
$this->assertTrue($definition->getSQLDefinition() === 'TIMESTAMP_TZ');
158+
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ');
126159
}
127160

128161
/**

0 commit comments

Comments
 (0)