OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 APP_SQL_CONNECTION_H_ | 5 #ifndef APP_SQL_CONNECTION_H_ |
6 #define APP_SQL_CONNECTION_H_ | 6 #define APP_SQL_CONNECTION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // We need this to insert into our map. | 59 // We need this to insert into our map. |
60 bool operator<(const StatementID& other) const; | 60 bool operator<(const StatementID& other) const; |
61 | 61 |
62 private: | 62 private: |
63 int number_; | 63 int number_; |
64 const char* str_; | 64 const char* str_; |
65 }; | 65 }; |
66 | 66 |
67 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) | 67 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) |
68 | 68 |
| 69 class Connection; |
| 70 |
| 71 // ErrorDelegate defines the interface to implement error handling and recovery |
| 72 // for sqlite operations. This allows the rest of the classes to return true or |
| 73 // false while the actual error code and causing statement are delivered using |
| 74 // the OnError() callback. |
| 75 // The tipical usage is to centralize the code designed to handle database |
| 76 // corruption, low-level IO errors or locking violations. |
| 77 class ErrorDelegate : public base::RefCounted<ErrorDelegate> { |
| 78 public: |
| 79 virtual ~ErrorDelegate() {} |
| 80 // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h |
| 81 // |connection| is db connection where the error happened and |stmt| is |
| 82 // our best guess at the statement that triggered the error. Do not store |
| 83 // these pointers. |
| 84 // If the error condition has been fixed an the original statement succesfuly |
| 85 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended |
| 86 // that you return the original |error| or the appropiae error code. |
| 87 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; |
| 88 }; |
| 89 |
69 class Connection { | 90 class Connection { |
70 private: | 91 private: |
71 class StatementRef; // Forward declaration, see real one below. | 92 class StatementRef; // Forward declaration, see real one below. |
72 | 93 |
73 public: | 94 public: |
74 // The database is opened by calling Init(). Any uncommitted transactions | 95 // The database is opened by calling Init(). Any uncommitted transactions |
75 // will be rolled back when this object is deleted. | 96 // will be rolled back when this object is deleted. |
76 Connection(); | 97 Connection(); |
77 ~Connection(); | 98 ~Connection(); |
78 | 99 |
(...skipping 18 matching lines...) Expand all Loading... |
97 // transaition (requires another access to the DB) and because we don't | 118 // transaition (requires another access to the DB) and because we don't |
98 // actually need it. | 119 // actually need it. |
99 // | 120 // |
100 // Exclusive mode means that the database is not unlocked at the end of each | 121 // Exclusive mode means that the database is not unlocked at the end of each |
101 // transaction, which means there may be less time spent initializing the | 122 // transaction, which means there may be less time spent initializing the |
102 // next transaction because it doesn't have to re-aquire locks. | 123 // next transaction because it doesn't have to re-aquire locks. |
103 // | 124 // |
104 // This must be called before Init() to have an effect. | 125 // This must be called before Init() to have an effect. |
105 void set_exclusive_locking() { exclusive_locking_ = true; } | 126 void set_exclusive_locking() { exclusive_locking_ = true; } |
106 | 127 |
| 128 // Sets the object that will handle errors. Recomended that it should be set |
| 129 // before calling Init(). If not set, the default is to ignore errors on |
| 130 // release and assert on debug builds. |
| 131 void set_error_delegate(ErrorDelegate* delegate) { |
| 132 error_delegate_ = delegate; |
| 133 } |
| 134 |
107 // Initialization ------------------------------------------------------------ | 135 // Initialization ------------------------------------------------------------ |
108 | 136 |
109 // Initializes the SQL connection for the given file, returning true if the | 137 // Initializes the SQL connection for the given file, returning true if the |
110 // file could be opened. | 138 // file could be opened. |
111 bool Init(const FilePath& path); | 139 bool Init(const FilePath& path); |
112 | 140 |
113 // Closes the database. This is automatically performed on destruction for | 141 // Closes the database. This is automatically performed on destruction for |
114 // you, but this allows you to close the database early. You must not call | 142 // you, but this allows you to close the database early. You must not call |
115 // any other functions after closing it. It is permissable to call Close on | 143 // any other functions after closing it. It is permissable to call Close on |
116 // an uninitialized or already-closed database. | 144 // an uninitialized or already-closed database. |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 void DoRollback(); | 299 void DoRollback(); |
272 | 300 |
273 // Called by a StatementRef when it's being created or destroyed. See | 301 // Called by a StatementRef when it's being created or destroyed. See |
274 // open_statements_ below. | 302 // open_statements_ below. |
275 void StatementRefCreated(StatementRef* ref); | 303 void StatementRefCreated(StatementRef* ref); |
276 void StatementRefDeleted(StatementRef* ref); | 304 void StatementRefDeleted(StatementRef* ref); |
277 | 305 |
278 // Frees all cached statements from statement_cache_. | 306 // Frees all cached statements from statement_cache_. |
279 void ClearCache(); | 307 void ClearCache(); |
280 | 308 |
| 309 // Called by Statement objects when an sqlite function returns an error. |
| 310 // The return value is the error code reflected back to client code. |
| 311 int OnSqliteError(int err, Statement* stmt); |
| 312 |
281 // The actual sqlite database. Will be NULL before Init has been called or if | 313 // The actual sqlite database. Will be NULL before Init has been called or if |
282 // Init resulted in an error. | 314 // Init resulted in an error. |
283 sqlite3* db_; | 315 sqlite3* db_; |
284 | 316 |
285 // Parameters we'll configure in sqlite before doing anything else. Zero means | 317 // Parameters we'll configure in sqlite before doing anything else. Zero means |
286 // use the default value. | 318 // use the default value. |
287 int page_size_; | 319 int page_size_; |
288 int cache_size_; | 320 int cache_size_; |
289 bool exclusive_locking_; | 321 bool exclusive_locking_; |
290 | 322 |
(...skipping 10 matching lines...) Expand all Loading... |
301 StatementRefSet open_statements_; | 333 StatementRefSet open_statements_; |
302 | 334 |
303 // Number of currently-nested transactions. | 335 // Number of currently-nested transactions. |
304 int transaction_nesting_; | 336 int transaction_nesting_; |
305 | 337 |
306 // True if any of the currently nested transactions have been rolled back. | 338 // True if any of the currently nested transactions have been rolled back. |
307 // When we get to the outermost transaction, this will determine if we do | 339 // When we get to the outermost transaction, this will determine if we do |
308 // a rollback instead of a commit. | 340 // a rollback instead of a commit. |
309 bool needs_rollback_; | 341 bool needs_rollback_; |
310 | 342 |
| 343 // This object handles errors resulting from all forms of executing sqlite |
| 344 // commands or statements. It can be null which means default handling. |
| 345 scoped_refptr<ErrorDelegate> error_delegate_; |
| 346 |
311 DISALLOW_COPY_AND_ASSIGN(Connection); | 347 DISALLOW_COPY_AND_ASSIGN(Connection); |
312 }; | 348 }; |
313 | 349 |
314 } // namespace sql | 350 } // namespace sql |
315 | 351 |
316 #endif // APP_SQL_CONNECTION_H_ | 352 #endif // APP_SQL_CONNECTION_H_ |
OLD | NEW |