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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <map> | 10 #include <map> |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // | 143 // |
144 // This must be called before Open() to have an effect. | 144 // This must be called before Open() to have an effect. |
145 void set_exclusive_locking() { exclusive_locking_ = true; } | 145 void set_exclusive_locking() { exclusive_locking_ = true; } |
146 | 146 |
147 // Call to cause Open() to restrict access permissions of the | 147 // Call to cause Open() to restrict access permissions of the |
148 // database file to only the owner. | 148 // database file to only the owner. |
149 // TODO(shess): Currently only supported on OS_POSIX, is a noop on | 149 // TODO(shess): Currently only supported on OS_POSIX, is a noop on |
150 // other platforms. | 150 // other platforms. |
151 void set_restrict_to_user() { restrict_to_user_ = true; } | 151 void set_restrict_to_user() { restrict_to_user_ = true; } |
152 | 152 |
| 153 // Call to use alternative status-tracking for mmap. Usually this is tracked |
| 154 // in the meta table, but some databases have no meta table. |
| 155 // TODO(shess): Maybe just have all databases use the alt option? |
| 156 void set_mmap_alt_status() { mmap_alt_status_ = true; } |
| 157 |
153 // Call to opt out of memory-mapped file I/O. | 158 // Call to opt out of memory-mapped file I/O. |
154 void set_mmap_disabled() { mmap_disabled_ = true; } | 159 void set_mmap_disabled() { mmap_disabled_ = true; } |
155 | 160 |
156 // Set an error-handling callback. On errors, the error number (and | 161 // Set an error-handling callback. On errors, the error number (and |
157 // statement, if available) will be passed to the callback. | 162 // statement, if available) will be passed to the callback. |
158 // | 163 // |
159 // If no callback is set, the default action is to crash in debug | 164 // If no callback is set, the default action is to crash in debug |
160 // mode or return failure in release mode. | 165 // mode or return failure in release mode. |
161 typedef base::Callback<void(int, Statement*)> ErrorCallback; | 166 typedef base::Callback<void(int, Statement*)> ErrorCallback; |
162 void set_error_callback(const ErrorCallback& callback) { | 167 void set_error_callback(const ErrorCallback& callback) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 EVENT_MMAP_META_MISSING, // No meta table present. | 217 EVENT_MMAP_META_MISSING, // No meta table present. |
213 EVENT_MMAP_META_FAILURE_READ, // Failed reading meta table. | 218 EVENT_MMAP_META_FAILURE_READ, // Failed reading meta table. |
214 EVENT_MMAP_META_FAILURE_UPDATE, // Failed updating meta table. | 219 EVENT_MMAP_META_FAILURE_UPDATE, // Failed updating meta table. |
215 EVENT_MMAP_VFS_FAILURE, // Failed to access VFS. | 220 EVENT_MMAP_VFS_FAILURE, // Failed to access VFS. |
216 EVENT_MMAP_FAILED, // Failure from past run. | 221 EVENT_MMAP_FAILED, // Failure from past run. |
217 EVENT_MMAP_FAILED_NEW, // Read error in this run. | 222 EVENT_MMAP_FAILED_NEW, // Read error in this run. |
218 EVENT_MMAP_SUCCESS_NEW, // Read to EOF in this run. | 223 EVENT_MMAP_SUCCESS_NEW, // Read to EOF in this run. |
219 EVENT_MMAP_SUCCESS_PARTIAL, // Read but did not reach EOF. | 224 EVENT_MMAP_SUCCESS_PARTIAL, // Read but did not reach EOF. |
220 EVENT_MMAP_SUCCESS_NO_PROGRESS, // Read quota exhausted. | 225 EVENT_MMAP_SUCCESS_NO_PROGRESS, // Read quota exhausted. |
221 | 226 |
| 227 EVENT_MMAP_STATUS_FAILURE_READ, // Failure reading MmapStatus view. |
| 228 EVENT_MMAP_STATUS_FAILURE_UPDATE,// Failure updating MmapStatus view. |
| 229 |
222 // Leave this at the end. | 230 // Leave this at the end. |
223 // TODO(shess): |EVENT_MAX| causes compile fail on Windows. | 231 // TODO(shess): |EVENT_MAX| causes compile fail on Windows. |
224 EVENT_MAX_VALUE | 232 EVENT_MAX_VALUE |
225 }; | 233 }; |
226 void RecordEvent(Events event, size_t count); | 234 void RecordEvent(Events event, size_t count); |
227 void RecordOneEvent(Events event) { | 235 void RecordOneEvent(Events event) { |
228 RecordEvent(event, 1); | 236 RecordEvent(event, 1); |
229 } | 237 } |
230 | 238 |
231 // Run "PRAGMA integrity_check" and post each line of | 239 // Run "PRAGMA integrity_check" and post each line of |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 444 |
437 // Returns a non-cached statement for the given SQL. Use this for SQL that | 445 // Returns a non-cached statement for the given SQL. Use this for SQL that |
438 // is only executed once or only rarely (there is overhead associated with | 446 // is only executed once or only rarely (there is overhead associated with |
439 // keeping a statement cached). | 447 // keeping a statement cached). |
440 // | 448 // |
441 // See GetCachedStatement above for examples and error information. | 449 // See GetCachedStatement above for examples and error information. |
442 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql); | 450 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql); |
443 | 451 |
444 // Info querying ------------------------------------------------------------- | 452 // Info querying ------------------------------------------------------------- |
445 | 453 |
446 // Returns true if the given table (or index) exists. Instead of | 454 // Returns true if the given structure exists. Instead of test-then-create, |
447 // test-then-create, callers should almost always prefer "CREATE TABLE IF NOT | 455 // callers should almost always prefer the "IF NOT EXISTS" version of the |
448 // EXISTS" or "CREATE INDEX IF NOT EXISTS". | 456 // CREATE statement. |
| 457 bool DoesIndexExist(const char* index_name) const; |
449 bool DoesTableExist(const char* table_name) const; | 458 bool DoesTableExist(const char* table_name) const; |
450 bool DoesIndexExist(const char* index_name) const; | 459 bool DoesViewExist(const char* table_name) const; |
451 | 460 |
452 // Returns true if a column with the given name exists in the given table. | 461 // Returns true if a column with the given name exists in the given table. |
453 bool DoesColumnExist(const char* table_name, const char* column_name) const; | 462 bool DoesColumnExist(const char* table_name, const char* column_name) const; |
454 | 463 |
455 // Returns sqlite's internal ID for the last inserted row. Valid only | 464 // Returns sqlite's internal ID for the last inserted row. Valid only |
456 // immediately after an insert. | 465 // immediately after an insert. |
457 int64_t GetLastInsertRowId() const; | 466 int64_t GetLastInsertRowId() const; |
458 | 467 |
459 // Returns sqlite's count of the number of rows modified by the last | 468 // Returns sqlite's count of the number of rows modified by the last |
460 // statement executed. Will be 0 if no statement has executed or the database | 469 // statement executed. Will be 0 if no statement has executed or the database |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 // Statement accesses StatementRef which we don't want to expose to everybody | 510 // Statement accesses StatementRef which we don't want to expose to everybody |
502 // (they should go through Statement). | 511 // (they should go through Statement). |
503 friend class Statement; | 512 friend class Statement; |
504 | 513 |
505 friend class test::ScopedCommitHook; | 514 friend class test::ScopedCommitHook; |
506 friend class test::ScopedScalarFunction; | 515 friend class test::ScopedScalarFunction; |
507 friend class test::ScopedMockTimeSource; | 516 friend class test::ScopedMockTimeSource; |
508 | 517 |
509 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, CollectDiagnosticInfo); | 518 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, CollectDiagnosticInfo); |
510 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, GetAppropriateMmapSize); | 519 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, GetAppropriateMmapSize); |
| 520 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, GetAppropriateMmapSizeAltStatus); |
511 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, OnMemoryDump); | 521 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, OnMemoryDump); |
512 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, RegisterIntentToUpload); | 522 FRIEND_TEST_ALL_PREFIXES(SQLConnectionTest, RegisterIntentToUpload); |
513 | 523 |
514 // Internal initialize function used by both Init and InitInMemory. The file | 524 // Internal initialize function used by both Init and InitInMemory. The file |
515 // name is always 8 bits since we want to use the 8-bit version of | 525 // name is always 8 bits since we want to use the 8-bit version of |
516 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 526 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
517 // | 527 // |
518 // |retry_flag| controls retrying the open if the error callback | 528 // |retry_flag| controls retrying the open if the error callback |
519 // addressed errors using RazeAndClose(). | 529 // addressed errors using RazeAndClose(). |
520 enum Retry { | 530 enum Retry { |
521 NO_RETRY = 0, | 531 NO_RETRY = 0, |
522 RETRY_ON_POISON | 532 RETRY_ON_POISON |
523 }; | 533 }; |
524 bool OpenInternal(const std::string& file_name, Retry retry_flag); | 534 bool OpenInternal(const std::string& file_name, Retry retry_flag); |
525 | 535 |
526 // Internal close function used by Close() and RazeAndClose(). | 536 // Internal close function used by Close() and RazeAndClose(). |
527 // |forced| indicates that orderly-shutdown checks should not apply. | 537 // |forced| indicates that orderly-shutdown checks should not apply. |
528 void CloseInternal(bool forced); | 538 void CloseInternal(bool forced); |
529 | 539 |
530 // Check whether the current thread is allowed to make IO calls, but only | 540 // Check whether the current thread is allowed to make IO calls, but only |
531 // if database wasn't open in memory. Function is inlined to be a no-op in | 541 // if database wasn't open in memory. Function is inlined to be a no-op in |
532 // official build. | 542 // official build. |
533 void AssertIOAllowed() const { | 543 void AssertIOAllowed() const { |
534 if (!in_memory_) | 544 if (!in_memory_) |
535 base::ThreadRestrictions::AssertIOAllowed(); | 545 base::ThreadRestrictions::AssertIOAllowed(); |
536 } | 546 } |
537 | 547 |
538 // Internal helper for DoesTableExist and DoesIndexExist. | 548 // Internal helper for Does*Exist() functions. |
539 bool DoesTableOrIndexExist(const char* name, const char* type) const; | 549 bool DoesSchemaItemExist(const char* name, const char* type) const; |
540 | 550 |
541 // Accessors for global error-expecter, for injecting behavior during tests. | 551 // Accessors for global error-expecter, for injecting behavior during tests. |
542 // See test/scoped_error_expecter.h. | 552 // See test/scoped_error_expecter.h. |
543 typedef base::Callback<bool(int)> ErrorExpecterCallback; | 553 typedef base::Callback<bool(int)> ErrorExpecterCallback; |
544 static ErrorExpecterCallback* current_expecter_cb_; | 554 static ErrorExpecterCallback* current_expecter_cb_; |
545 static void SetErrorExpecter(ErrorExpecterCallback* expecter); | 555 static void SetErrorExpecter(ErrorExpecterCallback* expecter); |
546 static void ResetErrorExpecter(); | 556 static void ResetErrorExpecter(); |
547 | 557 |
548 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. | 558 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. |
549 // Refcounting allows us to give these statements out to sql::Statement | 559 // Refcounting allows us to give these statements out to sql::Statement |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 // Calculates a value appropriate to pass to "PRAGMA mmap_size = ". So errors | 725 // Calculates a value appropriate to pass to "PRAGMA mmap_size = ". So errors |
716 // can make it unsafe to map a file, so the file is read using regular I/O, | 726 // can make it unsafe to map a file, so the file is read using regular I/O, |
717 // with any errors causing 0 (don't map anything) to be returned. If the | 727 // with any errors causing 0 (don't map anything) to be returned. If the |
718 // entire file is read without error, a large value is returned which will | 728 // entire file is read without error, a large value is returned which will |
719 // allow the entire file to be mapped in most cases. | 729 // allow the entire file to be mapped in most cases. |
720 // | 730 // |
721 // Results are recorded in the database's meta table for future reference, so | 731 // Results are recorded in the database's meta table for future reference, so |
722 // the file should only be read through once. | 732 // the file should only be read through once. |
723 size_t GetAppropriateMmapSize(); | 733 size_t GetAppropriateMmapSize(); |
724 | 734 |
| 735 // Helpers for GetAppropriateMmapSize(). |
| 736 bool GetMmapAltStatus(int64_t* status); |
| 737 bool SetMmapAltStatus(int64_t status); |
| 738 |
725 // The actual sqlite database. Will be NULL before Init has been called or if | 739 // The actual sqlite database. Will be NULL before Init has been called or if |
726 // Init resulted in an error. | 740 // Init resulted in an error. |
727 sqlite3* db_; | 741 sqlite3* db_; |
728 | 742 |
729 // Parameters we'll configure in sqlite before doing anything else. Zero means | 743 // Parameters we'll configure in sqlite before doing anything else. Zero means |
730 // use the default value. | 744 // use the default value. |
731 int page_size_; | 745 int page_size_; |
732 int cache_size_; | 746 int cache_size_; |
733 bool exclusive_locking_; | 747 bool exclusive_locking_; |
734 bool restrict_to_user_; | 748 bool restrict_to_user_; |
(...skipping 21 matching lines...) Expand all Loading... |
756 // True if database is open with OpenInMemory(), False if database is open | 770 // True if database is open with OpenInMemory(), False if database is open |
757 // with Open(). | 771 // with Open(). |
758 bool in_memory_; | 772 bool in_memory_; |
759 | 773 |
760 // |true| if the connection was closed using RazeAndClose(). Used | 774 // |true| if the connection was closed using RazeAndClose(). Used |
761 // to enable diagnostics to distinguish calls to never-opened | 775 // to enable diagnostics to distinguish calls to never-opened |
762 // databases (incorrect use of the API) from calls to once-valid | 776 // databases (incorrect use of the API) from calls to once-valid |
763 // databases. | 777 // databases. |
764 bool poisoned_; | 778 bool poisoned_; |
765 | 779 |
| 780 // |true| to use alternate storage for tracking mmap status. |
| 781 bool mmap_alt_status_; |
| 782 |
766 // |true| if SQLite memory-mapped I/O is not desired for this connection. | 783 // |true| if SQLite memory-mapped I/O is not desired for this connection. |
767 bool mmap_disabled_; | 784 bool mmap_disabled_; |
768 | 785 |
769 // |true| if SQLite memory-mapped I/O was enabled for this connection. | 786 // |true| if SQLite memory-mapped I/O was enabled for this connection. |
770 // Used by ReleaseCacheMemoryIfNeeded(). | 787 // Used by ReleaseCacheMemoryIfNeeded(). |
771 bool mmap_enabled_; | 788 bool mmap_enabled_; |
772 | 789 |
773 // Used by ReleaseCacheMemoryIfNeeded() to track if new changes have happened | 790 // Used by ReleaseCacheMemoryIfNeeded() to track if new changes have happened |
774 // since memory was last released. | 791 // since memory was last released. |
775 int total_changes_at_last_release_; | 792 int total_changes_at_last_release_; |
(...skipping 25 matching lines...) Expand all Loading... |
801 | 818 |
802 // Stores the dump provider object when db is open. | 819 // Stores the dump provider object when db is open. |
803 std::unique_ptr<ConnectionMemoryDumpProvider> memory_dump_provider_; | 820 std::unique_ptr<ConnectionMemoryDumpProvider> memory_dump_provider_; |
804 | 821 |
805 DISALLOW_COPY_AND_ASSIGN(Connection); | 822 DISALLOW_COPY_AND_ASSIGN(Connection); |
806 }; | 823 }; |
807 | 824 |
808 } // namespace sql | 825 } // namespace sql |
809 | 826 |
810 #endif // SQL_CONNECTION_H_ | 827 #endif // SQL_CONNECTION_H_ |
OLD | NEW |