query.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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 "addtogroup database".
  87. */