Refactor SetOmahaExperimentLabel out of gcpai and into install_util.
[chromium-blink-merge.git] / sql / statement.cc
blob84dfd2eb9aa850407cff6e84c83e49cce646d865
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
3 // found in the LICENSE file.
5 #include "sql/statement.h"
7 #include "base/logging.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
10 #include "third_party/sqlite/sqlite3.h"
12 namespace sql {
14 // This empty constructor initializes our reference with an empty one so that
15 // we don't have to NULL-check the ref_ to see if the statement is valid: we
16 // only have to check the ref's validity bit.
17 Statement::Statement()
18 : ref_(new Connection::StatementRef),
19 succeeded_(false) {
22 Statement::Statement(scoped_refptr<Connection::StatementRef> ref)
23 : ref_(ref),
24 succeeded_(false) {
27 Statement::~Statement() {
28 // Free the resources associated with this statement. We assume there's only
29 // one statement active for a given sqlite3_stmt at any time, so this won't
30 // mess with anything.
31 Reset(true);
34 void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) {
35 Reset(true);
36 ref_ = ref;
39 void Statement::Clear() {
40 Assign(new Connection::StatementRef);
41 succeeded_ = false;
44 bool Statement::CheckValid() const {
45 if (!is_valid())
46 DLOG(FATAL) << "Cannot call mutating statements on an invalid statement.";
47 return is_valid();
50 bool Statement::Run() {
51 ref_->AssertIOAllowed();
52 if (!CheckValid())
53 return false;
55 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE;
58 bool Statement::Step() {
59 ref_->AssertIOAllowed();
60 if (!CheckValid())
61 return false;
63 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW;
66 void Statement::Reset(bool clear_bound_vars) {
67 ref_->AssertIOAllowed();
68 if (is_valid()) {
69 // We don't call CheckError() here because sqlite3_reset() returns
70 // the last error that Step() caused thereby generating a second
71 // spurious error callback.
72 if (clear_bound_vars)
73 sqlite3_clear_bindings(ref_->stmt());
74 sqlite3_reset(ref_->stmt());
77 succeeded_ = false;
80 bool Statement::Succeeded() const {
81 if (!is_valid())
82 return false;
84 return succeeded_;
87 bool Statement::BindNull(int col) {
88 if (!is_valid())
89 return false;
91 return CheckOk(sqlite3_bind_null(ref_->stmt(), col + 1));
94 bool Statement::BindBool(int col, bool val) {
95 return BindInt(col, val ? 1 : 0);
98 bool Statement::BindInt(int col, int val) {
99 if (!is_valid())
100 return false;
102 return CheckOk(sqlite3_bind_int(ref_->stmt(), col + 1, val));
105 bool Statement::BindInt64(int col, int64 val) {
106 if (!is_valid())
107 return false;
109 return CheckOk(sqlite3_bind_int64(ref_->stmt(), col + 1, val));
112 bool Statement::BindDouble(int col, double val) {
113 if (!is_valid())
114 return false;
116 return CheckOk(sqlite3_bind_double(ref_->stmt(), col + 1, val));
119 bool Statement::BindCString(int col, const char* val) {
120 if (!is_valid())
121 return false;
123 return CheckOk(
124 sqlite3_bind_text(ref_->stmt(), col + 1, val, -1, SQLITE_TRANSIENT));
127 bool Statement::BindString(int col, const std::string& val) {
128 if (!is_valid())
129 return false;
131 return CheckOk(sqlite3_bind_text(ref_->stmt(),
132 col + 1,
133 val.data(),
134 val.size(),
135 SQLITE_TRANSIENT));
138 bool Statement::BindString16(int col, const string16& value) {
139 return BindString(col, UTF16ToUTF8(value));
142 bool Statement::BindBlob(int col, const void* val, int val_len) {
143 if (!is_valid())
144 return false;
146 return CheckOk(
147 sqlite3_bind_blob(ref_->stmt(), col + 1, val, val_len, SQLITE_TRANSIENT));
150 int Statement::ColumnCount() const {
151 if (!is_valid())
152 return 0;
154 return sqlite3_column_count(ref_->stmt());
157 ColType Statement::ColumnType(int col) const {
158 // Verify that our enum matches sqlite's values.
159 COMPILE_ASSERT(COLUMN_TYPE_INTEGER == SQLITE_INTEGER, integer_no_match);
160 COMPILE_ASSERT(COLUMN_TYPE_FLOAT == SQLITE_FLOAT, float_no_match);
161 COMPILE_ASSERT(COLUMN_TYPE_TEXT == SQLITE_TEXT, integer_no_match);
162 COMPILE_ASSERT(COLUMN_TYPE_BLOB == SQLITE_BLOB, blob_no_match);
163 COMPILE_ASSERT(COLUMN_TYPE_NULL == SQLITE_NULL, null_no_match);
165 return static_cast<ColType>(sqlite3_column_type(ref_->stmt(), col));
168 ColType Statement::DeclaredColumnType(int col) const {
169 std::string column_type(sqlite3_column_decltype(ref_->stmt(), col));
170 StringToLowerASCII(&column_type);
172 if (column_type == "integer")
173 return COLUMN_TYPE_INTEGER;
174 else if (column_type == "float")
175 return COLUMN_TYPE_FLOAT;
176 else if (column_type == "text")
177 return COLUMN_TYPE_TEXT;
178 else if (column_type == "blob")
179 return COLUMN_TYPE_BLOB;
181 return COLUMN_TYPE_NULL;
184 bool Statement::ColumnBool(int col) const {
185 return !!ColumnInt(col);
188 int Statement::ColumnInt(int col) const {
189 if (!CheckValid())
190 return 0;
192 return sqlite3_column_int(ref_->stmt(), col);
195 int64 Statement::ColumnInt64(int col) const {
196 if (!CheckValid())
197 return 0;
199 return sqlite3_column_int64(ref_->stmt(), col);
202 double Statement::ColumnDouble(int col) const {
203 if (!CheckValid())
204 return 0;
206 return sqlite3_column_double(ref_->stmt(), col);
209 std::string Statement::ColumnString(int col) const {
210 if (!CheckValid())
211 return "";
213 const char* str = reinterpret_cast<const char*>(
214 sqlite3_column_text(ref_->stmt(), col));
215 int len = sqlite3_column_bytes(ref_->stmt(), col);
217 std::string result;
218 if (str && len > 0)
219 result.assign(str, len);
220 return result;
223 string16 Statement::ColumnString16(int col) const {
224 if (!CheckValid())
225 return string16();
227 std::string s = ColumnString(col);
228 return !s.empty() ? UTF8ToUTF16(s) : string16();
231 int Statement::ColumnByteLength(int col) const {
232 if (!CheckValid())
233 return 0;
235 return sqlite3_column_bytes(ref_->stmt(), col);
238 const void* Statement::ColumnBlob(int col) const {
239 if (!CheckValid())
240 return NULL;
242 return sqlite3_column_blob(ref_->stmt(), col);
245 bool Statement::ColumnBlobAsString(int col, std::string* blob) {
246 if (!CheckValid())
247 return false;
249 const void* p = ColumnBlob(col);
250 size_t len = ColumnByteLength(col);
251 blob->resize(len);
252 if (blob->size() != len) {
253 return false;
255 blob->assign(reinterpret_cast<const char*>(p), len);
256 return true;
259 bool Statement::ColumnBlobAsString16(int col, string16* val) const {
260 if (!CheckValid())
261 return false;
263 const void* data = ColumnBlob(col);
264 size_t len = ColumnByteLength(col) / sizeof(char16);
265 val->resize(len);
266 if (val->size() != len)
267 return false;
268 val->assign(reinterpret_cast<const char16*>(data), len);
269 return true;
272 bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const {
273 val->clear();
275 if (!CheckValid())
276 return false;
278 const void* data = sqlite3_column_blob(ref_->stmt(), col);
279 int len = sqlite3_column_bytes(ref_->stmt(), col);
280 if (data && len > 0) {
281 val->resize(len);
282 memcpy(&(*val)[0], data, len);
284 return true;
287 bool Statement::ColumnBlobAsVector(
288 int col,
289 std::vector<unsigned char>* val) const {
290 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val));
293 const char* Statement::GetSQLStatement() {
294 return sqlite3_sql(ref_->stmt());
297 bool Statement::CheckOk(int err) const {
298 // Binding to a non-existent variable is evidence of a serious error.
299 // TODO(gbillock,shess): make this invalidate the statement so it
300 // can't wreak havoc.
301 if (err == SQLITE_RANGE)
302 DLOG(FATAL) << "Bind value out of range";
303 return err == SQLITE_OK;
306 int Statement::CheckError(int err) {
307 // Please don't add DCHECKs here, OnSqliteError() already has them.
308 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE);
309 if (!succeeded_ && is_valid())
310 return ref_->connection()->OnSqliteError(err, this);
311 return err;
314 } // namespace sql