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

Side by Side Diff: sql/statement.h

Issue 8899012: Put debugging assertions into sql::Statement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Oops, Execute() should stay loose. Created 9 years 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
« no previous file with comments | « sql/sqlite_features_unittest.cc ('k') | sql/statement.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_STATEMENT_H_ 5 #ifndef SQL_STATEMENT_H_
6 #define SQL_STATEMENT_H_ 6 #define SQL_STATEMENT_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 11 matching lines...) Expand all
22 enum ColType { 22 enum ColType {
23 COLUMN_TYPE_INTEGER = 1, 23 COLUMN_TYPE_INTEGER = 1,
24 COLUMN_TYPE_FLOAT = 2, 24 COLUMN_TYPE_FLOAT = 2,
25 COLUMN_TYPE_TEXT = 3, 25 COLUMN_TYPE_TEXT = 3,
26 COLUMN_TYPE_BLOB = 4, 26 COLUMN_TYPE_BLOB = 4,
27 COLUMN_TYPE_NULL = 5, 27 COLUMN_TYPE_NULL = 5,
28 }; 28 };
29 29
30 // Normal usage: 30 // Normal usage:
31 // sql::Statement s(connection_.GetUniqueStatement(...)); 31 // sql::Statement s(connection_.GetUniqueStatement(...));
32 // if (!s) // You should check for errors before using the statement.
33 // return false;
34 //
35 // s.BindInt(0, a); 32 // s.BindInt(0, a);
36 // if (s.Step()) 33 // if (s.Step())
37 // return s.ColumnString(0); 34 // return s.ColumnString(0);
38 // 35 //
36 // If there are errors getting the statement, the statement will be inert; no
37 // mutating or database-access methods will work. If you need to check for
38 // validity, use:
39 // if (!s.is_valid())
40 // return false;
41 //
39 // Step() and Run() just return true to signal success. If you want to handle 42 // Step() and Run() just return true to signal success. If you want to handle
40 // specific errors such as database corruption, install an error handler in 43 // specific errors such as database corruption, install an error handler in
41 // in the connection object using set_error_delegate(). 44 // in the connection object using set_error_delegate().
42 class SQL_EXPORT Statement { 45 class SQL_EXPORT Statement {
43 public: 46 public:
44 // Creates an uninitialized statement. The statement will be invalid until 47 // Creates an uninitialized statement. The statement will be invalid until
45 // you initialize it via Assign. 48 // you initialize it via Assign.
46 Statement(); 49 Statement();
47 50
48 explicit Statement(scoped_refptr<Connection::StatementRef> ref); 51 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
49 ~Statement(); 52 ~Statement();
50 53
51 // Initializes this object with the given statement, which may or may not 54 // Initializes this object with the given statement, which may or may not
52 // be valid. Use is_valid() to check if it's OK. 55 // be valid. Use is_valid() to check if it's OK.
53 void Assign(scoped_refptr<Connection::StatementRef> ref); 56 void Assign(scoped_refptr<Connection::StatementRef> ref);
54 57
55 // Returns true if the statement can be executed. All functions can still 58 // Returns true if the statement can be executed. All functions can still
56 // be used if the statement is invalid, but they will return failure or some 59 // be used if the statement is invalid, but they will return failure or some
57 // default value. This is because the statement can become invalid in the 60 // default value. This is because the statement can become invalid in the
58 // middle of executing a command if there is a serioud error and the database 61 // middle of executing a command if there is a serioud error and the database
59 // has to be reset. 62 // has to be reset.
60 bool is_valid() const { return ref_->is_valid(); } 63 bool is_valid() const { return ref_->is_valid(); }
61 64
62 // These operators allow conveniently checking if the statement is valid 65 // These operators allow conveniently checking if the statement is valid
63 // or not. See the pattern above for an example. 66 // or not. See the pattern above for an example.
67 // TODO(shess,gbillock): Remove these once clients are converted.
64 operator bool() const { return is_valid(); } 68 operator bool() const { return is_valid(); }
65 bool operator!() const { return !is_valid(); } 69 bool operator!() const { return !is_valid(); }
66 70
67 // Running ------------------------------------------------------------------- 71 // Running -------------------------------------------------------------------
68 72
69 // Executes the statement, returning true on success. This is like Step but 73 // Executes the statement, returning true on success. This is like Step but
70 // for when there is no output, like an INSERT statement. 74 // for when there is no output, like an INSERT statement.
71 bool Run(); 75 bool Run();
72 76
73 // Executes the statement, returning true if there is a row of data returned. 77 // Executes the statement, returning true if there is a row of data returned.
(...skipping 15 matching lines...) Expand all
89 // the bound variables and any current result row. 93 // the bound variables and any current result row.
90 void Reset(); 94 void Reset();
91 95
92 // Returns true if the last executed thing in this statement succeeded. If 96 // Returns true if the last executed thing in this statement succeeded. If
93 // there was no last executed thing or the statement is invalid, this will 97 // there was no last executed thing or the statement is invalid, this will
94 // return false. 98 // return false.
95 bool Succeeded() const; 99 bool Succeeded() const;
96 100
97 // Binding ------------------------------------------------------------------- 101 // Binding -------------------------------------------------------------------
98 102
99 // These all take a 0-based argument index and return true on failure. You 103 // These all take a 0-based argument index and return true on success. You
100 // may not always care about the return value (they'll DCHECK if they fail). 104 // may not always care about the return value (they'll DCHECK if they fail).
101 // The main thing you may want to check is when binding large blobs or 105 // The main thing you may want to check is when binding large blobs or
102 // strings there may be out of memory. 106 // strings there may be out of memory.
103 bool BindNull(int col); 107 bool BindNull(int col);
104 bool BindBool(int col, bool val); 108 bool BindBool(int col, bool val);
105 bool BindInt(int col, int val); 109 bool BindInt(int col, int val);
106 bool BindInt64(int col, int64 val); 110 bool BindInt64(int col, int64 val);
107 bool BindDouble(int col, double val); 111 bool BindDouble(int col, double val);
108 bool BindCString(int col, const char* val); 112 bool BindCString(int col, const char* val);
109 bool BindString(int col, const std::string& val); 113 bool BindString(int col, const std::string& val);
(...skipping 20 matching lines...) Expand all
130 double ColumnDouble(int col) const; 134 double ColumnDouble(int col) const;
131 std::string ColumnString(int col) const; 135 std::string ColumnString(int col) const;
132 string16 ColumnString16(int col) const; 136 string16 ColumnString16(int col) const;
133 137
134 // When reading a blob, you can get a raw pointer to the underlying data, 138 // When reading a blob, you can get a raw pointer to the underlying data,
135 // along with the length, or you can just ask us to copy the blob into a 139 // along with the length, or you can just ask us to copy the blob into a
136 // vector. Danger! ColumnBlob may return NULL if there is no data! 140 // vector. Danger! ColumnBlob may return NULL if there is no data!
137 int ColumnByteLength(int col) const; 141 int ColumnByteLength(int col) const;
138 const void* ColumnBlob(int col) const; 142 const void* ColumnBlob(int col) const;
139 bool ColumnBlobAsString(int col, std::string* blob); 143 bool ColumnBlobAsString(int col, std::string* blob);
140 void ColumnBlobAsVector(int col, std::vector<char>* val) const; 144 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
141 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; 145 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
142 146
143 // Diagnostics -------------------------------------------------------------- 147 // Diagnostics --------------------------------------------------------------
144 148
145 // Returns the original text of sql statement. Do not keep a pointer to it. 149 // Returns the original text of sql statement. Do not keep a pointer to it.
146 const char* GetSQLStatement(); 150 const char* GetSQLStatement();
147 151
148 private: 152 private:
149 // This is intended to check for serious errors and report them to the 153 // This is intended to check for serious errors and report them to the
150 // connection object. It takes a sqlite error code, and returns the same 154 // connection object. It takes a sqlite error code, and returns the same
151 // code. Currently this function just updates the succeeded flag, but will be 155 // code. Currently this function just updates the succeeded flag, but will be
152 // enhanced in the future to do the notification. 156 // enhanced in the future to do the notification.
153 int CheckError(int err); 157 int CheckError(int err);
154 158
159 // Contraction for checking an error code against SQLITE_OK. Does not set the
160 // succeeded flag.
161 bool CheckOk(int err) const;
162
163 // Should be called by all mutating methods to check that the statement is
164 // valid. Returns true if the statement is valid. DCHECKS and returns false
165 // if it is not.
166 // The reason for this is to handle two specific cases in which a Statement
167 // may be invalid. The first case is that the programmer made an SQL error.
168 // Those cases need to be DCHECKed so that we are guaranteed to find them
169 // before release. The second case is that the computer has an error (probably
170 // out of disk space) which is prohibiting the correct operation of the
171 // database. Our testing apparatus should not exhibit this defect, but release
172 // situations may. Therefore, the code is handling disjoint situations in
173 // release and test. In test, we're ensuring correct SQL. In release, we're
174 // ensuring that contracts are honored in error edge cases.
175 bool CheckValid() const;
176
155 // The actual sqlite statement. This may be unique to us, or it may be cached 177 // The actual sqlite statement. This may be unique to us, or it may be cached
156 // by the connection, which is why it's refcounted. This pointer is 178 // by the connection, which is why it's refcounted. This pointer is
157 // guaranteed non-NULL. 179 // guaranteed non-NULL.
158 scoped_refptr<Connection::StatementRef> ref_; 180 scoped_refptr<Connection::StatementRef> ref_;
159 181
160 // See Succeeded() for what this holds. 182 // See Succeeded() for what this holds.
161 bool succeeded_; 183 bool succeeded_;
162 184
163 DISALLOW_COPY_AND_ASSIGN(Statement); 185 DISALLOW_COPY_AND_ASSIGN(Statement);
164 }; 186 };
165 187
166 } // namespace sql 188 } // namespace sql
167 189
168 #endif // SQL_STATEMENT_H_ 190 #endif // SQL_STATEMENT_H_
OLDNEW
« no previous file with comments | « sql/sqlite_features_unittest.cc ('k') | sql/statement.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698