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\SettingsTrait; |
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\SelectStatementInterface; |
28
|
|
|
use Phossa2\Query\Interfaces\Statement\InsertStatementInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Builder |
32
|
|
|
* |
33
|
|
|
* @package Phossa2\Query |
34
|
|
|
* @author Hong Zhang <[email protected]> |
35
|
|
|
* @see ObjectAbstract |
36
|
|
|
* @see BuilderInterface |
37
|
|
|
* @see StatementInterface |
38
|
|
|
* @see SelectStatementInterface |
39
|
|
|
* @see InsertStatementInterface |
40
|
|
|
* @version 2.0.0 |
41
|
|
|
* @since 2.0.0 added |
42
|
|
|
*/ |
43
|
|
|
class Builder extends ObjectAbstract implements BuilderInterface |
44
|
|
|
{ |
45
|
|
|
use DialectAwareTrait, SettingsTrait, ParameterAwareTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* tables |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
* @access protected |
52
|
|
|
*/ |
53
|
|
|
protected $tables = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor |
57
|
|
|
* |
58
|
|
|
* ```php |
59
|
|
|
* // builder with default table `users` and Mysql dialect |
60
|
|
|
* $users = new Builder('users', new Mysql()) |
61
|
|
|
* |
62
|
|
|
* // builder with defult tables: `users` and `accounts` AS `a` |
63
|
|
|
* $builder = new Builder(['users', 'accounts' => 'a']) |
64
|
|
|
* |
65
|
|
|
* // change default settings |
66
|
|
|
* $builder = new Builder('users', new Mysql(), ['autoQuote' => false]); |
67
|
|
|
* ``` |
68
|
|
|
* |
69
|
|
|
* @param string|array $table table[s] to build upon |
70
|
|
|
* @param DialectInterface $dialect default dialect is `Mysql` |
71
|
|
|
* @param array $settings builder settings |
72
|
|
|
* @access public |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
$table = '', |
76
|
|
|
DialectInterface $dialect = null, |
77
|
|
|
array $settings = [] |
78
|
|
|
) { |
79
|
|
|
$this |
80
|
|
|
->initParameter() |
81
|
|
|
->setSettings(array_replace($this->defaultSettings(), $settings)) |
82
|
|
|
->setDialect($dialect) |
83
|
|
|
->table($table); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Change table[s] |
88
|
|
|
* |
89
|
|
|
* @param $table change to table[s] |
90
|
|
|
* @return $this |
91
|
|
|
* @access public |
92
|
|
|
*/ |
93
|
|
|
public function __invoke($table) |
94
|
|
|
{ |
95
|
|
|
return $this->table($table); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function expr()/*# : ExpressionInterface */ |
102
|
|
|
{ |
103
|
|
|
return new Expression($this); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function raw(/*# string */ $string)/*# : OutputInterface */ |
110
|
|
|
{ |
111
|
|
|
// values found |
112
|
|
|
if (func_num_args() > 1) { |
113
|
|
|
$values = func_get_arg(1); |
114
|
|
|
$string = $this->getParameter() |
115
|
|
|
->replaceQuestionMark($string, $values); |
116
|
|
|
} |
117
|
|
|
return new Raw($string); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* If has existing tables, return a new instance with provided table[s] |
122
|
|
|
* |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function table($table, /*# string */ $alias = '') |
126
|
|
|
{ |
127
|
|
|
$tbl = $this->fixTable($table, $alias); |
128
|
|
|
$clone = [] === $this->tables ? $this : clone $this; |
129
|
|
|
$clone->tables = $tbl; |
130
|
|
|
return $clone; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Append to existing tables |
135
|
|
|
* |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
|
|
public function from($table, /*# string */ $alias = '') |
139
|
|
|
{ |
140
|
|
|
$tbl = $this->fixTable($table, $alias); |
141
|
|
|
$this->tables = array_merge($this->tables, $tbl); |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function select( |
149
|
|
|
$col = '', |
150
|
|
|
/*# string */ $alias = '' |
151
|
|
|
)/*# : SelectStatementInterface */ { |
152
|
|
|
/* @var SelectStatementInterface $select */ |
153
|
|
|
$select = $this->getDialect()->select($this); |
154
|
|
|
return $select->table($this->tables)->col($col, $alias); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function insert(array $values = [])/*# : InsertStatementInterface */ |
161
|
|
|
{ |
162
|
|
|
/* @var SelectStatementInterface $insert */ |
163
|
|
|
$insert = $this->getDialect()->insert($this); |
164
|
|
|
return $insert->into(current($this->tables))->set($values); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Convert to [$table => alias] or [$table] |
169
|
|
|
* |
170
|
|
|
* @param string|string[] $table |
171
|
|
|
* @param string $alias |
172
|
|
|
* @return array |
173
|
|
|
* @access protected |
174
|
|
|
*/ |
175
|
|
|
protected function fixTable( |
176
|
|
|
$table, |
177
|
|
|
/*# string */ $alias = '' |
178
|
|
|
)/*# : array */ { |
179
|
|
|
if (empty($table)) { |
180
|
|
|
$table = []; |
181
|
|
|
} elseif (!is_array($table)) { |
182
|
|
|
$table = empty($alias) ? [$table] : [$table => $alias]; |
183
|
|
|
} |
184
|
|
|
return $table; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Builder default settings |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
* @access protected |
192
|
|
|
*/ |
193
|
|
|
protected function defaultSettings()/*# : array */ |
194
|
|
|
{ |
195
|
|
|
return [ |
196
|
|
|
'autoQuote' => true, |
197
|
|
|
'positionedParam' => false, |
198
|
|
|
'namedParam' => false, |
199
|
|
|
'seperator' => ' ', |
200
|
|
|
'indent' => '', |
201
|
|
|
'escapeFunction' => null, |
202
|
|
|
'useNullAsDefault' => false, |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.