OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SQL_CONNECTION_H_ | 5 #ifndef SQL_CONNECTION_H_ |
6 #define SQL_CONNECTION_H_ | 6 #define SQL_CONNECTION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 struct sqlite3; | 22 struct sqlite3; |
23 struct sqlite3_stmt; | 23 struct sqlite3_stmt; |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 class FilePath; | 26 class FilePath; |
27 } | 27 } |
28 | 28 |
29 namespace sql { | 29 namespace sql { |
30 | 30 |
| 31 class Recovery; |
31 class Statement; | 32 class Statement; |
32 | 33 |
33 // Uniquely identifies a statement. There are two modes of operation: | 34 // Uniquely identifies a statement. There are two modes of operation: |
34 // | 35 // |
35 // - In the most common mode, you will use the source file and line number to | 36 // - In the most common mode, you will use the source file and line number to |
36 // identify your statement. This is a convienient way to get uniqueness for | 37 // identify your statement. This is a convienient way to get uniqueness for |
37 // a statement that is only used in one place. Use the SQL_FROM_HERE macro | 38 // a statement that is only used in one place. Use the SQL_FROM_HERE macro |
38 // to generate a StatementID. | 39 // to generate a StatementID. |
39 // | 40 // |
40 // - In the "custom" mode you may use the statement from different places or | 41 // - In the "custom" mode you may use the statement from different places or |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 151 |
151 // Initializes the SQL connection for the given file, returning true if the | 152 // Initializes the SQL connection for the given file, returning true if the |
152 // file could be opened. You can call this or OpenInMemory. | 153 // file could be opened. You can call this or OpenInMemory. |
153 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; | 154 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; |
154 | 155 |
155 // Initializes the SQL connection for a temporary in-memory database. There | 156 // Initializes the SQL connection for a temporary in-memory database. There |
156 // will be no associated file on disk, and the initial database will be | 157 // will be no associated file on disk, and the initial database will be |
157 // empty. You can call this or Open. | 158 // empty. You can call this or Open. |
158 bool OpenInMemory() WARN_UNUSED_RESULT; | 159 bool OpenInMemory() WARN_UNUSED_RESULT; |
159 | 160 |
| 161 // Create a temporary on-disk database. The database will be |
| 162 // deleted after close. This kind of database is similar to |
| 163 // OpenInMemory() for small databases, but can page to disk if the |
| 164 // database becomes large. |
| 165 bool OpenTemporary() WARN_UNUSED_RESULT; |
| 166 |
160 // Returns true if the database has been successfully opened. | 167 // Returns true if the database has been successfully opened. |
161 bool is_open() const { return !!db_; } | 168 bool is_open() const { return !!db_; } |
162 | 169 |
163 // Closes the database. This is automatically performed on destruction for | 170 // Closes the database. This is automatically performed on destruction for |
164 // you, but this allows you to close the database early. You must not call | 171 // you, but this allows you to close the database early. You must not call |
165 // any other functions after closing it. It is permissable to call Close on | 172 // any other functions after closing it. It is permissable to call Close on |
166 // an uninitialized or already-closed database. | 173 // an uninitialized or already-closed database. |
167 void Close(); | 174 void Close(); |
168 | 175 |
169 // Pre-loads the first <cache-size> pages into the cache from the file. | 176 // Pre-loads the first <cache-size> pages into the cache from the file. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // Breaks all outstanding transactions (as initiated by | 224 // Breaks all outstanding transactions (as initiated by |
218 // BeginTransaction()), calls Raze() to destroy the database, then | 225 // BeginTransaction()), calls Raze() to destroy the database, then |
219 // closes the database. After this is called, any operations | 226 // closes the database. After this is called, any operations |
220 // against the connections (or statements prepared by the | 227 // against the connections (or statements prepared by the |
221 // connection) should fail safely. | 228 // connection) should fail safely. |
222 // | 229 // |
223 // The value from Raze() is returned, with Close() called in all | 230 // The value from Raze() is returned, with Close() called in all |
224 // cases. | 231 // cases. |
225 bool RazeAndClose(); | 232 bool RazeAndClose(); |
226 | 233 |
| 234 // Like RazeAndClose(), but without the Raze(). |
| 235 void CloseAndPoison(); |
| 236 |
227 // Delete the underlying database files associated with |path|. | 237 // Delete the underlying database files associated with |path|. |
228 // This should be used on a database which has no existing | 238 // This should be used on a database which has no existing |
229 // connections. If any other connections are open to the same | 239 // connections. If any other connections are open to the same |
230 // database, this could cause odd results or corruption (for | 240 // database, this could cause odd results or corruption (for |
231 // instance if a hot journal is deleted but the associated database | 241 // instance if a hot journal is deleted but the associated database |
232 // is not). | 242 // is not). |
233 // | 243 // |
234 // Returns true if the database file and associated journals no | 244 // Returns true if the database file and associated journals no |
235 // longer exist, false otherwise. If the database has never | 245 // longer exist, false otherwise. If the database has never |
236 // existed, this will return true. | 246 // existed, this will return true. |
237 static bool Delete(const base::FilePath& path); | 247 static bool Delete(const base::FilePath& path); |
238 | 248 |
239 // Transactions -------------------------------------------------------------- | 249 // Transactions -------------------------------------------------------------- |
240 | 250 |
241 // Transaction management. We maintain a virtual transaction stack to emulate | 251 // Transaction management. We maintain a virtual transaction stack to emulate |
242 // nested transactions since sqlite can't do nested transactions. The | 252 // nested transactions since sqlite can't do nested transactions. The |
243 // limitation is you can't roll back a sub transaction: if any transaction | 253 // limitation is you can't roll back a sub transaction: if any transaction |
244 // fails, all transactions open will also be rolled back. Any nested | 254 // fails, all transactions open will also be rolled back. Any nested |
245 // transactions after one has rolled back will return fail for Begin(). If | 255 // transactions after one has rolled back will return fail for Begin(). If |
246 // Begin() fails, you must not call Commit or Rollback(). | 256 // Begin() fails, you must not call Commit or Rollback(). |
247 // | 257 // |
248 // Normally you should use sql::Transaction to manage a transaction, which | 258 // Normally you should use sql::Transaction to manage a transaction, which |
249 // will scope it to a C++ context. | 259 // will scope it to a C++ context. |
250 bool BeginTransaction(); | 260 bool BeginTransaction(); |
251 void RollbackTransaction(); | 261 void RollbackTransaction(); |
252 bool CommitTransaction(); | 262 bool CommitTransaction(); |
253 | 263 |
| 264 // Rollback all outstanding transactions. Use with care, there may |
| 265 // be scoped transactions on the stack. |
| 266 void RollbackAllTransactions(); |
| 267 |
254 // Returns the current transaction nesting, which will be 0 if there are | 268 // Returns the current transaction nesting, which will be 0 if there are |
255 // no open transactions. | 269 // no open transactions. |
256 int transaction_nesting() const { return transaction_nesting_; } | 270 int transaction_nesting() const { return transaction_nesting_; } |
257 | 271 |
258 // Statements ---------------------------------------------------------------- | 272 // Statements ---------------------------------------------------------------- |
259 | 273 |
260 // Executes the given SQL string, returning true on success. This is | 274 // Executes the given SQL string, returning true on success. This is |
261 // normally used for simple, 1-off statements that don't take any bound | 275 // normally used for simple, 1-off statements that don't take any bound |
262 // parameters and don't return any data (e.g. CREATE TABLE). | 276 // parameters and don't return any data (e.g. CREATE TABLE). |
263 // | 277 // |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 354 |
341 // Returns the errno associated with GetErrorCode(). See | 355 // Returns the errno associated with GetErrorCode(). See |
342 // SQLITE_LAST_ERRNO in SQLite documentation. | 356 // SQLITE_LAST_ERRNO in SQLite documentation. |
343 int GetLastErrno() const; | 357 int GetLastErrno() const; |
344 | 358 |
345 // Returns a pointer to a statically allocated string associated with the | 359 // Returns a pointer to a statically allocated string associated with the |
346 // last sqlite operation. | 360 // last sqlite operation. |
347 const char* GetErrorMessage() const; | 361 const char* GetErrorMessage() const; |
348 | 362 |
349 private: | 363 private: |
| 364 // For recovery module. |
| 365 friend class Recovery; |
| 366 |
350 // Allow test-support code to set/reset error ignorer. | 367 // Allow test-support code to set/reset error ignorer. |
351 friend class ScopedErrorIgnorer; | 368 friend class ScopedErrorIgnorer; |
352 | 369 |
353 // Statement accesses StatementRef which we don't want to expose to everybody | 370 // Statement accesses StatementRef which we don't want to expose to everybody |
354 // (they should go through Statement). | 371 // (they should go through Statement). |
355 friend class Statement; | 372 friend class Statement; |
356 | 373 |
357 // Internal initialize function used by both Init and InitInMemory. The file | 374 // Internal initialize function used by both Init and InitInMemory. The file |
358 // name is always 8 bits since we want to use the 8-bit version of | 375 // name is always 8 bits since we want to use the 8-bit version of |
359 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 376 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 | 530 |
514 // Tag for auxiliary histograms. | 531 // Tag for auxiliary histograms. |
515 std::string histogram_tag_; | 532 std::string histogram_tag_; |
516 | 533 |
517 DISALLOW_COPY_AND_ASSIGN(Connection); | 534 DISALLOW_COPY_AND_ASSIGN(Connection); |
518 }; | 535 }; |
519 | 536 |
520 } // namespace sql | 537 } // namespace sql |
521 | 538 |
522 #endif // SQL_CONNECTION_H_ | 539 #endif // SQL_CONNECTION_H_ |
OLD | NEW |