Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- added native support for UUID type
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Nov 14, 2019
1 parent 290060a commit 585d09b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 44 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGELOG for 0.9.0 RC
======================

2.6.2 (14.11.2019)
-----
- added native support for UUID type

2.6.1 (05.11.2019)
-----
- force the database disconned in case of connection error

2.6.0 (08.10.2019)
-----
- minimum PHP version is set as 7.2
Expand Down
1 change: 1 addition & 0 deletions src/Driver/MySQL/Schema/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class MySQLColumn extends AbstractColumn

//Additional types
'json' => 'text',
'uuid' => ['type' => 'varchar', 'size' => 36],
];

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/Postgres/Schema/PostgresColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class PostgresColumn extends AbstractColumn

//Additional types
'json' => 'json',
'jsonb' => 'jsonb'
'jsonb' => 'jsonb',
'uuid' => 'uuid'
];

/**
Expand Down
83 changes: 42 additions & 41 deletions src/Driver/SQLServer/Schema/SQLServerColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,71 +43,72 @@ class SQLServerColumn extends AbstractColumn
*/
protected $mapping = [
//Primary sequences
'primary' => ['type' => 'int', 'identity' => true, 'nullable' => false],
'bigPrimary' => ['type' => 'bigint', 'identity' => true, 'nullable' => false],
'primary' => ['type' => 'int', 'identity' => true, 'nullable' => false],
'bigPrimary' => ['type' => 'bigint', 'identity' => true, 'nullable' => false],

//Enum type (mapped via method)
'enum' => 'enum',
'enum' => 'enum',

//Logical types
'boolean' => 'bit',
'boolean' => 'bit',

//Integer types (size can always be changed with size method), longInteger has method alias
//bigInteger
'integer' => 'int',
'integer' => 'int',
'tinyInteger' => 'tinyint',
'bigInteger' => 'bigint',
'bigInteger' => 'bigint',

//String with specified length (mapped via method)
'string' => 'varchar',
'string' => 'varchar',

//Generic types
'text' => ['type' => 'varchar', 'size' => 0],
'tinyText' => ['type' => 'varchar', 'size' => 0],
'longText' => ['type' => 'varchar', 'size' => 0],
'text' => ['type' => 'varchar', 'size' => 0],
'tinyText' => ['type' => 'varchar', 'size' => 0],
'longText' => ['type' => 'varchar', 'size' => 0],

//Real types
'double' => 'float',
'float' => 'real',
'double' => 'float',
'float' => 'real',

//Decimal type (mapped via method)
'decimal' => 'decimal',
'decimal' => 'decimal',

//Date and Time types
'datetime' => 'datetime',
'date' => 'date',
'time' => 'time',
'timestamp' => 'datetime',
'datetime' => 'datetime',
'date' => 'date',
'time' => 'time',
'timestamp' => 'datetime',

//Binary types
'binary' => ['type' => 'varbinary', 'size' => 0],
'tinyBinary' => ['type' => 'varbinary', 'size' => 0],
'longBinary' => ['type' => 'varbinary', 'size' => 0],
'binary' => ['type' => 'varbinary', 'size' => 0],
'tinyBinary' => ['type' => 'varbinary', 'size' => 0],
'longBinary' => ['type' => 'varbinary', 'size' => 0],

//Additional types
'json' => ['type' => 'varchar', 'size' => 0],
'json' => ['type' => 'varchar', 'size' => 0],
'uuid' => ['type' => 'varchar', 'size' => 36],
];

/**
* {@inheritdoc}
*/
protected $reverseMapping = [
'primary' => [['type' => 'int', 'identity' => true]],
'bigPrimary' => [['type' => 'bigint', 'identity' => true]],
'enum' => ['enum'],
'boolean' => ['bit'],
'integer' => ['int'],
'primary' => [['type' => 'int', 'identity' => true]],
'bigPrimary' => [['type' => 'bigint', 'identity' => true]],
'enum' => ['enum'],
'boolean' => ['bit'],
'integer' => ['int'],
'tinyInteger' => ['tinyint', 'smallint'],
'bigInteger' => ['bigint'],
'text' => [['type' => 'varchar', 'size' => 0]],
'string' => ['varchar', 'char'],
'double' => ['float'],
'float' => ['real'],
'decimal' => ['decimal'],
'timestamp' => ['datetime'],
'date' => ['date'],
'time' => ['time'],
'binary' => ['varbinary'],
'bigInteger' => ['bigint'],
'text' => [['type' => 'varchar', 'size' => 0]],
'string' => ['varchar', 'char'],
'double' => ['float'],
'float' => ['real'],
'decimal' => ['decimal'],
'timestamp' => ['datetime'],
'date' => ['date'],
'time' => ['time'],
'binary' => ['varbinary'],
];

/**
Expand Down Expand Up @@ -227,7 +228,7 @@ public function sqlStatement(DriverInterface $driver, bool $withEnum = true): st
* will be dropped separately.
*
* @param DriverInterface $driver
* @param AbstractColumn $initial
* @param AbstractColumn $initial
* @return array
*/
public function alterOperations(DriverInterface $driver, AbstractColumn $initial): array
Expand Down Expand Up @@ -291,8 +292,8 @@ public function alterOperations(DriverInterface $driver, AbstractColumn $initial
}

/**
* @param string $table Table name.
* @param array $schema
* @param string $table Table name.
* @param array $schema
* @param DriverInterface $driver SQLServer columns are bit more complex.
*
* @return SQLServerColumn
Expand Down Expand Up @@ -432,7 +433,7 @@ private function normalizeDefault(): void
if (
$this->getType() != 'string'
&& ($this->defaultValue[0] == '('
&& $this->defaultValue[strlen($this->defaultValue) - 1] == ')')
&& $this->defaultValue[strlen($this->defaultValue) - 1] == ')')
) {
//Cut another braces
$this->defaultValue = substr($this->defaultValue, 1, -1);
Expand All @@ -443,7 +444,7 @@ private function normalizeDefault(): void
* Resolve enum values if any.
*
* @param DriverInterface $driver
* @param array $schema
* @param array $schema
* @param SQLServerColumn $column
*/
private static function resolveEnum(
Expand Down
1 change: 1 addition & 0 deletions src/Driver/SQLite/Schema/SQLiteColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SQLiteColumn extends AbstractColumn

//Additional types
'json' => 'text',
'uuid' => ['type' => 'varchar', 'size' => 36],
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Schema/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @method AbstractColumn|$this tinyBinary()
* @method AbstractColumn|$this longBinary()
* @method AbstractColumn|$this json()
* @method AbstractColumn|$this uuid()
*/
abstract class AbstractColumn implements ColumnInterface, ElementInterface
{
Expand Down
4 changes: 2 additions & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public function eraseData(): void
* $table->insertOne(["name" => "Wolfy-J", "balance" => 10]);
*
* @param array $rowset
* @return int
* @return int|null
*
* @throws BuilderException
*/
public function insertOne(array $rowset = []): int
public function insertOne(array $rowset = []): ?int
{
return $this->database->insert($this->name)->values($rowset)->run();
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/ConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,25 @@ public function testTime(): void
$this->assertTrue($schema->exists());
$this->assertTrue($schema->column('target')->compare($column));
}

public function testUuid(): void
{
$schema = $this->schema('table');
$this->assertFalse($schema->exists());

$column = $schema->uuid('target');

$schema->save();
$schema = $this->schema('table');
$this->assertTrue($schema->exists());
$this->assertTrue($schema->column('target')->compare($column));

$this->database->table('table')->insertOne([
'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
]);

$this->assertEquals([
'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
], $this->database->table('table')->select()->fetchAll()[0]);
}
}

0 comments on commit 585d09b

Please sign in to comment.