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\QueryBuilder\AbstractBuilder; |
15
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Component; |
16
|
|
|
use Phuria\SQLBuilder\QueryBuilder\SelectBuilder; |
17
|
|
|
use Phuria\SQLBuilder\Table\AbstractTable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class TableCompiler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param AbstractTable $table |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
28 |
|
private function compileTableDeclaration(AbstractTable $table) |
30
|
|
|
{ |
31
|
28 |
|
$declaration = ''; |
32
|
|
|
|
33
|
28 |
|
if ($table->isJoin()) { |
34
|
3 |
|
$declaration .= $table->getJoinType() . ' '; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
28 |
|
$declaration .= $table->getTableName(); |
38
|
|
|
|
39
|
28 |
|
if ($alias = $table->getAlias()) { |
40
|
9 |
|
$declaration .= ' AS ' . $alias; |
41
|
9 |
|
} |
42
|
|
|
|
43
|
28 |
|
if ($joinOn = $table->getJoinOn()) { |
44
|
2 |
|
$declaration .= ' ON ' . $joinOn; |
45
|
2 |
|
} |
46
|
|
|
|
47
|
28 |
|
return $declaration; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param AbstractBuilder $qb |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
29 |
|
public function compileRootTables(AbstractBuilder $qb) |
56
|
|
|
{ |
57
|
29 |
|
$rootTables = ''; |
58
|
|
|
|
59
|
29 |
|
if ($qb instanceof SelectBuilder) { |
60
|
24 |
|
$rootTables .= 'FROM '; |
61
|
24 |
|
} |
62
|
|
|
|
63
|
29 |
|
if ($qb instanceof Component\TableComponentInterface && $qb->getRootTables()) { |
64
|
28 |
|
$rootTables .= implode(', ', array_map([$this, 'compileTableDeclaration'], $qb->getRootTables())); |
65
|
28 |
|
} else { |
66
|
1 |
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
28 |
|
return $rootTables; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param AbstractBuilder $qb |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
29 |
|
public function compileJoinTables(AbstractBuilder $qb) |
78
|
|
|
{ |
79
|
29 |
|
if ($qb instanceof Component\JoinComponentInterface) { |
80
|
24 |
|
return implode(' ', array_map([$this, 'compileTableDeclaration'], $qb->getJoinTables())); |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
return ''; |
84
|
|
|
} |
85
|
|
|
} |