|
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; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Query\Misc\Raw; |
|
18
|
|
|
use Phossa2\Query\Dialect\Mysql; |
|
19
|
|
|
use Phossa2\Query\Misc\Expression; |
|
20
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
|
21
|
|
|
use Phossa2\Query\Traits\SettingsAwareTrait; |
|
22
|
|
|
use Phossa2\Query\Traits\DialectAwareTrait; |
|
23
|
|
|
use Phossa2\Query\Traits\ParameterAwareTrait; |
|
24
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
|
25
|
|
|
use Phossa2\Query\Interfaces\DialectInterface; |
|
26
|
|
|
use Phossa2\Query\Interfaces\StatementInterface; |
|
27
|
|
|
use Phossa2\Query\Interfaces\Statement\UnionStatementInterface; |
|
28
|
|
|
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface; |
|
29
|
|
|
use Phossa2\Query\Interfaces\Statement\InsertStatementInterface; |
|
30
|
|
|
use Phossa2\Query\Interfaces\Statement\UpdateStatementInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Builder |
|
34
|
|
|
* |
|
35
|
|
|
* @package Phossa2\Query |
|
36
|
|
|
* @author Hong Zhang <[email protected]> |
|
37
|
|
|
* @see ObjectAbstract |
|
38
|
|
|
* @see BuilderInterface |
|
39
|
|
|
* @see StatementInterface |
|
40
|
|
|
* @see SelectStatementInterface |
|
41
|
|
|
* @see InsertStatementInterface |
|
42
|
|
|
* @see UnionStatementInterface |
|
43
|
|
|
* @see UpdateStatementInterface |
|
44
|
|
|
* @version 2.0.0 |
|
45
|
|
|
* @since 2.0.0 added |
|
46
|
|
|
*/ |
|
47
|
|
|
class Builder extends ObjectAbstract implements BuilderInterface |
|
48
|
|
|
{ |
|
49
|
|
|
use DialectAwareTrait, SettingsAwareTrait, ParameterAwareTrait; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* tables |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
* @access protected |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $tables = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor |
|
61
|
|
|
* |
|
62
|
|
|
* ```php |
|
63
|
|
|
* // builder with default table `users` and Mysql dialect |
|
64
|
|
|
* $users = new Builder('users', new Mysql()) |
|
65
|
|
|
* |
|
66
|
|
|
* // builder with defult tables: `users` and `accounts` AS `a` |
|
67
|
|
|
* $builder = new Builder(['users', 'accounts' => 'a']) |
|
68
|
|
|
* |
|
69
|
|
|
* // change default settings |
|
70
|
|
|
* $builder = new Builder('users', new Mysql(), ['autoQuote' => false]); |
|
71
|
|
|
* ``` |
|
72
|
|
|
* |
|
73
|
|
|
* @param string|array $table table[s] to build upon |
|
74
|
|
|
* @param DialectInterface $dialect default dialect is `Mysql` |
|
75
|
|
|
* @param array $settings builder settings |
|
76
|
|
|
* @access public |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct( |
|
79
|
|
|
$table = '', |
|
80
|
|
|
DialectInterface $dialect = null, |
|
81
|
|
|
array $settings = [] |
|
82
|
|
|
) { |
|
83
|
|
|
$this |
|
84
|
|
|
->initParameter() |
|
85
|
|
|
->setSettings(array_replace($this->defaultSettings(), $settings)) |
|
86
|
|
|
->setDialect($dialect) |
|
87
|
|
|
->table($table); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Change table[s] |
|
92
|
|
|
* |
|
93
|
|
|
* @param $table change to table[s] |
|
94
|
|
|
* @return $this |
|
95
|
|
|
* @access public |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __invoke($table) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->table($table); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function expr()/*# : ExpressionInterface */ |
|
106
|
|
|
{ |
|
107
|
|
|
return new Expression($this); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function raw(/*# string */ $string)/*# : OutputInterface */ |
|
114
|
|
|
{ |
|
115
|
|
|
// values found |
|
116
|
|
|
if (func_num_args() > 1) { |
|
117
|
|
|
$values = func_get_arg(1); |
|
118
|
|
|
$string = $this->getParameter() |
|
119
|
|
|
->replaceQuestionMark($string, $values); |
|
120
|
|
|
} |
|
121
|
|
|
return new Raw($string); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* If has existing tables, return a new instance with provided table[s] |
|
126
|
|
|
* |
|
127
|
|
|
* {@inheritDoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function table($table, /*# string */ $alias = '') |
|
130
|
|
|
{ |
|
131
|
|
|
$tbl = $this->fixTable($table, $alias); |
|
132
|
|
|
$clone = [] === $this->tables ? $this : clone $this; |
|
133
|
|
|
$clone->tables = $tbl; |
|
134
|
|
|
return $clone; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Append to existing tables |
|
139
|
|
|
* |
|
140
|
|
|
* {@inheritDoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function from($table, /*# string */ $alias = '') |
|
143
|
|
|
{ |
|
144
|
|
|
$tbl = $this->fixTable($table, $alias); |
|
145
|
|
|
$this->tables = array_merge($this->tables, $tbl); |
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritDoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function select()/*# : SelectStatementInterface */ |
|
153
|
|
|
{ |
|
154
|
|
|
/* @var SelectStatementInterface $select */ |
|
155
|
|
|
$select = $this->getDialect()->select($this); |
|
156
|
|
|
return $select->table($this->tables)->col(func_get_args()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritDoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function insert(array $values = [])/*# : InsertStatementInterface */ |
|
163
|
|
|
{ |
|
164
|
|
|
/* @var InsertStatementInterface $insert */ |
|
165
|
|
|
$insert = $this->getDialect()->insert($this); |
|
166
|
|
|
return $insert->into(current($this->tables))->set($values); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritDoc} |
|
171
|
|
|
*/ |
|
172
|
|
|
public function update(array $values = [])/*# : UpdateStatementInterface */ |
|
173
|
|
|
{ |
|
174
|
|
|
/* @var UpdateStatementInterface $update */ |
|
175
|
|
|
$update = $this->getDialect()->update($this); |
|
176
|
|
|
return $update->from(current($this->tables))->set($values); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritDoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function union()/*# : UnionStatementInterface */ |
|
183
|
|
|
{ |
|
184
|
|
|
/* @var UnionStatementInterface $union */ |
|
185
|
|
|
$union = $this->getDialect()->union($this); |
|
186
|
|
|
if (func_num_args() > 0) { // acception variable parameters |
|
187
|
|
|
$args = func_get_args(); |
|
188
|
|
|
foreach ($args as $arg) { |
|
189
|
|
|
$union->union($arg); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
return $union; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Convert to [$table => alias] or [$table] |
|
197
|
|
|
* |
|
198
|
|
|
* @param string|string[] $table |
|
199
|
|
|
* @param string $alias |
|
200
|
|
|
* @return array |
|
201
|
|
|
* @access protected |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function fixTable( |
|
204
|
|
|
$table, |
|
205
|
|
|
/*# string */ $alias = '' |
|
206
|
|
|
)/*# : array */ { |
|
207
|
|
|
if (empty($table)) { |
|
208
|
|
|
$table = []; |
|
209
|
|
|
} elseif (!is_array($table)) { |
|
210
|
|
|
$table = empty($alias) ? [$table] : [$table => $alias]; |
|
211
|
|
|
} |
|
212
|
|
|
return $table; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Builder default settings |
|
217
|
|
|
* |
|
218
|
|
|
* @return array |
|
219
|
|
|
* @access protected |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function defaultSettings()/*# : array */ |
|
222
|
|
|
{ |
|
223
|
|
|
return [ |
|
224
|
|
|
'autoQuote' => true, |
|
225
|
|
|
'positionedParam' => false, |
|
226
|
|
|
'namedParam' => false, |
|
227
|
|
|
'seperator' => ' ', |
|
228
|
|
|
'indent' => '', |
|
229
|
|
|
'escapeFunction' => null, |
|
230
|
|
|
'useNullAsDefault' => false, |
|
231
|
|
|
]; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|