query.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 (method_exists($this->connection, 'escapeFields')) {
  40. $insert_fields = $this->connection->escapeFields($insert_fields);
  41. }
  42. // If we're selecting from a SelectQuery, finish building the query and
  43. // pass it back, as any remaining options are irrelevant.
  44. if (!empty($this->fromQuery)) {
  45. $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
  46. return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
  47. }
  48. $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
  49. $max_placeholder = 0;
  50. $values = array();
  51. if (count($this->insertValues)) {
  52. foreach ($this->insertValues as $insert_values) {
  53. $placeholders = array();
  54. // Default fields aren't really placeholders, but this is the most convenient
  55. // way to handle them.
  56. $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
  57. $new_placeholder = $max_placeholder + count($insert_values);
  58. for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
  59. $placeholders[] = ':db_insert_placeholder_' . $i;
  60. }
  61. $max_placeholder = $new_placeholder;
  62. $values[] = '(' . implode(', ', $placeholders) . ')';
  63. }
  64. }
  65. else {
  66. // If there are no values, then this is a default-only query. We still need to handle that.
  67. $placeholders = array_fill(0, count($this->defaultFields), 'default');
  68. $values[] = '(' . implode(', ', $placeholders) . ')';
  69. }
  70. $query .= implode(', ', $values);
  71. return $query;
  72. }
  73. }
  74. class TruncateQuery_mysql extends TruncateQuery { }
  75. class UpdateQuery_mysql extends UpdateQuery {
  76. public function __toString() {
  77. if (method_exists($this->connection, 'escapeField')) {
  78. $escapedFields = array();
  79. foreach ($this->fields as $field => $data) {
  80. $field = $this->connection->escapeField($field);
  81. $escapedFields[$field] = $data;
  82. }
  83. $this->fields = $escapedFields;
  84. }
  85. return parent::__toString();
  86. }
  87. }
  88. /**
  89. * @} End of "addtogroup database".
  90. */