| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 namespace blink { | 79 namespace blink { |
| 80 | 80 |
| 81 SQLiteStatement::SQLiteStatement(SQLiteDatabase& db, const String& sql) | 81 SQLiteStatement::SQLiteStatement(SQLiteDatabase& db, const String& sql) |
| 82 : database_(db), query_(sql), statement_(0) {} | 82 : database_(db), query_(sql), statement_(0) {} |
| 83 | 83 |
| 84 SQLiteStatement::~SQLiteStatement() { | 84 SQLiteStatement::~SQLiteStatement() { |
| 85 Finalize(); | 85 Finalize(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 int SQLiteStatement::Prepare() { | 88 int SQLiteStatement::Prepare() { |
| 89 ASSERT(!is_prepared_); | 89 DCHECK(!is_prepared_); |
| 90 | 90 |
| 91 CString query = query_.StripWhiteSpace().Utf8(); | 91 CString query = query_.StripWhiteSpace().Utf8(); |
| 92 | 92 |
| 93 // Need to pass non-stack |const char*| and |sqlite3_stmt*| to avoid race | 93 // Need to pass non-stack |const char*| and |sqlite3_stmt*| to avoid race |
| 94 // with Oilpan stack scanning. | 94 // with Oilpan stack scanning. |
| 95 std::unique_ptr<const char*> tail = WTF::WrapUnique(new const char*); | 95 std::unique_ptr<const char*> tail = WTF::WrapUnique(new const char*); |
| 96 std::unique_ptr<sqlite3_stmt*> statement = WTF::WrapUnique(new sqlite3_stmt*); | 96 std::unique_ptr<sqlite3_stmt*> statement = WTF::WrapUnique(new sqlite3_stmt*); |
| 97 *tail = nullptr; | 97 *tail = nullptr; |
| 98 *statement = nullptr; | 98 *statement = nullptr; |
| 99 int error; | 99 int error; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 return SQLITE_OK; | 149 return SQLITE_OK; |
| 150 SQL_DVLOG(1) << "SQL - finalize - " << query_; | 150 SQL_DVLOG(1) << "SQL - finalize - " << query_; |
| 151 int result = sqlite3_finalize(statement_); | 151 int result = sqlite3_finalize(statement_); |
| 152 statement_ = 0; | 152 statement_ = 0; |
| 153 return restrictError(result); | 153 return restrictError(result); |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool SQLiteStatement::ExecuteCommand() { | 156 bool SQLiteStatement::ExecuteCommand() { |
| 157 if (!statement_ && Prepare() != SQLITE_OK) | 157 if (!statement_ && Prepare() != SQLITE_OK) |
| 158 return false; | 158 return false; |
| 159 ASSERT(is_prepared_); | 159 DCHECK(is_prepared_); |
| 160 if (Step() != SQLITE_DONE) { | 160 if (Step() != SQLITE_DONE) { |
| 161 Finalize(); | 161 Finalize(); |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 Finalize(); | 164 Finalize(); |
| 165 return true; | 165 return true; |
| 166 } | 166 } |
| 167 | 167 |
| 168 int SQLiteStatement::BindText(int index, const String& text) { | 168 int SQLiteStatement::BindText(int index, const String& text) { |
| 169 ASSERT(is_prepared_); | 169 DCHECK(is_prepared_); |
| 170 ASSERT(index > 0); | 170 DCHECK_GT(index, 0); |
| 171 ASSERT(static_cast<unsigned>(index) <= BindParameterCount()); | 171 DCHECK_LE(static_cast<unsigned>(index), BindParameterCount()); |
| 172 | 172 |
| 173 String text16(text); | 173 String text16(text); |
| 174 text16.Ensure16Bit(); | 174 text16.Ensure16Bit(); |
| 175 return restrictError( | 175 return restrictError( |
| 176 sqlite3_bind_text16(statement_, index, text16.Characters16(), | 176 sqlite3_bind_text16(statement_, index, text16.Characters16(), |
| 177 sizeof(UChar) * text16.length(), SQLITE_TRANSIENT)); | 177 sizeof(UChar) * text16.length(), SQLITE_TRANSIENT)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 int SQLiteStatement::BindDouble(int index, double number) { | 180 int SQLiteStatement::BindDouble(int index, double number) { |
| 181 ASSERT(is_prepared_); | 181 DCHECK(is_prepared_); |
| 182 ASSERT(index > 0); | 182 DCHECK_GT(index, 0); |
| 183 ASSERT(static_cast<unsigned>(index) <= BindParameterCount()); | 183 DCHECK_LE(static_cast<unsigned>(index), BindParameterCount()); |
| 184 | 184 |
| 185 return restrictError(sqlite3_bind_double(statement_, index, number)); | 185 return restrictError(sqlite3_bind_double(statement_, index, number)); |
| 186 } | 186 } |
| 187 | 187 |
| 188 int SQLiteStatement::BindNull(int index) { | 188 int SQLiteStatement::BindNull(int index) { |
| 189 ASSERT(is_prepared_); | 189 DCHECK(is_prepared_); |
| 190 ASSERT(index > 0); | 190 DCHECK_GT(index, 0); |
| 191 ASSERT(static_cast<unsigned>(index) <= BindParameterCount()); | 191 DCHECK_LE(static_cast<unsigned>(index), BindParameterCount()); |
| 192 | 192 |
| 193 return restrictError(sqlite3_bind_null(statement_, index)); | 193 return restrictError(sqlite3_bind_null(statement_, index)); |
| 194 } | 194 } |
| 195 | 195 |
| 196 int SQLiteStatement::BindValue(int index, const SQLValue& value) { | 196 int SQLiteStatement::BindValue(int index, const SQLValue& value) { |
| 197 switch (value.GetType()) { | 197 switch (value.GetType()) { |
| 198 case SQLValue::kStringValue: | 198 case SQLValue::kStringValue: |
| 199 return BindText(index, value.GetString()); | 199 return BindText(index, value.GetString()); |
| 200 case SQLValue::kNumberValue: | 200 case SQLValue::kNumberValue: |
| 201 return BindDouble(index, value.Number()); | 201 return BindDouble(index, value.Number()); |
| 202 case SQLValue::kNullValue: | 202 case SQLValue::kNullValue: |
| 203 return BindNull(index); | 203 return BindNull(index); |
| 204 } | 204 } |
| 205 | 205 |
| 206 ASSERT_NOT_REACHED(); | 206 NOTREACHED(); |
| 207 return SQLITE_ERROR; | 207 return SQLITE_ERROR; |
| 208 } | 208 } |
| 209 | 209 |
| 210 unsigned SQLiteStatement::BindParameterCount() const { | 210 unsigned SQLiteStatement::BindParameterCount() const { |
| 211 ASSERT(is_prepared_); | 211 DCHECK(is_prepared_); |
| 212 if (!statement_) | 212 if (!statement_) |
| 213 return 0; | 213 return 0; |
| 214 return sqlite3_bind_parameter_count(statement_); | 214 return sqlite3_bind_parameter_count(statement_); |
| 215 } | 215 } |
| 216 | 216 |
| 217 int SQLiteStatement::ColumnCount() { | 217 int SQLiteStatement::ColumnCount() { |
| 218 ASSERT(is_prepared_); | 218 DCHECK(is_prepared_); |
| 219 if (!statement_) | 219 if (!statement_) |
| 220 return 0; | 220 return 0; |
| 221 return sqlite3_data_count(statement_); | 221 return sqlite3_data_count(statement_); |
| 222 } | 222 } |
| 223 | 223 |
| 224 String SQLiteStatement::GetColumnName(int col) { | 224 String SQLiteStatement::GetColumnName(int col) { |
| 225 ASSERT(col >= 0); | 225 DCHECK_GE(col, 0); |
| 226 if (!statement_) | 226 if (!statement_) |
| 227 if (PrepareAndStep() != SQLITE_ROW) | 227 if (PrepareAndStep() != SQLITE_ROW) |
| 228 return String(); | 228 return String(); |
| 229 if (ColumnCount() <= col) | 229 if (ColumnCount() <= col) |
| 230 return String(); | 230 return String(); |
| 231 return String( | 231 return String( |
| 232 reinterpret_cast<const UChar*>(sqlite3_column_name16(statement_, col))); | 232 reinterpret_cast<const UChar*>(sqlite3_column_name16(statement_, col))); |
| 233 } | 233 } |
| 234 | 234 |
| 235 SQLValue SQLiteStatement::GetColumnValue(int col) { | 235 SQLValue SQLiteStatement::GetColumnValue(int col) { |
| 236 ASSERT(col >= 0); | 236 DCHECK_GE(col, 0); |
| 237 if (!statement_) | 237 if (!statement_) |
| 238 if (PrepareAndStep() != SQLITE_ROW) | 238 if (PrepareAndStep() != SQLITE_ROW) |
| 239 return SQLValue(); | 239 return SQLValue(); |
| 240 if (ColumnCount() <= col) | 240 if (ColumnCount() <= col) |
| 241 return SQLValue(); | 241 return SQLValue(); |
| 242 | 242 |
| 243 // SQLite is typed per value. optional column types are | 243 // SQLite is typed per value. optional column types are |
| 244 // "(mostly) ignored" | 244 // "(mostly) ignored" |
| 245 sqlite3_value* value = sqlite3_column_value(statement_, col); | 245 sqlite3_value* value = sqlite3_column_value(statement_, col); |
| 246 switch (sqlite3_value_type(value)) { | 246 switch (sqlite3_value_type(value)) { |
| 247 case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use | 247 case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use |
| 248 // FLOAT -case | 248 // FLOAT -case |
| 249 case SQLITE_FLOAT: | 249 case SQLITE_FLOAT: |
| 250 return SQLValue(sqlite3_value_double(value)); | 250 return SQLValue(sqlite3_value_double(value)); |
| 251 case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT | 251 case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT |
| 252 // -case | 252 // -case |
| 253 case SQLITE_TEXT: { | 253 case SQLITE_TEXT: { |
| 254 const UChar* string = | 254 const UChar* string = |
| 255 reinterpret_cast<const UChar*>(sqlite3_value_text16(value)); | 255 reinterpret_cast<const UChar*>(sqlite3_value_text16(value)); |
| 256 unsigned length = sqlite3_value_bytes16(value) / sizeof(UChar); | 256 unsigned length = sqlite3_value_bytes16(value) / sizeof(UChar); |
| 257 return SQLValue(StringImpl::Create8BitIfPossible(string, length)); | 257 return SQLValue(StringImpl::Create8BitIfPossible(string, length)); |
| 258 } | 258 } |
| 259 case SQLITE_NULL: | 259 case SQLITE_NULL: |
| 260 return SQLValue(); | 260 return SQLValue(); |
| 261 default: | 261 default: |
| 262 break; | 262 break; |
| 263 } | 263 } |
| 264 ASSERT_NOT_REACHED(); | 264 NOTREACHED(); |
| 265 return SQLValue(); | 265 return SQLValue(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 String SQLiteStatement::GetColumnText(int col) { | 268 String SQLiteStatement::GetColumnText(int col) { |
| 269 ASSERT(col >= 0); | 269 DCHECK_GE(col, 0); |
| 270 if (!statement_) | 270 if (!statement_) |
| 271 if (PrepareAndStep() != SQLITE_ROW) | 271 if (PrepareAndStep() != SQLITE_ROW) |
| 272 return String(); | 272 return String(); |
| 273 if (ColumnCount() <= col) | 273 if (ColumnCount() <= col) |
| 274 return String(); | 274 return String(); |
| 275 const UChar* string = | 275 const UChar* string = |
| 276 reinterpret_cast<const UChar*>(sqlite3_column_text16(statement_, col)); | 276 reinterpret_cast<const UChar*>(sqlite3_column_text16(statement_, col)); |
| 277 return StringImpl::Create8BitIfPossible( | 277 return StringImpl::Create8BitIfPossible( |
| 278 string, sqlite3_column_bytes16(statement_, col) / sizeof(UChar)); | 278 string, sqlite3_column_bytes16(statement_, col) / sizeof(UChar)); |
| 279 } | 279 } |
| 280 | 280 |
| 281 int SQLiteStatement::GetColumnInt(int col) { | 281 int SQLiteStatement::GetColumnInt(int col) { |
| 282 ASSERT(col >= 0); | 282 DCHECK_GE(col, 0); |
| 283 if (!statement_) | 283 if (!statement_) |
| 284 if (PrepareAndStep() != SQLITE_ROW) | 284 if (PrepareAndStep() != SQLITE_ROW) |
| 285 return 0; | 285 return 0; |
| 286 if (ColumnCount() <= col) | 286 if (ColumnCount() <= col) |
| 287 return 0; | 287 return 0; |
| 288 return sqlite3_column_int(statement_, col); | 288 return sqlite3_column_int(statement_, col); |
| 289 } | 289 } |
| 290 | 290 |
| 291 int64_t SQLiteStatement::GetColumnInt64(int col) { | 291 int64_t SQLiteStatement::GetColumnInt64(int col) { |
| 292 ASSERT(col >= 0); | 292 DCHECK_GE(col, 0); |
| 293 if (!statement_) | 293 if (!statement_) |
| 294 if (PrepareAndStep() != SQLITE_ROW) | 294 if (PrepareAndStep() != SQLITE_ROW) |
| 295 return 0; | 295 return 0; |
| 296 if (ColumnCount() <= col) | 296 if (ColumnCount() <= col) |
| 297 return 0; | 297 return 0; |
| 298 return sqlite3_column_int64(statement_, col); | 298 return sqlite3_column_int64(statement_, col); |
| 299 } | 299 } |
| 300 | 300 |
| 301 } // namespace blink | 301 } // namespace blink |
| OLD | NEW |