| 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/time.h" | 17 #include "base/time.h" |
| 17 #include "sql/sql_export.h" | 18 #include "sql/sql_export.h" |
| 18 | 19 |
| 19 class FilePath; | 20 class FilePath; |
| 20 struct sqlite3; | 21 struct sqlite3; |
| 21 struct sqlite3_stmt; | 22 struct sqlite3_stmt; |
| 22 | 23 |
| 23 namespace sql { | 24 namespace sql { |
| 24 | 25 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) | 72 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) |
| 72 | 73 |
| 73 class Connection; | 74 class Connection; |
| 74 | 75 |
| 75 // ErrorDelegate defines the interface to implement error handling and recovery | 76 // ErrorDelegate defines the interface to implement error handling and recovery |
| 76 // for sqlite operations. This allows the rest of the classes to return true or | 77 // for sqlite operations. This allows the rest of the classes to return true or |
| 77 // false while the actual error code and causing statement are delivered using | 78 // false while the actual error code and causing statement are delivered using |
| 78 // the OnError() callback. | 79 // the OnError() callback. |
| 79 // The tipical usage is to centralize the code designed to handle database | 80 // The tipical usage is to centralize the code designed to handle database |
| 80 // corruption, low-level IO errors or locking violations. | 81 // corruption, low-level IO errors or locking violations. |
| 81 class SQL_EXPORT ErrorDelegate : public base::RefCounted<ErrorDelegate> { | 82 class SQL_EXPORT ErrorDelegate { |
| 82 public: | 83 public: |
| 83 ErrorDelegate(); | 84 virtual ~ErrorDelegate(); |
| 84 | 85 |
| 85 // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h | 86 // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h |
| 86 // |connection| is db connection where the error happened and |stmt| is | 87 // |connection| is db connection where the error happened and |stmt| is |
| 87 // our best guess at the statement that triggered the error. Do not store | 88 // our best guess at the statement that triggered the error. Do not store |
| 88 // these pointers. | 89 // these pointers. |
| 89 // | 90 // |
| 90 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on | 91 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on |
| 91 // initialization). | 92 // initialization). |
| 92 // | 93 // |
| 93 // If the error condition has been fixed an the original statement succesfuly | 94 // If the error condition has been fixed an the original statement succesfuly |
| 94 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended | 95 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended |
| 95 // that you return the original |error| or the appropiae error code. | 96 // that you return the original |error| or the appropiae error code. |
| 96 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; | 97 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; |
| 97 | |
| 98 protected: | |
| 99 friend class base::RefCounted<ErrorDelegate>; | |
| 100 | |
| 101 virtual ~ErrorDelegate(); | |
| 102 }; | 98 }; |
| 103 | 99 |
| 104 class SQL_EXPORT Connection { | 100 class SQL_EXPORT Connection { |
| 105 private: | 101 private: |
| 106 class StatementRef; // Forward declaration, see real one below. | 102 class StatementRef; // Forward declaration, see real one below. |
| 107 | 103 |
| 108 public: | 104 public: |
| 109 // The database is opened by calling Open[InMemory](). Any uncommitted | 105 // The database is opened by calling Open[InMemory](). Any uncommitted |
| 110 // transactions will be rolled back when this object is deleted. | 106 // transactions will be rolled back when this object is deleted. |
| 111 Connection(); | 107 Connection(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 135 // Exclusive mode means that the database is not unlocked at the end of each | 131 // Exclusive mode means that the database is not unlocked at the end of each |
| 136 // transaction, which means there may be less time spent initializing the | 132 // transaction, which means there may be less time spent initializing the |
| 137 // next transaction because it doesn't have to re-aquire locks. | 133 // next transaction because it doesn't have to re-aquire locks. |
| 138 // | 134 // |
| 139 // This must be called before Open() to have an effect. | 135 // This must be called before Open() to have an effect. |
| 140 void set_exclusive_locking() { exclusive_locking_ = true; } | 136 void set_exclusive_locking() { exclusive_locking_ = true; } |
| 141 | 137 |
| 142 // Sets the object that will handle errors. Recomended that it should be set | 138 // Sets the object that will handle errors. Recomended that it should be set |
| 143 // before calling Open(). If not set, the default is to ignore errors on | 139 // before calling Open(). If not set, the default is to ignore errors on |
| 144 // release and assert on debug builds. | 140 // release and assert on debug builds. |
| 141 // Takes ownership of |delegate|. |
| 145 void set_error_delegate(ErrorDelegate* delegate) { | 142 void set_error_delegate(ErrorDelegate* delegate) { |
| 146 error_delegate_ = delegate; | 143 error_delegate_.reset(delegate); |
| 147 } | 144 } |
| 148 | 145 |
| 149 // Initialization ------------------------------------------------------------ | 146 // Initialization ------------------------------------------------------------ |
| 150 | 147 |
| 151 // Initializes the SQL connection for the given file, returning true if the | 148 // Initializes the SQL connection for the given file, returning true if the |
| 152 // file could be opened. You can call this or OpenInMemory. | 149 // file could be opened. You can call this or OpenInMemory. |
| 153 bool Open(const FilePath& path) WARN_UNUSED_RESULT; | 150 bool Open(const FilePath& path) WARN_UNUSED_RESULT; |
| 154 | 151 |
| 155 // Initializes the SQL connection for a temporary in-memory database. There | 152 // Initializes the SQL connection for a temporary in-memory database. There |
| 156 // will be no associated file on disk, and the initial database will be | 153 // will be no associated file on disk, and the initial database will be |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 // When we get to the outermost transaction, this will determine if we do | 435 // When we get to the outermost transaction, this will determine if we do |
| 439 // a rollback instead of a commit. | 436 // a rollback instead of a commit. |
| 440 bool needs_rollback_; | 437 bool needs_rollback_; |
| 441 | 438 |
| 442 // True if database is open with OpenInMemory(), False if database is open | 439 // True if database is open with OpenInMemory(), False if database is open |
| 443 // with Open(). | 440 // with Open(). |
| 444 bool in_memory_; | 441 bool in_memory_; |
| 445 | 442 |
| 446 // This object handles errors resulting from all forms of executing sqlite | 443 // This object handles errors resulting from all forms of executing sqlite |
| 447 // commands or statements. It can be null which means default handling. | 444 // commands or statements. It can be null which means default handling. |
| 448 scoped_refptr<ErrorDelegate> error_delegate_; | 445 scoped_ptr<ErrorDelegate> error_delegate_; |
| 449 | 446 |
| 450 DISALLOW_COPY_AND_ASSIGN(Connection); | 447 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 451 }; | 448 }; |
| 452 | 449 |
| 453 } // namespace sql | 450 } // namespace sql |
| 454 | 451 |
| 455 #endif // SQL_CONNECTION_H_ | 452 #endif // SQL_CONNECTION_H_ |
| OLD | NEW |