/
SQLComposerInsert.class.php
266 lines (222 loc) · 5.79 KB
/
SQLComposerInsert.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
require_once 'SQLComposer.class.php';
/**
* SQLComposerInsert
*
* An INSERT query
*
* @package SQLComposer
* @author Shane Smith
*/
class SQLComposerInsert extends SQLComposerBase {
/**
* IGNORE
*
* @var bool
*/
protected $ignore = false;
/**
* To create an INSERT INTO ... SELECT ...
*
* @var SQLComposerSelect
*/
protected $select;
/**
* ON DUPLICATE KEY UPDATE
*
* @var array
*/
protected $on_duplicate = array( );
/*******************
** CONSTRUCTOR **
*******************/
/**
* Constructor.
*
* @param string $table
*/
public function __construct($table = null) {
if (isset($table)) $this->into($table);
}
/***************
** METHODS **
***************/
/**
* INSERT INTO
*
* @param string $table
* @return SQLComposerInsert
*/
public function insert_into($table) {
return $this->into($table);
}
/**
* INSERT INTO
*
* @param string $table
* @return SQLComposerInsert
*/
public function into($table) {
$this->add_table($table);
return $this;
}
/**
* IGNORE
*
* @param bool $ignore
* @return SQLComposerInsert
*/
public function ignore($ignore = true) {
$this->ignore = $ignore;
return $this;
}
/**
* Set the columns for INSERT INTO table (col1, col2, ...)
*
* If no columns are specified by rendering time and the first set of values
* is an associative array, the array's keys will become the column names.
*
* @param string|array $column
* @return SQLComposerInsert
*/
public function columns($column) {
$this->columns = array_merge($this->columns, (array)$column);
return $this;
}
/**
* Provide a set of values to be inserted.
*
* If no columns are specified by rendering time and the first set of values
* is an associative array, the array's keys will become the column names.
*
* ex:
* SQLComposer::insert_into('table')->values(array( 'id' => '25', 'name' => 'joe', 'fav_color' => 'green' ));
*
* will result in
*
* INSERT INTO table (id, name, fav_color) VALUES (25, 'joe', 'green')
*
* @param array $values
* @param string $mysqli_types
* @return SQLComposerInsert
*/
public function values(array $values, $mysqli_types = "") {
if (isset($this->select)) throw new SQLComposerException("Cannot use 'INSERT INTO ... VALUES' when a SELECT is already set!");
return $this->_add_params('values', array( $values ), $mysqli_types);
}
/**
* Return a SQLComposerSelect object to be used in a query of the type INSERT INTO ... SELECT ...
*
* @param string|array $select
* @param array $params
* @param string $mysqli_types
* @return SQLComposerSelect
*/
public function select($select = null, array $params = null, $mysqli_types = "") {
if (isset($this->params['values'])) throw new SQLComposerException("Cannot use 'INSERT INTO ... SELECT' when values are already set!");
if (!isset($this->select)) {
$this->select = SQLComposer::select();
}
if (isset($select)) {
$this->select->select($select, $params, $mysqli_types);
}
return $this->select;
}
/**
* ON DUPLICATE KEY UPDATE
*
* @param string|array $update
* @param array $params
* @param string $mysqli_types
* @return SQLComposerInsert
*/
public function on_duplicate($update, array $params = null, $mysqli_types = "") {
$this->on_duplicate = array_merge($this->on_duplicate, (array)$update);
$this->_add_params('on_duplicate', $params, $mysqli_types);
return $this;
}
/*****************
** RENDERING **
*****************/
/**
* Render the INSERT query
*
* @return string
*/
public function render() {
$table = $this->tables[0];
$ignore = $this->ignore ? "IGNORE" : "";
$columns = $this->_get_columns();
$columns = empty($columns) ? "" : "(" . implode(", ", $columns) . ")";
if (isset($this->select)) {
$values = "\n" . $this->select->render();
} else {
$placeholders = "(" . implode(", ", array_fill(0, $this->_num_columns(), "?")) . ")";
$num_values = count($this->params['values']);
$values = "\nVALUES " . implode(", ", array_fill(0, $num_values, $placeholders));
}
$on_duplicate = (empty($this->on_duplicate)) ? "" : "\nON DUPLICATE KEY UPDATE " . implode(", ", $this->on_duplicate);
return "INSERT {$ignore} INTO {$table} {$columns} {$values} {$on_duplicate}";
}
/**
* Get the parameters array
*
* @return array
*/
public function getParams() {
if (isset($this->select)) {
$params = $this->select->getParams();
} else {
$params = array( );
$columns = $this->_get_columns();
$num_cols = $this->_num_columns();
foreach ($this->params["values"] as $values) {
if (SQLComposer::is_assoc($values)) {
foreach ($columns as $col) $params[] = $values[$col];
} else {
$params = array_merge($params, array_slice($values, 0, $num_cols));
}
}
}
$params = array_merge($params, (array)$this->params['on_duplicate']);
if (!empty($this->mysqli_types)) {
if (isset($this->select)) {
$params[0] .= $this->mysqli_types['on_duplicate'];
} else {
$types = $this->mysqli_types['values'] . $this->mysqli_types['on_duplicate'];
array_unshift($params, $types);
}
}
return $params;
}
/**
* Get the currently set columns,
* or, if none set, the keys of the first values array if it is associative
*
* @return array
*/
protected function _get_columns() {
if (!empty($this->columns)) {
return $this->columns;
}
elseif (SQLComposer::is_assoc($this->params['values'][0])) {
return array_keys($this->params['values'][0]);
}
else {
return array( );
}
}
/**
* Get the number of defined columns,
* or, if none defined, the number of the first values array
*
* @return int
*/
protected function _num_columns() {
if (!empty($this->columns)) {
return count($this->columns);
} else {
return count($this->params['values'][0]);
}
}
}