Projet

Général

Profil

Paste
Télécharger (4,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / includes / database / sqlite / query.inc @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @file
5
 * Query code for SQLite embedded database engine.
6
 */
7

    
8
/**
9
 * @addtogroup database
10
 * @{
11
 */
12

    
13
/**
14
 * SQLite specific implementation of InsertQuery.
15
 *
16
 * We ignore all the default fields and use the clever SQLite syntax:
17
 *   INSERT INTO table DEFAULT VALUES
18
 * for degenerated "default only" queries.
19
 */
20
class InsertQuery_sqlite extends InsertQuery {
21

    
22
  public function execute() {
23
    if (!$this->preExecute()) {
24
      return NULL;
25
    }
26
    if (count($this->insertFields) || !empty($this->fromQuery)) {
27
      return parent::execute();
28
    }
29
    else {
30
      return $this->connection->query('INSERT INTO {' . $this->table . '} DEFAULT VALUES', array(), $this->queryOptions);
31
    }
32
  }
33

    
34
  public function __toString() {
35
    // Create a sanitized comment string to prepend to the query.
36
    $comments = $this->connection->makeComment($this->comments);
37

    
38
    // Produce as many generic placeholders as necessary.
39
    $placeholders = array();
40
    if (!empty($this->insertFields)) {
41
      $placeholders = array_fill(0, count($this->insertFields), '?');
42
    }
43

    
44
    // If we're selecting from a SelectQuery, finish building the query and
45
    // pass it back, as any remaining options are irrelevant.
46
    if (!empty($this->fromQuery)) {
47
      $insert_fields_string = $this->insertFields ? ' (' . implode(', ', $this->insertFields) . ') ' : ' ';
48
      return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
49
    }
50

    
51
    return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
52
  }
53

    
54
}
55

    
56
/**
57
 * SQLite specific implementation of UpdateQuery.
58
 *
59
 * SQLite counts all the rows that match the conditions as modified, even if they
60
 * will not be affected by the query. We workaround this by ensuring that
61
 * we don't select those rows.
62
 *
63
 * A query like this one:
64
 *   UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1
65
 * will become:
66
 *   UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 AND (col1 <> 'newcol1' OR col2 <> 'newcol2')
67
 */
68
class UpdateQuery_sqlite extends UpdateQuery {
69
  public function execute() {
70
    if (!empty($this->queryOptions['sqlite_return_matched_rows'])) {
71
      return parent::execute();
72
    }
73

    
74
    // Get the fields used in the update query.
75
    $fields = $this->expressionFields + $this->fields;
76

    
77
    // Add the inverse of the fields to the condition.
78
    $condition = new DatabaseCondition('OR');
79
    foreach ($fields as $field => $data) {
80
      if (is_array($data)) {
81
        // The field is an expression.
82
        $condition->where($field . ' <> ' . $data['expression']);
83
        $condition->isNull($field);
84
      }
85
      elseif (!isset($data)) {
86
        // The field will be set to NULL.
87
        $condition->isNotNull($field);
88
      }
89
      else {
90
        $condition->condition($field, $data, '<>');
91
        $condition->isNull($field);
92
      }
93
    }
94
    if (count($condition)) {
95
      $condition->compile($this->connection, $this);
96
      $this->condition->where((string) $condition, $condition->arguments());
97
    }
98
    return parent::execute();
99
  }
100

    
101
}
102

    
103
/**
104
 * SQLite specific implementation of DeleteQuery.
105
 */
106
class DeleteQuery_sqlite extends DeleteQuery {
107
  public function execute() {
108
    // When the WHERE is omitted from a DELETE statement and the table being
109
    // deleted has no triggers, SQLite uses an optimization to erase the entire
110
    // table content without having to visit each row of the table individually.
111
    // Prior to SQLite 3.6.5, SQLite does not return the actual number of rows
112
    // deleted by that optimized "truncate" optimization. But we want to return
113
    // the number of rows affected, so we calculate it directly.
114
    if (!count($this->condition)) {
115
      $total_rows = $this->connection->query('SELECT COUNT(*) FROM {' . $this->connection->escapeTable($this->table) . '}')->fetchField();
116
      parent::execute();
117
      return $total_rows;
118
    }
119
    else {
120
      return parent::execute();
121
    }
122
  }
123
}
124

    
125
/**
126
 * SQLite specific implementation of TruncateQuery.
127
 *
128
 * SQLite doesn't support TRUNCATE, but a DELETE query with no condition has
129
 * exactly the effect (it is implemented by DROPing the table).
130
 */
131
class TruncateQuery_sqlite extends TruncateQuery {
132
  public function __toString() {
133
    // Create a sanitized comment string to prepend to the query.
134
    $comments = $this->connection->makeComment($this->comments);
135

    
136
    return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
137
  }
138
}
139

    
140
/**
141
 * @} End of "addtogroup database".
142
 */