Skip to content

Commit 0f5b65c

Browse files
committed
Merge remote-tracking branch 'origin/master'
Conflicts: src/Databases/MySQL.php
2 parents 0d580ea + 394d036 commit 0f5b65c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"email": "[email protected]"
88
}],
99
"require": {
10-
"php": ">= 5.4"
10+
"php": ">= 5.4",
11+
"psr/log": "~1"
1112
},
1213
"require-dev": {
1314
"phpunit/phpunit": "~4.0",

src/Builder/Select.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ class Select extends Statement {
4444
*/
4545
public function field($expression, $alias = null) {
4646
if(is_object($expression)) {
47-
$expression = (string)$expression;
47+
$expression = (string) $expression;
48+
$expression = trim($expression);
49+
$expression = rtrim($expression, ';');
50+
$expression = trim($expression);
51+
$lines = explode("\n", $expression);
52+
$lines = array_map(function ($line) { return "\t\t{$line}"; }, $lines);
53+
$expression = join("\n", $lines);
54+
$expression = sprintf("(\n%s\n\t)", $expression);
4855
}
4956
if($alias === null) {
5057
$this->fields[] = $expression;

src/Databases/MySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MySQL implements Database {
1616
*/
1717
private static $tableFields = array();
1818
/**
19-
* @var \PDO
19+
* @var PDO
2020
*/
2121
private $pdo;
2222
/**

tests/Builder/SelectTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,21 @@ public function testCount() {
157157

158158
$this->assertEquals("SELECT\n\tCOUNT(*)\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nWHERE\n\t(t1.id > 10)\n;\n", $query);
159159
}
160+
161+
public function testSubselectAsField() {
162+
$query = TestSelect::create()
163+
->field('COUNT(*)')
164+
->from('t1', 'test1')
165+
->joinInner('t2', 'test2', 't1.id=t2.id')
166+
->where('t1.id > 10');
167+
168+
$query = TestSelect::create()
169+
->field($query, 'testfield')
170+
->from('t1', 'test1')
171+
->joinInner('t2', 'test2', 't1.id=t2.id')
172+
->where('t1.id > 10')
173+
->asString();
174+
175+
$this->assertEquals("SELECT\n\t(\n\t\tSELECT\n\t\t\tCOUNT(*)\n\t\tFROM\n\t\t\ttest1 t1\n\t\tINNER JOIN\n\t\t\ttest2 t2 ON t1.id=t2.id\n\t\tWHERE\n\t\t\t(t1.id > 10)\n\t) AS `testfield`\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nWHERE\n\t(t1.id > 10)\n;\n", $query);
176+
}
160177
}

0 commit comments

Comments
 (0)