Skip to content

Commit 2589844

Browse files
authored
Merge pull request #58 from keboola/mj-KBC-2780-basetype-to-type
KBC-2780 basetype to type mapping
2 parents 82022b4 + 1014bb3 commit 2589844

14 files changed

Lines changed: 192 additions & 0 deletions

src/Definition/BaseType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@ class BaseType
1313
public const NUMERIC = 'NUMERIC';
1414
public const STRING = 'STRING';
1515
public const TIMESTAMP = 'TIMESTAMP';
16+
17+
public const TYPES = [
18+
self::BOOLEAN,
19+
self::DATE,
20+
self::FLOAT,
21+
self::INTEGER,
22+
self::NUMERIC,
23+
self::STRING,
24+
self::TIMESTAMP,
25+
];
26+
27+
public static function isValid(string $basetype): bool
28+
{
29+
return array_key_exists($basetype, array_flip(self::TYPES));
30+
}
1631
}

src/Definition/Common.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ abstract public function getBasetype(): string;
7171
*/
7272
abstract public function toArray(): array;
7373

74+
abstract public static function getTypeByBasetype(string $basetype): string;
75+
7476
/**
7577
* @param string|int|null $length
7678
*/

src/Definition/DefinitionInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public function getSQLDefinition(): string;
1414
public function toArray(): array;
1515

1616
public function getBasetype(): string;
17+
18+
public static function getTypeByBasetype(string $basetype): string;
1719
}

src/Definition/Exasol.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
88
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
99
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
10+
use LogicException;
1011

1112
/**
1213
* Class Exasol
@@ -407,4 +408,32 @@ public function getBasetype(): string
407408
}
408409
return $basetype;
409410
}
411+
412+
public static function getTypeByBasetype(string $basetype): string
413+
{
414+
$basetype = strtoupper($basetype);
415+
416+
if (!BaseType::isValid($basetype)) {
417+
throw new InvalidTypeException(sprintf('Base type "%s" is not valid.', $basetype));
418+
}
419+
420+
switch ($basetype) {
421+
case BaseType::BOOLEAN:
422+
return self::TYPE_BOOLEAN;
423+
case BaseType::DATE:
424+
return self::TYPE_DATE;
425+
case BaseType::FLOAT:
426+
return self::TYPE_DOUBLE_PRECISION;
427+
case BaseType::INTEGER:
428+
return self::TYPE_INTEGER;
429+
case BaseType::NUMERIC:
430+
return self::TYPE_DECIMAL;
431+
case BaseType::STRING:
432+
return self::TYPE_VARCHAR;
433+
case BaseType::TIMESTAMP:
434+
return self::TYPE_TIMESTAMP;
435+
}
436+
437+
throw new LogicException(sprintf('Definition for base type "%s" is missing.', $basetype));
438+
}
410439
}

src/Definition/GenericStorage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Keboola\Datatype\Definition;
66

7+
use LogicException;
8+
79
class GenericStorage extends Common
810
{
911
public const DATE_TYPES = ['date'];
@@ -130,4 +132,9 @@ public function getBasetype(): string
130132
}
131133
return $baseType;
132134
}
135+
136+
public static function getTypeByBasetype(string $basetype): string
137+
{
138+
throw new LogicException('Method is not implemented yet.');
139+
}
133140
}

src/Definition/MySQL.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
88
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
99
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
10+
use LogicException;
1011

1112
class MySQL extends Common
1213
{
@@ -301,4 +302,9 @@ public function getBasetype(): string
301302
}
302303
return $basetype;
303304
}
305+
306+
public static function getTypeByBasetype(string $basetype): string
307+
{
308+
throw new LogicException('Method is not implemented yet.');
309+
}
304310
}

src/Definition/Redshift.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
99
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
1010
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
11+
use LogicException;
1112

1213
class Redshift extends Common
1314
{
@@ -376,6 +377,11 @@ public function getBasetype(): string
376377
return $basetype;
377378
}
378379

380+
public static function getTypeByBasetype(string $basetype): string
381+
{
382+
throw new LogicException('Method is not implemented yet.');
383+
}
384+
379385
/**
380386
* @return array<int, array{key:string,value:mixed}>
381387
*/

src/Definition/Snowflake.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
88
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
99
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
10+
use LogicException;
1011

1112
class Snowflake extends Common
1213
{
@@ -330,4 +331,32 @@ public function getBasetype(): string
330331
}
331332
return $basetype;
332333
}
334+
335+
public static function getTypeByBasetype(string $basetype): string
336+
{
337+
$basetype = strtoupper($basetype);
338+
339+
if (!BaseType::isValid($basetype)) {
340+
throw new InvalidTypeException(sprintf('Base type "%s" is not valid.', $basetype));
341+
}
342+
343+
switch ($basetype) {
344+
case BaseType::BOOLEAN:
345+
return self::TYPE_BOOLEAN;
346+
case BaseType::DATE:
347+
return self::TYPE_DATE;
348+
case BaseType::FLOAT:
349+
return self::TYPE_FLOAT;
350+
case BaseType::INTEGER:
351+
return self::TYPE_INTEGER;
352+
case BaseType::NUMERIC:
353+
return self::TYPE_NUMBER;
354+
case BaseType::STRING:
355+
return self::TYPE_VARCHAR;
356+
case BaseType::TIMESTAMP:
357+
return self::TYPE_TIMESTAMP;
358+
}
359+
360+
throw new LogicException(sprintf('Definition for base type "%s" is missing.', $basetype));
361+
}
333362
}

src/Definition/Synapse.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
88
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
99
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
10+
use LogicException;
1011

1112
/**
1213
* Class Synapse
@@ -271,4 +272,32 @@ public function getBasetype(): string
271272
}
272273
return $basetype;
273274
}
275+
276+
public static function getTypeByBasetype(string $basetype): string
277+
{
278+
$basetype = strtoupper($basetype);
279+
280+
if (!BaseType::isValid($basetype)) {
281+
throw new InvalidTypeException(sprintf('Base type "%s" is not valid.', $basetype));
282+
}
283+
284+
switch ($basetype) {
285+
case BaseType::BOOLEAN:
286+
return self::TYPE_BIT;
287+
case BaseType::DATE:
288+
return self::TYPE_DATE;
289+
case BaseType::FLOAT:
290+
return self::TYPE_FLOAT;
291+
case BaseType::INTEGER:
292+
return self::TYPE_INT;
293+
case BaseType::NUMERIC:
294+
return self::TYPE_NUMERIC;
295+
case BaseType::STRING:
296+
return self::TYPE_NVARCHAR;
297+
case BaseType::TIMESTAMP:
298+
return self::TYPE_DATETIMEOFFSET;
299+
}
300+
301+
throw new LogicException(sprintf('Definition for base type "%s" is missing.', $basetype));
302+
}
274303
}

src/Definition/Teradata.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Keboola\Datatype\Definition\Exception\InvalidLengthException;
99
use Keboola\Datatype\Definition\Exception\InvalidOptionException;
1010
use Keboola\Datatype\Definition\Exception\InvalidTypeException;
11+
use LogicException;
1112

1213
/**
1314
* Class Teradata
@@ -673,4 +674,9 @@ public function getBasetype(): string
673674
}
674675
return $basetype;
675676
}
677+
678+
public static function getTypeByBasetype(string $basetype): string
679+
{
680+
throw new LogicException('Method is not implemented yet.');
681+
}
676682
}

0 commit comments

Comments
 (0)