diff --git a/README.md b/README.md index 11075e2..e5296ae 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This Query Builder is also used in [Kathamo](https://kathamo.dev) Framework. Kat DB::create('querybuilder') ->column('ID')->bigInt()->unsigned()->autoIncrement()->primary()->required() ->column('name')->string(255)->required() - ->column('email')->string(255)->default('NULL') + ->column('email')->string(255)->nullable() ->index(['ID']) ->execute(); ``` diff --git a/src/Api/CreateInterface.php b/src/Api/CreateInterface.php index 8006d1c..e9b64e4 100644 --- a/src/Api/CreateInterface.php +++ b/src/Api/CreateInterface.php @@ -4,41 +4,43 @@ interface CreateInterface { - function column(string $column_name): self; + public function column(string $column_name): self; - function int(int $size = 255): self; + public function int(int $size = 255): self; - function bigInt(int $size = 255): self; + public function bigInt(int $size = 255): self; - function double(int $size = 255, int $d = 2): self; + public function double(int $size = 255, int $d = 2): self; - function boolean(): self; + public function boolean(): self; - function string(int $size = 255): self; + public function string(int $size = 255): self; - function text(int $size = 10000): self; + public function text(int $size = 10000): self; - function longText(int $size): self; + public function longText(int $size): self; - function required(): self; + public function required(): self; - function primary($columns = []): self; + public function nullable(): self; - function index(array $columns): self; + public function primary($columns = []): self; - function date(): self; + public function index(array $columns): self; - function dateTime(): self; + public function date(): self; - function unsigned(): self; + public function dateTime(): self; - function autoIncrement(): self; + public function unsigned(): self; - function default($value): self; + public function autoIncrement(): self; - function foreignKey(string $column, string $reference_table, string $reference_column): self; + public function default($value): self; - function getSql(); - - function execute(); + public function foreignKey(string $column, string $reference_table, string $reference_column): self; + + public function getSql(); + + public function execute(); } diff --git a/src/Api/DeleteInterface.php b/src/Api/DeleteInterface.php index cb692d3..f014f59 100644 --- a/src/Api/DeleteInterface.php +++ b/src/Api/DeleteInterface.php @@ -4,17 +4,17 @@ interface DeleteInterface { - function where($column, string $operator = null, string $value = null): self; + public function where($column, string $operator = null, string $value = null): self; - function andWhere(string $column, string $operator = null, string $value = null): self; + public function andWhere(string $column, string $operator = null, string $value = null): self; - function orWhere(string $column, string $operator = null, string $value = null): self; + public function orWhere(string $column, string $operator = null, string $value = null): self; - function drop(); + public function drop(); - function dropIfExists(); + public function dropIfExists(); - function getSql(); + public function getSql(); - function execute(); + public function execute(); } diff --git a/src/Api/DropInterface.php b/src/Api/DropInterface.php index 8095e8b..0a29d82 100644 --- a/src/Api/DropInterface.php +++ b/src/Api/DropInterface.php @@ -4,7 +4,7 @@ interface DropInterface { - function drop(); + public function drop(); - function dropIfExists(); + public function dropIfExists(); } diff --git a/src/Api/SelectInterface.php b/src/Api/SelectInterface.php index e48bddc..67f8618 100644 --- a/src/Api/SelectInterface.php +++ b/src/Api/SelectInterface.php @@ -4,13 +4,13 @@ interface SelectInterface { - function distinct(): self; + public function distinct(): self; - function columns(...$columns): self; + public function columns(...$columns): self; - function alias(string $name): self; + public function alias(string $name): self; - function from(string $table_name): self; + public function from(string $table_name): self; /** * JOIN table(s) ON condition of @@ -22,7 +22,7 @@ function from(string $table_name): self; * * @return CodesVault\Howdyqb\Api\SelectInterface */ - function join($table_name, string $col1 = null, string $col2 = null): self; + public function join($table_name, string $col1 = null, string $col2 = null): self; /** * INNER JOIN table(s) ON condition of @@ -34,7 +34,7 @@ function join($table_name, string $col1 = null, string $col2 = null): self; * * @return CodesVault\Howdyqb\Api\SelectInterface */ - function innerJoin($table_name, string $col1 = null, string $col2 = null): self; + public function innerJoin($table_name, string $col1 = null, string $col2 = null): self; /** * LEFT JOIN table(s) ON condition of @@ -46,7 +46,7 @@ function innerJoin($table_name, string $col1 = null, string $col2 = null): self; * * @return CodesVault\Howdyqb\Api\SelectInterface */ - function leftJoin($table_name, string $col1 = null, string $col2 = null): self; + public function leftJoin($table_name, string $col1 = null, string $col2 = null): self; /** * RIGHT JOIN table(s) ON condition of @@ -58,33 +58,33 @@ function leftJoin($table_name, string $col1 = null, string $col2 = null): self; * * @return CodesVault\Howdyqb\Api\SelectInterface */ - function rightJoin($table_name, string $col1 = null, string $col2 = null): self; + public function rightJoin($table_name, string $col1 = null, string $col2 = null): self; - function where($column, ?string $operator = null, ?string $value = null): self; + public function where($column, ?string $operator = null, ?string $value = null): self; - function andWhere(string $column, ?string $operator, ?string $value): self; + public function andWhere(string $column, ?string $operator, ?string $value): self; - function orWhere(string $column, ?string $operator, ?string $value): self; + public function orWhere(string $column, ?string $operator, ?string $value): self; - function whereNot(string $column, ?string $operator, ?string $value): self; + public function whereNot(string $column, ?string $operator, ?string $value): self; - function andNot(string $column, ?string $operator, ?string $value): self; + public function andNot(string $column, ?string $operator, ?string $value): self; - function whereIn(string $column, ...$value): self; + public function whereIn(string $column, ...$value): self; - function orderBy($column, string $sort): self; + public function orderBy($column, string $sort): self; - function groupBy($column): self; + public function groupBy($column): self; - function limit(int $count): self; + public function limit(int $count): self; - function offset(int $count): self; + public function offset(int $count): self; - function count(string $column, string $alias = ''): self; + public function count(string $column, string $alias = ''): self; - function raw(string $sql): self; + public function raw(string $sql): self; - function getSql(); + public function getSql(); - function get(); + public function get(); } diff --git a/src/Api/UpdateInterface.php b/src/Api/UpdateInterface.php index b77010c..4051a7e 100644 --- a/src/Api/UpdateInterface.php +++ b/src/Api/UpdateInterface.php @@ -4,13 +4,13 @@ interface UpdateInterface { - function where($column, string $operator = null, string $value = null): self; + public function where($column, string $operator = null, string $value = null): self; - function andWhere(string $column, string $operator = null, string $value = null): self; + public function andWhere(string $column, string $operator = null, string $value = null): self; - function orWhere(string $column, string $operator = null, string $value = null): self; + public function orWhere(string $column, string $operator = null, string $value = null): self; function getSql(); - function execute(); + public function execute(); } diff --git a/src/Statement/Create.php b/src/Statement/Create.php index a088334..209d67a 100644 --- a/src/Statement/Create.php +++ b/src/Statement/Create.php @@ -85,6 +85,11 @@ public function required(): self return $this; } + public function nullable(): self + { + return $this->default('NULL'); + } + public function primary($columns = []): self { if (! empty($columns)) { diff --git a/tests/CreateTest.php b/tests/CreateTest.php index 6868414..3d5d0e7 100644 --- a/tests/CreateTest.php +++ b/tests/CreateTest.php @@ -7,12 +7,11 @@ class CreateTest extends TestCase { - public function testCreateTable() + public function test_create_table() { - $sql = "CREATE TABLE IF NOT EXISTS wp_test (ID BIGINT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) DEFAULT 'nil', INDEX (ID) )"; + $sql = "CREATE TABLE IF NOT EXISTS wp_test (ID BIGINT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) DEFAULT 'nil', INDEX (ID))"; - $query = - CreateApi::create('test') + $query = CreateApi::create('test') ->column('ID')->bigInt()->unsigned()->autoIncrement()->primary()->required() ->column('name')->string(255)->required() ->column('email')->string(255)->default('nil') @@ -21,4 +20,22 @@ public function testCreateTable() $this->assertSame($sql, $query); } + + public function test_nullable() + { + $sql = "CREATE TABLE IF NOT EXISTS wp_test (secondary_email VARCHAR(255) DEFAULT 'NULL')"; + + $query = CreateApi::create('test') + ->column('secondary_email')->string(255)->default('NULL') + ->getSql(); + + $this->assertSame($sql, $query); + + $query = CreateApi::create('test') + ->column('secondary_email')->string(255)->nullable() + ->getSql(); + + $this->assertSame($sql, $query); + + } }