|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Muffin\Queries\Snippets\Joins; |
|
4
|
|
|
|
|
5
|
|
|
use Muffin\Snippet; |
|
6
|
|
|
use Muffin\Queries\Snippets\Join; |
|
7
|
|
|
use Muffin\Queries\Snippets; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractJoin implements Join, Snippet |
|
10
|
|
|
{ |
|
11
|
|
|
private |
|
12
|
|
|
$table, |
|
13
|
|
|
$using, |
|
14
|
|
|
$on; |
|
15
|
|
|
|
|
16
|
23 |
|
public function __construct($table, $alias = null) |
|
17
|
|
|
{ |
|
18
|
23 |
|
$this->table = new Snippets\TableName($table, $alias); |
|
19
|
23 |
|
$this->on = array(); |
|
20
|
23 |
|
} |
|
21
|
|
|
|
|
22
|
9 |
|
public function using($column) |
|
23
|
|
|
{ |
|
24
|
9 |
|
$this->on = array(); |
|
25
|
|
|
|
|
26
|
9 |
|
$this->using = new Snippets\Using($column); |
|
27
|
|
|
|
|
28
|
9 |
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
16 |
|
public function on($leftColumn, $rightColumn) |
|
32
|
|
|
{ |
|
33
|
16 |
|
$this->using = null; |
|
34
|
16 |
|
$this->on[] = new Snippets\On($leftColumn, $rightColumn); |
|
35
|
|
|
|
|
36
|
16 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
23 |
|
public function toString() |
|
40
|
|
|
{ |
|
41
|
23 |
|
$joinQueryPart = sprintf( |
|
42
|
23 |
|
'%s %s', |
|
43
|
23 |
|
$this->getJoinDeclaration(), |
|
44
|
23 |
|
$this->table->toString() |
|
45
|
23 |
|
); |
|
46
|
|
|
|
|
47
|
23 |
|
$joinQueryPart .= $this->buildOnConditionClause(); |
|
48
|
23 |
|
$joinQueryPart .= $this->buildUsingConditionClause(); |
|
49
|
|
|
|
|
50
|
23 |
|
return $joinQueryPart; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
abstract protected function getJoinDeclaration(); |
|
54
|
|
|
|
|
55
|
23 |
|
private function buildUsingConditionClause() |
|
56
|
|
|
{ |
|
57
|
23 |
|
if(!$this->using instanceof Snippet) |
|
58
|
23 |
|
{ |
|
59
|
17 |
|
return ''; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
return ' ' . $this->using->toString(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
23 |
|
private function buildOnConditionClause() |
|
66
|
|
|
{ |
|
67
|
23 |
|
$conditionClause = array(); |
|
68
|
|
|
|
|
69
|
23 |
|
foreach($this->on as $on) |
|
70
|
|
|
{ |
|
71
|
16 |
|
if($on instanceof Snippet) |
|
72
|
16 |
|
{ |
|
73
|
16 |
|
$conditionClause[] = $on->toString(); |
|
74
|
16 |
|
} |
|
75
|
23 |
|
} |
|
76
|
|
|
|
|
77
|
23 |
|
return empty($conditionClause) ? '' : ' ' . implode('', $conditionClause); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|