Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: sql/connection.h

Issue 11111021: Remove ref counting on sql::ErrorDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 ErrorDelegate();
84 85
86 virtual ~ErrorDelegate();
87
85 // |error| is an sqlite result code as seen in sqlite\preprocessed\sqlite3.h 88 // |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 89 // |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 90 // our best guess at the statement that triggered the error. Do not store
88 // these pointers. 91 // these pointers.
89 // 92 //
90 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on 93 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on
91 // initialization). 94 // initialization).
92 // 95 //
93 // If the error condition has been fixed an the original statement succesfuly 96 // If the error condition has been fixed an the original statement succesfuly
94 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended 97 // re-tried then returning SQLITE_OK is appropiate; otherwise is recomended
95 // that you return the original |error| or the appropiae error code. 98 // that you return the original |error| or the appropiae error code.
96 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0; 99 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0;
97 100
98 protected: 101 private:
99 friend class base::RefCounted<ErrorDelegate>; 102 DISALLOW_COPY_AND_ASSIGN(ErrorDelegate);
100
101 virtual ~ErrorDelegate();
102 }; 103 };
103 104
104 class SQL_EXPORT Connection { 105 class SQL_EXPORT Connection {
105 private: 106 private:
106 class StatementRef; // Forward declaration, see real one below. 107 class StatementRef; // Forward declaration, see real one below.
107 108
108 public: 109 public:
109 // The database is opened by calling Open[InMemory](). Any uncommitted 110 // The database is opened by calling Open[InMemory](). Any uncommitted
110 // transactions will be rolled back when this object is deleted. 111 // transactions will be rolled back when this object is deleted.
111 Connection(); 112 Connection();
(...skipping 23 matching lines...) Expand all
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 // 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 144 // before calling Open(). If not set, the default is to ignore errors on
144 // release and assert on debug builds. 145 // release and assert on debug builds.
146 // Takes ownership of |delegate|.
145 void set_error_delegate(ErrorDelegate* delegate) { 147 void set_error_delegate(ErrorDelegate* delegate) {
146 error_delegate_ = delegate; 148 error_delegate_.reset(delegate);
147 } 149 }
148 150
149 // Initialization ------------------------------------------------------------ 151 // Initialization ------------------------------------------------------------
150 152
151 // Initializes the SQL connection for the given file, returning true if the 153 // Initializes the SQL connection for the given file, returning true if the
152 // file could be opened. You can call this or OpenInMemory. 154 // file could be opened. You can call this or OpenInMemory.
153 bool Open(const FilePath& path) WARN_UNUSED_RESULT; 155 bool Open(const FilePath& path) WARN_UNUSED_RESULT;
154 156
155 // Initializes the SQL connection for a temporary in-memory database. There 157 // 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 158 // 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
438 // When we get to the outermost transaction, this will determine if we do 440 // When we get to the outermost transaction, this will determine if we do
439 // a rollback instead of a commit. 441 // a rollback instead of a commit.
440 bool needs_rollback_; 442 bool needs_rollback_;
441 443
442 // True if database is open with OpenInMemory(), False if database is open 444 // True if database is open with OpenInMemory(), False if database is open
443 // with Open(). 445 // with Open().
444 bool in_memory_; 446 bool in_memory_;
445 447
446 // This object handles errors resulting from all forms of executing sqlite 448 // This object handles errors resulting from all forms of executing sqlite
447 // commands or statements. It can be null which means default handling. 449 // commands or statements. It can be null which means default handling.
448 scoped_refptr<ErrorDelegate> error_delegate_; 450 scoped_ptr<ErrorDelegate> error_delegate_;
449 451
450 DISALLOW_COPY_AND_ASSIGN(Connection); 452 DISALLOW_COPY_AND_ASSIGN(Connection);
451 }; 453 };
452 454
453 } // namespace sql 455 } // namespace sql
454 456
455 #endif // SQL_CONNECTION_H_ 457 #endif // SQL_CONNECTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698