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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 const void* p = ColumnBlob(col); | 242 const void* p = ColumnBlob(col); |
224 size_t len = ColumnByteLength(col); | 243 size_t len = ColumnByteLength(col); |
225 blob->resize(len); | 244 blob->resize(len); |
226 if (blob->size() != len) { | 245 if (blob->size() != len) { |
227 return false; | 246 return false; |
228 } | 247 } |
229 blob->assign(reinterpret_cast<const char*>(p), len); | 248 blob->assign(reinterpret_cast<const char*>(p), len); |
230 return true; | 249 return true; |
231 } | 250 } |
232 | 251 |
| 252 bool Statement::ColumnBlobAsString16(int col, string16* val) const { |
| 253 if (!CheckValid()) |
| 254 return false; |
| 255 |
| 256 const void* data = ColumnBlob(col); |
| 257 size_t len = ColumnByteLength(col) / sizeof(char16); |
| 258 val->resize(len); |
| 259 if (val->size() != len) |
| 260 return false; |
| 261 val->assign(reinterpret_cast<const char16*>(data), len); |
| 262 return true; |
| 263 } |
| 264 |
233 bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const { | 265 bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const { |
234 val->clear(); | 266 val->clear(); |
235 | 267 |
236 if (!CheckValid()) | 268 if (!CheckValid()) |
237 return false; | 269 return false; |
238 | 270 |
239 const void* data = sqlite3_column_blob(ref_->stmt(), col); | 271 const void* data = sqlite3_column_blob(ref_->stmt(), col); |
240 int len = sqlite3_column_bytes(ref_->stmt(), col); | 272 int len = sqlite3_column_bytes(ref_->stmt(), col); |
241 if (data && len > 0) { | 273 if (data && len > 0) { |
242 val->resize(len); | 274 val->resize(len); |
(...skipping 23 matching lines...) Expand all Loading... |
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 |