Skip navigation
Help

query.inc

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

Query code for MySQL embedded database engine.

Classes

NameDescription
InsertQuery_mysql
TruncateQuery_mysql

File

drupal/includes/database/mysql/query.inc
View source
  1. <?php
  2. /**
  3. * @ingroup 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. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
  43. }
  44. $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
  45. $max_placeholder = 0;
  46. $values = array();
  47. if (count($this->insertValues)) {
  48. foreach ($this->insertValues as $insert_values) {
  49. $placeholders = array();
  50. // Default fields aren't really placeholders, but this is the most convenient
  51. // way to handle them.
  52. $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
  53. $new_placeholder = $max_placeholder + count($insert_values);
  54. for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
  55. $placeholders[] = ':db_insert_placeholder_' . $i;
  56. }
  57. $max_placeholder = $new_placeholder;
  58. $values[] = '(' . implode(', ', $placeholders) . ')';
  59. }
  60. }
  61. else {
  62. // If there are no values, then this is a default-only query. We still need to handle that.
  63. $placeholders = array_fill(0, count($this->defaultFields), 'default');
  64. $values[] = '(' . implode(', ', $placeholders) . ')';
  65. }
  66. $query .= implode(', ', $values);
  67. return $query;
  68. }
  69. }
  70. class TruncateQuery_mysql extends TruncateQuery {
  71. public function __toString() {
  72. // TRUNCATE is actually a DDL statement on MySQL, and DDL statements are
  73. // not transactional, and result in an implicit COMMIT. When we are in a
  74. // transaction, fallback to the slower, but transactional, DELETE.
  75. if ($this->connection->inTransaction()) {
  76. // Create a comment string to prepend to the query.
  77. $comments = $this->connection->makeComment($this->comments);
  78. return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '}';
  79. }
  80. else {
  81. return parent::__toString();
  82. }
  83. }
  84. }
  85. /**
  86. * @} End of "ingroup database".
  87. */