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"
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
),
22 Statement::Statement(scoped_refptr
<Connection::StatementRef
> ref
)
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.
34 void Statement::Assign(scoped_refptr
<Connection::StatementRef
> ref
) {
39 void Statement::Clear() {
40 Assign(new Connection::StatementRef
);
44 bool Statement::CheckValid() const {
46 DLOG(FATAL
) << "Cannot call mutating statements on an invalid statement.";
50 bool Statement::Run() {
51 ref_
->AssertIOAllowed();
55 return CheckError(sqlite3_step(ref_
->stmt())) == SQLITE_DONE
;
58 bool Statement::Step() {
59 ref_
->AssertIOAllowed();
63 return CheckError(sqlite3_step(ref_
->stmt())) == SQLITE_ROW
;
66 void Statement::Reset(bool clear_bound_vars
) {
67 ref_
->AssertIOAllowed();
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.
73 sqlite3_clear_bindings(ref_
->stmt());
74 sqlite3_reset(ref_
->stmt());
80 bool Statement::Succeeded() const {
87 bool Statement::BindNull(int col
) {
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
) {
102 return CheckOk(sqlite3_bind_int(ref_
->stmt(), col
+ 1, val
));
105 bool Statement::BindInt64(int col
, int64 val
) {
109 return CheckOk(sqlite3_bind_int64(ref_
->stmt(), col
+ 1, val
));
112 bool Statement::BindDouble(int col
, double val
) {
116 return CheckOk(sqlite3_bind_double(ref_
->stmt(), col
+ 1, val
));
119 bool Statement::BindCString(int col
, const char* val
) {
124 sqlite3_bind_text(ref_
->stmt(), col
+ 1, val
, -1, SQLITE_TRANSIENT
));
127 bool Statement::BindString(int col
, const std::string
& val
) {
131 return CheckOk(sqlite3_bind_text(ref_
->stmt(),
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
) {
147 sqlite3_bind_blob(ref_
->stmt(), col
+ 1, val
, val_len
, SQLITE_TRANSIENT
));
150 int Statement::ColumnCount() const {
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 {
192 return sqlite3_column_int(ref_
->stmt(), col
);
195 int64
Statement::ColumnInt64(int col
) const {
199 return sqlite3_column_int64(ref_
->stmt(), col
);
202 double Statement::ColumnDouble(int col
) const {
206 return sqlite3_column_double(ref_
->stmt(), col
);
209 std::string
Statement::ColumnString(int col
) const {
213 const char* str
= reinterpret_cast<const char*>(
214 sqlite3_column_text(ref_
->stmt(), col
));
215 int len
= sqlite3_column_bytes(ref_
->stmt(), col
);
219 result
.assign(str
, len
);
223 string16
Statement::ColumnString16(int col
) const {
227 std::string s
= ColumnString(col
);
228 return !s
.empty() ? UTF8ToUTF16(s
) : string16();
231 int Statement::ColumnByteLength(int col
) const {
235 return sqlite3_column_bytes(ref_
->stmt(), col
);
238 const void* Statement::ColumnBlob(int col
) const {
242 return sqlite3_column_blob(ref_
->stmt(), col
);
245 bool Statement::ColumnBlobAsString(int col
, std::string
* blob
) {
249 const void* p
= ColumnBlob(col
);
250 size_t len
= ColumnByteLength(col
);
252 if (blob
->size() != len
) {
255 blob
->assign(reinterpret_cast<const char*>(p
), len
);
259 bool Statement::ColumnBlobAsString16(int col
, string16
* val
) const {
263 const void* data
= ColumnBlob(col
);
264 size_t len
= ColumnByteLength(col
) / sizeof(char16
);
266 if (val
->size() != len
)
268 val
->assign(reinterpret_cast<const char16
*>(data
), len
);
272 bool Statement::ColumnBlobAsVector(int col
, std::vector
<char>* val
) const {
278 const void* data
= sqlite3_column_blob(ref_
->stmt(), col
);
279 int len
= sqlite3_column_bytes(ref_
->stmt(), col
);
280 if (data
&& len
> 0) {
282 memcpy(&(*val
)[0], data
, len
);
287 bool Statement::ColumnBlobAsVector(
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);