| 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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 157 |
| 158 // Initializes the SQL connection for the given file, returning true if the | 158 // Initializes the SQL connection for the given file, returning true if the |
| 159 // file could be opened. You can call this or OpenInMemory. | 159 // file could be opened. You can call this or OpenInMemory. |
| 160 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; | 160 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT; |
| 161 | 161 |
| 162 // Initializes the SQL connection for a temporary in-memory database. There | 162 // Initializes the SQL connection for a temporary in-memory database. There |
| 163 // will be no associated file on disk, and the initial database will be | 163 // will be no associated file on disk, and the initial database will be |
| 164 // empty. You can call this or Open. | 164 // empty. You can call this or Open. |
| 165 bool OpenInMemory() WARN_UNUSED_RESULT; | 165 bool OpenInMemory() WARN_UNUSED_RESULT; |
| 166 | 166 |
| 167 // Returns trie if the database has been successfully opened. | 167 // Returns true if the database has been successfully opened. |
| 168 bool is_open() const { return !!db_; } | 168 bool is_open() const { return !!db_; } |
| 169 | 169 |
| 170 // Closes the database. This is automatically performed on destruction for | 170 // Closes the database. This is automatically performed on destruction for |
| 171 // 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 |
| 172 // 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 |
| 173 // an uninitialized or already-closed database. | 173 // an uninitialized or already-closed database. |
| 174 void Close(); | 174 void Close(); |
| 175 | 175 |
| 176 // 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. |
| 177 // If you expect to soon use a substantial portion of the database, this | 177 // If you expect to soon use a substantial portion of the database, this |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 // | 214 // |
| 215 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, | 215 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1, |
| 216 // so Raze() sets auto_vacuum to 1. | 216 // so Raze() sets auto_vacuum to 1. |
| 217 // | 217 // |
| 218 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. | 218 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB. |
| 219 // TODO(shess): Bake auto_vacuum into Connection's API so it can | 219 // TODO(shess): Bake auto_vacuum into Connection's API so it can |
| 220 // just pick up the default. | 220 // just pick up the default. |
| 221 bool Raze(); | 221 bool Raze(); |
| 222 bool RazeWithTimout(base::TimeDelta timeout); | 222 bool RazeWithTimout(base::TimeDelta timeout); |
| 223 | 223 |
| 224 // Breaks all outstanding transactions (as initiated by |
| 225 // BeginTransaction()), calls Raze() to destroy the database, then |
| 226 // closes the database. After this is called, any operations |
| 227 // against the connections (or statements prepared by the |
| 228 // connection) should fail safely. |
| 229 // |
| 230 // The value from Raze() is returned, with Close() called in all |
| 231 // cases. |
| 232 bool RazeAndClose(); |
| 233 |
| 224 // Transactions -------------------------------------------------------------- | 234 // Transactions -------------------------------------------------------------- |
| 225 | 235 |
| 226 // Transaction management. We maintain a virtual transaction stack to emulate | 236 // Transaction management. We maintain a virtual transaction stack to emulate |
| 227 // nested transactions since sqlite can't do nested transactions. The | 237 // nested transactions since sqlite can't do nested transactions. The |
| 228 // limitation is you can't roll back a sub transaction: if any transaction | 238 // limitation is you can't roll back a sub transaction: if any transaction |
| 229 // fails, all transactions open will also be rolled back. Any nested | 239 // fails, all transactions open will also be rolled back. Any nested |
| 230 // transactions after one has rolled back will return fail for Begin(). If | 240 // transactions after one has rolled back will return fail for Begin(). If |
| 231 // Begin() fails, you must not call Commit or Rollback(). | 241 // Begin() fails, you must not call Commit or Rollback(). |
| 232 // | 242 // |
| 233 // Normally you should use sql::Transaction to manage a transaction, which | 243 // Normally you should use sql::Transaction to manage a transaction, which |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 private: | 344 private: |
| 335 // Statement accesses StatementRef which we don't want to expose to everybody | 345 // Statement accesses StatementRef which we don't want to expose to everybody |
| 336 // (they should go through Statement). | 346 // (they should go through Statement). |
| 337 friend class Statement; | 347 friend class Statement; |
| 338 | 348 |
| 339 // Internal initialize function used by both Init and InitInMemory. The file | 349 // Internal initialize function used by both Init and InitInMemory. The file |
| 340 // name is always 8 bits since we want to use the 8-bit version of | 350 // name is always 8 bits since we want to use the 8-bit version of |
| 341 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 351 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
| 342 bool OpenInternal(const std::string& file_name); | 352 bool OpenInternal(const std::string& file_name); |
| 343 | 353 |
| 354 // Internal close function used by Close() and RazeAndClose(). |
| 355 // |forced| indicates that orderly-shutdown checks should not apply. |
| 356 void CloseInternal(bool forced); |
| 357 |
| 344 // Check whether the current thread is allowed to make IO calls, but only | 358 // Check whether the current thread is allowed to make IO calls, but only |
| 345 // if database wasn't open in memory. Function is inlined to be a no-op in | 359 // if database wasn't open in memory. Function is inlined to be a no-op in |
| 346 // official build. | 360 // official build. |
| 347 void AssertIOAllowed() { | 361 void AssertIOAllowed() { |
| 348 if (!in_memory_) | 362 if (!in_memory_) |
| 349 base::ThreadRestrictions::AssertIOAllowed(); | 363 base::ThreadRestrictions::AssertIOAllowed(); |
| 350 } | 364 } |
| 351 | 365 |
| 352 // Internal helper for DoesTableExist and DoesIndexExist. | 366 // Internal helper for DoesTableExist and DoesIndexExist. |
| 353 bool DoesTableOrIndexExist(const char* name, const char* type) const; | 367 bool DoesTableOrIndexExist(const char* name, const char* type) const; |
| 354 | 368 |
| 355 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. | 369 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. |
| 356 // Refcounting allows us to give these statements out to sql::Statement | 370 // Refcounting allows us to give these statements out to sql::Statement |
| 357 // objects while also optionally maintaining a cache of compiled statements | 371 // objects while also optionally maintaining a cache of compiled statements |
| 358 // by just keeping a refptr to these objects. | 372 // by just keeping a refptr to these objects. |
| 359 // | 373 // |
| 360 // A statement ref can be valid, in which case it can be used, or invalid to | 374 // A statement ref can be valid, in which case it can be used, or invalid to |
| 361 // indicate that the statement hasn't been created yet, has an error, or has | 375 // indicate that the statement hasn't been created yet, has an error, or has |
| 362 // been destroyed. | 376 // been destroyed. |
| 363 // | 377 // |
| 364 // The Connection may revoke a StatementRef in some error cases, so callers | 378 // The Connection may revoke a StatementRef in some error cases, so callers |
| 365 // should always check validity before using. | 379 // should always check validity before using. |
| 366 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> { | 380 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> { |
| 367 public: | 381 public: |
| 368 // Default constructor initializes to an invalid statement. | 382 // |connection| is the sql::Connection instance associated with |
| 369 StatementRef(); | 383 // the statement, and is used for tracking outstanding statements |
| 370 explicit StatementRef(sqlite3_stmt* stmt); | 384 // and for error handling. Set to NULL for invalid or untracked |
| 371 StatementRef(Connection* connection, sqlite3_stmt* stmt); | 385 // refs. |stmt| is the actual statement, and should only be NULL |
| 386 // to create an invalid ref. |was_valid| indicates whether the |
| 387 // statement should be considered valid for diagnistic purposes. |
| 388 // |was_valid| can be true for NULL |stmt| if the connection has |
| 389 // been forcibly closed by an error handler. |
| 390 StatementRef(Connection* connection, sqlite3_stmt* stmt, bool was_valid); |
| 372 | 391 |
| 373 // When true, the statement can be used. | 392 // When true, the statement can be used. |
| 374 bool is_valid() const { return !!stmt_; } | 393 bool is_valid() const { return !!stmt_; } |
| 375 | 394 |
| 395 // When true, the statement is either currently valid, or was |
| 396 // previously valid but the connection was forcibly closed. Used |
| 397 // for diagnostic checks. |
| 398 bool was_valid() const { return was_valid_; } |
| 399 |
| 376 // If we've not been linked to a connection, this will be NULL. | 400 // If we've not been linked to a connection, this will be NULL. |
| 377 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(), | 401 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(), |
| 378 // which prevents Statement::OnError() from forwarding errors. | 402 // which prevents Statement::OnError() from forwarding errors. |
| 379 Connection* connection() const { return connection_; } | 403 Connection* connection() const { return connection_; } |
| 380 | 404 |
| 381 // Returns the sqlite statement if any. If the statement is not active, | 405 // Returns the sqlite statement if any. If the statement is not active, |
| 382 // this will return NULL. | 406 // this will return NULL. |
| 383 sqlite3_stmt* stmt() const { return stmt_; } | 407 sqlite3_stmt* stmt() const { return stmt_; } |
| 384 | 408 |
| 385 // Destroys the compiled statement and marks it NULL. The statement will | 409 // Destroys the compiled statement and marks it NULL. The statement will |
| 386 // no longer be active. | 410 // no longer be active. |forced| is used to indicate if orderly-shutdown |
| 387 void Close(); | 411 // checks should apply (see Connection::RazeAndClose()). |
| 412 void Close(bool forced); |
| 388 | 413 |
| 389 // Check whether the current thread is allowed to make IO calls, but only | 414 // Check whether the current thread is allowed to make IO calls, but only |
| 390 // if database wasn't open in memory. | 415 // if database wasn't open in memory. |
| 391 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); } | 416 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); } |
| 392 | 417 |
| 393 private: | 418 private: |
| 394 friend class base::RefCounted<StatementRef>; | 419 friend class base::RefCounted<StatementRef>; |
| 395 | 420 |
| 396 ~StatementRef(); | 421 ~StatementRef(); |
| 397 | 422 |
| 398 Connection* connection_; | 423 Connection* connection_; |
| 399 sqlite3_stmt* stmt_; | 424 sqlite3_stmt* stmt_; |
| 425 bool was_valid_; |
| 400 | 426 |
| 401 DISALLOW_COPY_AND_ASSIGN(StatementRef); | 427 DISALLOW_COPY_AND_ASSIGN(StatementRef); |
| 402 }; | 428 }; |
| 403 friend class StatementRef; | 429 friend class StatementRef; |
| 404 | 430 |
| 405 // Executes a rollback statement, ignoring all transaction state. Used | 431 // Executes a rollback statement, ignoring all transaction state. Used |
| 406 // internally in the transaction management code. | 432 // internally in the transaction management code. |
| 407 void DoRollback(); | 433 void DoRollback(); |
| 408 | 434 |
| 409 // Called by a StatementRef when it's being created or destroyed. See | 435 // Called by a StatementRef when it's being created or destroyed. See |
| 410 // open_statements_ below. | 436 // open_statements_ below. |
| 411 void StatementRefCreated(StatementRef* ref); | 437 void StatementRefCreated(StatementRef* ref); |
| 412 void StatementRefDeleted(StatementRef* ref); | 438 void StatementRefDeleted(StatementRef* ref); |
| 413 | 439 |
| 414 // Frees all cached statements from statement_cache_. | |
| 415 void ClearCache(); | |
| 416 | |
| 417 // Called by Statement objects when an sqlite function returns an error. | 440 // Called by Statement objects when an sqlite function returns an error. |
| 418 // The return value is the error code reflected back to client code. | 441 // The return value is the error code reflected back to client code. |
| 419 int OnSqliteError(int err, Statement* stmt); | 442 int OnSqliteError(int err, Statement* stmt); |
| 420 | 443 |
| 421 // Like |Execute()|, but retries if the database is locked. | 444 // Like |Execute()|, but retries if the database is locked. |
| 422 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout) | 445 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout) |
| 423 WARN_UNUSED_RESULT; | 446 WARN_UNUSED_RESULT; |
| 424 | 447 |
| 425 // Internal helper for const functions. Like GetUniqueStatement(), | 448 // Internal helper for const functions. Like GetUniqueStatement(), |
| 426 // except the statement is not entered into open_statements_, | 449 // except the statement is not entered into open_statements_, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 457 | 480 |
| 458 // True if any of the currently nested transactions have been rolled back. | 481 // True if any of the currently nested transactions have been rolled back. |
| 459 // When we get to the outermost transaction, this will determine if we do | 482 // When we get to the outermost transaction, this will determine if we do |
| 460 // a rollback instead of a commit. | 483 // a rollback instead of a commit. |
| 461 bool needs_rollback_; | 484 bool needs_rollback_; |
| 462 | 485 |
| 463 // True if database is open with OpenInMemory(), False if database is open | 486 // True if database is open with OpenInMemory(), False if database is open |
| 464 // with Open(). | 487 // with Open(). |
| 465 bool in_memory_; | 488 bool in_memory_; |
| 466 | 489 |
| 490 // |true| if the connection was closed using RazeAndClose(). Used |
| 491 // to enable diagnostics to distinguish calls to never-opened |
| 492 // databases (incorrect use of the API) from calls to once-valid |
| 493 // databases. |
| 494 bool poisoned_; |
| 495 |
| 467 // This object handles errors resulting from all forms of executing sqlite | 496 // This object handles errors resulting from all forms of executing sqlite |
| 468 // commands or statements. It can be null which means default handling. | 497 // commands or statements. It can be null which means default handling. |
| 469 scoped_ptr<ErrorDelegate> error_delegate_; | 498 scoped_ptr<ErrorDelegate> error_delegate_; |
| 470 | 499 |
| 471 // Auxiliary error-code histogram. | 500 // Auxiliary error-code histogram. |
| 472 std::string error_histogram_name_; | 501 std::string error_histogram_name_; |
| 473 | 502 |
| 474 DISALLOW_COPY_AND_ASSIGN(Connection); | 503 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 475 }; | 504 }; |
| 476 | 505 |
| 477 } // namespace sql | 506 } // namespace sql |
| 478 | 507 |
| 479 #endif // SQL_CONNECTION_H_ | 508 #endif // SQL_CONNECTION_H_ |
| OLD | NEW |