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 #include "sql/statement.h" | 5 #include "sql/statement.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
9 #include "third_party/sqlite/sqlite3.h" | 11 #include "third_party/sqlite/sqlite3.h" |
10 | 12 |
11 namespace sql { | 13 namespace sql { |
12 | 14 |
13 // This empty constructor initializes our reference with an empty one so that | 15 // This empty constructor initializes our reference with an empty one so that |
14 // we don't have to NULL-check the ref_ to see if the statement is valid: we | 16 // we don't have to NULL-check the ref_ to see if the statement is valid: we |
15 // only have to check the ref's validity bit. | 17 // only have to check the ref's validity bit. |
16 Statement::Statement() | 18 Statement::Statement() |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Verify that our enum matches sqlite's values. | 150 // Verify that our enum matches sqlite's values. |
149 COMPILE_ASSERT(COLUMN_TYPE_INTEGER == SQLITE_INTEGER, integer_no_match); | 151 COMPILE_ASSERT(COLUMN_TYPE_INTEGER == SQLITE_INTEGER, integer_no_match); |
150 COMPILE_ASSERT(COLUMN_TYPE_FLOAT == SQLITE_FLOAT, float_no_match); | 152 COMPILE_ASSERT(COLUMN_TYPE_FLOAT == SQLITE_FLOAT, float_no_match); |
151 COMPILE_ASSERT(COLUMN_TYPE_TEXT == SQLITE_TEXT, integer_no_match); | 153 COMPILE_ASSERT(COLUMN_TYPE_TEXT == SQLITE_TEXT, integer_no_match); |
152 COMPILE_ASSERT(COLUMN_TYPE_BLOB == SQLITE_BLOB, blob_no_match); | 154 COMPILE_ASSERT(COLUMN_TYPE_BLOB == SQLITE_BLOB, blob_no_match); |
153 COMPILE_ASSERT(COLUMN_TYPE_NULL == SQLITE_NULL, null_no_match); | 155 COMPILE_ASSERT(COLUMN_TYPE_NULL == SQLITE_NULL, null_no_match); |
154 | 156 |
155 return static_cast<ColType>(sqlite3_column_type(ref_->stmt(), col)); | 157 return static_cast<ColType>(sqlite3_column_type(ref_->stmt(), col)); |
156 } | 158 } |
157 | 159 |
| 160 ColType Statement::DeclaredColumnType(int col) const { |
| 161 std::string column_type(sqlite3_column_decltype(ref_->stmt(), col)); |
| 162 std::transform(column_type.begin(), column_type.end(), column_type.begin(), |
| 163 ::tolower); |
| 164 |
| 165 if (column_type == "integer") |
| 166 return COLUMN_TYPE_INTEGER; |
| 167 else if (column_type == "float") |
| 168 return COLUMN_TYPE_FLOAT; |
| 169 else if (column_type == "text") |
| 170 return COLUMN_TYPE_TEXT; |
| 171 else if (column_type == "blob") |
| 172 return COLUMN_TYPE_BLOB; |
| 173 |
| 174 return COLUMN_TYPE_NULL; |
| 175 } |
| 176 |
158 bool Statement::ColumnBool(int col) const { | 177 bool Statement::ColumnBool(int col) const { |
159 return !!ColumnInt(col); | 178 return !!ColumnInt(col); |
160 } | 179 } |
161 | 180 |
162 int Statement::ColumnInt(int col) const { | 181 int Statement::ColumnInt(int col) const { |
163 if (!CheckValid()) | 182 if (!CheckValid()) |
164 return 0; | 183 return 0; |
165 | 184 |
166 return sqlite3_column_int(ref_->stmt(), col); | 185 return sqlite3_column_int(ref_->stmt(), col); |
167 } | 186 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 263 } |
245 return true; | 264 return true; |
246 } | 265 } |
247 | 266 |
248 bool Statement::ColumnBlobAsVector( | 267 bool Statement::ColumnBlobAsVector( |
249 int col, | 268 int col, |
250 std::vector<unsigned char>* val) const { | 269 std::vector<unsigned char>* val) const { |
251 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | 270 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); |
252 } | 271 } |
253 | 272 |
| 273 bool Statement::ColumnBlobAsString16(int col, string16* val) const { |
| 274 if (!CheckValid()) |
| 275 return false; |
| 276 |
| 277 const void* data = sqlite3_column_blob(ref_->stmt(), col); |
| 278 int len = sqlite3_column_bytes(ref_->stmt(), col); |
| 279 if (data && len > 0) { |
| 280 val->resize(len); |
| 281 val->assign(reinterpret_cast<const char16*>(data)); |
| 282 } |
| 283 return true; |
| 284 } |
| 285 |
254 const char* Statement::GetSQLStatement() { | 286 const char* Statement::GetSQLStatement() { |
255 return sqlite3_sql(ref_->stmt()); | 287 return sqlite3_sql(ref_->stmt()); |
256 } | 288 } |
257 | 289 |
258 bool Statement::CheckOk(int err) const { | 290 bool Statement::CheckOk(int err) const { |
259 // Binding to a non-existent variable is evidence of a serious error. | 291 // Binding to a non-existent variable is evidence of a serious error. |
260 // TODO(gbillock,shess): make this invalidate the statement so it | 292 // TODO(gbillock,shess): make this invalidate the statement so it |
261 // can't wreak havoc. | 293 // can't wreak havoc. |
262 if (err == SQLITE_RANGE) | 294 if (err == SQLITE_RANGE) |
263 DLOG(FATAL) << "Bind value out of range"; | 295 DLOG(FATAL) << "Bind value out of range"; |
264 return err == SQLITE_OK; | 296 return err == SQLITE_OK; |
265 } | 297 } |
266 | 298 |
267 int Statement::CheckError(int err) { | 299 int Statement::CheckError(int err) { |
268 // Please don't add DCHECKs here, OnSqliteError() already has them. | 300 // Please don't add DCHECKs here, OnSqliteError() already has them. |
269 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 301 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
270 if (!succeeded_ && is_valid()) | 302 if (!succeeded_ && is_valid()) |
271 return ref_->connection()->OnSqliteError(err, this); | 303 return ref_->connection()->OnSqliteError(err, this); |
272 return err; | 304 return err; |
273 } | 305 } |
274 | 306 |
275 } // namespace sql | 307 } // namespace sql |
OLD | NEW |