| 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 #ifndef CHROME_COMMON_SQLITEUTILS_H_ | 5 #ifndef CHROME_COMMON_SQLITEUTILS_H_ |
| 6 #define CHROME_COMMON_SQLITEUTILS_H_ | 6 #define CHROME_COMMON_SQLITEUTILS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/string16.h" |
| 13 #include "base/string_util.h" |
| 12 | 14 |
| 13 #include "third_party/sqlite/preprocessed/sqlite3.h" | 15 #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 14 | 16 |
| 15 // forward declarations of classes defined here | 17 // forward declarations of classes defined here |
| 16 class FilePath; | 18 class FilePath; |
| 17 class SQLTransaction; | 19 class SQLTransaction; |
| 18 class SQLNestedTransaction; | 20 class SQLNestedTransaction; |
| 19 class SQLNestedTransactionSite; | 21 class SQLNestedTransactionSite; |
| 20 class scoped_sqlite3_stmt_ptr; | 22 class scoped_sqlite3_stmt_ptr; |
| 21 class SQLStatement; | 23 class SQLStatement; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 public: | 205 public: |
| 204 SQLStatement() { | 206 SQLStatement() { |
| 205 } | 207 } |
| 206 | 208 |
| 207 int prepare(sqlite3* db, const char* sql) { | 209 int prepare(sqlite3* db, const char* sql) { |
| 208 return prepare(db, sql, -1); | 210 return prepare(db, sql, -1); |
| 209 } | 211 } |
| 210 | 212 |
| 211 int prepare(sqlite3* db, const char* sql, int sql_len); | 213 int prepare(sqlite3* db, const char* sql, int sql_len); |
| 212 | 214 |
| 213 int prepare16(sqlite3* db, const wchar_t* sql) { | |
| 214 return prepare16(db, sql, -1); | |
| 215 } | |
| 216 | |
| 217 // sql_len is number of characters or may be negative | |
| 218 // a for null-terminated sql string | |
| 219 int prepare16(sqlite3* db, const wchar_t* sql, int sql_len); | |
| 220 int step(); | 215 int step(); |
| 221 int reset(); | 216 int reset(); |
| 222 sqlite_int64 last_insert_rowid(); | 217 sqlite_int64 last_insert_rowid(); |
| 223 sqlite3* db_handle(); | 218 sqlite3* db_handle(); |
| 224 | 219 |
| 225 // | 220 // |
| 226 // Parameter binding helpers (NOTE: index is 0-based) | 221 // Parameter binding helpers (NOTE: index is 0-based) |
| 227 // | 222 // |
| 228 | 223 |
| 229 int bind_parameter_count(); | 224 int bind_parameter_count(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 242 int bind_string(int index, const std::string& value) { | 237 int bind_string(int index, const std::string& value) { |
| 243 // don't use c_str so it doesn't have to fix up the null terminator | 238 // don't use c_str so it doesn't have to fix up the null terminator |
| 244 // (sqlite just uses the length) | 239 // (sqlite just uses the length) |
| 245 return bind_text(index, value.data(), | 240 return bind_text(index, value.data(), |
| 246 static_cast<int>(value.length()), SQLITE_TRANSIENT); | 241 static_cast<int>(value.length()), SQLITE_TRANSIENT); |
| 247 } | 242 } |
| 248 | 243 |
| 249 int bind_wstring(int index, const std::wstring& value) { | 244 int bind_wstring(int index, const std::wstring& value) { |
| 250 // don't use c_str so it doesn't have to fix up the null terminator | 245 // don't use c_str so it doesn't have to fix up the null terminator |
| 251 // (sqlite just uses the length) | 246 // (sqlite just uses the length) |
| 252 return bind_text16(index, value.data(), | 247 std::string value_utf8(WideToUTF8(value)); |
| 253 static_cast<int>(value.length()), SQLITE_TRANSIENT); | 248 return bind_text(index, value_utf8.data(), |
| 249 static_cast<int>(value_utf8.length()), SQLITE_TRANSIENT); |
| 254 } | 250 } |
| 255 | 251 |
| 256 int bind_text(int index, const char* value) { | 252 int bind_text(int index, const char* value) { |
| 257 return bind_text(index, value, -1, SQLITE_TRANSIENT); | 253 return bind_text(index, value, -1, SQLITE_TRANSIENT); |
| 258 } | 254 } |
| 259 | 255 |
| 260 // value_len is number of characters or may be negative | 256 // value_len is number of characters or may be negative |
| 261 // a for null-terminated value string | 257 // a for null-terminated value string |
| 262 int bind_text(int index, const char* value, int value_len) { | 258 int bind_text(int index, const char* value, int value_len) { |
| 263 return bind_text(index, value, value_len, SQLITE_TRANSIENT); | 259 return bind_text(index, value, value_len, SQLITE_TRANSIENT); |
| 264 } | 260 } |
| 265 | 261 |
| 266 // value_len is number of characters or may be negative | 262 // value_len is number of characters or may be negative |
| 267 // a for null-terminated value string | 263 // a for null-terminated value string |
| 268 int bind_text(int index, const char* value, int value_len, | 264 int bind_text(int index, const char* value, int value_len, |
| 269 Function dtor); | 265 Function dtor); |
| 270 | 266 |
| 271 int bind_text16(int index, const wchar_t* value) { | 267 int bind_text16(int index, const char16* value) { |
| 272 return bind_text16(index, value, -1, SQLITE_TRANSIENT); | 268 return bind_text16(index, value, -1, SQLITE_TRANSIENT); |
| 273 } | 269 } |
| 274 | 270 |
| 275 // value_len is number of characters or may be negative | 271 // value_len is number of characters or may be negative |
| 276 // a for null-terminated value string | 272 // a for null-terminated value string |
| 277 int bind_text16(int index, const wchar_t* value, int value_len) { | 273 int bind_text16(int index, const char16* value, int value_len) { |
| 278 return bind_text16(index, value, value_len, SQLITE_TRANSIENT); | 274 return bind_text16(index, value, value_len, SQLITE_TRANSIENT); |
| 279 } | 275 } |
| 280 | 276 |
| 281 // value_len is number of characters or may be negative | 277 // value_len is number of characters or may be negative |
| 282 // a for null-terminated value string | 278 // a for null-terminated value string |
| 283 int bind_text16(int index, const wchar_t* value, int value_len, | 279 int bind_text16(int index, const char16* value, int value_len, |
| 284 Function dtor); | 280 Function dtor); |
| 285 | 281 |
| 286 int bind_value(int index, const sqlite3_value* value); | 282 int bind_value(int index, const sqlite3_value* value); |
| 287 | 283 |
| 288 // | 284 // |
| 289 // Column helpers (NOTE: index is 0-based) | 285 // Column helpers (NOTE: index is 0-based) |
| 290 // | 286 // |
| 291 | 287 |
| 292 int column_count(); | 288 int column_count(); |
| 293 int column_type(int index); | 289 int column_type(int index); |
| 294 const wchar_t* column_name16(int index); | |
| 295 const void* column_blob(int index); | 290 const void* column_blob(int index); |
| 296 bool column_blob_as_vector(int index, std::vector<unsigned char>* blob); | 291 bool column_blob_as_vector(int index, std::vector<unsigned char>* blob); |
| 297 bool column_blob_as_string(int index, std::string* blob); | 292 bool column_blob_as_string(int index, std::string* blob); |
| 298 int column_bytes(int index); | 293 int column_bytes(int index); |
| 299 int column_bytes16(int index); | 294 int column_bytes16(int index); |
| 300 double column_double(int index); | 295 double column_double(int index); |
| 301 bool column_bool(int index); | 296 bool column_bool(int index); |
| 302 int column_int(int index); | 297 int column_int(int index); |
| 303 sqlite_int64 column_int64(int index); | 298 sqlite_int64 column_int64(int index); |
| 304 const char* column_text(int index); | 299 const char* column_text(int index); |
| 305 bool column_string(int index, std::string* str); | 300 bool column_string(int index, std::string* str); |
| 306 std::string column_string(int index); | 301 std::string column_string(int index); |
| 307 const wchar_t* column_text16(int index); | 302 const char16* column_text16(int index); |
| 308 bool column_string16(int index, std::wstring* str); | 303 bool column_wstring(int index, std::wstring* str); |
| 309 std::wstring column_string16(int index); | 304 std::wstring column_wstring(int index); |
| 310 | 305 |
| 311 private: | 306 private: |
| 312 DISALLOW_COPY_AND_ASSIGN(SQLStatement); | 307 DISALLOW_COPY_AND_ASSIGN(SQLStatement); |
| 313 }; | 308 }; |
| 314 | 309 |
| 315 // TODO(estade): wrap the following static functions in a namespace. | 310 // TODO(estade): wrap the following static functions in a namespace. |
| 316 | 311 |
| 317 // Opens the DB in the file pointed to by |filepath|. | 312 // Opens the DB in the file pointed to by |filepath|. |
| 318 // See http://www.sqlite.org/capi3ref.html#sqlite3_open for an explanation | 313 // See http://www.sqlite.org/capi3ref.html#sqlite3_open for an explanation |
| 319 // of the return value. | 314 // of the return value. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 347 const char* column_type) { | 342 const char* column_type) { |
| 348 return DoesSqliteColumnExist(db, NULL, table_name, column_name, column_type); | 343 return DoesSqliteColumnExist(db, NULL, table_name, column_name, column_type); |
| 349 } | 344 } |
| 350 | 345 |
| 351 // Test whether a table has one or more rows. Returns true if the table | 346 // Test whether a table has one or more rows. Returns true if the table |
| 352 // has one or more rows and false if the table is empty or doesn't exist. | 347 // has one or more rows and false if the table is empty or doesn't exist. |
| 353 bool DoesSqliteTableHaveRow(sqlite3* db, const char* table_name); | 348 bool DoesSqliteTableHaveRow(sqlite3* db, const char* table_name); |
| 354 | 349 |
| 355 #endif // CHROME_COMMON_SQLITEUTILS_H_ | 350 #endif // CHROME_COMMON_SQLITEUTILS_H_ |
| 356 | 351 |
| OLD | NEW |