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\UnderQuery\QueryBuilder; |
13
|
|
|
|
14
|
|
|
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface; |
15
|
|
|
use Phuria\UnderQuery\Query\Query; |
16
|
|
|
use Phuria\UnderQuery\Query\QueryFactoryInterface; |
17
|
|
|
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface; |
18
|
|
|
use Phuria\UnderQuery\Reference\ReferenceCollectionInterface; |
19
|
|
|
use Phuria\UnderQuery\TableFactory\TableFactoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractBuilder implements BuilderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TableFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $tableFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QueryCompilerInterface |
33
|
|
|
*/ |
34
|
|
|
private $queryCompiler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ParameterCollectionInterface |
38
|
|
|
*/ |
39
|
|
|
private $parameterCollection; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ReferenceCollectionInterface |
43
|
|
|
*/ |
44
|
|
|
private $referenceCollection; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var QueryFactoryInterface |
48
|
|
|
*/ |
49
|
|
|
private $queryFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param TableFactoryInterface $tableFactory |
53
|
|
|
* @param QueryCompilerInterface $queryCompiler |
54
|
|
|
* @param QueryFactoryInterface $queryFactory |
55
|
|
|
* @param array $options |
56
|
|
|
*/ |
57
|
6 |
|
public function __construct( |
58
|
|
|
TableFactoryInterface $tableFactory, |
59
|
|
|
QueryCompilerInterface $queryCompiler, |
60
|
|
|
QueryFactoryInterface $queryFactory, |
61
|
|
|
array $options |
62
|
|
|
) { |
63
|
6 |
|
$this->tableFactory = $tableFactory; |
64
|
6 |
|
$this->queryCompiler = $queryCompiler; |
65
|
6 |
|
$this->queryFactory = $queryFactory; |
66
|
|
|
|
67
|
6 |
|
$this->parameterCollection = new $options['parameter_collection_class']; |
68
|
6 |
|
$this->referenceCollection = new $options['reference_collection_class']; |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return BuilderInterface |
73
|
|
|
*/ |
74
|
3 |
|
public function getQueryBuilder() |
75
|
|
|
{ |
76
|
3 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return TableFactoryInterface |
81
|
|
|
*/ |
82
|
1 |
|
public function getTableFactory() |
83
|
|
|
{ |
84
|
1 |
|
return $this->tableFactory; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return QueryCompilerInterface |
89
|
|
|
*/ |
90
|
3 |
|
public function getQueryCompiler() |
91
|
|
|
{ |
92
|
3 |
|
return $this->queryCompiler; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ParameterCollectionInterface |
97
|
|
|
*/ |
98
|
3 |
|
public function getParameters() |
99
|
|
|
{ |
100
|
3 |
|
return $this->parameterCollection; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ReferenceCollectionInterface |
105
|
|
|
*/ |
106
|
3 |
|
public function getReferences() |
107
|
|
|
{ |
108
|
3 |
|
return $this->referenceCollection; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
2 |
|
public function buildSQL() |
115
|
|
|
{ |
116
|
2 |
|
return $this->getQueryCompiler()->compile($this->getQueryBuilder()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
1 |
|
public function objectToString($object) |
123
|
|
|
{ |
124
|
1 |
|
return $this->getReferences()->createReference($object); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
1 |
|
public function setParameter($name, $value) |
131
|
|
|
{ |
132
|
1 |
|
$parameter = $this->getParameters()->getParameter($name); |
133
|
1 |
|
$parameter->setValue($value); |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed $connectionHint |
140
|
|
|
* |
141
|
|
|
* @return Query |
142
|
|
|
*/ |
143
|
1 |
|
public function buildQuery($connectionHint = 'default') |
144
|
|
|
{ |
145
|
1 |
|
return $this->queryFactory->buildQuery( |
146
|
1 |
|
$this->buildSQL(), |
147
|
1 |
|
$this->getParameters()->toArray(), |
148
|
|
|
$connectionHint |
149
|
1 |
|
); |
150
|
|
|
} |
151
|
|
|
} |