query.inc

  1. 7.x drupal-7.x/includes/database/query.inc
  2. 7.x drupal-7.x/includes/database/mysql/query.inc
  3. 7.x drupal-7.x/includes/database/pgsql/query.inc
  4. 7.x drupal-7.x/includes/database/sqlite/query.inc

Query code for MySQL embedded database engine.

File

drupal-7.x/includes/database/mysql/query.inc
View source
  1. <?php
  2. /**
  3. * @addtogroup database
  4. * @{
  5. */
  6. /**
  7. * @file
  8. * Query code for MySQL embedded database engine.
  9. */
  10. class InsertQuery_mysql extends InsertQuery {
  11. public function execute() {
  12. if (!$this->preExecute()) {
  13. return NULL;
  14. }
  15. // If we're selecting from a SelectQuery, finish building the query and
  16. // pass it back, as any remaining options are irrelevant.
  17. if (empty($this->fromQuery)) {
  18. $max_placeholder = 0;
  19. $values = array();
  20. foreach ($this->insertValues as $insert_values) {
  21. foreach ($insert_values as $value) {
  22. $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
  23. }
  24. }
  25. }
  26. else {
  27. $values = $this->fromQuery->getArguments();
  28. }
  29. $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions);
  30. // Re-initialize the values array so that we can re-use this query.
  31. $this->insertValues = array();
  32. return $last_insert_id;
  33. }
  34. public function __toString() {
  35. // Create a sanitized comment string to prepend to the query.
  36. $comments = $this->connection->makeComment($this->comments);
  37. // Default fields are always placed first for consistency.
  38. $insert_fields = array_merge($this->defaultFields, $this->insertFields);
  39. // If we're selecting from a SelectQuery, finish building the query and
  40. // pass it back, as any remaining options are irrelevant.
  41. if (!empty($this->fromQuery)) {
  42. $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
  43. return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
  44. }
  45. $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
  46. $max_placeholder = 0;
  47. $values = array();
  48. if (count($this->insertValues)) {
  49. foreach ($this->insertValues as $insert_values) {
  50. $placeholders = array();
  51. // Default fields aren't really placeholders, but this is the most convenient
  52. // way to handle them.
  53. $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
  54. $new_placeholder = $max_placeholder + count($insert_values);
  55. for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
  56. $placeholders[] = ':db_insert_placeholder_' . $i;
  57. }
  58. $max_placeholder = $new_placeholder;
  59. $values[] = '(' . implode(', ', $placeholders) . ')';
  60. }
  61. }
  62. else {
  63. // If there are no values, then this is a default-only query. We still need to handle that.
  64. $placeholders = array_fill(0, count($this->defaultFields), 'default');
  65. $values[] = '(' . implode(', ', $placeholders) . ')';
  66. }
  67. $query .= implode(', ', $values);
  68. return $query;
  69. }
  70. }
  71. class TruncateQuery_mysql extends TruncateQuery { }
  72. /**
  73. * @} End of "addtogroup database".
  74. */