1 | <?php |
||
21 | class Insert extends AbstractDmlQuery implements InsertInterface |
||
22 | { |
||
23 | /** |
||
24 | * |
||
25 | * The table to insert into (quoted). |
||
26 | * |
||
27 | * @var string |
||
28 | * |
||
29 | */ |
||
30 | protected $into; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * The table to insert into (raw, for last-insert-id use). |
||
35 | * |
||
36 | * @var string |
||
37 | * |
||
38 | */ |
||
39 | protected $into_raw; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * A map of fully-qualified `table.column` names to last-insert-id names. |
||
44 | * This is used to look up the right last-insert-id name for a given table |
||
45 | * and column. Generally useful only for extended tables in Postgres. |
||
46 | * |
||
47 | * @var array |
||
48 | * |
||
49 | */ |
||
50 | protected $last_insert_id_names; |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * The current row-number we are adding column values for. This comes into |
||
55 | * play only with bulk inserts. |
||
56 | * |
||
57 | * @var int |
||
58 | * |
||
59 | */ |
||
60 | protected $row = 0; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * A collection of `$col_values` for previous rows in bulk inserts. |
||
65 | * |
||
66 | * @var array |
||
67 | * |
||
68 | */ |
||
69 | protected $col_values_bulk = array(); |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * A collection of `$bind_values` for previous rows in bulk inserts. |
||
74 | * |
||
75 | * @var array |
||
76 | * |
||
77 | */ |
||
78 | protected $bind_values_bulk = array(); |
||
79 | |||
80 | /** |
||
81 | * |
||
82 | * The order in which columns will be bulk-inserted; this is taken from the |
||
83 | * very first inserted row. |
||
84 | * |
||
85 | * @var array |
||
86 | * |
||
87 | */ |
||
88 | protected $col_order = array(); |
||
89 | |||
90 | /** |
||
91 | * |
||
92 | * Sets the map of fully-qualified `table.column` names to last-insert-id |
||
93 | * names. Generally useful only for extended tables in Postgres. |
||
94 | * |
||
95 | * @param array $last_insert_id_names The list of ID names. |
||
96 | * |
||
97 | */ |
||
98 | 79 | public function setLastInsertIdNames(array $last_insert_id_names) |
|
102 | |||
103 | /** |
||
104 | * |
||
105 | * Sets the table to insert into. |
||
106 | * |
||
107 | * @param string $into The table to insert into. |
||
108 | * |
||
109 | * @return $this |
||
110 | * |
||
111 | */ |
||
112 | 58 | public function into($into) |
|
118 | |||
119 | /** |
||
120 | * |
||
121 | * Builds this query object into a string. |
||
122 | * |
||
123 | * @return string |
||
124 | * |
||
125 | */ |
||
126 | 43 | protected function build() |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | * Returns the proper name for passing to `PDO::lastInsertId()`. |
||
145 | * |
||
146 | * @param string $col The last insert ID column. |
||
147 | * |
||
148 | * @return mixed Normally null, since most drivers do not need a name; |
||
149 | * alternatively, a string from `$last_insert_id_names`. |
||
150 | * |
||
151 | */ |
||
152 | 10 | public function getLastInsertIdName($col) |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | * Sets one column value placeholder; if an optional second parameter is |
||
163 | * passed, that value is bound to the placeholder. |
||
164 | * |
||
165 | * @param string $col The column name. |
||
166 | * |
||
167 | * @param array $value Optional: a value to bind to the placeholder. |
||
168 | * |
||
169 | * @return $this |
||
170 | * |
||
171 | */ |
||
172 | 20 | public function col($col, ...$value) |
|
176 | |||
177 | /** |
||
178 | * |
||
179 | * Sets multiple column value placeholders. If an element is a key-value |
||
180 | * pair, the key is treated as the column name and the value is bound to |
||
181 | * that column. |
||
182 | * |
||
183 | * @param array $cols A list of column names, optionally as key-value |
||
184 | * pairs where the key is a column name and the value is a bind value for |
||
185 | * that column. |
||
186 | * |
||
187 | * @return $this |
||
188 | * |
||
189 | */ |
||
190 | 48 | public function cols(array $cols) |
|
194 | |||
195 | /** |
||
196 | * |
||
197 | * Sets a column value directly; the value will not be escaped, although |
||
198 | * fully-qualified identifiers in the value will be quoted. |
||
199 | * |
||
200 | * @param string $col The column name. |
||
201 | * |
||
202 | * @param string $value The column value expression. |
||
203 | * |
||
204 | * @return $this |
||
205 | * |
||
206 | */ |
||
207 | 33 | public function set($col, $value) |
|
211 | |||
212 | /** |
||
213 | * |
||
214 | * Gets the values to bind to placeholders. |
||
215 | * |
||
216 | * @return array |
||
217 | * |
||
218 | */ |
||
219 | 31 | public function getBindValues() |
|
223 | |||
224 | /** |
||
225 | * |
||
226 | * Adds multiple rows for bulk insert. |
||
227 | * |
||
228 | * @param array $rows An array of rows, where each element is an array of |
||
229 | * column key-value pairs. The values are bound to placeholders. |
||
230 | * |
||
231 | * @return $this |
||
232 | * |
||
233 | */ |
||
234 | 15 | public function addRows(array $rows) |
|
244 | |||
245 | /** |
||
246 | * |
||
247 | * Add one row for bulk insert; increments the row counter and optionally |
||
248 | * adds columns to the new row. |
||
249 | * |
||
250 | * When adding the first row, the counter is not incremented. |
||
251 | * |
||
252 | * After calling `addRow()`, you can further call `col()`, `cols()`, and |
||
253 | * `set()` to work with the newly-added row. Calling `addRow()` again will |
||
254 | * finish off the current row and start a new one. |
||
255 | * |
||
256 | * @param array $cols An array of column key-value pairs; the values are |
||
257 | * bound to placeholders. |
||
258 | * |
||
259 | * @return $this |
||
260 | * |
||
261 | */ |
||
262 | 30 | public function addRow(array $cols = array()) |
|
277 | |||
278 | /** |
||
279 | * |
||
280 | * Adds IGNORE flag depending on DB syntax. |
||
281 | * |
||
282 | * @param bool $enable Set or unset flag (default true). |
||
283 | * @throws Exception |
||
284 | * @return \Aura\SqlQuery\Sqlite\Insert |
||
285 | * |
||
286 | */ |
||
287 | 1 | public function ignore($enable = true) |
|
292 | |||
293 | /** |
||
294 | * |
||
295 | * Finishes off the current row in a bulk insert, collecting the bulk |
||
296 | * values and resetting for the next row. |
||
297 | * |
||
298 | * @return null |
||
299 | * |
||
300 | */ |
||
301 | 25 | protected function finishRow() |
|
314 | |||
315 | /** |
||
316 | * |
||
317 | * Finishes off a single column of the current row in a bulk insert. |
||
318 | * |
||
319 | * @param string $col The column to finish off. |
||
320 | * |
||
321 | * @return null |
||
322 | * |
||
323 | * @throws Exception on named column missing from row. |
||
324 | * |
||
325 | */ |
||
326 | 25 | protected function finishCol($col) |
|
353 | } |
||
354 |