1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Query\Traits\Clause\ExtraClauseTrait; |
19
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
20
|
|
|
use Phossa2\Query\Interfaces\StatementInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* StatementAbstract |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Query |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ObjectAbstract |
28
|
|
|
* @see StatementInterface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface |
33
|
|
|
{ |
34
|
|
|
use SettingsAwareTrait, BuilderAwareTrait, ExtraClauseTrait, PreviousTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* value bindings |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $bindings; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* clause configs,[name => prefix] |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $configs = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param BuilderInterface $builder |
56
|
|
|
* @access public |
57
|
|
|
*/ |
58
|
|
|
public function __construct(BuilderInterface $builder) |
59
|
|
|
{ |
60
|
|
|
$this->setBuilder($builder); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function getStatement(array $settings = [])/*# : string */ |
67
|
|
|
{ |
68
|
|
|
// combine settings |
69
|
|
|
$settings = $this->combineSettings($settings); |
70
|
|
|
|
71
|
|
|
// build current sql |
72
|
|
|
$sql = $this->buildSql($settings); |
73
|
|
|
|
74
|
|
|
// replace with ?, :name or real values |
75
|
|
|
return $this->bindValues($sql, $settings); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function getNamedStatement(array $settings = [])/*# : string */ |
82
|
|
|
{ |
83
|
|
|
$settings['positionedParam'] = false; |
84
|
|
|
$settings['namedParam'] = true; |
85
|
|
|
return $this->getStatement($settings); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the sql string (with parameter replaced) |
90
|
|
|
* |
91
|
|
|
* @param array $settings extra settings if any |
92
|
|
|
* @return string |
93
|
|
|
* @access public |
94
|
|
|
* @api |
95
|
|
|
*/ |
96
|
|
|
public function getSql(array $settings = [])/*# : string */ |
97
|
|
|
{ |
98
|
|
|
$settings['positionedParam'] = false; |
99
|
|
|
$settings['namedParam'] = false; |
100
|
|
|
return $this->getStatement($settings); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function getBindings()/*# : array */ |
107
|
|
|
{ |
108
|
|
|
return $this->bindings; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function __toString()/*# : string */ |
115
|
|
|
{ |
116
|
|
|
return $this->getSql(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Combine builder/statement and provided settings |
121
|
|
|
* |
122
|
|
|
* @param array $settings statement specific settings |
123
|
|
|
* @return array |
124
|
|
|
* @access protected |
125
|
|
|
*/ |
126
|
|
|
protected function combineSettings(array $settings)/*# : array */ |
127
|
|
|
{ |
128
|
|
|
return array_replace( |
129
|
|
|
$this->getBuilder()->getSettings(), // builder settings |
130
|
|
|
$this->getBuilder()->getDialect()->dialectSettings(), // dialect |
131
|
|
|
$settings, // user settings |
132
|
|
|
$this->getSettings() // statement settings |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Build SQL statement clauses |
138
|
|
|
* |
139
|
|
|
* @param array $settings |
140
|
|
|
* @param string |
141
|
|
|
* @access protected |
142
|
|
|
*/ |
143
|
|
|
protected function buildSql(array $settings)/*# : string */ |
144
|
|
|
{ |
145
|
|
|
$result = $this->getType(); // type |
146
|
|
|
$result .= $this->buildBeforeAfter('AFTER', 'TYPE', $settings); // hint |
147
|
|
|
$settings['join'] = $settings['seperator'] . $settings['indent']; |
148
|
|
|
|
149
|
|
|
foreach ($this->getConfigs() as $pos => $prefix) { |
150
|
|
|
// before |
151
|
|
|
$result .= $this->buildBeforeAfter('BEFORE', $pos, $settings); |
152
|
|
|
|
153
|
|
|
$method = 'build' . ucfirst(strtolower($pos)); |
154
|
|
|
$result .= $this->{$method}($prefix, $settings); |
155
|
|
|
|
156
|
|
|
// after |
157
|
|
|
$result .= $this->buildBeforeAfter('AFTER', $pos, $settings); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$result .= $this->buildBeforeAfter('AFTER', 'STMT', $settings); |
161
|
|
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Replace placeholders in the sql with '?', ':named' or real value |
166
|
|
|
* |
167
|
|
|
* @param string $sql |
168
|
|
|
* @param array $settings |
169
|
|
|
* @return string |
170
|
|
|
* @access protected |
171
|
|
|
*/ |
172
|
|
|
protected function bindValues(/*# string */ $sql, array $settings)/*# : string */ |
173
|
|
|
{ |
174
|
|
|
// init bindings |
175
|
|
|
$this->bindings = []; |
176
|
|
|
|
177
|
|
|
// bind values |
178
|
|
|
return $this->getBuilder()->getParameter() |
179
|
|
|
->bindValues($sql, $this->bindings, $settings); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Clause configs ['name' => 'prefix'] |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
* @access protected |
187
|
|
|
*/ |
188
|
|
|
abstract protected function getConfigs()/*# : array */; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get current statement type. e.g. 'SELECT' etc. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
* @access protected |
195
|
|
|
*/ |
196
|
|
|
abstract protected function getType()/*# : string */; |
197
|
|
|
} |
198
|
|
|
|