Skip to content

Commit fd8ef08

Browse files
authored
Merge pull request #22 from WebFiori/dev
New Feature (#21)
2 parents 739c9e4 + 0195cb3 commit fd8ef08

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

tests/HelloTable.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace webfiori\database\tests;
4+
5+
use webfiori\database\mysql\MySQLTable;
6+
use webfiori\database\mysql\MySQLColumn;
7+
/**
8+
* Description of HelloTable
9+
*
10+
* @author Ibrahim
11+
*/
12+
class HelloTable extends MySQLTable {
13+
public function __construct() {
14+
parent::__construct('hello');
15+
$this->addColumn('user-id', new MySQLColumn('user_id', 'int', 11));
16+
$this->addColumn('username', new MySQLColumn('username', 'varchar', 15));
17+
$this->addColumn('pass', new MySQLColumn('password', 'varchar', 64));
18+
}
19+
}

tests/SchemaTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,48 @@ public function test00() {
6565
$s->page(5, 40);
6666
$this->assertEquals("select * from `hello` where `hello`.`user_id` = 31 and `hello`.`user_id` < 44 and `hello`.`username` != 'Ibrahim' limit 40 offset 200",$s->getLastQuery());
6767
}
68+
69+
/**
70+
* @test
71+
*/
72+
public function test01() {
73+
$connInfo = new ConnectionInfo('mysql','root', '123456', 'testing_db', '127.0.0.1');
74+
$s = new Database($connInfo);
75+
76+
$s->table(HelloTable::class)->drop();
77+
$this->assertEquals('drop table `hello`;', $s->getLastQuery());
78+
$s->createTable();
79+
$this->assertEquals("create table if not exists `hello` (\n"
80+
. " `user_id` int not null,\n"
81+
. " `username` varchar(15) not null collate utf8mb4_unicode_520_ci,\n"
82+
. " `password` varchar(64) not null collate utf8mb4_unicode_520_ci\n\n"
83+
. ")\n"
84+
. "engine = InnoDB\n"
85+
. "default charset = utf8mb4\n"
86+
. "collate = utf8mb4_unicode_520_ci;", $s->getLastQuery());
87+
$s->table('hello')->insert([
88+
'user-id' => 33,
89+
'username' => 'Ibrahim',
90+
'pass' => 'rand_pass'
91+
]);
92+
$this->assertEquals('insert into `hello` (`user_id`, `username`, `password`) '
93+
. "values (33, 'Ibrahim', 'rand_pass');", $s->getLastQuery());
94+
$s->table('hello')->select();
95+
$this->assertEquals('select * from `hello`',$s->getLastQuery());
96+
$s->where('user-id', '=', 66);
97+
$this->assertEquals('select * from `hello` where `hello`.`user_id` = 66',$s->getLastQuery());
98+
$s->where('user-id', '=', 77);
99+
$this->assertEquals('select * from `hello` where `hello`.`user_id` = 66 and `hello`.`user_id` = 77',$s->getLastQuery());
100+
$s->clear();
101+
$s->table('hello')->select()->where(
102+
$s->where(
103+
$s->where('user-id', '=', 31)
104+
)->where('user-id', '<', 44, 'or')
105+
)->where('username', '!=', 'Ibrahim', 'and');
106+
$this->assertEquals("select * from `hello` where `hello`.`user_id` = 31 and `hello`.`user_id` < 44 and `hello`.`username` != 'Ibrahim'",$s->getLastQuery());
107+
$s->page(1, 40);
108+
$this->assertEquals("select * from `hello` where `hello`.`user_id` = 31 and `hello`.`user_id` < 44 and `hello`.`username` != 'Ibrahim' limit 40 offset 40",$s->getLastQuery());
109+
$s->page(5, 40);
110+
$this->assertEquals("select * from `hello` where `hello`.`user_id` = 31 and `hello`.`user_id` < 44 and `hello`.`username` != 'Ibrahim' limit 40 offset 200",$s->getLastQuery());
111+
}
68112
}

tests/boot.php

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
require_once $rootDir.$DS.'webfiori'.$DS.'database'.$DS.'mssql'.$DS.'MSSQLConnection.php';
7474

7575
require_once $rootDir.'tests'.$DS.'mssql'.$DS.'MSSQLTestSchema.php';
76+
require_once $rootDir.'tests'.$DS.'HelloTable.php';
77+
7678
use webfiori\database\ConnectionInfo;
7779
use webfiori\database\mysql\MySQLConnection;
7880
use webfiori\database\tests\MySQLTestSchema;

webfiori/database/AbstractQuery.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,13 @@ public function setTable(Table $table) {
10441044
* @since 1.0
10451045
*/
10461046
public function table($tblName) {
1047-
$tableObj = $this->getSchema()->getTable($tblName);
1047+
$tableObj = $this->checkIsClass($tblName);
1048+
1049+
if ($tableObj === null) {
1050+
$tableObj = $this->getSchema()->getTable($tblName);
1051+
} else {
1052+
$this->getSchema()->addTable($tableObj);
1053+
}
10481054
$this->prevQueryObj = $this->copyQuery();
10491055

10501056
if (strlen($this->query) != 0) {
@@ -1056,6 +1062,21 @@ public function table($tblName) {
10561062

10571063
return $this;
10581064
}
1065+
private function checkIsClass($str) {
1066+
if (class_exists($str)) {
1067+
try {
1068+
$clazz = new $str();
1069+
if ($clazz instanceof Table) {
1070+
return $clazz;
1071+
}
1072+
} catch (Exception $ex) {
1073+
1074+
} catch (\ErrorException $ex) {
1075+
1076+
}
1077+
}
1078+
}
1079+
10591080
/**
10601081
* Constructs a query which will truncate a database table when executed.
10611082
*

0 commit comments

Comments
 (0)