Skip to content

Commit

Permalink
Merge pull request #130 from keboola/jirka-CT-1285-add-new-types
Browse files Browse the repository at this point in the history
CT-1285 add varying types
  • Loading branch information
jirkasemmler authored Feb 27, 2024
2 parents 1b4307e + c09367d commit 2b998cf
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 22 deletions.
32 changes: 28 additions & 4 deletions packages/php-datatypes/src/Definition/Snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

class Snowflake extends Common
{
public const TYPES_WITH_COMPLEX_LENGTH = [self::TYPE_NUMBER, self::TYPE_DECIMAL, self::TYPE_NUMERIC];
public const TYPES_WITH_COMPLEX_LENGTH = [
self::TYPE_NUMBER,
self::TYPE_DECIMAL,
self::TYPE_DEC,
self::TYPE_NUMERIC,
];
public const METADATA_BACKEND = 'snowflake';
public const TYPE_NUMBER = 'NUMBER';
public const TYPE_DEC = 'DEC';
public const TYPE_DECIMAL = 'DECIMAL';
public const TYPE_NUMERIC = 'NUMERIC';
public const TYPE_INT = 'INT';
Expand All @@ -31,6 +37,12 @@ class Snowflake extends Common
public const TYPE_VARCHAR = 'VARCHAR';
public const TYPE_CHAR = 'CHAR';
public const TYPE_CHARACTER = 'CHARACTER';
public const TYPE_CHAR_VARYING = 'CHAR VARYING';
public const TYPE_CHARACTER_VARYING = 'CHARACTER VARYING';
public const TYPE_NCHAR_VARYING = 'NCHAR VARYING';
public const TYPE_NCHAR = 'NCHAR';
public const TYPE_NVARCHAR = 'NVARCHAR';
public const TYPE_NVARCHAR2 = 'NVARCHAR2';
public const TYPE_STRING = 'STRING';
public const TYPE_TEXT = 'TEXT';
public const TYPE_BOOLEAN = 'BOOLEAN';
Expand All @@ -48,9 +60,9 @@ class Snowflake extends Common
public const TYPE_ARRAY = 'ARRAY';
public const TYPE_GEOGRAPHY = 'GEOGRAPHY';
public const TYPE_GEOMETRY = 'GEOMETRY';

public const TYPES = [
self::TYPE_NUMBER,
self::TYPE_DEC,
self::TYPE_DECIMAL,
self::TYPE_NUMERIC,
self::TYPE_INT,
Expand All @@ -68,8 +80,14 @@ class Snowflake extends Common
self::TYPE_VARCHAR,
self::TYPE_CHAR,
self::TYPE_CHARACTER,
self::TYPE_CHAR_VARYING,
self::TYPE_CHARACTER_VARYING,
self::TYPE_STRING,
self::TYPE_TEXT,
self::TYPE_NCHAR_VARYING,
self::TYPE_NCHAR,
self::TYPE_NVARCHAR,
self::TYPE_NVARCHAR2,
self::TYPE_BOOLEAN,
self::TYPE_DATE,
self::TYPE_DATETIME,
Expand All @@ -86,7 +104,6 @@ class Snowflake extends Common
self::TYPE_GEOGRAPHY,
self::TYPE_GEOMETRY,
];

public const MAX_VARCHAR_LENGTH = 16777216;
public const MAX_VARBINARY_LENGTH = 8388608;

Expand Down Expand Up @@ -117,7 +134,6 @@ public function __construct(string $type, array $options = [])
parent::__construct($type, $options);
}


public function getTypeOnlySQLDefinition(): string
{
$out = $this->getType();
Expand Down Expand Up @@ -251,6 +267,7 @@ private function validateLength(string $type, $length = null): void
$valid = true;
switch (strtoupper($type)) {
case self::TYPE_NUMBER:
case self::TYPE_DEC:
case self::TYPE_DECIMAL:
case self::TYPE_NUMERIC:
if (is_null($length) || $length === '') {
Expand Down Expand Up @@ -281,8 +298,14 @@ private function validateLength(string $type, $length = null): void
case self::TYPE_VARCHAR:
case self::TYPE_CHAR:
case self::TYPE_CHARACTER:
case self::TYPE_CHAR_VARYING:
case self::TYPE_CHARACTER_VARYING:
case self::TYPE_STRING:
case self::TYPE_TEXT:
case self::TYPE_NCHAR_VARYING:
case self::TYPE_NCHAR:
case self::TYPE_NVARCHAR:
case self::TYPE_NVARCHAR2:
if (is_null($length) || $length === '') {
break;
}
Expand Down Expand Up @@ -352,6 +375,7 @@ public function getBasetype(): string
break;
case self::TYPE_NUMBER:
case self::TYPE_DECIMAL:
case self::TYPE_DEC:
case self::TYPE_NUMERIC:
$basetype = BaseType::NUMERIC;
break;
Expand Down
43 changes: 25 additions & 18 deletions packages/php-datatypes/tests/SnowflakeDatatypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,61 +115,67 @@ public function testValidBinaryLengths(): void
public function testSqlDefinition(): void
{
$definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC NOT NULL');
$this->assertSame('NUMERIC NOT NULL', $definition->getSQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL');
$this->assertSame('NUMERIC (10,10) NOT NULL', $definition->getSQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) NOT NULL');
$this->assertSame('NUMERIC (10,10) NOT NULL', $definition->getSQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true]);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10)');
$this->assertSame('NUMERIC (10,10)', $definition->getSQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true, 'default' => '10']);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10) DEFAULT 10');
$this->assertSame('NUMERIC (10,10) DEFAULT 10', $definition->getSQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => true, 'default' => '']);
$this->assertSame($definition->getSQLDefinition(), 'NUMERIC (10,10)');
$this->assertSame('NUMERIC (10,10)', $definition->getSQLDefinition());

$definition = new Snowflake('VARCHAR', ['length' => '10', 'nullable' => true, 'default' => '\'\'']);
$this->assertSame($definition->getSQLDefinition(), 'VARCHAR (10) DEFAULT \'\'');
$this->assertSame('VARCHAR (10) DEFAULT \'\'', $definition->getSQLDefinition());

$definition = new Snowflake('NVARCHAR', ['length' => '10', 'nullable' => true, 'default' => '\'\'']);
$this->assertSame('NVARCHAR (10) DEFAULT \'\'', $definition->getSQLDefinition());

$definition = new Snowflake('NVARCHAR2', ['length' => '10', 'nullable' => true, 'default' => '\'\'']);
$this->assertSame('NVARCHAR2 (10) DEFAULT \'\'', $definition->getSQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']);
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (0)');
$this->assertSame('TIMESTAMP_TZ (0)', $definition->getSQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']);
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ (9)');
$this->assertSame('TIMESTAMP_TZ (9)', $definition->getSQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']);
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ');
$this->assertSame('TIMESTAMP_TZ', $definition->getSQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ');
$this->assertSame($definition->getSQLDefinition(), 'TIMESTAMP_TZ');
$this->assertSame('TIMESTAMP_TZ', $definition->getSQLDefinition());
}

public function testTypeOnlySqlDefinition(): void
{
$definition = new Snowflake('NUMERIC', ['length' => '', 'nullable' => false]);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC');
$this->assertSame('NUMERIC', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false]);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)');
$this->assertSame('NUMERIC (10,10)', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('NUMERIC', ['length' => '10,10', 'nullable' => false, 'default' => '10']);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'NUMERIC (10,10)');
$this->assertSame('NUMERIC (10,10)', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '0']);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (0)');
$this->assertSame('TIMESTAMP_TZ (0)', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '9']);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ (9)');
$this->assertSame('TIMESTAMP_TZ (9)', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ', ['length' => '']);
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ');
$this->assertSame('TIMESTAMP_TZ', $definition->getTypeOnlySQLDefinition());

$definition = new Snowflake('TIMESTAMP_TZ');
$this->assertSame($definition->getTypeOnlySQLDefinition(), 'TIMESTAMP_TZ');
$this->assertSame('TIMESTAMP_TZ', $definition->getTypeOnlySQLDefinition());
}

/**
Expand Down Expand Up @@ -274,6 +280,7 @@ public function testBasetypes(): void
break;
case 'NUMBER':
case 'DECIMAL':
case 'DEC':
case 'NUMERIC':
$this->assertEquals('NUMERIC', $basetype);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function isTemporary(): bool
}

/**
* @phpstan-impure
* @return array<int, array<string, mixed>>
* array{
* schema_name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public function isTemporary(): bool
}

/**
* @phpstan-impure
* @return array<int, array<string, mixed>>
* array{
* schema_name: string,
Expand Down Expand Up @@ -256,6 +257,7 @@ public static function getDependentViewsForObject(
}

/**
* @phpstan-impure
* @return array<int, array<string, mixed>>
* array{
* schema_name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function isTemporary(): bool
}

/**
* @phpstan-impure
* @return array{
* schema_name: string,
* name: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(Connection $connection, string $schemaName, string $
}

/**
* @phpstan-impure
* @return array<int, array<string, mixed>>
* array{
* schema_name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(Connection $connection, string $schemaName, string $
}

/**
* @phpstan-impure
* @return array<int, array<string, mixed>>
* array{
* schema_name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(Connection $connection, string $schemaName, string $
}

/**
* @phpstan-impure
* @return array<mixed, array{schema_name: string, name: string}>
*/
public function getDependentViews(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(Connection $connection, string $databaseName, string
}

/**
* @phpstan-impure
* @return array<mixed, array{schema_name: string, name: string}>
*/
public function getDependentViews(): array
Expand Down

0 comments on commit 2b998cf

Please sign in to comment.