Skip to content

Commit a49845d

Browse files
authored
Merge pull request #49 from keboola/jirka-td-generate-type-and-length
externalize method to generate type with length
2 parents 85edbfd + fce748f commit a49845d

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/Definition/Teradata.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,30 @@ public static function convertCodeToType($code)
348348
* @return string
349349
*/
350350
public function getSQLDefinition()
351+
{
352+
$definition = $this->buildTypeWithLength();
353+
354+
if (!$this->isNullable()) {
355+
$definition .= ' NOT NULL';
356+
}
357+
if ($this->getDefault() !== null) {
358+
$definition .= ' DEFAULT ' . $this->getDefault();
359+
}
360+
361+
if (in_array($this->getType(), self::CHARACTER_SET_TYPES, true)) {
362+
$definition .= ' CHARACTER SET ' . ($this->isLatin() ? 'LATIN' : 'UNICODE');
363+
}
364+
365+
return $definition;
366+
}
367+
368+
/**
369+
* generates type with length
370+
* most of the types just append it, but some of them are complex and some have no length...
371+
* used here and in i/e lib
372+
* @return string
373+
*/
374+
public function buildTypeWithLength()
351375
{
352376
$type = $this->getType();
353377
$definition = $type;
@@ -364,17 +388,6 @@ public function getSQLDefinition()
364388
}
365389
}
366390

367-
if (!$this->isNullable()) {
368-
$definition .= ' NOT NULL';
369-
}
370-
if ($this->getDefault() !== null) {
371-
$definition .= ' DEFAULT ' . $this->getDefault();
372-
}
373-
374-
if (in_array($this->getType(), self::CHARACTER_SET_TYPES, true)) {
375-
$definition .= ' CHARACTER SET ' . ($this->isLatin() ? 'LATIN' : 'UNICODE');
376-
}
377-
378391
return $definition;
379392
}
380393

tests/TeradataDatatypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ public function testSqlDefinition($type, $options, $expectedDefinition)
110110
self::assertEquals($expectedDefinition . $suffix, $definition->getSQLDefinition());
111111
}
112112

113+
/**
114+
* @param string $type
115+
* @param array|null $options
116+
* @param string $expectedDefinition
117+
* @dataProvider expectedSqlDefinitions
118+
*/
119+
public function testBuildTypeWithLength($type, $options, $expectedDefinition)
120+
{
121+
$definition = new Teradata($type, $options);
122+
self::assertEquals($expectedDefinition, $definition->buildTypeWithLength());
123+
}
124+
113125

114126
public function testSqlDefinitionWhenLatin()
115127
{

0 commit comments

Comments
 (0)