Skip to content

Commit

Permalink
feat: support costume character set (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana authored Sep 22, 2023
1 parent 2584f9b commit b1fe68d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/System/Database/MySchema/Table/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ class Create extends Query
/** @var string */
private $store_engine;

private string $character_set;

/** @var string */
private $table_name;

public function __construct(string $database_name, string $table_name, MyPDO $pdo)
{
$this->table_name = $database_name . '.' . $table_name;
$this->pdo = $pdo;
$this->columns = [];
$this->primaryKeys = [];
$this->uniques = [];
$this->store_engine = '';
$this->table_name = $database_name . '.' . $table_name;
$this->pdo = $pdo;
$this->columns = [];
$this->primaryKeys = [];
$this->uniques = [];
$this->store_engine = '';
$this->character_set = '';
}

public function __invoke(string $column_name): DataType
Expand Down Expand Up @@ -87,12 +90,19 @@ public function engine(string $engine): self
return $this;
}

public function character(string $character_set): self
{
$this->character_set = $character_set;

return $this;
}

protected function builder(): string
{
/** @var string[] */
$columns = array_merge($this->getColumns(), $this->getPrimarykey(), $this->getUnique());
$columns = $this->join($columns, ', ');
$query = $this->join([$this->table_name, '(', $columns, ')' . $this->getStoreEngine()]);
$query = $this->join([$this->table_name, '(', $columns, ')' . $this->getStoreEngine() . $this->getCharacterSet()]);

return 'CREATE TABLE ' . $query;
}
Expand Down Expand Up @@ -137,4 +147,9 @@ private function getStoreEngine(): string
{
return $this->store_engine === '' ? '' : ' ENGINE=' . $this->store_engine;
}

private function getCharacterSet(): string
{
return $this->character_set === '' ? '' : " CHARACTER SET {$this->character_set}";
}
}
31 changes: 31 additions & 0 deletions tests/DataBase/Query/Schema/Table/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,35 @@ public function itCanGenerateQueryWithStorageEngine()
$schema->__toString()
);
}

/** @test */
public function itCanGenerateQueryWithCharacterSet()
{
$schema = new Create('testing_db', 'test', $this->pdo_schame);
$schema->addColumn()->raw('PersonID int');
$schema->addColumn()->raw('LastName varchar(255)');
$schema->primaryKey('PersonID');
$schema->character('utf8mb4');

$this->assertEquals(
'CREATE TABLE testing_db.test ( PersonID int, LastName varchar(255), PRIMARY KEY (`PersonID`) ) CHARACTER SET utf8mb4',
$schema->__toString()
);
}

/** @test */
public function itCanGenerateQueryWithEngineStoreAndCharacterSet()
{
$schema = new Create('testing_db', 'test', $this->pdo_schame);
$schema->addColumn()->raw('PersonID int');
$schema->addColumn()->raw('LastName varchar(255)');
$schema->primaryKey('PersonID');
$schema->engine(Create::INNODB);
$schema->character('utf8mb4');

$this->assertEquals(
'CREATE TABLE testing_db.test ( PersonID int, LastName varchar(255), PRIMARY KEY (`PersonID`) ) ENGINE=INNODB CHARACTER SET utf8mb4',
$schema->__toString()
);
}
}
19 changes: 19 additions & 0 deletions tests/DataBase/RealDatabase/Schema/Table/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,23 @@ public function itCanGenerateCreateDatabase()

$this->assertTrue($schema->execute());
}

/**
* @test
*
* @group database
*/
public function itCanGenerateCreateDatabaseWithEngine()
{
$schema = new Create($this->pdo_schema->configs()['database_name'], 'profiles', $this->pdo_schema);

$schema('id')->int(3)->notNull();
$schema('name')->varchar(32)->notNull();
$schema('gender')->int(1);
$schema->primaryKey('id');
$schema->engine(Create::INNODB);
$schema->character('utf8mb4');

$this->assertTrue($schema->execute());
}
}

0 comments on commit b1fe68d

Please sign in to comment.