1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Phuria SQL Builder package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\SQLBuilder\QueryCompiler; |
13
|
|
|
|
14
|
|
|
use Phuria\SQLBuilder\JoinType; |
15
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Component; |
16
|
|
|
use Phuria\SQLBuilder\QueryBuilder\DeleteBuilder; |
17
|
|
|
use Phuria\SQLBuilder\QueryBuilder\SelectBuilder; |
18
|
|
|
use Phuria\SQLBuilder\Table\AbstractTable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TableCompiler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $joinPrefixes; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* TableCompiler constructor. |
32
|
|
|
*/ |
33
|
9 |
|
public function __construct() |
34
|
|
|
{ |
35
|
9 |
|
$this->joinPrefixes = [ |
36
|
9 |
|
JoinType::CROSS_JOIN => 'CROSS', |
37
|
9 |
|
JoinType::LEFT_JOIN => 'LEFT', |
38
|
9 |
|
JoinType::RIGHT_JOIN => 'RIGHT', |
39
|
9 |
|
JoinType::INNER_JOIN => 'INNER', |
40
|
9 |
|
JoinType::STRAIGHT_JOIN => 'STRAIGHT_JOIN' |
41
|
9 |
|
]; |
42
|
9 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AbstractTable $table |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
9 |
|
public function compileTableDeclaration(AbstractTable $table) |
50
|
|
|
{ |
51
|
9 |
|
$declaration = ''; |
52
|
|
|
|
53
|
9 |
|
if ($table->isJoin()) { |
54
|
5 |
|
$declaration .= $this->compileJoinName($table) . ' '; |
55
|
5 |
|
} |
56
|
|
|
|
57
|
9 |
|
$declaration .= $table->getTableName(); |
58
|
|
|
|
59
|
9 |
|
if ($alias = $table->getAlias()) { |
60
|
|
|
$declaration .= ' AS ' . $alias; |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
if ($joinOn = $table->getJoinOn()) { |
64
|
|
|
$declaration .= ' ON ' . $joinOn; |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
return $declaration; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param AbstractTable $table |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
5 |
|
private function compileJoinName(AbstractTable $table) |
76
|
|
|
{ |
77
|
5 |
|
return implode(' ', array_filter([ |
78
|
5 |
|
$table->isNaturalJoin() ? 'NATURAL' : '', |
79
|
5 |
|
$this->compileJoinPrefix($table->getJoinType()), |
80
|
5 |
|
$table->isOuterJoin() ? 'OUTER' : '', |
81
|
5 |
|
$this->compileJoinSuffix($table->getJoinType()) |
82
|
5 |
|
])); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $joinType |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
5 |
|
private function compileJoinPrefix($joinType) |
91
|
|
|
{ |
92
|
5 |
|
if (array_key_exists($joinType, $this->joinPrefixes)) { |
93
|
4 |
|
return $this->joinPrefixes[$joinType]; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $joinType |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
5 |
|
private function compileJoinSuffix($joinType) |
105
|
|
|
{ |
106
|
5 |
|
return $joinType === JoinType::STRAIGHT_JOIN ? '' : 'JOIN'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param CompilerPayload $payload |
111
|
|
|
* |
112
|
|
|
* @return CompilerPayload |
113
|
|
|
*/ |
114
|
4 |
|
public function compileRootTables(CompilerPayload $payload) |
115
|
|
|
{ |
116
|
4 |
|
$builder = $payload->getBuilder(); |
117
|
4 |
|
$newSQL = ''; |
118
|
|
|
|
119
|
4 |
|
if ($builder instanceof Component\TableComponentInterface && $builder->getRootTables()) { |
120
|
4 |
|
$newSQL = implode(', ', array_map([$this, 'compileTableDeclaration'], $builder->getRootTables())); |
121
|
4 |
|
} |
122
|
|
|
|
123
|
4 |
|
if ($builder instanceof SelectBuilder || $builder instanceof DeleteBuilder) { |
124
|
|
|
$newSQL = $newSQL ? 'FROM ' . $newSQL : ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
return $payload->appendSQL($newSQL); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param CompilerPayload $payload |
132
|
|
|
* |
133
|
|
|
* @return CompilerPayload |
134
|
|
|
*/ |
135
|
4 |
|
public function compileJoinTables(CompilerPayload $payload) |
136
|
|
|
{ |
137
|
4 |
|
$builder = $payload->getBuilder(); |
138
|
|
|
|
139
|
4 |
|
if ($builder instanceof Component\JoinComponentInterface) { |
140
|
|
|
$newSQL = implode(' ', array_map([$this, 'compileTableDeclaration'], $builder->getJoinTables())); |
141
|
|
|
return $payload->appendSQL($newSQL); |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
return $payload; |
145
|
|
|
} |
146
|
|
|
} |