Skip to content

Commit

Permalink
Merge pull request #7 from keboola/mj-teradata-basetypes
Browse files Browse the repository at this point in the history
Teradata by-basetype conversion
  • Loading branch information
martinjunger authored Nov 10, 2022
2 parents a66c962 + e23e302 commit 8e6caff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/php-datatypes/src/Definition/Teradata.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,29 @@ public function getBasetype(): string

public static function getTypeByBasetype(string $basetype): string
{
throw new LogicException('Method is not implemented yet.');
$basetype = strtoupper($basetype);

if (!BaseType::isValid($basetype)) {
throw new InvalidTypeException(sprintf('Base type "%s" is not valid.', $basetype));
}

switch ($basetype) {
case BaseType::BOOLEAN:
return self::TYPE_BYTEINT;
case BaseType::DATE:
return self::TYPE_DATE;
case BaseType::FLOAT:
return self::TYPE_FLOAT;
case BaseType::INTEGER:
return self::TYPE_INTEGER;
case BaseType::NUMERIC:
return self::TYPE_NUMBER;
case BaseType::STRING:
return self::TYPE_VARCHAR;
case BaseType::TIMESTAMP:
return self::TYPE_TIMESTAMP;
}

throw new LogicException(sprintf('Definition for base type "%s" is missing.', $basetype));
}
}
14 changes: 14 additions & 0 deletions packages/php-datatypes/tests/TeradataDatatypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,18 @@ public function invalidLengths(): array
['INTERVAL YEAR TO MONTH', '5'],
];
}

public function testGetTypeByBasetype(): void
{
$this->assertSame('DATE', Teradata::getTypeByBasetype('DATE'));

$this->assertSame('VARCHAR', Teradata::getTypeByBasetype('STRING'));

// not only upper case
$this->assertSame('DATE', Teradata::getTypeByBasetype('Date'));

$this->expectException(InvalidTypeException::class);
$this->expectExceptionMessage('Base type "FOO" is not valid.');
Teradata::getTypeByBasetype('foo');
}
}

0 comments on commit 8e6caff

Please sign in to comment.