Insert.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Drupal\Core\Database\Driver\sqlite;
  3. use Drupal\Core\Database\Query\Insert as QueryInsert;
  4. /**
  5. * SQLite implementation of \Drupal\Core\Database\Query\Insert.
  6. *
  7. * We ignore all the default fields and use the clever SQLite syntax:
  8. * INSERT INTO table DEFAULT VALUES
  9. * for degenerated "default only" queries.
  10. */
  11. class Insert extends QueryInsert {
  12. public function execute() {
  13. if (!$this->preExecute()) {
  14. return NULL;
  15. }
  16. if (count($this->insertFields) || !empty($this->fromQuery)) {
  17. return parent::execute();
  18. }
  19. else {
  20. return $this->connection->query('INSERT INTO {' . $this->table . '} DEFAULT VALUES', [], $this->queryOptions);
  21. }
  22. }
  23. public function __toString() {
  24. // Create a sanitized comment string to prepend to the query.
  25. $comments = $this->connection->makeComment($this->comments);
  26. // Produce as many generic placeholders as necessary.
  27. $placeholders = [];
  28. if (!empty($this->insertFields)) {
  29. $placeholders = array_fill(0, count($this->insertFields), '?');
  30. }
  31. // If we're selecting from a SelectQuery, finish building the query and
  32. // pass it back, as any remaining options are irrelevant.
  33. if (!empty($this->fromQuery)) {
  34. $insert_fields_string = $this->insertFields ? ' (' . implode(', ', $this->insertFields) . ') ' : ' ';
  35. return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
  36. }
  37. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
  38. }
  39. }