Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 79b6c86

Browse files
author
Nik Barham
committed
Ability to specify join type in Query Builder.
Can now SELECT FROM (`subclause`) as subclause ... in QueryBuilder
1 parent 579afe9 commit 79b6c86

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/Automatorm/Database/QueryBuilder.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,24 @@ public function orderBy($sort, $dir = 'desc') { return $this->sortBy($sort, $dir
273273
* @param string $table Name of table to select from
274274
* @return self
275275
*/
276-
public function join($table)
277-
{
278-
$this->joins[] = ['table' => $table, 'where' => [], 'on' => []];
276+
public function join($table, $rawtype = null)
277+
{
278+
$type = 'JOIN';
279+
if ($rawtype) switch (strtolower($rawtype)) {
280+
case 'left':
281+
$type = 'LEFT JOIN';
282+
break;
283+
case 'left outer':
284+
$type = 'LEFT OUTER JOIN';
285+
break;
286+
case 'cross':
287+
$type = 'CROSS JOIN';
288+
break;
289+
default:
290+
throw new Exception\Query('Unknown Join Type');
291+
}
292+
293+
$this->joins[] = ['table' => $table, 'type' => $type, 'where' => [], 'on' => []];
279294

280295
return $this;
281296
}
@@ -287,9 +302,24 @@ public function join($table)
287302
* @param string $alias Alias for the derived table
288303
* @return self
289304
*/
290-
public function joinSubquery($subquery, $alias)
291-
{
292-
$this->joins[] = ['table' => null, 'subquery' => $subquery, 'alias' => $alias, 'where' => [], 'on' => []];
305+
public function joinSubquery($subquery, $alias, $rawtype = null)
306+
{
307+
$type = 'JOIN';
308+
if ($rawtype) switch (strtolower($rawtype)) {
309+
case 'left':
310+
$type = 'LEFT JOIN';
311+
break;
312+
case 'left outer':
313+
$type = 'LEFT OUTER JOIN';
314+
break;
315+
case 'cross':
316+
$type = 'CROSS JOIN';
317+
break;
318+
default:
319+
throw new Exception\Query('Unknown Join Type');
320+
}
321+
322+
$this->joins[] = ['table' => null, 'subquery' => $subquery, 'type' => $type, 'alias' => $alias, 'where' => [], 'on' => []];
293323
return $this;
294324
}
295325

@@ -410,6 +440,12 @@ public function resolve()
410440

411441
public function resolveTable()
412442
{
443+
if ($this->table instanceof QueryBuilder)
444+
{
445+
list($sql, $subdata) = $this->table->resolve();
446+
$this->data = array_merge($this->data, $subdata);
447+
return '(' . $this->table->resolve . ') AS subquery';
448+
}
413449
return $this->escapeTable($this->table);
414450
}
415451

@@ -423,15 +459,15 @@ public function resolveJoins()
423459
{
424460
if ($join['table']) {
425461
$clauses = [];
426-
$joinstring .= ' JOIN ' . $this->escapeTable($join['table']);
462+
$joinstring .= " {$join['type']} " . $this->escapeTable($join['table']);
427463
} elseif ($join['subquery']) {
428464
$sql = $join['subquery'];
429465
if ($sql instanceof QueryBuilder) {
430466
list ($sql, $subdata) = $join['subquery']->resolve();
431467
$this->data = array_merge($this->data, $subdata);
432468
}
433469

434-
$joinstring .= " JOIN ($sql) as {$join['alias']}";
470+
$joinstring .= " {$join['type']} ($sql) as {$join['alias']}";
435471
}
436472

437473
if ($join['where'])

0 commit comments

Comments
 (0)