Skip to content

Commit 82022b4

Browse files
authored
Merge pull request #59 from keboola/jirka-add-build-definition-as-public-method
Add buildDefinitionString as public method
2 parents 277f9cd + a2dbaf7 commit 82022b4

4 files changed

Lines changed: 86 additions & 13 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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,25 @@ public function __construct(string $type, array $options = [])
102102
parent::__construct($type, $options);
103103
}
104104

105-
public function getSQLDefinition(): string
105+
106+
public function getTypeOnlySQLDefinition(): string
106107
{
107-
$definition = $this->getType();
108-
if ($this->getLength() !== null && $this->getLength() !== '') {
109-
$definition .= '(' . $this->getLength() . ')';
108+
$out = $this->getType();
109+
$length = $this->getLength();
110+
if ($length !== null && $length !== '') {
111+
$out .= ' (' . $length . ')';
110112
}
113+
return $out;
114+
}
115+
116+
public function getSQLDefinition(): string
117+
{
118+
$definition = $this->getTypeOnlySQLDefinition();
111119
if (!$this->isNullable()) {
112120
$definition .= ' NOT NULL';
113121
}
114122
return $definition;
123+
// TODO default value by basetype
115124
}
116125

117126
/**

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]);
119+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL');
120+
121+
$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true]);
122+
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,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)