Skip to content

Commit

Permalink
Refactoring: Use of Constants for Column Options
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 1, 2024
1 parent c5582b1 commit 4573b27
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 272 deletions.
35 changes: 18 additions & 17 deletions samples/createDatabase/UserBookmarksTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use webfiori\database\ColOption;
use webfiori\database\FK;
use webfiori\database\mysql\MySQLTable;

Expand All @@ -9,31 +10,31 @@ public function __construct() {

$this->addColumns([
'id' => [
'type' => 'int',
'size' => 6
ColOption::TYPE => 'int',
ColOption::SIZE => 6
],
'title' => [
'type' => 'varchar',
'size' => 128,
'default' => 'New Bookmark'
ColOption::TYPE => 'varchar',
ColOption::SIZE => 128,
ColOption::DEFAULT => 'New Bookmark'
],
'url' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256
],
'bookmarked-on' => [
'type' => 'timestamp',
'default' => 'current_timestamp'
ColOption::TYPE => 'timestamp',
ColOption::DEFAULT => 'current_timestamp'
],
'user_id' => [
'type' => 'int',
'size' => 5,
'fk' => [
'table' => UserInformation::class,
'name' => 'user_id_fk',
'col' => 'id',
'on-update' => FK::CASCADE,
'on-delete' => FK::RESTRICT
ColOption::TYPE => 'int',
ColOption::SIZE => 5,
ColOption::FK => [
ColOption::FK_TABLE => UserInformation::class,
ColOption::FK_NAME => 'user_id_fk',
ColOption::FK_COL => 'id',
ColOption::FK_ON_UPDATE => FK::CASCADE,
ColOption::FK_ON_DELETE => FK::RESTRICT
]
],
]);
Expand Down
19 changes: 10 additions & 9 deletions samples/createDatabase/UserInformationTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use webfiori\database\ColOption;
use webfiori\database\mysql\MySQLTable;

class UserInformationTable extends MySQLTable {
Expand All @@ -8,22 +9,22 @@ public function __construct() {

$this->addColumns([
'id' => [
'type' => 'int',
'size' => 5,
'primary' => true,
ColOption::TYPE => 'int',
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
'auto-inc' => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => 'varchar',
ColOption::SIZE => 128
]
]);
}
Expand Down
22 changes: 11 additions & 11 deletions samples/createDatabase/user-bookmarks-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
//Create Blueprint of second table.
$database->createBlueprint('user_bookmarks')->addColumns([
'id' => [
'type' => 'int',
'size' => 6
ColOption::TYPE => 'int',
ColOption::SIZE => 6
],
'title' => [
'type' => 'varchar',
'size' => 128,
'default' => 'New Bookmark'
ColOption::TYPE => 'varchar',
ColOption::SIZE => 128,
ColOption::DEFAULT => 'New Bookmark'
],
'url' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256
],
'bookmarked-on' => [
'type' => 'timestamp',
'default' => 'current_timestamp'
ColOption::TYPE => 'timestamp',
ColOption::DEFAULT => 'current_timestamp'
],
'user_id' => [
'type' => 'int',
'size' => 5
ColOption::TYPE => 'int',
ColOption::SIZE => 5
],
])->addReference('users_information', [
'user-id' => 'id'
Expand Down
20 changes: 10 additions & 10 deletions samples/createDatabase/user-information-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
//Create Blueprint of first table.
$database->createBlueprint('users_information')->addColumns([
'id' => [
'type' => 'int',
'size' => 5,
'primary' => true,
'auto-inc' => true
ColOption::TYPE => 'int',
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => 'varchar',
ColOption::SIZE => 128
]
]);
20 changes: 10 additions & 10 deletions samples/createEntity/user-information-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

$database->createBlueprint('users_information')->addColumns([
'id' => [
'type' => 'int',
'size' => 5,
'primary' => true,
'auto-inc' => true
ColOption::TYPE => 'int',
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => 'varchar',
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => 'varchar',
ColOption::SIZE => 128
]
]);
1 change: 1 addition & 0 deletions samples/mysql-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use webfiori\database\Database;
use webfiori\database\DatabaseException;
use webfiori\database\ResultSet;
use webfiori\database\ColOption;

function getDatabaseInstance() : Database {
$connection = new ConnectionInfo('mysql', 'root', '123456', 'testing_db');
Expand Down
99 changes: 50 additions & 49 deletions tests/webfiori/database/tests/common/InsertBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace webfiori\database\tests\common;

use PHPUnit\Framework\TestCase;
use webfiori\database\ColOption;
use webfiori\database\mssql\MSSQLInsertBuilder;
use webfiori\database\mssql\MSSQLTable;
use webfiori\database\mysql\MySQLInsertBuilder;
Expand All @@ -20,35 +21,35 @@ public function test00() {
$table = new MySQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'auto-inc' => true
ColOption::TYPE => 'int',
ColOption::SIZE => 11,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
'is-unique' => true
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256,
ColOption::UNIQUE => true
],
'username' => [
'type' => 'varchar',
'size' => 20,
'is-unique' => true
ColOption::TYPE => 'varchar',
ColOption::SIZE => 20,
ColOption::UNIQUE => true
],
'password' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256
],
'age' => [
'type' => 'decimal'
ColOption::TYPE => 'decimal'
],
'created-on' => [
'type' => 'timestamp',
'default' => 'now()',
ColOption::TYPE => 'timestamp',
ColOption::DEFAULT => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
ColOption::TYPE => 'bool',
ColOption::DEFAULT => true
]
]);

Expand Down Expand Up @@ -94,35 +95,35 @@ public function test01() {
$table = new MySQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'auto-inc' => true
ColOption::TYPE => 'int',
ColOption::SIZE => 11,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
'is-unique' => true
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256,
ColOption::UNIQUE => true
],
'username' => [
'type' => 'varchar',
'size' => 20,
'is-unique' => true
ColOption::TYPE => 'varchar',
ColOption::SIZE => 20,
ColOption::UNIQUE => true
],
'password' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256
],
'age' => [
'type' => 'decimal'
ColOption::TYPE => 'decimal'
],
'created-on' => [
'type' => 'timestamp',
'default' => 'now()',
ColOption::TYPE => 'timestamp',
ColOption::DEFAULT => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
ColOption::TYPE => 'bool',
ColOption::DEFAULT => true
]
]);

Expand Down Expand Up @@ -159,33 +160,33 @@ public function test03() {
$table = new MSSQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'identity' => true
ColOption::TYPE => 'int',
ColOption::SIZE => 11,
ColOption::PRIMARY => true,
ColOption::IDENTITY => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256,
],
'username' => [
'type' => 'varchar',
'size' => 20,
ColOption::TYPE => 'varchar',
ColOption::SIZE => 20,
],
'password' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => 'varchar',
ColOption::SIZE => 256
],
'age' => [
'type' => 'decimal'
ColOption::TYPE => 'decimal'
],
'created-on' => [
'type' => 'datetime2',
'default' => 'now()',
ColOption::TYPE => 'datetime2',
ColOption::DEFAULT => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
ColOption::TYPE => 'bool',
ColOption::DEFAULT => true
]
]);

Expand Down
Loading

0 comments on commit 4573b27

Please sign in to comment.