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

[Feat] Support costume character set #216

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading