Skip to content

Commit 5f0d12a

Browse files
committed
feat: add support sub query builder
- where clause - from clause delete, select, update
1 parent b1bbef1 commit 5f0d12a

File tree

7 files changed

+120
-41
lines changed

7 files changed

+120
-41
lines changed

src/System/Database/MyQuery/Delete.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use System\Database\MyPDO;
88
use System\Database\MyQuery\Traits\ConditionTrait;
9+
use System\Database\MyQuery\Traits\SubQueryTrait;
910

1011
class Delete extends Execute
1112
{
1213
use ConditionTrait;
14+
use SubQueryTrait;
1315

1416
public function __construct(string $table_name, MyPDO $PDO)
1517
{

src/System/Database/MyQuery/Select.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use System\Database\MyQuery;
99
use System\Database\MyQuery\Join\AbstractJoin;
1010
use System\Database\MyQuery\Traits\ConditionTrait;
11+
use System\Database\MyQuery\Traits\SubQueryTrait;
1112

1213
final class Select extends Fetch
1314
{
1415
use ConditionTrait;
16+
use SubQueryTrait;
1517

1618
/**
1719
* @param string $table_name Table name

src/System/Database/MyQuery/Traits/ConditionTrait.php

-37
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace System\Database\MyQuery\Traits;
66

77
use System\Database\MyQuery\Bind;
8-
use System\Database\MyQuery\Select;
98

109
/**
1110
* Trait to provide conditon under class extend with Query::class.
@@ -116,42 +115,6 @@ public function in(string $column_name, $value)
116115
return $this;
117116
}
118117

119-
/**
120-
* Insert 'where exists' condition (query bulider).
121-
*
122-
* @param Select $select Select class
123-
*
124-
* @return self
125-
*/
126-
public function whereExist(Select $select)
127-
{
128-
$binds = (fn () => $this->{'_binds'})->call($select);
129-
$this->_where[] = 'EXISTS (' . $select->__toString() . ')';
130-
foreach ($binds as $binds) {
131-
$this->_binds[] = $binds;
132-
}
133-
134-
return $this;
135-
}
136-
137-
/**
138-
* Insert 'where not exists' condition (query bulider).
139-
*
140-
* @param Select $select Select class
141-
*
142-
* @return self
143-
*/
144-
public function whereNotExist(Select $select)
145-
{
146-
$binds = (fn () => $this->{'_binds'})->call($select);
147-
$this->_where[] = 'NOT EXISTS (' . $select->__toString() . ')';
148-
foreach ($binds as $binds) {
149-
$this->_binds[] = $binds;
150-
}
151-
152-
return $this;
153-
}
154-
155118
/**
156119
* Where statment setter,
157120
* menambahakan syarat pada query builder.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Database\MyQuery\Traits;
6+
7+
use System\Database\MyQuery\Select;
8+
9+
/**
10+
* Sub where query trait.
11+
*/
12+
trait SubQueryTrait
13+
{
14+
/**
15+
* Add sub query to where statement.
16+
*/
17+
public function whereClause(string $clause, Select $select): self
18+
{
19+
$binds = (fn () => $this->{'_binds'})->call($select);
20+
$this->_where[] = implode(' ', [$clause, '(', (string) $select, ')']);
21+
foreach ($binds as $bind) {
22+
$this->_binds[] = $bind;
23+
}
24+
25+
return $this;
26+
}
27+
28+
public function whereCompare(string $column_name, string $operator, Select $select): self
29+
{
30+
return $this->whereClause($column_name . ' ' . $operator, $select);
31+
}
32+
33+
/**
34+
* Added 'where exists' condition (query bulider).
35+
*
36+
* @param Select $select Select class
37+
*
38+
* @return self
39+
*/
40+
public function whereExist(Select $select)
41+
{
42+
return $this->whereClause('EXISTS', $select);
43+
}
44+
45+
/**
46+
* Added 'where not exists' condition (query bulider).
47+
*
48+
* @param Select $select Select class
49+
*
50+
* @return self
51+
*/
52+
public function whereNotExist(Select $select)
53+
{
54+
return $this->whereClause('NOT EXISTS', $select);
55+
}
56+
57+
/**
58+
* Added 'where equal' condition (query bulider).
59+
*/
60+
public function whereEqual(string $column_name, Select $select): self
61+
{
62+
return $this->whereClause($column_name . ' =', $select);
63+
}
64+
65+
/**
66+
* Added 'where like' condition (query bulider).
67+
*/
68+
public function whereLike(string $column_name, Select $select): self
69+
{
70+
return $this->whereClause($column_name . ' LIKE', $select);
71+
}
72+
73+
/**
74+
* Added 'where in' condition (query bulider).
75+
*/
76+
public function whereIn(string $column_name, Select $select): self
77+
{
78+
return $this->whereClause($column_name . ' IN', $select);
79+
}
80+
}

src/System/Database/MyQuery/Update.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use System\Database\MyPDO;
88
use System\Database\MyQuery\Traits\ConditionTrait;
9+
use System\Database\MyQuery\Traits\SubQueryTrait;
910

1011
class Update extends Execute
1112
{
1213
use ConditionTrait;
14+
use SubQueryTrait;
1315

1416
public function __construct(string $table_name, MyPDO $PDO)
1517
{

src/System/Database/MyQuery/Where.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace System\Database\MyQuery;
66

77
use System\Database\MyQuery\Traits\ConditionTrait;
8+
use System\Database\MyQuery\Traits\SubQueryTrait;
89

910
class Where
1011
{
1112
use ConditionTrait;
13+
use SubQueryTrait;
1214

1315
/** @var string Table Name */
1416
private $_table;

tests/DataBase/Query/SelectTest.php

+32-4
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ public function itCanGenerateWhereExisQuery(): void
184184
;
185185

186186
$this->assertEquals(
187-
'SELECT * FROM `base_1` WHERE EXISTS (SELECT * FROM `base_2` WHERE ( (base_2.test = :test) ) AND base_1.id = base_2.id) ORDER BY `base_1`.`id` ASC LIMIT 1, 10',
187+
'SELECT * FROM `base_1` WHERE EXISTS ( SELECT * FROM `base_2` WHERE ( (base_2.test = :test) ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10',
188188
$select->__toString(),
189189
'where exist query'
190190
);
191191

192192
$this->assertEquals(
193-
"SELECT * FROM `base_1` WHERE EXISTS (SELECT * FROM `base_2` WHERE ( (base_2.test = 'success') ) AND base_1.id = base_2.id) ORDER BY `base_1`.`id` ASC LIMIT 1, 10",
193+
"SELECT * FROM `base_1` WHERE EXISTS ( SELECT * FROM `base_2` WHERE ( (base_2.test = 'success') ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10",
194194
$select->queryBind(),
195195
'where exist query'
196196
);
@@ -211,13 +211,41 @@ public function itCanGenerateWhereNotExisQuery(): void
211211
;
212212

213213
$this->assertEquals(
214-
'SELECT * FROM `base_1` WHERE NOT EXISTS (SELECT * FROM `base_2` WHERE ( (base_2.test = :test) ) AND base_1.id = base_2.id) ORDER BY `base_1`.`id` ASC LIMIT 1, 10',
214+
'SELECT * FROM `base_1` WHERE NOT EXISTS ( SELECT * FROM `base_2` WHERE ( (base_2.test = :test) ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10',
215215
$select->__toString(),
216216
'where exist query'
217217
);
218218

219219
$this->assertEquals(
220-
"SELECT * FROM `base_1` WHERE NOT EXISTS (SELECT * FROM `base_2` WHERE ( (base_2.test = 'success') ) AND base_1.id = base_2.id) ORDER BY `base_1`.`id` ASC LIMIT 1, 10",
220+
"SELECT * FROM `base_1` WHERE NOT EXISTS ( SELECT * FROM `base_2` WHERE ( (base_2.test = 'success') ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10",
221+
$select->queryBind(),
222+
'where exist query'
223+
);
224+
}
225+
226+
/** @test */
227+
public function itCanGenerateSelectWithSubQuery(): void
228+
{
229+
$select = MyQuery::from('base_1', $this->PDO)
230+
->select()
231+
->whereClause(
232+
'`user` =',
233+
(new Select('base_2', ['*'], $this->PDO))
234+
->equal('test', 'success')
235+
->where('base_1.id = base_2.id')
236+
)
237+
->limit(1, 10)
238+
->order('id', MyQuery::ORDER_ASC)
239+
;
240+
241+
$this->assertEquals(
242+
'SELECT * FROM `base_1` WHERE `user` = ( SELECT * FROM `base_2` WHERE ( (base_2.test = :test) ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10',
243+
$select->__toString(),
244+
'where exist query'
245+
);
246+
247+
$this->assertEquals(
248+
"SELECT * FROM `base_1` WHERE `user` = ( SELECT * FROM `base_2` WHERE ( (base_2.test = 'success') ) AND base_1.id = base_2.id ) ORDER BY `base_1`.`id` ASC LIMIT 1, 10",
221249
$select->queryBind(),
222250
'where exist query'
223251
);

0 commit comments

Comments
 (0)