query.inc 4.2 KB

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