Skip to content

Commit 01769e8

Browse files
committed
- Table-Names can now also be arrays
1 parent 5ff56a7 commit 01769e8

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

src/Builder/Insert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ private function addAllTo(array $data, array $mask = null, array $excludeFields
250250
foreach ($data as $field => $value) {
251251
call_user_func($fn, $field, $value);
252252
}
253+
return $this;
253254
}
254255

255256
/**

src/Builder/QueryStatement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,6 @@ private function exceptionHandler($fn) {
152152
} catch (PDOException $e) {
153153
$this->exceptionInterpreter->throwMoreConcreteException($e);
154154
}
155+
return null;
155156
}
156157
}

src/Builder/Traits/TableBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function buildTables($query) {
3636
}
3737

3838
/**
39-
* @return string[]
39+
* @return array[]
4040
*/
4141
protected function getTables() {
4242
return $this->tables;

src/Builder/Traits/TableNameBuilder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Builder\Traits;
33

4+
use Kir\MySQL\Database;
5+
46
trait TableNameBuilder {
57
use AbstractAliasReplacer;
68

@@ -19,10 +21,26 @@ protected function buildTableName($alias, $name) {
1921
$name = join("\n", $lines);
2022
$name = '(' . trim(rtrim(trim($name), ';')) . ')';
2123
}
24+
if(is_array($name)) {
25+
$parts = [];
26+
foreach($name as $bucket) {
27+
$values = [];
28+
foreach($bucket as $field => $value) {
29+
$values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field));
30+
}
31+
$parts[] = sprintf("SELECT %s", join(', ', $values));
32+
}
33+
$name = '(' . join("\n\tUNION\n\t", $parts) . ')';
34+
}
2235
$name = $this->aliasReplacer()->replace($name);
2336
if($alias !== null) {
2437
return sprintf("%s %s", $name, $alias);
2538
}
2639
return $name;
2740
}
41+
42+
/**
43+
* @return Database
44+
*/
45+
abstract protected function db();
2846
}

src/Databases/MySQL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ private function exceptionHandler($fn) {
377377
} catch (PDOException $e) {
378378
$this->exceptionInterpreter->throwMoreConcreteException($e);
379379
}
380+
return null;
380381
}
381382

382383
/**

tests/Builder/SelectTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function testFrom() {
2222
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\n", $str);
2323
}
2424

25+
public function testFromArray() {
26+
$str = TestSelect::create()
27+
->field('a.a')
28+
->field('b.b')
29+
->from('a', [['a' => 1, 'b' => 3], ['a' => 2, 'b' => 2], ['a' => 3, 'b' => 1]])
30+
->joinInner('b', [['a' => 1, 'b' => 3], ['a' => 2, 'b' => 2], ['a' => 3, 'b' => 1]])
31+
->asString();
32+
$this->assertEquals("SELECT\n\ta.a,\n\tb.b\nFROM\n\t(SELECT '1' AS `a`, '3' AS `b`\n\tUNION\n\tSELECT '2' AS `a`, '2' AS `b`\n\tUNION\n\tSELECT '3' AS `a`, '1' AS `b`) a\nINNER JOIN\n\t(SELECT '1' AS `a`, '3' AS `b`\n\tUNION\n\tSELECT '2' AS `a`, '2' AS `b`\n\tUNION\n\tSELECT '3' AS `a`, '1' AS `b`) b\n", $str);
33+
}
34+
2535
public function testMultipleFrom() {
2636
$str = TestSelect::create()
2737
->field('a')

0 commit comments

Comments
 (0)