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

Refactoring: Use of Constants for Column Options #85

Merged
merged 7 commits into from
Jan 2, 2024
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
37 changes: 20 additions & 17 deletions samples/createDatabase/UserBookmarksTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

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

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

$this->addColumns([
'id' => [
'type' => 'int',
'size' => 6
ColOption::TYPE => DataType::INT,
ColOption::SIZE => 6
],
'title' => [
'type' => 'varchar',
'size' => 128,
'default' => 'New Bookmark'
ColOption::TYPE => DataType::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 => DataType::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 => DataType::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
20 changes: 11 additions & 9 deletions samples/createDatabase/UserInformationTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

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

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

$this->addColumns([
'id' => [
'type' => 'int',
'size' => 5,
'primary' => true,
ColOption::TYPE => DataType::INT,
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
'auto-inc' => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 128
]
]);
}
Expand Down
23 changes: 12 additions & 11 deletions samples/createDatabase/user-bookmarks-table.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?php



//Create Blueprint of second table.
$database->createBlueprint('user_bookmarks')->addColumns([
'id' => [
'type' => 'int',
'size' => 6
ColOption::TYPE => DataType::INT,
ColOption::SIZE => 6
],
'title' => [
'type' => 'varchar',
'size' => 128,
'default' => 'New Bookmark'
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 128,
ColOption::DEFAULT => 'New Bookmark'
],
'url' => [
'type' => 'varchar',
'size' => 256
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 256
],
'bookmarked-on' => [
'type' => 'timestamp',
'default' => 'current_timestamp'
ColOption::TYPE => DataType::TIMESTAMP,
ColOption::DEFAULT => 'current_timestamp'
],
'user_id' => [
'type' => 'int',
'size' => 5
ColOption::TYPE => DataTypes::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 => DataType::INT,
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => DataType::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 => DataType::INT,
ColOption::SIZE => 5,
ColOption::PRIMARY => true,
ColOption::AUTO_INCREMENT => true
],
'first-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'last-name' => [
'type' => 'varchar',
'size' => 15
ColOption::TYPE => DataType::VARCHAR,
ColOption::SIZE => 15
],
'email' => [
'type' => 'varchar',
'size' => 128
ColOption::TYPE => DataType::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
1 change: 1 addition & 0 deletions tests/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require_once $rootDir.$DS.'vendor'.$DS.'autoload.php';

require_once $rootDir.$DS.'webfiori'.$DS.'database'.$DS.'DataType.php';
require_once $rootDir.$DS.'webfiori'.$DS.'database'.$DS.'AbstractQuery.php';
require_once $rootDir.$DS.'webfiori'.$DS.'database'.$DS.'Table.php';
require_once $rootDir.$DS.'webfiori'.$DS.'database'.$DS.'ForeignKey.php';
Expand Down
8 changes: 6 additions & 2 deletions tests/webfiori/database/tests/common/EntityMapperTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace webfiori\database\tests\common;

use PHPUnit\Framework\TestCase;
use UserClass;
use webfiori\database\ColOption;
use webfiori\database\DataType;
use webfiori\database\EntityMapper;
use webfiori\database\tests\mysql\MySQLTestSchema;

Expand All @@ -18,7 +22,7 @@ public function test00() {
$t = $schema->getTable('users');
$t->addColumns([
'c-x-file' => [
'type' => 'bool'
ColOption::TYPE => DataType::BOOL
]
]);
$entityMapper = new EntityMapper($t, 'UserClass');
Expand Down Expand Up @@ -136,7 +140,7 @@ public function test02(EntityMapper $m) {
'last_name' => 'BinAlshikh',
'age' => 28
]);
$this->assertTrue($obj instanceof \UserClass);
$this->assertTrue($obj instanceof UserClass);
$this->assertEquals(55, $obj->getId());
$this->assertEquals('Ibrahim', $obj->getFirstName());
$this->assertEquals('BinAlshikh', $obj->getLastName());
Expand Down
Loading
Loading