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 #include <vector> |
11 | 12 |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.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; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 // transaition (requires another access to the DB) and because we don't | 133 // transaition (requires another access to the DB) and because we don't |
133 // actually need it. | 134 // actually need it. |
134 // | 135 // |
135 // Exclusive mode means that the database is not unlocked at the end of each | 136 // 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 | 137 // transaction, which means there may be less time spent initializing the |
137 // next transaction because it doesn't have to re-aquire locks. | 138 // next transaction because it doesn't have to re-aquire locks. |
138 // | 139 // |
139 // This must be called before Open() to have an effect. | 140 // This must be called before Open() to have an effect. |
140 void set_exclusive_locking() { exclusive_locking_ = true; } | 141 void set_exclusive_locking() { exclusive_locking_ = true; } |
141 | 142 |
142 // Sets the object that will handle errors. Recomended that it should be set | 143 // Setup objects to handle errors. SQLite errors can occur as early |
143 // before calling Open(). If not set, the default is to ignore errors on | 144 // as Open() and as late as Close(). If no handlers are set, errors |
144 // release and assert on debug builds. | 145 // are ignored in release builds and crash in debug builds (in |
| 146 // development, SQLite errors almost always mean an error in code). |
| 147 // Returning SQLITE_OK from a delegate terminates error delegation. |
| 148 void add_error_delegate(ErrorDelegate* delegate) { |
| 149 error_delegates_.push_back(delegate); |
| 150 } |
| 151 void remove_error_delegate(ErrorDelegate* delegate) { |
| 152 std::vector<scoped_refptr<ErrorDelegate> >::iterator iter = |
| 153 error_delegates_.begin(); |
| 154 while (iter != error_delegates_.end()) { |
| 155 if (*iter == delegate) { |
| 156 iter = error_delegates_.erase(iter); |
| 157 } else { |
| 158 ++iter; |
| 159 } |
| 160 } |
| 161 } |
| 162 void clear_error_delegates() { |
| 163 error_delegates_.clear(); |
| 164 } |
| 165 |
| 166 // Previously there could only be one delegate. Retain this for |
| 167 // compatibility. |
145 void set_error_delegate(ErrorDelegate* delegate) { | 168 void set_error_delegate(ErrorDelegate* delegate) { |
146 error_delegate_ = delegate; | 169 clear_error_delegates(); |
| 170 add_error_delegate(delegate); |
147 } | 171 } |
148 | 172 |
149 // Initialization ------------------------------------------------------------ | 173 // Initialization ------------------------------------------------------------ |
150 | 174 |
151 // Initializes the SQL connection for the given file, returning true if the | 175 // Initializes the SQL connection for the given file, returning true if the |
152 // file could be opened. You can call this or OpenInMemory. | 176 // file could be opened. You can call this or OpenInMemory. |
153 bool Open(const FilePath& path) WARN_UNUSED_RESULT; | 177 bool Open(const FilePath& path) WARN_UNUSED_RESULT; |
154 | 178 |
155 // Initializes the SQL connection for a temporary in-memory database. There | 179 // 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 | 180 // will be no associated file on disk, and the initial database will be |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 460 |
437 // True if any of the currently nested transactions have been rolled back. | 461 // True if any of the currently nested transactions have been rolled back. |
438 // When we get to the outermost transaction, this will determine if we do | 462 // When we get to the outermost transaction, this will determine if we do |
439 // a rollback instead of a commit. | 463 // a rollback instead of a commit. |
440 bool needs_rollback_; | 464 bool needs_rollback_; |
441 | 465 |
442 // True if database is open with OpenInMemory(), False if database is open | 466 // True if database is open with OpenInMemory(), False if database is open |
443 // with Open(). | 467 // with Open(). |
444 bool in_memory_; | 468 bool in_memory_; |
445 | 469 |
446 // This object handles errors resulting from all forms of executing sqlite | 470 // These objects handle errors resulting from all forms of executing sqlite |
447 // commands or statements. It can be null which means default handling. | 471 // commands or statements. It can be empty which means default handling. |
448 scoped_refptr<ErrorDelegate> error_delegate_; | 472 std::vector<scoped_refptr<ErrorDelegate> > error_delegates_; |
449 | 473 |
450 DISALLOW_COPY_AND_ASSIGN(Connection); | 474 DISALLOW_COPY_AND_ASSIGN(Connection); |
451 }; | 475 }; |
452 | 476 |
453 } // namespace sql | 477 } // namespace sql |
454 | 478 |
455 #endif // SQL_CONNECTION_H_ | 479 #endif // SQL_CONNECTION_H_ |
OLD | NEW |