OLD | NEW |
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 Loading... |
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); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // the bound variables and any current result row. | 92 // the bound variables and any current result row. |
90 void Reset(); | 93 void Reset(); |
91 | 94 |
92 // Returns true if the last executed thing in this statement succeeded. If | 95 // 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 | 96 // there was no last executed thing or the statement is invalid, this will |
94 // return false. | 97 // return false. |
95 bool Succeeded() const; | 98 bool Succeeded() const; |
96 | 99 |
97 // Binding ------------------------------------------------------------------- | 100 // Binding ------------------------------------------------------------------- |
98 | 101 |
99 // These all take a 0-based argument index and return true on failure. You | 102 // 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). | 103 // 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 | 104 // The main thing you may want to check is when binding large blobs or |
102 // strings there may be out of memory. | 105 // strings there may be out of memory. |
103 bool BindNull(int col); | 106 bool BindNull(int col); |
104 bool BindBool(int col, bool val); | 107 bool BindBool(int col, bool val); |
105 bool BindInt(int col, int val); | 108 bool BindInt(int col, int val); |
106 bool BindInt64(int col, int64 val); | 109 bool BindInt64(int col, int64 val); |
107 bool BindDouble(int col, double val); | 110 bool BindDouble(int col, double val); |
108 bool BindCString(int col, const char* val); | 111 bool BindCString(int col, const char* val); |
109 bool BindString(int col, const std::string& val); | 112 bool BindString(int col, const std::string& val); |
(...skipping 20 matching lines...) Expand all Loading... |
130 double ColumnDouble(int col) const; | 133 double ColumnDouble(int col) const; |
131 std::string ColumnString(int col) const; | 134 std::string ColumnString(int col) const; |
132 string16 ColumnString16(int col) const; | 135 string16 ColumnString16(int col) const; |
133 | 136 |
134 // When reading a blob, you can get a raw pointer to the underlying data, | 137 // 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 | 138 // 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! | 139 // vector. Danger! ColumnBlob may return NULL if there is no data! |
137 int ColumnByteLength(int col) const; | 140 int ColumnByteLength(int col) const; |
138 const void* ColumnBlob(int col) const; | 141 const void* ColumnBlob(int col) const; |
139 bool ColumnBlobAsString(int col, std::string* blob); | 142 bool ColumnBlobAsString(int col, std::string* blob); |
140 void ColumnBlobAsVector(int col, std::vector<char>* val) const; | 143 bool ColumnBlobAsVector(int col, std::vector<char>* val) const; |
141 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; | 144 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; |
142 | 145 |
143 // Diagnostics -------------------------------------------------------------- | 146 // Diagnostics -------------------------------------------------------------- |
144 | 147 |
145 // Returns the original text of sql statement. Do not keep a pointer to it. | 148 // Returns the original text of sql statement. Do not keep a pointer to it. |
146 const char* GetSQLStatement(); | 149 const char* GetSQLStatement(); |
147 | 150 |
148 private: | 151 private: |
149 // This is intended to check for serious errors and report them to the | 152 // 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 | 153 // 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 | 154 // code. Currently this function just updates the succeeded flag, but will be |
152 // enhanced in the future to do the notification. | 155 // enhanced in the future to do the notification. |
153 int CheckError(int err); | 156 int CheckError(int err); |
154 | 157 |
| 158 // Contraction for checking an error code against SQLITE_OK. Does not set the |
| 159 // succeeded flag. |
| 160 bool CheckOk(int err) const; |
| 161 |
| 162 // Should be called by all mutating methods to check that the statement is |
| 163 // valid. Returns true if the statement is valid. DCHECKS and returns false |
| 164 // if it is not. |
| 165 // The reason for this is to handle two specific cases in which a Statement |
| 166 // may be invalid. The first case is that the programmer made an SQL error. |
| 167 // Those cases need to be DCHECKed so that we are guaranteed to find them |
| 168 // before release. The second case is that the computer has an error (probably |
| 169 // out of disk space) which is prohibiting the correct operation of the |
| 170 // database. Our testing apparatus should not exhibit this defect, but release |
| 171 // situations may. Therefore, the code is handling disjoint situations in |
| 172 // release and test. In test, we're ensuring correct SQL. In release, we're |
| 173 // ensuring that contracts are honored in error edge cases. |
| 174 bool CheckValid() const; |
| 175 |
155 // The actual sqlite statement. This may be unique to us, or it may be cached | 176 // 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 | 177 // by the connection, which is why it's refcounted. This pointer is |
157 // guaranteed non-NULL. | 178 // guaranteed non-NULL. |
158 scoped_refptr<Connection::StatementRef> ref_; | 179 scoped_refptr<Connection::StatementRef> ref_; |
159 | 180 |
160 // See Succeeded() for what this holds. | 181 // See Succeeded() for what this holds. |
161 bool succeeded_; | 182 bool succeeded_; |
162 | 183 |
163 DISALLOW_COPY_AND_ASSIGN(Statement); | 184 DISALLOW_COPY_AND_ASSIGN(Statement); |
164 }; | 185 }; |
165 | 186 |
166 } // namespace sql | 187 } // namespace sql |
167 | 188 |
168 #endif // SQL_STATEMENT_H_ | 189 #endif // SQL_STATEMENT_H_ |
OLD | NEW |