query.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * Query code for SQLite embedded database engine.
  5. */
  6. /**
  7. * @addtogroup database
  8. * @{
  9. */
  10. /**
  11. * SQLite specific implementation of InsertQuery.
  12. *
  13. * We ignore all the default fields and use the clever SQLite syntax:
  14. * INSERT INTO table DEFAULT VALUES
  15. * for degenerated "default only" queries.
  16. */
  17. class InsertQuery_sqlite extends InsertQuery {
  18. public function execute() {
  19. if (!$this->preExecute()) {
  20. return NULL;
  21. }
  22. if (count($this->insertFields)) {
  23. return parent::execute();
  24. }
  25. else {
  26. return $this->connection->query('INSERT INTO {' . $this->table . '} DEFAULT VALUES', array(), $this->queryOptions);
  27. }
  28. }
  29. public function __toString() {
  30. // Create a sanitized comment string to prepend to the query.
  31. $comments = $this->connection->makeComment($this->comments);
  32. // Produce as many generic placeholders as necessary.
  33. $placeholders = array_fill(0, count($this->insertFields), '?');
  34. // If we're selecting from a SelectQuery, finish building the query and
  35. // pass it back, as any remaining options are irrelevant.
  36. if (!empty($this->fromQuery)) {
  37. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') ' . $this->fromQuery;
  38. }
  39. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
  40. }
  41. }
  42. /**
  43. * SQLite specific implementation of UpdateQuery.
  44. *
  45. * SQLite counts all the rows that match the conditions as modified, even if they
  46. * will not be affected by the query. We workaround this by ensuring that
  47. * we don't select those rows.
  48. *
  49. * A query like this one:
  50. * UPDATE test SET name = 'newname' WHERE tid = 1
  51. * will become:
  52. * UPDATE test SET name = 'newname' WHERE tid = 1 AND name <> 'newname'
  53. */
  54. class UpdateQuery_sqlite extends UpdateQuery {
  55. /**
  56. * Helper function that removes the fields that are already in a condition.
  57. *
  58. * @param $fields
  59. * The fields.
  60. * @param QueryConditionInterface $condition
  61. * A database condition.
  62. */
  63. protected function removeFieldsInCondition(&$fields, QueryConditionInterface $condition) {
  64. foreach ($condition->conditions() as $child_condition) {
  65. if ($child_condition['field'] instanceof QueryConditionInterface) {
  66. $this->removeFieldsInCondition($fields, $child_condition['field']);
  67. }
  68. else {
  69. unset($fields[$child_condition['field']]);
  70. }
  71. }
  72. }
  73. public function execute() {
  74. if (!empty($this->queryOptions['sqlite_return_matched_rows'])) {
  75. return parent::execute();
  76. }
  77. // Get the fields used in the update query, and remove those that are already
  78. // in the condition.
  79. $fields = $this->expressionFields + $this->fields;
  80. $this->removeFieldsInCondition($fields, $this->condition);
  81. // Add the inverse of the fields to the condition.
  82. $condition = new DatabaseCondition('OR');
  83. foreach ($fields as $field => $data) {
  84. if (is_array($data)) {
  85. // The field is an expression.
  86. $condition->where($field . ' <> ' . $data['expression']);
  87. $condition->isNull($field);
  88. }
  89. elseif (!isset($data)) {
  90. // The field will be set to NULL.
  91. $condition->isNotNull($field);
  92. }
  93. else {
  94. $condition->condition($field, $data, '<>');
  95. $condition->isNull($field);
  96. }
  97. }
  98. if (count($condition)) {
  99. $condition->compile($this->connection, $this);
  100. $this->condition->where((string) $condition, $condition->arguments());
  101. }
  102. return parent::execute();
  103. }
  104. }
  105. /**
  106. * SQLite specific implementation of DeleteQuery.
  107. *
  108. * When the WHERE is omitted from a DELETE statement and the table being deleted
  109. * has no triggers, SQLite uses an optimization to erase the entire table content
  110. * without having to visit each row of the table individually.
  111. *
  112. * Prior to SQLite 3.6.5, SQLite does not return the actual number of rows deleted
  113. * by that optimized "truncate" optimization.
  114. */
  115. class DeleteQuery_sqlite extends DeleteQuery {
  116. public function execute() {
  117. if (!count($this->condition)) {
  118. $total_rows = $this->connection->query('SELECT COUNT(*) FROM {' . $this->connection->escapeTable($this->table) . '}')->fetchField();
  119. parent::execute();
  120. return $total_rows;
  121. }
  122. else {
  123. return parent::execute();
  124. }
  125. }
  126. }
  127. /**
  128. * SQLite specific implementation of TruncateQuery.
  129. *
  130. * SQLite doesn't support TRUNCATE, but a DELETE query with no condition has
  131. * exactly the effect (it is implemented by DROPing the table).
  132. */
  133. class TruncateQuery_sqlite extends TruncateQuery {
  134. public function __toString() {
  135. // Create a sanitized comment string to prepend to the query.
  136. $comments = $this->connection->makeComment($this->comments);
  137. return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
  138. }
  139. }
  140. /**
  141. * @} End of "addtogroup database".
  142. */