| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/common/sqlite_utils.h" | 5 #include "chrome/common/sqlite_utils.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string16.h" |
| 9 | 10 |
| 10 int OpenSqliteDb(const FilePath& filepath, sqlite3** database) { | 11 int OpenSqliteDb(const FilePath& filepath, sqlite3** database) { |
| 11 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 12 return sqlite3_open16(filepath.value().c_str(), database); | 13 return sqlite3_open16(filepath.value().c_str(), database); |
| 13 #elif defined(OS_POSIX) | 14 #elif defined(OS_POSIX) |
| 14 return sqlite3_open(filepath.value().c_str(), database); | 15 return sqlite3_open(filepath.value().c_str(), database); |
| 15 #endif | 16 #endif |
| 16 } | 17 } |
| 17 | 18 |
| 18 bool DoesSqliteTableExist(sqlite3* db, | 19 bool DoesSqliteTableExist(sqlite3* db, |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 197 |
| 197 int SQLStatement::prepare(sqlite3* db, const char* sql, int sql_len) { | 198 int SQLStatement::prepare(sqlite3* db, const char* sql, int sql_len) { |
| 198 DCHECK(!stmt_); | 199 DCHECK(!stmt_); |
| 199 int rv = sqlite3_prepare_v2(db, sql, sql_len, &stmt_, NULL); | 200 int rv = sqlite3_prepare_v2(db, sql, sql_len, &stmt_, NULL); |
| 200 if (rv != SQLITE_OK) { | 201 if (rv != SQLITE_OK) { |
| 201 DLOG(ERROR) << "SQLStatement.prepare_v2 failed: " << sqlite3_errmsg(db); | 202 DLOG(ERROR) << "SQLStatement.prepare_v2 failed: " << sqlite3_errmsg(db); |
| 202 } | 203 } |
| 203 return rv; | 204 return rv; |
| 204 } | 205 } |
| 205 | 206 |
| 206 int SQLStatement::prepare16(sqlite3* db, const wchar_t* sql, int sql_len) { | |
| 207 DCHECK(!stmt_); | |
| 208 sql_len *= sizeof(wchar_t); | |
| 209 int rv = sqlite3_prepare16_v2(db, sql, sql_len, &stmt_, NULL); | |
| 210 if (rv != SQLITE_OK) { | |
| 211 DLOG(ERROR) << "SQLStatement.prepare16_v2 failed: " << sqlite3_errmsg(db); | |
| 212 } | |
| 213 return rv; | |
| 214 } | |
| 215 | |
| 216 int SQLStatement::step() { | 207 int SQLStatement::step() { |
| 217 DCHECK(stmt_); | 208 DCHECK(stmt_); |
| 218 return sqlite3_step(stmt_); | 209 return sqlite3_step(stmt_); |
| 219 } | 210 } |
| 220 | 211 |
| 221 int SQLStatement::reset() { | 212 int SQLStatement::reset() { |
| 222 DCHECK(stmt_); | 213 DCHECK(stmt_); |
| 223 return sqlite3_reset(stmt_); | 214 return sqlite3_reset(stmt_); |
| 224 } | 215 } |
| 225 | 216 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 DCHECK(stmt_); | 273 DCHECK(stmt_); |
| 283 return sqlite3_bind_null(stmt_, index + 1); | 274 return sqlite3_bind_null(stmt_, index + 1); |
| 284 } | 275 } |
| 285 | 276 |
| 286 int SQLStatement::bind_text(int index, const char* value, int value_len, | 277 int SQLStatement::bind_text(int index, const char* value, int value_len, |
| 287 Function dtor) { | 278 Function dtor) { |
| 288 DCHECK(stmt_); | 279 DCHECK(stmt_); |
| 289 return sqlite3_bind_text(stmt_, index + 1, value, value_len, dtor); | 280 return sqlite3_bind_text(stmt_, index + 1, value, value_len, dtor); |
| 290 } | 281 } |
| 291 | 282 |
| 292 int SQLStatement::bind_text16(int index, const wchar_t* value, int value_len, | 283 int SQLStatement::bind_text16(int index, const char16* value, int value_len, |
| 293 Function dtor) { | 284 Function dtor) { |
| 294 DCHECK(stmt_); | 285 DCHECK(stmt_); |
| 295 value_len *= sizeof(wchar_t); | 286 value_len *= sizeof(char16); |
| 296 return sqlite3_bind_text16(stmt_, index + 1, value, value_len, dtor); | 287 return sqlite3_bind_text16(stmt_, index + 1, value, value_len, dtor); |
| 297 } | 288 } |
| 298 | 289 |
| 299 int SQLStatement::bind_value(int index, const sqlite3_value* value) { | 290 int SQLStatement::bind_value(int index, const sqlite3_value* value) { |
| 300 DCHECK(stmt_); | 291 DCHECK(stmt_); |
| 301 return sqlite3_bind_value(stmt_, index + 1, value); | 292 return sqlite3_bind_value(stmt_, index + 1, value); |
| 302 } | 293 } |
| 303 | 294 |
| 304 int SQLStatement::column_count() { | 295 int SQLStatement::column_count() { |
| 305 DCHECK(stmt_); | 296 DCHECK(stmt_); |
| 306 return sqlite3_column_count(stmt_); | 297 return sqlite3_column_count(stmt_); |
| 307 } | 298 } |
| 308 | 299 |
| 309 int SQLStatement::column_type(int index) { | 300 int SQLStatement::column_type(int index) { |
| 310 DCHECK(stmt_); | 301 DCHECK(stmt_); |
| 311 return sqlite3_column_type(stmt_, index); | 302 return sqlite3_column_type(stmt_, index); |
| 312 } | 303 } |
| 313 | 304 |
| 314 const wchar_t* SQLStatement::column_name16(int index) { | |
| 315 DCHECK(stmt_); | |
| 316 return static_cast<const wchar_t*>( sqlite3_column_name16(stmt_, index) ); | |
| 317 } | |
| 318 | |
| 319 const void* SQLStatement::column_blob(int index) { | 305 const void* SQLStatement::column_blob(int index) { |
| 320 DCHECK(stmt_); | 306 DCHECK(stmt_); |
| 321 return sqlite3_column_blob(stmt_, index); | 307 return sqlite3_column_blob(stmt_, index); |
| 322 } | 308 } |
| 323 | 309 |
| 324 bool SQLStatement::column_blob_as_vector(int index, | 310 bool SQLStatement::column_blob_as_vector(int index, |
| 325 std::vector<unsigned char>* blob) { | 311 std::vector<unsigned char>* blob) { |
| 326 DCHECK(stmt_); | 312 DCHECK(stmt_); |
| 327 const void* p = column_blob(index); | 313 const void* p = column_blob(index); |
| 328 size_t len = column_bytes(index); | 314 size_t len = column_bytes(index); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 365 |
| 380 const char* SQLStatement::column_text(int index) { | 366 const char* SQLStatement::column_text(int index) { |
| 381 DCHECK(stmt_); | 367 DCHECK(stmt_); |
| 382 return reinterpret_cast<const char*>(sqlite3_column_text(stmt_, index)); | 368 return reinterpret_cast<const char*>(sqlite3_column_text(stmt_, index)); |
| 383 } | 369 } |
| 384 | 370 |
| 385 bool SQLStatement::column_string(int index, std::string* str) { | 371 bool SQLStatement::column_string(int index, std::string* str) { |
| 386 DCHECK(stmt_); | 372 DCHECK(stmt_); |
| 387 DCHECK(str); | 373 DCHECK(str); |
| 388 const char* s = column_text(index); | 374 const char* s = column_text(index); |
| 389 str->assign(s ? s : std::string("")); | 375 str->assign(s ? s : std::string()); |
| 390 return s != NULL; | 376 return s != NULL; |
| 391 } | 377 } |
| 392 | 378 |
| 393 std::string SQLStatement::column_string(int index) { | 379 std::string SQLStatement::column_string(int index) { |
| 394 std::string str; | 380 std::string str; |
| 395 column_string(index, &str); | 381 column_string(index, &str); |
| 396 return str; | 382 return str; |
| 397 } | 383 } |
| 398 | 384 |
| 399 const wchar_t* SQLStatement::column_text16(int index) { | 385 const char16* SQLStatement::column_text16(int index) { |
| 400 DCHECK(stmt_); | 386 DCHECK(stmt_); |
| 401 return static_cast<const wchar_t*>( sqlite3_column_text16(stmt_, index) ); | 387 return static_cast<const char16*>(sqlite3_column_text16(stmt_, index)); |
| 402 } | 388 } |
| 403 | 389 |
| 404 bool SQLStatement::column_string16(int index, std::wstring* str) { | 390 bool SQLStatement::column_wstring(int index, std::wstring* str) { |
| 405 DCHECK(stmt_); | 391 DCHECK(stmt_); |
| 406 DCHECK(str); | 392 DCHECK(str); |
| 407 const wchar_t* s = column_text16(index); | 393 const char* s = column_text(index); |
| 408 str->assign(s ? s : std::wstring(L"")); | 394 str->assign(s ? UTF8ToWide(s) : std::wstring()); |
| 409 return (s != NULL); | 395 return (s != NULL); |
| 410 } | 396 } |
| 411 | 397 |
| 412 std::wstring SQLStatement::column_string16(int index) { | 398 std::wstring SQLStatement::column_wstring(int index) { |
| 413 std::wstring wstr; | 399 std::wstring wstr; |
| 414 column_string16(index, &wstr); | 400 column_wstring(index, &wstr); |
| 415 return wstr; | 401 return wstr; |
| 416 } | 402 } |
| 417 | 403 |
| OLD | NEW |