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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 // | 211 // |
212 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, | 212 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, |
213 // so Raze() sets auto_vacuum to 1. | 213 // so Raze() sets auto_vacuum to 1. |
214 // | 214 // |
215 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. | 215 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. |
216 // TODO(shess): Bake auto_vacuum into Connection's API so it can | 216 // TODO(shess): Bake auto_vacuum into Connection's API so it can |
217 // just pick up the default. | 217 // just pick up the default. |
218 bool Raze(); | 218 bool Raze(); |
219 bool RazeWithTimout(base::TimeDelta timeout); | 219 bool RazeWithTimout(base::TimeDelta timeout); |
220 | 220 |
221 // Breaks all outstanding transactions (as initiated by | |
222 // BeginTransaction()), calls Raze() to destroy the database, then | |
223 // closes the database. After this is called, any operations | |
pkotwicz
2013/01/31 19:22:00
Nit: 1 space instead of 2. You seem to have two sp
Scott Hess - ex-Googler
2013/02/05 01:20:25
I bet I have tens of thousands of instances of thi
| |
224 // against the connections (or statements prepared by the | |
225 // connection) should fail safely. | |
226 // | |
227 // The value from Raze() is returned, with Close() called in all | |
228 // cases. | |
229 bool RazeAndClose(); | |
230 | |
221 // Transactions -------------------------------------------------------------- | 231 // Transactions -------------------------------------------------------------- |
222 | 232 |
223 // Transaction management. We maintain a virtual transaction stack to emulate | 233 // Transaction management. We maintain a virtual transaction stack to emulate |
224 // nested transactions since sqlite can't do nested transactions. The | 234 // nested transactions since sqlite can't do nested transactions. The |
225 // limitation is you can't roll back a sub transaction: if any transaction | 235 // limitation is you can't roll back a sub transaction: if any transaction |
226 // fails, all transactions open will also be rolled back. Any nested | 236 // fails, all transactions open will also be rolled back. Any nested |
227 // transactions after one has rolled back will return fail for Begin(). If | 237 // transactions after one has rolled back will return fail for Begin(). If |
228 // Begin() fails, you must not call Commit or Rollback(). | 238 // Begin() fails, you must not call Commit or Rollback(). |
229 // | 239 // |
230 // Normally you should use sql::Transaction to manage a transaction, which | 240 // Normally you should use sql::Transaction to manage a transaction, which |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 private: | 341 private: |
332 // Statement accesses StatementRef which we don't want to expose to everybody | 342 // Statement accesses StatementRef which we don't want to expose to everybody |
333 // (they should go through Statement). | 343 // (they should go through Statement). |
334 friend class Statement; | 344 friend class Statement; |
335 | 345 |
336 // Internal initialize function used by both Init and InitInMemory. The file | 346 // Internal initialize function used by both Init and InitInMemory. The file |
337 // name is always 8 bits since we want to use the 8-bit version of | 347 // name is always 8 bits since we want to use the 8-bit version of |
338 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 348 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
339 bool OpenInternal(const std::string& file_name); | 349 bool OpenInternal(const std::string& file_name); |
340 | 350 |
351 // Internal close function used by Close() and RazeAndClose(). | |
352 // |forced| indicates that orderly-shutdown checks should not apply. | |
353 void CloseInternal(bool forced); | |
354 | |
341 // Check whether the current thread is allowed to make IO calls, but only | 355 // Check whether the current thread is allowed to make IO calls, but only |
342 // if database wasn't open in memory. Function is inlined to be a no-op in | 356 // if database wasn't open in memory. Function is inlined to be a no-op in |
343 // official build. | 357 // official build. |
344 void AssertIOAllowed() { | 358 void AssertIOAllowed() { |
345 if (!in_memory_) | 359 if (!in_memory_) |
346 base::ThreadRestrictions::AssertIOAllowed(); | 360 base::ThreadRestrictions::AssertIOAllowed(); |
347 } | 361 } |
348 | 362 |
349 // Internal helper for DoesTableExist and DoesIndexExist. | 363 // Internal helper for DoesTableExist and DoesIndexExist. |
350 bool DoesTableOrIndexExist(const char* name, const char* type) const; | 364 bool DoesTableOrIndexExist(const char* name, const char* type) const; |
(...skipping 12 matching lines...) Expand all Loading... | |
363 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> { | 377 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> { |
364 public: | 378 public: |
365 // Default constructor initializes to an invalid statement. | 379 // Default constructor initializes to an invalid statement. |
366 StatementRef(); | 380 StatementRef(); |
367 explicit StatementRef(sqlite3_stmt* stmt); | 381 explicit StatementRef(sqlite3_stmt* stmt); |
368 StatementRef(Connection* connection, sqlite3_stmt* stmt); | 382 StatementRef(Connection* connection, sqlite3_stmt* stmt); |
369 | 383 |
370 // When true, the statement can be used. | 384 // When true, the statement can be used. |
371 bool is_valid() const { return !!stmt_; } | 385 bool is_valid() const { return !!stmt_; } |
372 | 386 |
387 // When true, the statement is either currently valid, or was | |
388 // previously valid but the connection was forcibly closed. Used | |
389 // for diagnostic checks. | |
390 bool was_valid() const { return was_valid_; } | |
391 | |
373 // If we've not been linked to a connection, this will be NULL. | 392 // If we've not been linked to a connection, this will be NULL. |
374 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(), | 393 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(), |
375 // which prevents Statement::OnError() from forwarding errors. | 394 // which prevents Statement::OnError() from forwarding errors. |
376 Connection* connection() const { return connection_; } | 395 Connection* connection() const { return connection_; } |
377 | 396 |
378 // Returns the sqlite statement if any. If the statement is not active, | 397 // Returns the sqlite statement if any. If the statement is not active, |
379 // this will return NULL. | 398 // this will return NULL. |
380 sqlite3_stmt* stmt() const { return stmt_; } | 399 sqlite3_stmt* stmt() const { return stmt_; } |
381 | 400 |
382 // Destroys the compiled statement and marks it NULL. The statement will | 401 // Destroys the compiled statement and marks it NULL. The statement will |
383 // no longer be active. | 402 // no longer be active. |forced| is used to indicate if orderly-shutdown |
384 void Close(); | 403 // checks should apply (see Connection::RazeAndClose()). |
404 void Close(bool forced); | |
385 | 405 |
386 // Check whether the current thread is allowed to make IO calls, but only | 406 // Check whether the current thread is allowed to make IO calls, but only |
387 // if database wasn't open in memory. | 407 // if database wasn't open in memory. |
388 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); } | 408 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); } |
389 | 409 |
390 private: | 410 private: |
391 friend class base::RefCounted<StatementRef>; | 411 friend class base::RefCounted<StatementRef>; |
392 | 412 |
393 ~StatementRef(); | 413 ~StatementRef(); |
394 | 414 |
395 Connection* connection_; | 415 Connection* connection_; |
396 sqlite3_stmt* stmt_; | 416 sqlite3_stmt* stmt_; |
417 bool was_valid_; | |
397 | 418 |
398 DISALLOW_COPY_AND_ASSIGN(StatementRef); | 419 DISALLOW_COPY_AND_ASSIGN(StatementRef); |
399 }; | 420 }; |
400 friend class StatementRef; | 421 friend class StatementRef; |
401 | 422 |
402 // Executes a rollback statement, ignoring all transaction state. Used | 423 // Executes a rollback statement, ignoring all transaction state. Used |
403 // internally in the transaction management code. | 424 // internally in the transaction management code. |
404 void DoRollback(); | 425 void DoRollback(); |
405 | 426 |
406 // Called by a StatementRef when it's being created or destroyed. See | 427 // Called by a StatementRef when it's being created or destroyed. See |
407 // open_statements_ below. | 428 // open_statements_ below. |
408 void StatementRefCreated(StatementRef* ref); | 429 void StatementRefCreated(StatementRef* ref); |
409 void StatementRefDeleted(StatementRef* ref); | 430 void StatementRefDeleted(StatementRef* ref); |
410 | 431 |
411 // Frees all cached statements from statement_cache_. | |
412 void ClearCache(); | |
413 | |
414 // Called by Statement objects when an sqlite function returns an error. | 432 // Called by Statement objects when an sqlite function returns an error. |
415 // The return value is the error code reflected back to client code. | 433 // The return value is the error code reflected back to client code. |
416 int OnSqliteError(int err, Statement* stmt); | 434 int OnSqliteError(int err, Statement* stmt); |
417 | 435 |
418 // Like |Execute()|, but retries if the database is locked. | 436 // Like |Execute()|, but retries if the database is locked. |
419 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout) | 437 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout) |
420 WARN_UNUSED_RESULT; | 438 WARN_UNUSED_RESULT; |
421 | 439 |
422 // Internal helper for const functions. Like GetUniqueStatement(), | 440 // Internal helper for const functions. Like GetUniqueStatement(), |
423 // except the statement is not entered into open_statements_, | 441 // except the statement is not entered into open_statements_, |
(...skipping 30 matching lines...) Expand all Loading... | |
454 | 472 |
455 // True if any of the currently nested transactions have been rolled back. | 473 // True if any of the currently nested transactions have been rolled back. |
456 // When we get to the outermost transaction, this will determine if we do | 474 // When we get to the outermost transaction, this will determine if we do |
457 // a rollback instead of a commit. | 475 // a rollback instead of a commit. |
458 bool needs_rollback_; | 476 bool needs_rollback_; |
459 | 477 |
460 // True if database is open with OpenInMemory(), False if database is open | 478 // True if database is open with OpenInMemory(), False if database is open |
461 // with Open(). | 479 // with Open(). |
462 bool in_memory_; | 480 bool in_memory_; |
463 | 481 |
482 // |true| if the connection was closed using RazeAndClose(). Used | |
483 // to enable diagnostics to distinguish calls to never-opened | |
484 // databases (incorrect use of the API) from calls to once-valid | |
485 // databases. | |
486 bool poisoned_; | |
487 | |
464 // This object handles errors resulting from all forms of executing sqlite | 488 // This object handles errors resulting from all forms of executing sqlite |
465 // commands or statements. It can be null which means default handling. | 489 // commands or statements. It can be null which means default handling. |
466 scoped_ptr<ErrorDelegate> error_delegate_; | 490 scoped_ptr<ErrorDelegate> error_delegate_; |
467 | 491 |
468 // Auxiliary error-code histogram. | 492 // Auxiliary error-code histogram. |
469 std::string error_histogram_name_; | 493 std::string error_histogram_name_; |
470 | 494 |
471 DISALLOW_COPY_AND_ASSIGN(Connection); | 495 DISALLOW_COPY_AND_ASSIGN(Connection); |
472 }; | 496 }; |
473 | 497 |
474 } // namespace sql | 498 } // namespace sql |
475 | 499 |
476 #endif // SQL_CONNECTION_H_ | 500 #endif // SQL_CONNECTION_H_ |
OLD | NEW |