| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 class Connection; | 69 class Connection; |
| 70 | 70 |
| 71 // ErrorDelegate defines the interface to implement error handling and recovery | 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 | 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 | 73 // false while the actual error code and causing statement are delivered using |
| 74 // the OnError() callback. | 74 // the OnError() callback. |
| 75 // The tipical usage is to centralize the code designed to handle database | 75 // The tipical usage is to centralize the code designed to handle database |
| 76 // corruption, low-level IO errors or locking violations. | 76 // corruption, low-level IO errors or locking violations. |
| 77 class ErrorDelegate : public base::RefCounted<ErrorDelegate> { | 77 class ErrorDelegate : public base::RefCounted<ErrorDelegate> { |
| 78 public: | 78 public: |
| 79 virtual ~ErrorDelegate() {} | |
| 80 // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h | 79 // |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 | 80 // |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 | 81 // our best guess at the statement that triggered the error. Do not store |
| 83 // these pointers. | 82 // these pointers. |
| 84 // | 83 // |
| 85 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on | 84 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on |
| 86 // initialization). | 85 // initialization). |
| 87 // | 86 // |
| 88 // If the error condition has been fixed an the original statement succesfuly | 87 // If the error condition has been fixed an the original statement succesfuly |
| 89 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended | 88 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended |
| 90 // that you return the original |error| or the appropiae error code. | 89 // that you return the original |error| or the appropiae error code. |
| 91 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; | 90 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; |
| 91 |
| 92 protected: |
| 93 friend class base::RefCounted<ErrorDelegate>; |
| 94 |
| 95 virtual ~ErrorDelegate() {} |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 class Connection { | 98 class Connection { |
| 95 private: | 99 private: |
| 96 class StatementRef; // Forward declaration, see real one below. | 100 class StatementRef; // Forward declaration, see real one below. |
| 97 | 101 |
| 98 public: | 102 public: |
| 99 // The database is opened by calling Open[InMemory](). Any uncommitted | 103 // The database is opened by calling Open[InMemory](). Any uncommitted |
| 100 // transactions will be rolled back when this object is deleted. | 104 // transactions will be rolled back when this object is deleted. |
| 101 Connection(); | 105 Connection(); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // indicate that the statement hasn't been created yet, has an error, or has | 283 // indicate that the statement hasn't been created yet, has an error, or has |
| 280 // been destroyed. | 284 // been destroyed. |
| 281 // | 285 // |
| 282 // The Connection may revoke a StatementRef in some error cases, so callers | 286 // The Connection may revoke a StatementRef in some error cases, so callers |
| 283 // should always check validity before using. | 287 // should always check validity before using. |
| 284 class StatementRef : public base::RefCounted<StatementRef> { | 288 class StatementRef : public base::RefCounted<StatementRef> { |
| 285 public: | 289 public: |
| 286 // Default constructor initializes to an invalid statement. | 290 // Default constructor initializes to an invalid statement. |
| 287 StatementRef(); | 291 StatementRef(); |
| 288 StatementRef(Connection* connection, sqlite3_stmt* stmt); | 292 StatementRef(Connection* connection, sqlite3_stmt* stmt); |
| 289 ~StatementRef(); | |
| 290 | 293 |
| 291 // When true, the statement can be used. | 294 // When true, the statement can be used. |
| 292 bool is_valid() const { return !!stmt_; } | 295 bool is_valid() const { return !!stmt_; } |
| 293 | 296 |
| 294 // If we've not been linked to a connection, this will be NULL. Guaranteed | 297 // If we've not been linked to a connection, this will be NULL. Guaranteed |
| 295 // non-NULL when is_valid(). | 298 // non-NULL when is_valid(). |
| 296 Connection* connection() const { return connection_; } | 299 Connection* connection() const { return connection_; } |
| 297 | 300 |
| 298 // Returns the sqlite statement if any. If the statement is not active, | 301 // Returns the sqlite statement if any. If the statement is not active, |
| 299 // this will return NULL. | 302 // this will return NULL. |
| 300 sqlite3_stmt* stmt() const { return stmt_; } | 303 sqlite3_stmt* stmt() const { return stmt_; } |
| 301 | 304 |
| 302 // Destroys the compiled statement and marks it NULL. The statement will | 305 // Destroys the compiled statement and marks it NULL. The statement will |
| 303 // no longer be active. | 306 // no longer be active. |
| 304 void Close(); | 307 void Close(); |
| 305 | 308 |
| 306 private: | 309 private: |
| 310 friend class base::RefCounted<StatementRef>; |
| 311 |
| 312 ~StatementRef(); |
| 313 |
| 307 Connection* connection_; | 314 Connection* connection_; |
| 308 sqlite3_stmt* stmt_; | 315 sqlite3_stmt* stmt_; |
| 309 | 316 |
| 310 DISALLOW_COPY_AND_ASSIGN(StatementRef); | 317 DISALLOW_COPY_AND_ASSIGN(StatementRef); |
| 311 }; | 318 }; |
| 312 friend class StatementRef; | 319 friend class StatementRef; |
| 313 | 320 |
| 314 // Executes a rollback statement, ignoring all transaction state. Used | 321 // Executes a rollback statement, ignoring all transaction state. Used |
| 315 // internally in the transaction management code. | 322 // internally in the transaction management code. |
| 316 void DoRollback(); | 323 void DoRollback(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 // This object handles errors resulting from all forms of executing sqlite | 367 // This object handles errors resulting from all forms of executing sqlite |
| 361 // commands or statements. It can be null which means default handling. | 368 // commands or statements. It can be null which means default handling. |
| 362 scoped_refptr<ErrorDelegate> error_delegate_; | 369 scoped_refptr<ErrorDelegate> error_delegate_; |
| 363 | 370 |
| 364 DISALLOW_COPY_AND_ASSIGN(Connection); | 371 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 365 }; | 372 }; |
| 366 | 373 |
| 367 } // namespace sql | 374 } // namespace sql |
| 368 | 375 |
| 369 #endif // APP_SQL_CONNECTION_H_ | 376 #endif // APP_SQL_CONNECTION_H_ |
| OLD | NEW |