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