query.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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) || !empty($this->fromQuery)) {
  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();
  34. if (!empty($this->insertFields)) {
  35. $placeholders = array_fill(0, count($this->insertFields), '?');
  36. }
  37. // If we're selecting from a SelectQuery, finish building the query and
  38. // pass it back, as any remaining options are irrelevant.
  39. if (!empty($this->fromQuery)) {
  40. $insert_fields_string = $this->insertFields ? ' (' . implode(', ', $this->insertFields) . ') ' : ' ';
  41. return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
  42. }
  43. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
  44. }
  45. }
  46. /**
  47. * SQLite specific implementation of UpdateQuery.
  48. *
  49. * SQLite counts all the rows that match the conditions as modified, even if they
  50. * will not be affected by the query. We workaround this by ensuring that
  51. * we don't select those rows.
  52. *
  53. * A query like this one:
  54. * UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1
  55. * will become:
  56. * UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 AND (col1 <> 'newcol1' OR col2 <> 'newcol2')
  57. */
  58. class UpdateQuery_sqlite extends UpdateQuery {
  59. public function execute() {
  60. if (!empty($this->queryOptions['sqlite_return_matched_rows'])) {
  61. return parent::execute();
  62. }
  63. // Get the fields used in the update query.
  64. $fields = $this->expressionFields + $this->fields;
  65. // Add the inverse of the fields to the condition.
  66. $condition = new DatabaseCondition('OR');
  67. foreach ($fields as $field => $data) {
  68. if (is_array($data)) {
  69. // The field is an expression.
  70. $condition->where($field . ' <> ' . $data['expression']);
  71. $condition->isNull($field);
  72. }
  73. elseif (!isset($data)) {
  74. // The field will be set to NULL.
  75. $condition->isNotNull($field);
  76. }
  77. else {
  78. $condition->condition($field, $data, '<>');
  79. $condition->isNull($field);
  80. }
  81. }
  82. if (count($condition)) {
  83. $condition->compile($this->connection, $this);
  84. $this->condition->where((string) $condition, $condition->arguments());
  85. }
  86. return parent::execute();
  87. }
  88. }
  89. /**
  90. * SQLite specific implementation of DeleteQuery.
  91. */
  92. class DeleteQuery_sqlite extends DeleteQuery {
  93. public function execute() {
  94. // When the WHERE is omitted from a DELETE statement and the table being
  95. // deleted has no triggers, SQLite uses an optimization to erase the entire
  96. // table content without having to visit each row of the table individually.
  97. // Prior to SQLite 3.6.5, SQLite does not return the actual number of rows
  98. // deleted by that optimized "truncate" optimization. But we want to return
  99. // the number of rows affected, so we calculate it directly.
  100. if (!count($this->condition)) {
  101. $total_rows = $this->connection->query('SELECT COUNT(*) FROM {' . $this->connection->escapeTable($this->table) . '}')->fetchField();
  102. parent::execute();
  103. return $total_rows;
  104. }
  105. else {
  106. return parent::execute();
  107. }
  108. }
  109. }
  110. /**
  111. * SQLite specific implementation of TruncateQuery.
  112. *
  113. * SQLite doesn't support TRUNCATE, but a DELETE query with no condition has
  114. * exactly the effect (it is implemented by DROPing the table).
  115. */
  116. class TruncateQuery_sqlite extends TruncateQuery {
  117. public function __toString() {
  118. // Create a sanitized comment string to prepend to the query.
  119. $comments = $this->connection->makeComment($this->comments);
  120. return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
  121. }
  122. }
  123. /**
  124. * @} End of "addtogroup database".
  125. */