diff --git a/src/System/Database/MySchema/Table/Create.php b/src/System/Database/MySchema/Table/Create.php index 2246287e..6c7191e6 100644 --- a/src/System/Database/MySchema/Table/Create.php +++ b/src/System/Database/MySchema/Table/Create.php @@ -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 @@ -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; } @@ -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}"; + } } diff --git a/tests/DataBase/Query/Schema/Table/CreateTest.php b/tests/DataBase/Query/Schema/Table/CreateTest.php index 936c31d5..b63240e9 100644 --- a/tests/DataBase/Query/Schema/Table/CreateTest.php +++ b/tests/DataBase/Query/Schema/Table/CreateTest.php @@ -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() + ); + } } diff --git a/tests/DataBase/RealDatabase/Schema/Table/CreateTest.php b/tests/DataBase/RealDatabase/Schema/Table/CreateTest.php index 304f86ef..06f7ce5b 100644 --- a/tests/DataBase/RealDatabase/Schema/Table/CreateTest.php +++ b/tests/DataBase/RealDatabase/Schema/Table/CreateTest.php @@ -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()); + } }