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