| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 int number_; | 73 int number_; |
| 74 const char* str_; | 74 const char* str_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) | 77 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) |
| 78 | 78 |
| 79 class Connection; | 79 class Connection; |
| 80 | 80 |
| 81 // ErrorDelegate defines the interface to implement error handling and recovery | |
| 82 // for sqlite operations. This allows the rest of the classes to return true or | |
| 83 // false while the actual error code and causing statement are delivered using | |
| 84 // the OnError() callback. | |
| 85 // The tipical usage is to centralize the code designed to handle database | |
| 86 // corruption, low-level IO errors or locking violations. | |
| 87 class SQL_EXPORT ErrorDelegate { | |
| 88 public: | |
| 89 virtual ~ErrorDelegate(); | |
| 90 | |
| 91 // |error| is an sqlite result code as seen in sqlite3.h. |connection| is the | |
| 92 // db connection where the error happened and |stmt| is our best guess at the | |
| 93 // statement that triggered the error. Do not store these pointers. | |
| 94 // | |
| 95 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on | |
| 96 // initialization). | |
| 97 // | |
| 98 // If the error condition has been fixed and the original statement succesfuly | |
| 99 // re-tried then returning SQLITE_OK is appropriate; otherwise it is | |
| 100 // recommended that you return the original |error| or the appropriate error | |
| 101 // code. | |
| 102 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; | |
| 103 }; | |
| 104 | |
| 105 class SQL_EXPORT Connection { | 81 class SQL_EXPORT Connection { |
| 106 private: | 82 private: |
| 107 class StatementRef; // Forward declaration, see real one below. | 83 class StatementRef; // Forward declaration, see real one below. |
| 108 | 84 |
| 109 public: | 85 public: |
| 110 // The database is opened by calling Open[InMemory](). Any uncommitted | 86 // The database is opened by calling Open[InMemory](). Any uncommitted |
| 111 // transactions will be rolled back when this object is deleted. | 87 // transactions will be rolled back when this object is deleted. |
| 112 Connection(); | 88 Connection(); |
| 113 ~Connection(); | 89 ~Connection(); |
| 114 | 90 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 138 // next transaction because it doesn't have to re-aquire locks. | 114 // next transaction because it doesn't have to re-aquire locks. |
| 139 // | 115 // |
| 140 // This must be called before Open() to have an effect. | 116 // This must be called before Open() to have an effect. |
| 141 void set_exclusive_locking() { exclusive_locking_ = true; } | 117 void set_exclusive_locking() { exclusive_locking_ = true; } |
| 142 | 118 |
| 143 // Set an error-handling callback. On errors, the error number (and | 119 // Set an error-handling callback. On errors, the error number (and |
| 144 // statement, if available) will be passed to the callback. | 120 // statement, if available) will be passed to the callback. |
| 145 // | 121 // |
| 146 // If no callback is set, the default action is to crash in debug | 122 // If no callback is set, the default action is to crash in debug |
| 147 // mode or return failure in release mode. | 123 // mode or return failure in release mode. |
| 148 // | |
| 149 // TODO(shess): ErrorDelegate allowed for returning a different | |
| 150 // error. Determine if this is necessary for the callback. In my | |
| 151 // experience, this is not well-tested and probably not safe, and | |
| 152 // current clients always return the same error passed. | |
| 153 // Additionally, most errors don't admit to a clean way to retry the | |
| 154 // failed operation, so converting an error to SQLITE_OK is probably | |
| 155 // not feasible. | |
| 156 typedef base::Callback<void(int, Statement*)> ErrorCallback; | 124 typedef base::Callback<void(int, Statement*)> ErrorCallback; |
| 157 void set_error_callback(const ErrorCallback& callback) { | 125 void set_error_callback(const ErrorCallback& callback) { |
| 158 error_callback_ = callback; | 126 error_callback_ = callback; |
| 159 } | 127 } |
| 160 void reset_error_callback() { | 128 void reset_error_callback() { |
| 161 error_callback_.Reset(); | 129 error_callback_.Reset(); |
| 162 } | 130 } |
| 163 | 131 |
| 164 // Sets the object that will handle errors. Recomended that it should be set | |
| 165 // before calling Open(). If not set, the default is to ignore errors on | |
| 166 // release and assert on debug builds. | |
| 167 // Takes ownership of |delegate|. | |
| 168 // NOTE(shess): Deprecated, use set_error_callback(). | |
| 169 void set_error_delegate(ErrorDelegate* delegate) { | |
| 170 error_delegate_.reset(delegate); | |
| 171 } | |
| 172 | |
| 173 // Set this tag to enable additional connection-type histogramming | 132 // Set this tag to enable additional connection-type histogramming |
| 174 // for SQLite error codes and database version numbers. | 133 // for SQLite error codes and database version numbers. |
| 175 void set_histogram_tag(const std::string& tag) { | 134 void set_histogram_tag(const std::string& tag) { |
| 176 histogram_tag_ = tag; | 135 histogram_tag_ = tag; |
| 177 } | 136 } |
| 178 | 137 |
| 179 // Record a sparse UMA histogram sample under | 138 // Record a sparse UMA histogram sample under |
| 180 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no | 139 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no |
| 181 // histogram is recorded. | 140 // histogram is recorded. |
| 182 void AddTaggedHistogram(const std::string& name, size_t sample) const; | 141 void AddTaggedHistogram(const std::string& name, size_t sample) const; |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 bool in_memory_; | 481 bool in_memory_; |
| 523 | 482 |
| 524 // |true| if the connection was closed using RazeAndClose(). Used | 483 // |true| if the connection was closed using RazeAndClose(). Used |
| 525 // to enable diagnostics to distinguish calls to never-opened | 484 // to enable diagnostics to distinguish calls to never-opened |
| 526 // databases (incorrect use of the API) from calls to once-valid | 485 // databases (incorrect use of the API) from calls to once-valid |
| 527 // databases. | 486 // databases. |
| 528 bool poisoned_; | 487 bool poisoned_; |
| 529 | 488 |
| 530 ErrorCallback error_callback_; | 489 ErrorCallback error_callback_; |
| 531 | 490 |
| 532 // This object handles errors resulting from all forms of executing sqlite | |
| 533 // commands or statements. It can be null which means default handling. | |
| 534 scoped_ptr<ErrorDelegate> error_delegate_; | |
| 535 | |
| 536 // Tag for auxiliary histograms. | 491 // Tag for auxiliary histograms. |
| 537 std::string histogram_tag_; | 492 std::string histogram_tag_; |
| 538 | 493 |
| 539 DISALLOW_COPY_AND_ASSIGN(Connection); | 494 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 540 }; | 495 }; |
| 541 | 496 |
| 542 } // namespace sql | 497 } // namespace sql |
| 543 | 498 |
| 544 #endif // SQL_CONNECTION_H_ | 499 #endif // SQL_CONNECTION_H_ |
| OLD | NEW |