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\Clause; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\ClauseInterface; |
18
|
|
|
use Phossa2\Query\Interfaces\Clause\SetInterface; |
19
|
|
|
use Phossa2\Query\Misc\Template; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SetTrait |
23
|
|
|
* |
24
|
|
|
* Implementation of SetInterface |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Query |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see SetInterface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
trait SetTrait |
33
|
|
|
{ |
34
|
|
|
use AbstractTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* data storage |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $set_data = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* data row number |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $set_row = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* storage for col names |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected $set_col = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function set($col, $value = ClauseInterface::NO_VALUE) |
64
|
|
|
{ |
65
|
|
|
if (empty($col)) { // nothing |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
if (is_array($col)) { // array provided |
69
|
|
|
return $this->setWithArrayData($col); |
70
|
|
|
} |
71
|
|
|
if (!isset($this->set_col[$col])) { // save col names |
72
|
|
|
$this->set_col[$col] = true; |
73
|
|
|
} |
74
|
|
|
$this->set_data[$this->set_row][$col] = $value; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function setRaw(/*# string */ $col, $value = ClauseInterface::NO_VALUE) |
82
|
|
|
{ |
83
|
|
|
if (ClauseInterface::NO_VALUE !== $value) { |
84
|
|
|
$value = $this->positionedParam($value, func_get_args(), 2); |
85
|
|
|
} |
86
|
|
|
return $this->set((string) $col, $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
public function setTpl(/*# string */ $col, /*# string */ $template, $field) |
93
|
|
|
{ |
94
|
|
|
$template = $this->positionedParam($template, func_get_args(), 3); |
95
|
|
|
return $this->set($col, new Template($template, $field)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Batch SET |
100
|
|
|
* |
101
|
|
|
* @param array $data |
102
|
|
|
* @return $this |
103
|
|
|
* @access protected |
104
|
|
|
*/ |
105
|
|
|
protected function setWithArrayData(array $data) |
106
|
|
|
{ |
107
|
|
|
if (isset($data[0])) { // multiple rows |
108
|
|
|
foreach ($data as $row) { |
109
|
|
|
$this->set($row); |
110
|
|
|
} |
111
|
|
|
} else { // multiple values |
112
|
|
|
foreach ($data as $col => $val) { |
113
|
|
|
$this->set($col, $val); |
114
|
|
|
} |
115
|
|
|
$this->set_row++; |
116
|
|
|
} |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Build SET |
122
|
|
|
* |
123
|
|
|
* @param string $prefix |
124
|
|
|
* @param array $settings |
125
|
|
|
* @return string |
126
|
|
|
* @access protected |
127
|
|
|
*/ |
128
|
|
|
protected function buildSet( |
129
|
|
|
/*# string */ $prefix, |
130
|
|
|
array $settings |
131
|
|
|
)/*# : array */ { |
132
|
|
|
if ('UPDATE' === $this->getType()) { |
133
|
|
|
return $this->buildUpdateSet($prefix, $settings); |
134
|
|
|
} else { |
135
|
|
|
return $this->buildInsertSet($prefix, $settings); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Build SET for INSERT |
141
|
|
|
* |
142
|
|
|
* @param string $prefix |
143
|
|
|
* @param array $settings |
144
|
|
|
* @return string |
145
|
|
|
* @access protected |
146
|
|
|
*/ |
147
|
|
|
protected function buildInsertSet( |
148
|
|
|
/*# string */ $prefix, |
149
|
|
|
array $settings |
150
|
|
|
)/*# : string */ { |
151
|
|
|
if (empty($this->set_data)) { |
152
|
|
|
return ''; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$cols = []; |
156
|
|
|
foreach (array_keys($this->set_col) as $col) { |
157
|
|
|
$cols[] = $this->quote($col, $settings); |
158
|
|
|
} |
159
|
|
|
return $settings['seperator'] . '(' . join(', ', $cols) . ')'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Build SET ... = ..., ... = ... |
164
|
|
|
* |
165
|
|
|
* |
166
|
|
|
* @param string $prefix |
167
|
|
|
* @param array $settings |
168
|
|
|
* @return string |
169
|
|
|
* @access protected |
170
|
|
|
*/ |
171
|
|
|
protected function buildUpdateSet( |
172
|
|
|
/*# string */ $prefix, |
173
|
|
|
array $settings |
174
|
|
|
)/*# : string */ { |
175
|
|
|
$result = []; |
176
|
|
|
foreach ($this->set_data[0] as $col => $val) { |
177
|
|
|
if (ClauseInterface::NO_VALUE === $val) { |
178
|
|
|
$result[] = $col; |
179
|
|
|
} else { |
180
|
|
|
$result[] = $this->quote($col, $settings) . ' = ' . |
181
|
|
|
$this->processValue($val, $settings); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
return $this->joinClause($prefix, ',', $result, $settings); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Build VALUES ( ... ) |
189
|
|
|
* |
190
|
|
|
* @param string $prefix |
191
|
|
|
* @param array $settings |
192
|
|
|
* @return string |
193
|
|
|
* @access protected |
194
|
|
|
*/ |
195
|
|
|
protected function buildValues( |
196
|
|
|
/*# string */ $prefix, |
197
|
|
|
array $settings |
198
|
|
|
)/*# : string */ { |
199
|
|
|
$rows = []; |
200
|
|
|
$cols = array_keys($this->set_col); |
201
|
|
|
foreach ($this->set_data as $num => $row) { |
202
|
|
|
$values = []; |
203
|
|
|
foreach ($cols as $col) { |
204
|
|
|
$values[] = isset($row[$col]) ? |
205
|
|
|
$this->processValue($row[$col], $settings) : |
206
|
|
|
$this->nullOrDefault($settings); |
207
|
|
|
} |
208
|
|
|
$rows[] = '(' . join(', ', $values) . ')'; |
209
|
|
|
} |
210
|
|
|
return $this->joinClause($prefix, ',', $rows, $settings); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get NULL or DEFAULT for values base on the settings |
215
|
|
|
* |
216
|
|
|
* @param array $settings |
217
|
|
|
* @return string |
218
|
|
|
* @access protected |
219
|
|
|
*/ |
220
|
|
|
protected function nullOrDefault(array $settings)/*# : string */ |
221
|
|
|
{ |
222
|
|
|
return $settings['useNullAsDefault'] ? 'NULL' : 'DEFAULT'; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|