Skip to content

Commit 62073e8

Browse files
committed
Added ability to handle inner selects
1 parent a248c79 commit 62073e8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Builder/Select.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,16 @@ private function buildForUpdate($query) {
418418
* @return string
419419
*/
420420
private function buildTableName($alias, $name) {
421-
$name = rtrim(trim($name), ';');
422-
return sprintf("(%s) %s", $name, $alias);
421+
if(is_object($name)) {
422+
$name = (string) $name;
423+
$lines = explode("\n", $name);
424+
foreach($lines as &$line) {
425+
$line = "\t{$line}";
426+
}
427+
$name = join("\n", $lines);
428+
$name = '(' . trim(rtrim(trim($name), ';')) . ')';
429+
}
430+
return sprintf("%s %s", $name, $alias);
423431
}
424432

425433
/**

tests/Builder/SelectTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,16 @@ public function testForUpdate() {
132132
->asString();
133133
$this->assertEquals('SELECT a FROM test t FOR UPDATE ;', $str);
134134
}
135+
136+
public function testInnerSelect() {
137+
$select = TestSelect::create()
138+
->from('a', 'table')
139+
->where('a.id=1');
140+
141+
$str = (string) TestSelect::create()
142+
->from('t', $select)
143+
->asString();
144+
145+
$this->assertEquals('SELECT * FROM (SELECT * FROM table a WHERE a.id=1) t ;', $str);
146+
}
135147
}

0 commit comments

Comments
 (0)