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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 | 154 |
154 // Initializes the SQL connection for the given file, returning true if the | 155 // Initializes the SQL connection for the given file, returning true if the |
155 // file could be opened. You can call this or OpenInMemory. | 156 // file could be opened. You can call this or OpenInMemory. |
156 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; | 157 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; |
157 | 158 |
158 // Initializes the SQL connection for a temporary in-memory database. There | 159 // Initializes the SQL connection for a temporary in-memory database. There |
159 // will be no associated file on disk, and the initial database will be | 160 // will be no associated file on disk, and the initial database will be |
160 // empty. You can call this or Open. | 161 // empty. You can call this or Open. |
161 bool OpenInMemory() WARN_UNUSED_RESULT; | 162 bool OpenInMemory() WARN_UNUSED_RESULT; |
162 | 163 |
164 // Create a temporary on-disk database. The database will be | |
165 // deleted after close. This kind of database is similar to | |
166 // OpenInMemory() for small databases, but can page to disk if the | |
167 // database becomes large. | |
168 bool OpenTemporary() WARN_UNUSED_RESULT; | |
169 | |
163 // Returns true if the database has been successfully opened. | 170 // Returns true if the database has been successfully opened. |
164 bool is_open() const { return !!db_; } | 171 bool is_open() const { return !!db_; } |
165 | 172 |
166 // Closes the database. This is automatically performed on destruction for | 173 // Closes the database. This is automatically performed on destruction for |
167 // you, but this allows you to close the database early. You must not call | 174 // you, but this allows you to close the database early. You must not call |
168 // any other functions after closing it. It is permissable to call Close on | 175 // any other functions after closing it. It is permissable to call Close on |
169 // an uninitialized or already-closed database. | 176 // an uninitialized or already-closed database. |
170 void Close(); | 177 void Close(); |
171 | 178 |
172 // Pre-loads the first <cache-size> pages into the cache from the file. | 179 // Pre-loads the first <cache-size> pages into the cache from the file. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, | 218 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, |
212 // so Raze() sets auto_vacuum to 1. | 219 // so Raze() sets auto_vacuum to 1. |
213 // | 220 // |
214 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. | 221 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. |
215 // TODO(shess): Bake auto_vacuum into Connection's API so it can | 222 // TODO(shess): Bake auto_vacuum into Connection's API so it can |
216 // just pick up the default. | 223 // just pick up the default. |
217 bool Raze(); | 224 bool Raze(); |
218 bool RazeWithTimout(base::TimeDelta timeout); | 225 bool RazeWithTimout(base::TimeDelta timeout); |
219 | 226 |
220 // Breaks all outstanding transactions (as initiated by | 227 // Breaks all outstanding transactions (as initiated by |
221 // BeginTransaction()), calls Raze() to destroy the database, then | 228 // BeginTransaction()), closes the SQLite database, and poisons the |
222 // closes the database. After this is called, any operations | 229 // object so that all future operations against the Connection (or |
223 // against the connections (or statements prepared by the | 230 // its Statements) fail safely, without side effects. |
224 // connection) should fail safely. | |
225 // | 231 // |
226 // The value from Raze() is returned, with Close() called in all | 232 // This is intended as an alternative to Close() in error callbacks. |
227 // cases. | 233 // Close() should still be called at some point. |
234 void Poison(); | |
235 | |
236 // Raze() the database and Poison() the handle. Returns the return | |
237 // value from Raze(). | |
238 // TODO(shess): Rename to RazeAndPoison(). | |
228 bool RazeAndClose(); | 239 bool RazeAndClose(); |
229 | 240 |
230 // Delete the underlying database files associated with |path|. | 241 // Delete the underlying database files associated with |path|. |
231 // This should be used on a database which has no existing | 242 // This should be used on a database which has no existing |
232 // connections. If any other connections are open to the same | 243 // connections. If any other connections are open to the same |
233 // database, this could cause odd results or corruption (for | 244 // database, this could cause odd results or corruption (for |
234 // instance if a hot journal is deleted but the associated database | 245 // instance if a hot journal is deleted but the associated database |
235 // is not). | 246 // is not). |
236 // | 247 // |
237 // Returns true if the database file and associated journals no | 248 // Returns true if the database file and associated journals no |
238 // longer exist, false otherwise. If the database has never | 249 // longer exist, false otherwise. If the database has never |
239 // existed, this will return true. | 250 // existed, this will return true. |
240 static bool Delete(const base::FilePath& path); | 251 static bool Delete(const base::FilePath& path); |
241 | 252 |
242 // Transactions -------------------------------------------------------------- | 253 // Transactions -------------------------------------------------------------- |
243 | 254 |
244 // Transaction management. We maintain a virtual transaction stack to emulate | 255 // Transaction management. We maintain a virtual transaction stack to emulate |
245 // nested transactions since sqlite can't do nested transactions. The | 256 // nested transactions since sqlite can't do nested transactions. The |
246 // limitation is you can't roll back a sub transaction: if any transaction | 257 // limitation is you can't roll back a sub transaction: if any transaction |
247 // fails, all transactions open will also be rolled back. Any nested | 258 // fails, all transactions open will also be rolled back. Any nested |
248 // transactions after one has rolled back will return fail for Begin(). If | 259 // transactions after one has rolled back will return fail for Begin(). If |
249 // Begin() fails, you must not call Commit or Rollback(). | 260 // Begin() fails, you must not call Commit or Rollback(). |
250 // | 261 // |
251 // Normally you should use sql::Transaction to manage a transaction, which | 262 // Normally you should use sql::Transaction to manage a transaction, which |
252 // will scope it to a C++ context. | 263 // will scope it to a C++ context. |
253 bool BeginTransaction(); | 264 bool BeginTransaction(); |
254 void RollbackTransaction(); | 265 void RollbackTransaction(); |
255 bool CommitTransaction(); | 266 bool CommitTransaction(); |
256 | 267 |
268 // Rollback all outstanding transactions. Use with care, there may | |
269 // be scoped transactions on the stack. | |
270 void RollbackAllTransactions(); | |
271 | |
257 // Returns the current transaction nesting, which will be 0 if there are | 272 // Returns the current transaction nesting, which will be 0 if there are |
258 // no open transactions. | 273 // no open transactions. |
259 int transaction_nesting() const { return transaction_nesting_; } | 274 int transaction_nesting() const { return transaction_nesting_; } |
260 | 275 |
276 // Attached databases--------------------------------------------------------- | |
277 | |
278 // SQLite supports attaching multiple database files to a single | |
279 // handle. Attach the database in |other_db_path| to the current | |
280 // handle under |attachment_point|. |attachment_point| should only | |
281 // contain characters from [a-zA-Z0-9_]. | |
282 // | |
283 // Note that calling attach or detach with an open transaction is an | |
284 // error. | |
285 bool AttachDatabase(const base::FilePath& other_db_path, | |
286 const char* attachment_point); | |
287 bool DetachDatabase(const char* attachment_point); | |
288 | |
261 // Statements ---------------------------------------------------------------- | 289 // Statements ---------------------------------------------------------------- |
262 | 290 |
263 // Executes the given SQL string, returning true on success. This is | 291 // Executes the given SQL string, returning true on success. This is |
264 // normally used for simple, 1-off statements that don't take any bound | 292 // normally used for simple, 1-off statements that don't take any bound |
265 // parameters and don't return any data (e.g. CREATE TABLE). | 293 // parameters and don't return any data (e.g. CREATE TABLE). |
266 // | 294 // |
267 // This will DCHECK if the |sql| contains errors. | 295 // This will DCHECK if the |sql| contains errors. |
268 // | 296 // |
269 // Do not use ignore_result() to ignore all errors. Use | 297 // Do not use ignore_result() to ignore all errors. Use |
270 // ExecuteAndReturnErrorCode() and ignore only specific errors. | 298 // ExecuteAndReturnErrorCode() and ignore only specific errors. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 | 371 |
344 // Returns the errno associated with GetErrorCode(). See | 372 // Returns the errno associated with GetErrorCode(). See |
345 // SQLITE_LAST_ERRNO in SQLite documentation. | 373 // SQLITE_LAST_ERRNO in SQLite documentation. |
346 int GetLastErrno() const; | 374 int GetLastErrno() const; |
347 | 375 |
348 // Returns a pointer to a statically allocated string associated with the | 376 // Returns a pointer to a statically allocated string associated with the |
349 // last sqlite operation. | 377 // last sqlite operation. |
350 const char* GetErrorMessage() const; | 378 const char* GetErrorMessage() const; |
351 | 379 |
352 private: | 380 private: |
381 // For recovery module. | |
382 friend class Recovery; | |
Scott Hess - ex-Googler
2013/07/15 21:10:34
Specifically for sqlite3_backup stuff. Given that
| |
383 | |
353 // Allow test-support code to set/reset error ignorer. | 384 // Allow test-support code to set/reset error ignorer. |
354 friend class ScopedErrorIgnorer; | 385 friend class ScopedErrorIgnorer; |
355 | 386 |
356 // Statement accesses StatementRef which we don't want to expose to everybody | 387 // Statement accesses StatementRef which we don't want to expose to everybody |
357 // (they should go through Statement). | 388 // (they should go through Statement). |
358 friend class Statement; | 389 friend class Statement; |
359 | 390 |
360 // Internal initialize function used by both Init and InitInMemory. The file | 391 // Internal initialize function used by both Init and InitInMemory. The file |
361 // name is always 8 bits since we want to use the 8-bit version of | 392 // name is always 8 bits since we want to use the 8-bit version of |
362 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 393 // 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... | |
516 | 547 |
517 // Tag for auxiliary histograms. | 548 // Tag for auxiliary histograms. |
518 std::string histogram_tag_; | 549 std::string histogram_tag_; |
519 | 550 |
520 DISALLOW_COPY_AND_ASSIGN(Connection); | 551 DISALLOW_COPY_AND_ASSIGN(Connection); |
521 }; | 552 }; |
522 | 553 |
523 } // namespace sql | 554 } // namespace sql |
524 | 555 |
525 #endif // SQL_CONNECTION_H_ | 556 #endif // SQL_CONNECTION_H_ |
OLD | NEW |