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 <stdint.h> | 8 #include <stdint.h> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "sql/sql_export.h" | 21 #include "sql/sql_export.h" |
22 | 22 |
23 struct sqlite3; | 23 struct sqlite3; |
24 struct sqlite3_stmt; | 24 struct sqlite3_stmt; |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 class FilePath; | 27 class FilePath; |
| 28 class HistogramBase; |
28 } | 29 } |
29 | 30 |
30 namespace sql { | 31 namespace sql { |
31 | 32 |
32 class Recovery; | 33 class Recovery; |
33 class Statement; | 34 class Statement; |
34 | 35 |
| 36 // To allow some test classes to be friended. |
| 37 namespace test { |
| 38 class ScopedScalarFunction; |
| 39 class ScopedCommitHook; |
| 40 } |
| 41 |
35 // Uniquely identifies a statement. There are two modes of operation: | 42 // Uniquely identifies a statement. There are two modes of operation: |
36 // | 43 // |
37 // - In the most common mode, you will use the source file and line number to | 44 // - In the most common mode, you will use the source file and line number to |
38 // identify your statement. This is a convienient way to get uniqueness for | 45 // identify your statement. This is a convienient way to get uniqueness for |
39 // a statement that is only used in one place. Use the SQL_FROM_HERE macro | 46 // a statement that is only used in one place. Use the SQL_FROM_HERE macro |
40 // to generate a StatementID. | 47 // to generate a StatementID. |
41 // | 48 // |
42 // - In the "custom" mode you may use the statement from different places or | 49 // - In the "custom" mode you may use the statement from different places or |
43 // need to manage it yourself for whatever reason. In this case, you should | 50 // need to manage it yourself for whatever reason. In this case, you should |
44 // make up your own unique name and pass it to the StatementID. This name | 51 // make up your own unique name and pass it to the StatementID. This name |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 void set_error_callback(const ErrorCallback& callback) { | 140 void set_error_callback(const ErrorCallback& callback) { |
134 error_callback_ = callback; | 141 error_callback_ = callback; |
135 } | 142 } |
136 bool has_error_callback() const { | 143 bool has_error_callback() const { |
137 return !error_callback_.is_null(); | 144 return !error_callback_.is_null(); |
138 } | 145 } |
139 void reset_error_callback() { | 146 void reset_error_callback() { |
140 error_callback_.Reset(); | 147 error_callback_.Reset(); |
141 } | 148 } |
142 | 149 |
143 // Set this tag to enable additional connection-type histogramming | 150 // Set this to enable additional per-connection histogramming. Must be called |
144 // for SQLite error codes and database version numbers. | 151 // before Open(). |
145 void set_histogram_tag(const std::string& tag) { | 152 void set_histogram_tag(const std::string& tag); |
146 histogram_tag_ = tag; | |
147 } | |
148 | 153 |
149 // Record a sparse UMA histogram sample under | 154 // Record a sparse UMA histogram sample under |
150 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no | 155 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no |
151 // histogram is recorded. | 156 // histogram is recorded. |
152 void AddTaggedHistogram(const std::string& name, size_t sample) const; | 157 void AddTaggedHistogram(const std::string& name, size_t sample) const; |
153 | 158 |
| 159 // Track various API calls and results. |
| 160 enum Events { |
| 161 // Number of statements run, either with sql::Statement or Execute*(). |
| 162 EVENT_STATEMENT_RUN = 0, |
| 163 |
| 164 // Number of rows returned by statements run. |
| 165 EVENT_STATEMENT_ROWS, |
| 166 |
| 167 // Number of statements successfully run (all steps returned SQLITE_DONE or |
| 168 // SQLITE_ROW). |
| 169 EVENT_STATEMENT_SUCCESS, |
| 170 |
| 171 // Number of statements run by Execute() or ExecuteAndReturnErrorCode(). |
| 172 EVENT_EXECUTE, |
| 173 |
| 174 // Number of rows changed by autocommit statements. |
| 175 EVENT_CHANGES_AUTOCOMMIT, |
| 176 |
| 177 // Number of rows changed by statements in transactions. |
| 178 EVENT_CHANGES, |
| 179 |
| 180 // Count actual SQLite transaction statements (not including nesting). |
| 181 EVENT_BEGIN, |
| 182 EVENT_COMMIT, |
| 183 EVENT_ROLLBACK, |
| 184 |
| 185 // Leave this at the end. |
| 186 // TODO(shess): |EVENT_MAX| causes compile fail on Windows. |
| 187 EVENT_MX |
| 188 }; |
| 189 void RecordEvent(Events event, size_t count); |
| 190 void RecordOneEvent(Events event) { |
| 191 RecordEvent(event, 1); |
| 192 } |
| 193 |
154 // Run "PRAGMA integrity_check" and post each line of | 194 // Run "PRAGMA integrity_check" and post each line of |
155 // results into |messages|. Returns the success of running the | 195 // results into |messages|. Returns the success of running the |
156 // statement - per the SQLite documentation, if no errors are found the | 196 // statement - per the SQLite documentation, if no errors are found the |
157 // call should succeed, and a single value "ok" should be in messages. | 197 // call should succeed, and a single value "ok" should be in messages. |
158 bool FullIntegrityCheck(std::vector<std::string>* messages); | 198 bool FullIntegrityCheck(std::vector<std::string>* messages); |
159 | 199 |
160 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method, | 200 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method, |
161 // interprets the results returning true if the the statement executes | 201 // interprets the results returning true if the the statement executes |
162 // without error and results in a single "ok" value. | 202 // without error and results in a single "ok" value. |
163 bool QuickIntegrityCheck() WARN_UNUSED_RESULT; | 203 bool QuickIntegrityCheck() WARN_UNUSED_RESULT; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 // For recovery module. | 448 // For recovery module. |
409 friend class Recovery; | 449 friend class Recovery; |
410 | 450 |
411 // Allow test-support code to set/reset error ignorer. | 451 // Allow test-support code to set/reset error ignorer. |
412 friend class ScopedErrorIgnorer; | 452 friend class ScopedErrorIgnorer; |
413 | 453 |
414 // Statement accesses StatementRef which we don't want to expose to everybody | 454 // Statement accesses StatementRef which we don't want to expose to everybody |
415 // (they should go through Statement). | 455 // (they should go through Statement). |
416 friend class Statement; | 456 friend class Statement; |
417 | 457 |
| 458 friend class test::ScopedScalarFunction; |
| 459 friend class test::ScopedCommitHook; |
| 460 |
418 // Internal initialize function used by both Init and InitInMemory. The file | 461 // Internal initialize function used by both Init and InitInMemory. The file |
419 // name is always 8 bits since we want to use the 8-bit version of | 462 // name is always 8 bits since we want to use the 8-bit version of |
420 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 463 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
421 // | 464 // |
422 // |retry_flag| controls retrying the open if the error callback | 465 // |retry_flag| controls retrying the open if the error callback |
423 // addressed errors using RazeAndClose(). | 466 // addressed errors using RazeAndClose(). |
424 enum Retry { | 467 enum Retry { |
425 NO_RETRY = 0, | 468 NO_RETRY = 0, |
426 RETRY_ON_POISON | 469 RETRY_ON_POISON |
427 }; | 470 }; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 // allowing this function to be const. Open statements can block | 584 // allowing this function to be const. Open statements can block |
542 // closing the database, so only use in cases where the last ref is | 585 // closing the database, so only use in cases where the last ref is |
543 // released before close could be called (which should always be the | 586 // released before close could be called (which should always be the |
544 // case for const functions). | 587 // case for const functions). |
545 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const; | 588 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const; |
546 | 589 |
547 bool IntegrityCheckHelper( | 590 bool IntegrityCheckHelper( |
548 const char* pragma_sql, | 591 const char* pragma_sql, |
549 std::vector<std::string>* messages) WARN_UNUSED_RESULT; | 592 std::vector<std::string>* messages) WARN_UNUSED_RESULT; |
550 | 593 |
| 594 // Record time spent executing explicit COMMIT statements. |
| 595 void CommitTime(const base::TimeDelta& delta); |
| 596 |
| 597 // Record time in DML (Data Manipulation Language) statements such as INSERT |
| 598 // or UPDATE outside of an explicit transaction. Due to implementation |
| 599 // limitations time spent on DDL (Data Definition Language) statements such as |
| 600 // ALTER and CREATE is not included. |
| 601 void AutoCommitTime(const base::TimeDelta& delta); |
| 602 |
| 603 // Record all time spent on updating the database. This includes CommitTime() |
| 604 // and AutoCommitTime(), plus any time spent spilling to the journal if |
| 605 // transactions do not fit in cache. |
| 606 void UpdateTime(const base::TimeDelta& delta); |
| 607 |
| 608 // Record all time spent running statements, including time spent doing |
| 609 // updates and time spent on read-only queries. |
| 610 void QueryTime(const base::TimeDelta& delta); |
| 611 |
| 612 // Record |delta| as query time if |read_only| (from sqlite3_stmt_readonly) is |
| 613 // true, autocommit time if the database is not in a transaction, or update |
| 614 // time if the database is in a transaction. Also records change count to |
| 615 // EVENT_CHANGES_AUTOCOMMIT or EVENT_CHANGES_COMMIT. |
| 616 void ChangeHelper(const base::TimeDelta& delta, bool read_only); |
| 617 |
551 // The actual sqlite database. Will be NULL before Init has been called or if | 618 // The actual sqlite database. Will be NULL before Init has been called or if |
552 // Init resulted in an error. | 619 // Init resulted in an error. |
553 sqlite3* db_; | 620 sqlite3* db_; |
554 | 621 |
555 // Parameters we'll configure in sqlite before doing anything else. Zero means | 622 // Parameters we'll configure in sqlite before doing anything else. Zero means |
556 // use the default value. | 623 // use the default value. |
557 int page_size_; | 624 int page_size_; |
558 int cache_size_; | 625 int cache_size_; |
559 bool exclusive_locking_; | 626 bool exclusive_locking_; |
560 bool restrict_to_user_; | 627 bool restrict_to_user_; |
(...skipping 26 matching lines...) Expand all Loading... |
587 // to enable diagnostics to distinguish calls to never-opened | 654 // to enable diagnostics to distinguish calls to never-opened |
588 // databases (incorrect use of the API) from calls to once-valid | 655 // databases (incorrect use of the API) from calls to once-valid |
589 // databases. | 656 // databases. |
590 bool poisoned_; | 657 bool poisoned_; |
591 | 658 |
592 ErrorCallback error_callback_; | 659 ErrorCallback error_callback_; |
593 | 660 |
594 // Tag for auxiliary histograms. | 661 // Tag for auxiliary histograms. |
595 std::string histogram_tag_; | 662 std::string histogram_tag_; |
596 | 663 |
| 664 // Linear histogram for RecordEvent(). |
| 665 base::HistogramBase* stats_histogram_; |
| 666 |
| 667 // Histogram for tracking time taken in commit. |
| 668 base::HistogramBase* commit_time_histogram_; |
| 669 |
| 670 // Histogram for tracking time taken in autocommit updates. |
| 671 base::HistogramBase* autocommit_time_histogram_; |
| 672 |
| 673 // Histogram for tracking time taken in updates (including commit and |
| 674 // autocommit). |
| 675 base::HistogramBase* update_time_histogram_; |
| 676 |
| 677 // Histogram for tracking time taken in all queries. |
| 678 base::HistogramBase* query_time_histogram_; |
| 679 |
597 DISALLOW_COPY_AND_ASSIGN(Connection); | 680 DISALLOW_COPY_AND_ASSIGN(Connection); |
598 }; | 681 }; |
599 | 682 |
600 } // namespace sql | 683 } // namespace sql |
601 | 684 |
602 #endif // SQL_CONNECTION_H_ | 685 #endif // SQL_CONNECTION_H_ |
OLD | NEW |