Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add varbinary support in MySQL #146

Merged
merged 11 commits into from
Dec 4, 2023
32 changes: 32 additions & 0 deletions src/Driver/MySQL/Schema/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Cycle\Database\Driver\DriverInterface;
use Cycle\Database\Exception\DefaultValueException;
use Cycle\Database\Exception\SchemaException;
use Cycle\Database\Injection\Fragment;
use Cycle\Database\Injection\FragmentInterface;
use Cycle\Database\Schema\AbstractColumn;
Expand Down Expand Up @@ -150,6 +151,9 @@ class MySQLColumn extends AbstractColumn
'json',
];

#[ColumnAttribute(['string', 'varbinary'])]
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
protected int $size = 0;

/**
* Column is auto incremental.
*/
Expand Down Expand Up @@ -310,6 +314,34 @@ public function set(string|array $values): self
return $this;
}

/**
* @param 0|positive-int $size
msmakouz marked this conversation as resolved.
Show resolved Hide resolved
*/
public function varbinary(int $size = 255): self
{
$this->type('varbinary');

$size < 0 && throw new SchemaException('Invalid varbinary size value');

$this->size = $size;

return $this;
}

/**
* @param 0|positive-int $size
msmakouz marked this conversation as resolved.
Show resolved Hide resolved
*/
public function binary(int $size = 0): self
{
if ($size > 0) {
return $this->varbinary($size);
}

$this->type('blob');

return $this;
}

/**
* Ensure that datetime fields are correctly formatted.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Driver/Postgres/Schema/PostgresColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ class PostgresColumn extends AbstractColumn
'bit' => ['bit', 'bit varying'],
];

#[ColumnAttribute(['string', 'bit', 'bit varying', 'datetime', 'time', 'timetz', 'timestamp', 'timestamptz'])]
protected int $size = 0;

/**
* Field is auto incremental.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Driver/SQLServer/Schema/SQLServerColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cycle\Database\Driver\DriverInterface;
use Cycle\Database\Exception\SchemaException;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\Attribute\ColumnAttribute;

class SQLServerColumn extends AbstractColumn
{
Expand All @@ -38,6 +39,15 @@ class SQLServerColumn extends AbstractColumn
'defaultConstraint',
'constrainedEnum',
'enumConstraint',
'attributes',
];

protected array $aliases = [
'int' => 'integer',
'smallint' => 'smallInteger',
'bigint' => 'bigInteger',
'bool' => 'boolean',
'varbinary' => 'binary',
];

protected array $mapping = [
Expand Down Expand Up @@ -110,6 +120,9 @@ class SQLServerColumn extends AbstractColumn
'binary' => ['varbinary'],
];

#[ColumnAttribute(['string', 'datetime2', 'varbinary'])]
protected int $size = 0;

/**
* Field is table identity.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Driver/SQLite/Schema/SQLiteColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Cycle\Database\Driver\DriverInterface;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\Attribute\ColumnAttribute;

class SQLiteColumn extends AbstractColumn
{
Expand Down Expand Up @@ -113,6 +114,9 @@ class SQLiteColumn extends AbstractColumn
*/
protected bool $primaryKey = false;

#[ColumnAttribute([])]
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
protected int $size = 0;

/**
* DBMS specific reverse mapping must map database specific type into limited set of abstract
* types.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\Common\Schema;

use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;

abstract class JsonColumnTest extends BaseTest
{
public function testColumnSizeIsIgnored(): void
{
$schema = $this->schema('table');
$schema->primary('id');
$schema->json('json_data', size: 255);
$schema->save();

$this->assertSameAsInDB($schema);

$this->assertSame(0, $schema->column('json_data')->getSize());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\Common\Schema;

use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;

abstract class StringColumnTest extends BaseTest
{
public function testStringDefaultSize(): void
{
$schema = $this->schema('table');

$column = $schema->string('column');
$schema->save();
$this->assertSameAsInDB($schema);

$this->assertSame(255, $column->getSize());
}

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

$column = $schema->string('column', size: 64);
$schema->save();
$this->assertSameAsInDB($schema);

$this->assertSame(64, $column->getSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testNamedArgumentsToConfigureInteger(): void
{
$schema = $this->schema('foo');
$schema->bigPrimary('id', zerofill: true)->unsigned(true);
$schema->bigInteger('foo', nullable: true, unsigned: true, zerofill: true, size: 18);
$schema->bigInteger('foo', nullable: true, unsigned: true, zerofill: true);
$schema->save();

$this->assertInstanceOf(MySQLColumn::class, $id = $this->fetchSchema($schema)->column('id'));
Expand All @@ -89,7 +89,6 @@ public function testNamedArgumentsToConfigureInteger(): void
$this->assertTrue($id->isUnsigned());
$this->assertTrue($foo->isUnsigned());
$this->assertSame(20, $id->getSize());
$this->assertSame(18, $foo->getSize());
$this->assertFalse($id->isNullable());
$this->assertTrue($foo->isNullable());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\MySQL\Schema;

use Cycle\Database\Driver\Handler;
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;

/**
* @group driver
* @group driver-mysql
*/
final class BinaryColumnTest extends BaseTest
{
public const DRIVER = 'mysql';

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

$schema->primary('id');
$schema->binary('binary_data');
$schema->save(Handler::DO_ALL);

$this->assertSameAsInDB($schema);

$this->assertSame('blob', $schema->column('binary_data')->getInternalType());
$this->assertSame(0, $schema->column('binary_data')->getSize());
}

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

$schema->primary('id');
$schema->binary('binary_data', 16);
$schema->save(Handler::DO_ALL);

$this->assertSameAsInDB($schema);

$this->assertSame('varbinary', $schema->column('binary_data')->getInternalType());
$this->assertSame(16, $schema->column('binary_data')->getSize());
}

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

$schema->primary('id');
$schema->varbinary('binary_data');
$schema->save(Handler::DO_ALL);

$this->assertSameAsInDB($schema);

$this->assertSame('varbinary', $schema->column('binary_data')->getInternalType());
$this->assertSame(255, $schema->column('binary_data')->getSize());
}

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

$schema->primary('id');
$schema->varbinary('binary_data', 16);
$schema->save(Handler::DO_ALL);

$this->assertSameAsInDB($schema);

$this->assertSame('varbinary', $schema->column('binary_data')->getInternalType());
$this->assertSame(16, $schema->column('binary_data')->getSize());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\MySQL\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\Schema\JsonColumnTest as CommonClass;

/**
* @group driver
* @group driver-mysql
*/
class JsonColumnTest extends CommonClass
{
public const DRIVER = 'mysql';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\MySQL\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\Schema\StringColumnTest as CommonClass;

/**
* @group driver
* @group driver-mysql
*/
class StringColumnTest extends CommonClass
{
public const DRIVER = 'mysql';
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Cycle\Database\Tests\Functional\Driver\Postgres\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;

/**
* @group driver
* @group driver-postgres
*/
final class BitColumnTest extends BaseTest
final class BinaryColumnTest extends BaseTest
{
public const DRIVER = 'postgres';

Expand Down
Loading
Loading