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

Commit e2b6a5e

Browse files
author
Nik Barham
committed
Added ->joinSubquery for QueryBuilder
Removed On/Where shortcut for ->join(
1 parent 20487ac commit e2b6a5e

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/Automatorm/Database/QueryBuilder.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,25 @@ public function orderBy($sort, $dir = 'desc') { return $this->sortBy($sort, $dir
271271
* Join a table to this query
272272
*
273273
* @param string $table Name of table to select from
274-
* @param mixed[] $columnclauses List of on clauses in the format column = column
275-
* @param mixed[] $valueclauses List of on clauses in the format column = value
276274
* @return self
277275
*/
278-
public function join($table, $columnclauses = [], $valueclauses = [])
276+
public function join($table)
279277
{
280278
$this->joins[] = ['table' => $table, 'where' => [], 'on' => []];
281279

282-
if ($columnclauses) $this->joinOn($columnclauses);
283-
if ($valueclauses) $this->joinWhere($valueclauses);
284-
280+
return $this;
281+
}
282+
283+
/**
284+
* Join a subquery as a derived table to this query
285+
*
286+
* @param mixed $subquery String or QueryBuilder object representing subquery
287+
* @param string $alias Alias for the derived table
288+
* @return self
289+
*/
290+
public function joinSubquery($subquery, $alias)
291+
{
292+
$this->joins[] = ['table' => null, 'subquery' => $subquery, 'alias' => $alias, 'where' => [], 'on' => []];
285293
return $this;
286294
}
287295

@@ -364,7 +372,7 @@ public function resolve()
364372
$sort = $this->resolveSort();
365373
$limit = $this->resolveLimit();
366374

367-
return ["SELECT $columns FROM $table{$join}{$where}{$group}{$having}{$sort}{$limit};", $this->data];
375+
return ["SELECT $columns FROM $table{$join}{$where}{$group}{$having}{$sort}{$limit}", $this->data];
368376

369377
case 'select':
370378
$columns = $this->resolveColumns();
@@ -375,28 +383,28 @@ public function resolve()
375383
$sort = $this->resolveSort();
376384
$limit = $this->resolveLimit();
377385

378-
return ["SELECT $columns FROM $table{$join}{$where}{$group}{$having}{$sort}{$limit};", $this->data];
386+
return ["SELECT $columns FROM $table{$join}{$where}{$group}{$having}{$sort}{$limit}", $this->data];
379387

380388
case 'insert':
381389
$join = $this->resolveJoins();
382390
$data = $this->resolveColumnData();
383391
$limit = $this->resolveLimit();
384392

385-
return ["INSERT INTO $table{$join}{$data}{$limit};", $this->data];
393+
return ["INSERT INTO $table{$join}{$data}{$limit}", $this->data];
386394

387395
case 'update':
388396
$join = $this->resolveJoins();
389397
$data = $this->resolveColumnData();
390398
$where = $this->resolveWhere();
391399
$limit = $this->resolveLimit();
392400

393-
return ["UPDATE $table{$join}{$data}{$where}{$limit};", $this->data];
401+
return ["UPDATE $table{$join}{$data}{$where}{$limit}", $this->data];
394402

395403
case 'delete':
396404
$where = $this->resolveWhere();
397405
$limit = $this->resolveLimit();
398406

399-
return ["DELETE FROM $table{$where}{$limit};", $this->data];
407+
return ["DELETE FROM $table{$where}{$limit}", $this->data];
400408
}
401409
}
402410

@@ -410,10 +418,19 @@ public function resolveJoins()
410418
if (!$this->joins) return '';
411419

412420
$joinstring = '';
421+
413422
foreach ($this->joins as $join)
414423
{
415-
$clauses = [];
416-
$joinstring .= ' JOIN ' . $this->escapeTable($join['table']);
424+
if ($join['table']) {
425+
$clauses = [];
426+
$joinstring .= ' JOIN ' . $this->escapeTable($join['table']);
427+
} elseif ($join['subquery']) {
428+
$sql = $join['subquery'];
429+
if ($sql instanceof QueryBuilder) $sql = $join['subquery']->resolve();
430+
431+
$joinstring .= " JOIN ($sql) as {$join['alias']}";
432+
}
433+
417434
if ($join['where'])
418435
{
419436
foreach ($join['where'] as $where) {

0 commit comments

Comments
 (0)