Skip to content

Commit

Permalink
Merge pull request #29 from hind-sagar-biswas:28-indexing-column
Browse files Browse the repository at this point in the history
indexing-column
  • Loading branch information
hind-sagar-biswas committed Nov 28, 2023
2 parents 08faa06 + 7da3f98 commit 4ef263e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/Table/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
class CreateTable
{
protected array $columnList; // Stores column definitions.
protected array $columns; // Stores column definitions.
protected array $columns; // Stores column definitions.
protected array $constraints = [
'UNIQUE INDEX' => [], // Stores UNIQUE INDEX constraints.
'INDEX' => [], // Stores INDEX-ed columns.
'UNIQUE' => [], // Stores UNIQUE constraints.
'PRIMARY KEY' => [], // Stores PRIMARY KEY constraints.
];
Expand Down Expand Up @@ -100,11 +102,23 @@ public function build(): string
}

foreach ($this->constraints['UNIQUE'] as $columnName) {
$keyName = $columnName . "_UC";
$keyName = $columnName . "_UNQ";
$constraintStr = "CONSTRAINT $keyName UNIQUE (`$columnName`)";
$this->columns[] = $constraintStr; // Add UNIQUE constraint to columns.
}

foreach ($this->constraints['INDEX'] as $columnName) {
$keyName = $columnName . "_IND";
$constraintStr = "INDEX $keyName (`$columnName`)";
$this->columns[] = $constraintStr; // Add INDEX constraint to columns.
}

foreach ($this->constraints['UNIQUE INDEX'] as $columnName) {
$keyName = $columnName . "_UIK";
$constraintStr = "UNIQUE INDEX $keyName (`$columnName`)";
$this->columns[] = $constraintStr; // Add UNIQUE INDEX constraint to columns.
}

foreach ($this->foreignList as $query) {
$this->columns[] = $query; // Add FOREIGN KEY constraints to columns.
}
Expand Down
16 changes: 15 additions & 1 deletion src/Table/Traits/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@

namespace Hindbiswas\QueBee\Table\Traits;

use BadMethodCallException;

trait Constraint
{
protected string|null $constraint = null;

public function unique(): self
{
$this->constraint = 'UNIQUE';
if ($this->constraint === 'PRIMARY KEY') throw new BadMethodCallException("Already marked column as Primary Key. Cannot Mark as UNIQUE.", 1);

if (!$this->constraint) $this->constraint = 'UNIQUE';
else $this->constraint = 'UNIQUE ' . $this->constraint;
return $this;
}

public function index(): self
{
if ($this->constraint === 'PRIMARY KEY') throw new BadMethodCallException("Already marked column as Primary Key. Cannot Index again.", 1);

if (!$this->constraint) $this->constraint = 'INDEX';
else $this->constraint .= ' INDEX';
return $this;
}

Expand Down
28 changes: 27 additions & 1 deletion tests/Column/FullTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class FullTableTest extends TestCase
{
public function test_basic_users_table_build()
{
$expected = "CREATE TABLE IF NOT EXISTS users (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `is_superadmin` INT(2) NOT NULL DEFAULT '0', `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIME, `update_time` DATETIME on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIME, CONSTRAINT users_PK PRIMARY KEY (id), CONSTRAINT username_UC UNIQUE (`username`), CONSTRAINT email_UC UNIQUE (`email`)) ENGINE = InnoDB;";
$expected = "CREATE TABLE IF NOT EXISTS users (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `is_superadmin` INT(2) NOT NULL DEFAULT '0', `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIME, `update_time` DATETIME on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIME, CONSTRAINT users_PK PRIMARY KEY (id), CONSTRAINT username_UNQ UNIQUE (`username`), CONSTRAINT email_UNQ UNIQUE (`email`)) ENGINE = InnoDB;";

$query = Table::create('users')->columns([
'id' => Col::integer(11)->unsigned()->ai()->pk(),
Expand All @@ -27,6 +27,32 @@ public function test_basic_users_table_build()
$this->assertSame($expected, $query);
}

public function test_index_build()
{
$expected = "CREATE TABLE IF NOT EXISTS test_table (`column1` INT(11) NOT NULL, `column2` VARCHAR(255) NOT NULL, `column3` VARCHAR(255) NOT NULL, CONSTRAINT test_table_PK PRIMARY KEY (column1), INDEX column3_IND (`column3`)) ENGINE = InnoDB;";

$query = Table::create('test_table')->columns([
'column1' => Col::integer(11)->pk(),
'column2' => Col::varchar(),
'column3' => Col::varchar()->index(),
])->build();

$this->assertSame($expected, $query);
}

public function test_unique_index_build()
{
$expected = "CREATE TABLE IF NOT EXISTS test_table (`column1` INT(11) NOT NULL, `column2` VARCHAR(255) NOT NULL, `column3` VARCHAR(255) NOT NULL, CONSTRAINT test_table_PK PRIMARY KEY (column1), CONSTRAINT column2_UNQ UNIQUE (`column2`), UNIQUE INDEX column3_UIK (`column3`)) ENGINE = InnoDB;";

$query = Table::create('test_table')->columns([
'column1' => Col::integer(11)->pk(),
'column2' => Col::varchar()->unique(),
'column3' => Col::varchar()->index()->unique(),
])->build();

$this->assertSame($expected, $query);
}

public function test_table_with_foreign_keys_build()
{
$expected = "CREATE TABLE IF NOT EXISTS tokens (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `selector` VARCHAR(255) NOT NULL, `hashed_validator` VARCHAR(255) NOT NULL, `user_id` INT(11) UNSIGNED NOT NULL, `expiry` DATETIME NOT NULL, CONSTRAINT tokens_PK PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE) ENGINE = InnoDB;";
Expand Down

0 comments on commit 4ef263e

Please sign in to comment.