1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of UnderQuery 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\UnderQuery\QueryCompiler; |
13
|
|
|
|
14
|
|
|
use Phuria\UnderQuery\JoinType; |
15
|
|
|
use Phuria\UnderQuery\QueryBuilder\AbstractBuilder; |
16
|
|
|
use Phuria\UnderQuery\QueryBuilder\Clause; |
17
|
|
|
use Phuria\UnderQuery\QueryBuilder\DeleteBuilder; |
18
|
|
|
use Phuria\UnderQuery\QueryBuilder\SelectBuilder; |
19
|
|
|
use Phuria\UnderQuery\Table\AbstractTable; |
20
|
|
|
use Phuria\UnderQuery\Table\JoinMetadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class TableCompiler |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $joinPrefixes; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TableCompiler constructor. |
34
|
|
|
*/ |
35
|
7 |
|
public function __construct() |
36
|
|
|
{ |
37
|
7 |
|
$this->joinPrefixes = [ |
38
|
7 |
|
JoinType::CROSS_JOIN => 'CROSS', |
39
|
7 |
|
JoinType::LEFT_JOIN => 'LEFT', |
40
|
7 |
|
JoinType::RIGHT_JOIN => 'RIGHT', |
41
|
7 |
|
JoinType::INNER_JOIN => 'INNER', |
42
|
7 |
|
JoinType::STRAIGHT_JOIN => 'STRAIGHT_JOIN' |
43
|
7 |
|
]; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AbstractTable $table |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
7 |
|
public function compileTableDeclaration(AbstractTable $table) |
52
|
|
|
{ |
53
|
7 |
|
$declaration = ''; |
54
|
|
|
|
55
|
7 |
|
if ($table->isJoin()) { |
56
|
6 |
|
$declaration .= $this->compileJoinName($table->getJoinMetadata()) . ' '; |
|
|
|
|
57
|
6 |
|
} |
58
|
|
|
|
59
|
7 |
|
$declaration .= $table->getTableName(); |
60
|
|
|
|
61
|
7 |
|
if ($alias = $table->getAlias()) { |
62
|
1 |
|
$declaration .= ' AS ' . $alias; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
7 |
|
if ($table->isJoin() && $joinOn = $table->getJoinMetadata()->getJoinOn()) { |
66
|
1 |
|
$declaration .= ' ON ' . $joinOn; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
7 |
|
return $declaration; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param JoinMetadata $metadata |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
6 |
|
private function compileJoinName(JoinMetadata $metadata) |
78
|
|
|
{ |
79
|
6 |
|
return implode(' ', array_filter([ |
80
|
6 |
|
$metadata->isNaturalJoin() ? 'NATURAL' : '', |
81
|
6 |
|
$this->compileJoinPrefix($metadata->getJoinType()), |
82
|
6 |
|
$metadata->isOuterJoin() ? 'OUTER' : '', |
83
|
6 |
|
$this->compileJoinSuffix($metadata->getJoinType()) |
84
|
6 |
|
])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $joinType |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
6 |
|
private function compileJoinPrefix($joinType) |
93
|
|
|
{ |
94
|
6 |
|
if (array_key_exists($joinType, $this->joinPrefixes)) { |
95
|
4 |
|
return $this->joinPrefixes[$joinType]; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $joinType |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
6 |
|
private function compileJoinSuffix($joinType) |
107
|
|
|
{ |
108
|
6 |
|
return $joinType === JoinType::STRAIGHT_JOIN ? '' : 'JOIN'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param CompilerPayload $payload |
113
|
|
|
* |
114
|
|
|
* @return CompilerPayload |
115
|
|
|
*/ |
116
|
|
|
public function compileRootTables(CompilerPayload $payload) |
117
|
|
|
{ |
118
|
|
|
$builder = $payload->getBuilder(); |
119
|
|
|
$newSQL = ''; |
120
|
|
|
|
121
|
|
|
if ($builder instanceof AbstractBuilder && $builder->getRootTables()) { |
122
|
|
|
$newSQL = implode(', ', array_map([$this, 'compileTableDeclaration'], $builder->getRootTables())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($builder instanceof SelectBuilder || $builder instanceof DeleteBuilder) { |
126
|
|
|
$newSQL = $newSQL ? 'FROM ' . $newSQL : ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $payload->appendSQL($newSQL); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param CompilerPayload $payload |
134
|
|
|
* |
135
|
|
|
* @return CompilerPayload |
136
|
|
|
*/ |
137
|
|
|
public function compileJoinTables(CompilerPayload $payload) |
138
|
|
|
{ |
139
|
|
|
$builder = $payload->getBuilder(); |
140
|
|
|
|
141
|
|
|
if ($builder instanceof Clause\JoinInterface) { |
142
|
|
|
$newSQL = implode(' ', array_map([$this, 'compileTableDeclaration'], $builder->getJoinTables())); |
143
|
|
|
return $payload->appendSQL($newSQL); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $payload; |
147
|
|
|
} |
148
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: