3d397c527545c88b5eed4c4f7dd9537a2f13f764
[yaffs-website] / web / core / lib / Drupal / Core / Database / Driver / mysql / Insert.php
1 <?php
2
3 namespace Drupal\Core\Database\Driver\mysql;
4
5 use Drupal\Core\Database\Query\Insert as QueryInsert;
6
7 /**
8  * MySQL implementation of \Drupal\Core\Database\Query\Insert.
9  */
10 class Insert extends QueryInsert {
11
12   public function execute() {
13     if (!$this->preExecute()) {
14       return NULL;
15     }
16
17     // If we're selecting from a SelectQuery, finish building the query and
18     // pass it back, as any remaining options are irrelevant.
19     if (empty($this->fromQuery)) {
20       $max_placeholder = 0;
21       $values = [];
22       foreach ($this->insertValues as $insert_values) {
23         foreach ($insert_values as $value) {
24           $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
25         }
26       }
27     }
28     else {
29       $values = $this->fromQuery->getArguments();
30     }
31
32     $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions);
33
34     // Re-initialize the values array so that we can re-use this query.
35     $this->insertValues = [];
36
37     return $last_insert_id;
38   }
39
40   public function __toString() {
41     // Create a sanitized comment string to prepend to the query.
42     $comments = $this->connection->makeComment($this->comments);
43
44     // Default fields are always placed first for consistency.
45     $insert_fields = array_merge($this->defaultFields, $this->insertFields);
46
47     $insert_fields = array_map(function ($field) {
48       return $this->connection->escapeField($field);
49     }, $insert_fields);
50
51     // If we're selecting from a SelectQuery, finish building the query and
52     // pass it back, as any remaining options are irrelevant.
53     if (!empty($this->fromQuery)) {
54       $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
55       return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
56     }
57
58     $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
59
60     $values = $this->getInsertPlaceholderFragment($this->insertValues, $this->defaultFields);
61     $query .= implode(', ', $values);
62
63     return $query;
64   }
65
66 }