Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Definition/Exasol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions src/Definition/Snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
33 changes: 32 additions & 1 deletion tests/ExasolDatatypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -132,7 +141,7 @@ public function testSqlDefinition(string $type, array $options, string $expected
/**
* @return array<string, mixed[]>
*/
public function expectedSqlDefinitions(): array
public function expectedSqlDefinitionsTypesOnly(): array
{
return [
'DECIMAL' => ['DECIMAL', [], 'DECIMAL (36,18)'],
Expand Down Expand Up @@ -175,6 +184,28 @@ public function expectedSqlDefinitions(): array
];
}

/**
* @return array<string, mixed[]>
*/
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
Expand Down
45 changes: 39 additions & 6 deletions tests/SnowflakeDatatypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down