query.inc 4.3 KB

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