/src/mozilla-central/storage/mozStorageStatement.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
3 | | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include <limits.h> |
8 | | #include <stdio.h> |
9 | | |
10 | | #include "nsError.h" |
11 | | #include "nsMemory.h" |
12 | | #include "nsThreadUtils.h" |
13 | | #include "nsIClassInfoImpl.h" |
14 | | #include "Variant.h" |
15 | | |
16 | | #include "mozIStorageError.h" |
17 | | |
18 | | #include "mozStorageBindingParams.h" |
19 | | #include "mozStorageConnection.h" |
20 | | #include "mozStorageStatementJSHelper.h" |
21 | | #include "mozStoragePrivateHelpers.h" |
22 | | #include "mozStorageStatementParams.h" |
23 | | #include "mozStorageStatementRow.h" |
24 | | #include "mozStorageStatement.h" |
25 | | #include "GeckoProfiler.h" |
26 | | |
27 | | #include "mozilla/Logging.h" |
28 | | #include "mozilla/Printf.h" |
29 | | |
30 | | |
31 | | extern mozilla::LazyLogModule gStorageLog; |
32 | | |
33 | | namespace mozilla { |
34 | | namespace storage { |
35 | | |
36 | | //////////////////////////////////////////////////////////////////////////////// |
37 | | //// nsIClassInfo |
38 | | |
39 | | NS_IMPL_CI_INTERFACE_GETTER(Statement, |
40 | | mozIStorageStatement, |
41 | | mozIStorageBaseStatement, |
42 | | mozIStorageBindingParams, |
43 | | mozIStorageValueArray, |
44 | | mozilla::storage::StorageBaseStatementInternal) |
45 | | |
46 | | class StatementClassInfo : public nsIClassInfo |
47 | | { |
48 | | public: |
49 | 0 | constexpr StatementClassInfo() {} |
50 | | |
51 | | NS_DECL_ISUPPORTS_INHERITED |
52 | | |
53 | | NS_IMETHOD |
54 | | GetInterfaces(uint32_t *_count, nsIID ***_array) override |
55 | 0 | { |
56 | 0 | return NS_CI_INTERFACE_GETTER_NAME(Statement)(_count, _array); |
57 | 0 | } |
58 | | |
59 | | NS_IMETHOD |
60 | | GetScriptableHelper(nsIXPCScriptable **_helper) override |
61 | 0 | { |
62 | 0 | static StatementJSHelper sJSHelper; |
63 | 0 | *_helper = &sJSHelper; |
64 | 0 | return NS_OK; |
65 | 0 | } |
66 | | |
67 | | NS_IMETHOD |
68 | | GetContractID(nsACString& aContractID) override |
69 | 0 | { |
70 | 0 | aContractID.SetIsVoid(true); |
71 | 0 | return NS_OK; |
72 | 0 | } |
73 | | |
74 | | NS_IMETHOD |
75 | | GetClassDescription(nsACString& aDesc) override |
76 | 0 | { |
77 | 0 | aDesc.SetIsVoid(true); |
78 | 0 | return NS_OK; |
79 | 0 | } |
80 | | |
81 | | NS_IMETHOD |
82 | | GetClassID(nsCID **_id) override |
83 | 0 | { |
84 | 0 | *_id = nullptr; |
85 | 0 | return NS_OK; |
86 | 0 | } |
87 | | |
88 | | NS_IMETHOD |
89 | | GetFlags(uint32_t *_flags) override |
90 | 0 | { |
91 | 0 | *_flags = 0; |
92 | 0 | return NS_OK; |
93 | 0 | } |
94 | | |
95 | | NS_IMETHOD |
96 | | GetClassIDNoAlloc(nsCID *_cid) override |
97 | 0 | { |
98 | 0 | return NS_ERROR_NOT_AVAILABLE; |
99 | 0 | } |
100 | | }; |
101 | | |
102 | 0 | NS_IMETHODIMP_(MozExternalRefCountType) StatementClassInfo::AddRef() { return 2; } |
103 | 0 | NS_IMETHODIMP_(MozExternalRefCountType) StatementClassInfo::Release() { return 1; } |
104 | | NS_IMPL_QUERY_INTERFACE(StatementClassInfo, nsIClassInfo) |
105 | | |
106 | | static StatementClassInfo sStatementClassInfo; |
107 | | |
108 | | //////////////////////////////////////////////////////////////////////////////// |
109 | | //// Statement |
110 | | |
111 | | Statement::Statement() |
112 | | : StorageBaseStatementInternal() |
113 | | , mDBStatement(nullptr) |
114 | | , mParamCount(0) |
115 | | , mResultColumnCount(0) |
116 | | , mColumnNames() |
117 | | , mExecuting(false) |
118 | 0 | { |
119 | 0 | } |
120 | | |
121 | | nsresult |
122 | | Statement::initialize(Connection *aDBConnection, |
123 | | sqlite3 *aNativeConnection, |
124 | | const nsACString &aSQLStatement) |
125 | 0 | { |
126 | 0 | MOZ_ASSERT(aDBConnection, "No database connection given!"); |
127 | 0 | MOZ_ASSERT(aDBConnection->isConnectionReadyOnThisThread(), |
128 | 0 | "Database connection should be valid"); |
129 | 0 | MOZ_ASSERT(!mDBStatement, "Statement already initialized!"); |
130 | 0 | MOZ_ASSERT(aNativeConnection, "No native connection given!"); |
131 | 0 |
|
132 | 0 | int srv = aDBConnection->prepareStatement(aNativeConnection, |
133 | 0 | PromiseFlatCString(aSQLStatement), |
134 | 0 | &mDBStatement); |
135 | 0 | if (srv != SQLITE_OK) { |
136 | 0 | MOZ_LOG(gStorageLog, LogLevel::Error, |
137 | 0 | ("Sqlite statement prepare error: %d '%s'", srv, |
138 | 0 | ::sqlite3_errmsg(aNativeConnection))); |
139 | 0 | MOZ_LOG(gStorageLog, LogLevel::Error, |
140 | 0 | ("Statement was: '%s'", PromiseFlatCString(aSQLStatement).get())); |
141 | 0 | return NS_ERROR_FAILURE; |
142 | 0 | } |
143 | 0 |
|
144 | 0 | MOZ_LOG(gStorageLog, LogLevel::Debug, ("Initialized statement '%s' (0x%p)", |
145 | 0 | PromiseFlatCString(aSQLStatement).get(), |
146 | 0 | mDBStatement)); |
147 | 0 |
|
148 | 0 | mDBConnection = aDBConnection; |
149 | 0 | mNativeConnection = aNativeConnection; |
150 | 0 | mParamCount = ::sqlite3_bind_parameter_count(mDBStatement); |
151 | 0 | mResultColumnCount = ::sqlite3_column_count(mDBStatement); |
152 | 0 | mColumnNames.Clear(); |
153 | 0 |
|
154 | 0 | nsCString* columnNames = mColumnNames.AppendElements(mResultColumnCount); |
155 | 0 | for (uint32_t i = 0; i < mResultColumnCount; i++) { |
156 | 0 | const char *name = ::sqlite3_column_name(mDBStatement, i); |
157 | 0 | columnNames[i].Assign(name); |
158 | 0 | } |
159 | 0 |
|
160 | | #ifdef DEBUG |
161 | | // We want to try and test for LIKE and that consumers are using |
162 | | // escapeStringForLIKE instead of just trusting user input. The idea to |
163 | | // check to see if they are binding a parameter after like instead of just |
164 | | // using a string. We only do this in debug builds because it's expensive! |
165 | | const nsCaseInsensitiveCStringComparator c; |
166 | | nsACString::const_iterator start, end, e; |
167 | | aSQLStatement.BeginReading(start); |
168 | | aSQLStatement.EndReading(end); |
169 | | e = end; |
170 | | while (::FindInReadable(NS_LITERAL_CSTRING(" LIKE"), start, e, c)) { |
171 | | // We have a LIKE in here, so we perform our tests |
172 | | // FindInReadable moves the iterator, so we have to get a new one for |
173 | | // each test we perform. |
174 | | nsACString::const_iterator s1, s2, s3; |
175 | | s1 = s2 = s3 = start; |
176 | | |
177 | | if (!(::FindInReadable(NS_LITERAL_CSTRING(" LIKE ?"), s1, end, c) || |
178 | | ::FindInReadable(NS_LITERAL_CSTRING(" LIKE :"), s2, end, c) || |
179 | | ::FindInReadable(NS_LITERAL_CSTRING(" LIKE @"), s3, end, c))) { |
180 | | // At this point, we didn't find a LIKE statement followed by ?, :, |
181 | | // or @, all of which are valid characters for binding a parameter. |
182 | | // We will warn the consumer that they may not be safely using LIKE. |
183 | | NS_WARNING("Unsafe use of LIKE detected! Please ensure that you " |
184 | | "are using mozIStorageStatement::escapeStringForLIKE " |
185 | | "and that you are binding that result to the statement " |
186 | | "to prevent SQL injection attacks."); |
187 | | } |
188 | | |
189 | | // resetting start and e |
190 | | start = e; |
191 | | e = end; |
192 | | } |
193 | | #endif |
194 | |
|
195 | 0 | return NS_OK; |
196 | 0 | } |
197 | | |
198 | | mozIStorageBindingParams * |
199 | | Statement::getParams() |
200 | 0 | { |
201 | 0 | nsresult rv; |
202 | 0 |
|
203 | 0 | // If we do not have an array object yet, make it. |
204 | 0 | if (!mParamsArray) { |
205 | 0 | nsCOMPtr<mozIStorageBindingParamsArray> array; |
206 | 0 | rv = NewBindingParamsArray(getter_AddRefs(array)); |
207 | 0 | NS_ENSURE_SUCCESS(rv, nullptr); |
208 | 0 |
|
209 | 0 | mParamsArray = static_cast<BindingParamsArray *>(array.get()); |
210 | 0 | } |
211 | 0 |
|
212 | 0 | // If there isn't already any rows added, we'll have to add one to use. |
213 | 0 | if (mParamsArray->length() == 0) { |
214 | 0 | RefPtr<BindingParams> params(new BindingParams(mParamsArray, this)); |
215 | 0 | NS_ENSURE_TRUE(params, nullptr); |
216 | 0 |
|
217 | 0 | rv = mParamsArray->AddParams(params); |
218 | 0 | NS_ENSURE_SUCCESS(rv, nullptr); |
219 | 0 |
|
220 | 0 | // We have to unlock our params because AddParams locks them. This is safe |
221 | 0 | // because no reference to the params object was, or ever will be given out. |
222 | 0 | params->unlock(this); |
223 | 0 |
|
224 | 0 | // We also want to lock our array at this point - we don't want anything to |
225 | 0 | // be added to it. Nothing has, or will ever get a reference to it, but we |
226 | 0 | // will get additional safety checks via assertions by doing this. |
227 | 0 | mParamsArray->lock(); |
228 | 0 | } |
229 | 0 |
|
230 | 0 | return *mParamsArray->begin(); |
231 | 0 | } |
232 | | |
233 | | Statement::~Statement() |
234 | 0 | { |
235 | 0 | (void)internalFinalize(true); |
236 | 0 | } |
237 | | |
238 | | //////////////////////////////////////////////////////////////////////////////// |
239 | | //// nsISupports |
240 | | |
241 | | NS_IMPL_ADDREF(Statement) |
242 | | NS_IMPL_RELEASE(Statement) |
243 | | |
244 | 0 | NS_INTERFACE_MAP_BEGIN(Statement) |
245 | 0 | NS_INTERFACE_MAP_ENTRY(mozIStorageStatement) |
246 | 0 | NS_INTERFACE_MAP_ENTRY(mozIStorageBaseStatement) |
247 | 0 | NS_INTERFACE_MAP_ENTRY(mozIStorageBindingParams) |
248 | 0 | NS_INTERFACE_MAP_ENTRY(mozIStorageValueArray) |
249 | 0 | NS_INTERFACE_MAP_ENTRY(mozilla::storage::StorageBaseStatementInternal) |
250 | 0 | if (aIID.Equals(NS_GET_IID(nsIClassInfo))) { |
251 | 0 | foundInterface = static_cast<nsIClassInfo *>(&sStatementClassInfo); |
252 | 0 | } |
253 | 0 | else |
254 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageStatement) |
255 | 0 | NS_INTERFACE_MAP_END |
256 | | |
257 | | |
258 | | //////////////////////////////////////////////////////////////////////////////// |
259 | | //// StorageBaseStatementInternal |
260 | | |
261 | | Connection * |
262 | | Statement::getOwner() |
263 | 0 | { |
264 | 0 | return mDBConnection; |
265 | 0 | } |
266 | | |
267 | | int |
268 | | Statement::getAsyncStatement(sqlite3_stmt **_stmt) |
269 | 0 | { |
270 | 0 | // If we have no statement, we shouldn't be calling this method! |
271 | 0 | NS_ASSERTION(mDBStatement != nullptr, "We have no statement to clone!"); |
272 | 0 |
|
273 | 0 | // If we do not yet have a cached async statement, clone our statement now. |
274 | 0 | if (!mAsyncStatement) { |
275 | 0 | nsDependentCString sql(::sqlite3_sql(mDBStatement)); |
276 | 0 | int rc = mDBConnection->prepareStatement(mNativeConnection, sql, |
277 | 0 | &mAsyncStatement); |
278 | 0 | if (rc != SQLITE_OK) { |
279 | 0 | *_stmt = nullptr; |
280 | 0 | return rc; |
281 | 0 | } |
282 | 0 | |
283 | 0 | MOZ_LOG(gStorageLog, LogLevel::Debug, |
284 | 0 | ("Cloned statement 0x%p to 0x%p", mDBStatement, mAsyncStatement)); |
285 | 0 | } |
286 | 0 |
|
287 | 0 | *_stmt = mAsyncStatement; |
288 | 0 | return SQLITE_OK; |
289 | 0 | } |
290 | | |
291 | | nsresult |
292 | | Statement::getAsynchronousStatementData(StatementData &_data) |
293 | 0 | { |
294 | 0 | if (!mDBStatement) |
295 | 0 | return NS_ERROR_UNEXPECTED; |
296 | 0 | |
297 | 0 | sqlite3_stmt *stmt; |
298 | 0 | int rc = getAsyncStatement(&stmt); |
299 | 0 | if (rc != SQLITE_OK) |
300 | 0 | return convertResultCode(rc); |
301 | 0 | |
302 | 0 | _data = StatementData(stmt, bindingParamsArray(), this); |
303 | 0 |
|
304 | 0 | return NS_OK; |
305 | 0 | } |
306 | | |
307 | | already_AddRefed<mozIStorageBindingParams> |
308 | | Statement::newBindingParams(mozIStorageBindingParamsArray *aOwner) |
309 | 0 | { |
310 | 0 | nsCOMPtr<mozIStorageBindingParams> params = new BindingParams(aOwner, this); |
311 | 0 | return params.forget(); |
312 | 0 | } |
313 | | |
314 | | |
315 | | //////////////////////////////////////////////////////////////////////////////// |
316 | | //// mozIStorageStatement |
317 | | |
318 | | // proxy to StorageBaseStatementInternal using its define helper. |
319 | | MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(Statement, (void)0;) |
320 | | |
321 | | NS_IMETHODIMP |
322 | | Statement::Clone(mozIStorageStatement **_statement) |
323 | 0 | { |
324 | 0 | RefPtr<Statement> statement(new Statement()); |
325 | 0 | NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY); |
326 | 0 |
|
327 | 0 | nsAutoCString sql(::sqlite3_sql(mDBStatement)); |
328 | 0 | nsresult rv = statement->initialize(mDBConnection, mNativeConnection, sql); |
329 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
330 | 0 |
|
331 | 0 | statement.forget(_statement); |
332 | 0 | return NS_OK; |
333 | 0 | } |
334 | | |
335 | | NS_IMETHODIMP |
336 | | Statement::Finalize() |
337 | 0 | { |
338 | 0 | return internalFinalize(false); |
339 | 0 | } |
340 | | |
341 | | nsresult |
342 | | Statement::internalFinalize(bool aDestructing) |
343 | 0 | { |
344 | 0 | if (!mDBStatement) |
345 | 0 | return NS_OK; |
346 | 0 | |
347 | 0 | int srv = SQLITE_OK; |
348 | 0 |
|
349 | 0 | { |
350 | 0 | // If the statement ends up being finalized twice, the second finalization |
351 | 0 | // would apply to a dangling pointer and may cause unexpected consequences. |
352 | 0 | // Thus we must be sure that the connection state won't change during this |
353 | 0 | // operation, to avoid racing with finalizations made by the closing |
354 | 0 | // connection. See Connection::internalClose(). |
355 | 0 | MutexAutoLock lockedScope(mDBConnection->sharedAsyncExecutionMutex); |
356 | 0 | if (!mDBConnection->isClosed(lockedScope)) { |
357 | 0 | MOZ_LOG(gStorageLog, LogLevel::Debug, ("Finalizing statement '%s' during garbage-collection", |
358 | 0 | ::sqlite3_sql(mDBStatement))); |
359 | 0 | srv = ::sqlite3_finalize(mDBStatement); |
360 | 0 | } |
361 | | #ifdef DEBUG |
362 | | else { |
363 | | // The database connection is closed. The sqlite |
364 | | // statement has either been finalized already by the connection |
365 | | // or is about to be finalized by the connection. |
366 | | // |
367 | | // Finalizing it here would be useless and segfaultish. |
368 | | // |
369 | | // Note that we can't display the statement itself, as the data structure |
370 | | // is not valid anymore. However, the address shown here should help |
371 | | // developers correlate with the more complete debug message triggered |
372 | | // by AsyncClose(). |
373 | | |
374 | | SmprintfPointer msg = ::mozilla::Smprintf("SQL statement (%p) should have been finalized" |
375 | | " before garbage-collection. For more details on this statement, set" |
376 | | " NSPR_LOG_MESSAGES=mozStorage:5 .", |
377 | | mDBStatement); |
378 | | NS_WARNING(msg.get()); |
379 | | |
380 | | // Use %s so we aren't exposing random strings to printf interpolation. |
381 | | MOZ_LOG(gStorageLog, LogLevel::Warning, ("%s", msg.get())); |
382 | | } |
383 | | #endif // DEBUG |
384 | | } |
385 | 0 |
|
386 | 0 | mDBStatement = nullptr; |
387 | 0 |
|
388 | 0 | if (mAsyncStatement) { |
389 | 0 | // If the destructor called us, there are no pending async statements (they |
390 | 0 | // hold a reference to us) and we can/must just kill the statement directly. |
391 | 0 | if (aDestructing) |
392 | 0 | destructorAsyncFinalize(); |
393 | 0 | else |
394 | 0 | asyncFinalize(); |
395 | 0 | } |
396 | 0 |
|
397 | 0 | // Release the holders, so they can release the reference to us. |
398 | 0 | mStatementParamsHolder = nullptr; |
399 | 0 | mStatementRowHolder = nullptr; |
400 | 0 |
|
401 | 0 | return convertResultCode(srv); |
402 | 0 | } |
403 | | |
404 | | NS_IMETHODIMP |
405 | | Statement::GetParameterCount(uint32_t *_parameterCount) |
406 | 0 | { |
407 | 0 | if (!mDBStatement) |
408 | 0 | return NS_ERROR_NOT_INITIALIZED; |
409 | 0 | |
410 | 0 | *_parameterCount = mParamCount; |
411 | 0 | return NS_OK; |
412 | 0 | } |
413 | | |
414 | | NS_IMETHODIMP |
415 | | Statement::GetParameterName(uint32_t aParamIndex, |
416 | | nsACString &_name) |
417 | 0 | { |
418 | 0 | if (!mDBStatement) |
419 | 0 | return NS_ERROR_NOT_INITIALIZED; |
420 | 0 | ENSURE_INDEX_VALUE(aParamIndex, mParamCount); |
421 | 0 |
|
422 | 0 | const char *name = ::sqlite3_bind_parameter_name(mDBStatement, |
423 | 0 | aParamIndex + 1); |
424 | 0 | if (name == nullptr) { |
425 | 0 | // this thing had no name, so fake one |
426 | 0 | nsAutoCString fakeName(":"); |
427 | 0 | fakeName.AppendInt(aParamIndex); |
428 | 0 | _name.Assign(fakeName); |
429 | 0 | } |
430 | 0 | else { |
431 | 0 | _name.Assign(nsDependentCString(name)); |
432 | 0 | } |
433 | 0 |
|
434 | 0 | return NS_OK; |
435 | 0 | } |
436 | | |
437 | | NS_IMETHODIMP |
438 | | Statement::GetParameterIndex(const nsACString &aName, |
439 | | uint32_t *_index) |
440 | 0 | { |
441 | 0 | if (!mDBStatement) |
442 | 0 | return NS_ERROR_NOT_INITIALIZED; |
443 | 0 | |
444 | 0 | // We do not accept any forms of names other than ":name", but we need to add |
445 | 0 | // the colon for SQLite. |
446 | 0 | nsAutoCString name(":"); |
447 | 0 | name.Append(aName); |
448 | 0 | int ind = ::sqlite3_bind_parameter_index(mDBStatement, name.get()); |
449 | 0 | if (ind == 0) // Named parameter not found. |
450 | 0 | return NS_ERROR_INVALID_ARG; |
451 | 0 | |
452 | 0 | *_index = ind - 1; // SQLite indexes are 1-based, we are 0-based. |
453 | 0 |
|
454 | 0 | return NS_OK; |
455 | 0 | } |
456 | | |
457 | | NS_IMETHODIMP |
458 | | Statement::GetColumnCount(uint32_t *_columnCount) |
459 | 0 | { |
460 | 0 | if (!mDBStatement) |
461 | 0 | return NS_ERROR_NOT_INITIALIZED; |
462 | 0 | |
463 | 0 | *_columnCount = mResultColumnCount; |
464 | 0 | return NS_OK; |
465 | 0 | } |
466 | | |
467 | | NS_IMETHODIMP |
468 | | Statement::GetColumnName(uint32_t aColumnIndex, |
469 | | nsACString &_name) |
470 | 0 | { |
471 | 0 | if (!mDBStatement) |
472 | 0 | return NS_ERROR_NOT_INITIALIZED; |
473 | 0 | ENSURE_INDEX_VALUE(aColumnIndex, mResultColumnCount); |
474 | 0 |
|
475 | 0 | const char *cname = ::sqlite3_column_name(mDBStatement, aColumnIndex); |
476 | 0 | _name.Assign(nsDependentCString(cname)); |
477 | 0 |
|
478 | 0 | return NS_OK; |
479 | 0 | } |
480 | | |
481 | | NS_IMETHODIMP |
482 | | Statement::GetColumnIndex(const nsACString &aName, |
483 | | uint32_t *_index) |
484 | 0 | { |
485 | 0 | if (!mDBStatement) |
486 | 0 | return NS_ERROR_NOT_INITIALIZED; |
487 | 0 | |
488 | 0 | // Surprisingly enough, SQLite doesn't provide an API for this. We have to |
489 | 0 | // determine it ourselves sadly. |
490 | 0 | for (uint32_t i = 0; i < mResultColumnCount; i++) { |
491 | 0 | if (mColumnNames[i].Equals(aName)) { |
492 | 0 | *_index = i; |
493 | 0 | return NS_OK; |
494 | 0 | } |
495 | 0 | } |
496 | 0 |
|
497 | 0 | return NS_ERROR_INVALID_ARG; |
498 | 0 | } |
499 | | |
500 | | NS_IMETHODIMP |
501 | | Statement::Reset() |
502 | 0 | { |
503 | 0 | if (!mDBStatement) |
504 | 0 | return NS_ERROR_NOT_INITIALIZED; |
505 | 0 | |
506 | | #ifdef DEBUG |
507 | | MOZ_LOG(gStorageLog, LogLevel::Debug, ("Resetting statement: '%s'", |
508 | | ::sqlite3_sql(mDBStatement))); |
509 | | |
510 | | checkAndLogStatementPerformance(mDBStatement); |
511 | | #endif |
512 | | |
513 | 0 | mParamsArray = nullptr; |
514 | 0 | (void)sqlite3_reset(mDBStatement); |
515 | 0 | (void)sqlite3_clear_bindings(mDBStatement); |
516 | 0 |
|
517 | 0 | mExecuting = false; |
518 | 0 |
|
519 | 0 | return NS_OK; |
520 | 0 | } |
521 | | |
522 | | NS_IMETHODIMP |
523 | | Statement::BindParameters(mozIStorageBindingParamsArray *aParameters) |
524 | 0 | { |
525 | 0 | NS_ENSURE_ARG_POINTER(aParameters); |
526 | 0 |
|
527 | 0 | if (!mDBStatement) |
528 | 0 | return NS_ERROR_NOT_INITIALIZED; |
529 | 0 | |
530 | 0 | BindingParamsArray *array = static_cast<BindingParamsArray *>(aParameters); |
531 | 0 | if (array->getOwner() != this) |
532 | 0 | return NS_ERROR_UNEXPECTED; |
533 | 0 | |
534 | 0 | if (array->length() == 0) |
535 | 0 | return NS_ERROR_UNEXPECTED; |
536 | 0 | |
537 | 0 | mParamsArray = array; |
538 | 0 | mParamsArray->lock(); |
539 | 0 |
|
540 | 0 | return NS_OK; |
541 | 0 | } |
542 | | |
543 | | NS_IMETHODIMP |
544 | | Statement::Execute() |
545 | 0 | { |
546 | 0 | if (!mDBStatement) |
547 | 0 | return NS_ERROR_NOT_INITIALIZED; |
548 | 0 | |
549 | 0 | bool ret; |
550 | 0 | nsresult rv = ExecuteStep(&ret); |
551 | 0 | nsresult rv2 = Reset(); |
552 | 0 |
|
553 | 0 | return NS_FAILED(rv) ? rv : rv2; |
554 | 0 | } |
555 | | |
556 | | NS_IMETHODIMP |
557 | | Statement::ExecuteStep(bool *_moreResults) |
558 | 0 | { |
559 | 0 | AUTO_PROFILER_LABEL("Statement::ExecuteStep", OTHER); |
560 | 0 |
|
561 | 0 | if (!mDBStatement) |
562 | 0 | return NS_ERROR_NOT_INITIALIZED; |
563 | 0 | |
564 | 0 | // Bind any parameters first before executing. |
565 | 0 | if (mParamsArray) { |
566 | 0 | // If we have more than one row of parameters to bind, they shouldn't be |
567 | 0 | // calling this method (and instead use executeAsync). |
568 | 0 | if (mParamsArray->length() != 1) |
569 | 0 | return NS_ERROR_UNEXPECTED; |
570 | 0 | |
571 | 0 | BindingParamsArray::iterator row = mParamsArray->begin(); |
572 | 0 | nsCOMPtr<IStorageBindingParamsInternal> bindingInternal = |
573 | 0 | do_QueryInterface(*row); |
574 | 0 | nsCOMPtr<mozIStorageError> error = bindingInternal->bind(mDBStatement); |
575 | 0 | if (error) { |
576 | 0 | int32_t srv; |
577 | 0 | (void)error->GetResult(&srv); |
578 | 0 | return convertResultCode(srv); |
579 | 0 | } |
580 | 0 | |
581 | 0 | // We have bound, so now we can clear our array. |
582 | 0 | mParamsArray = nullptr; |
583 | 0 | } |
584 | 0 | int srv = mDBConnection->stepStatement(mNativeConnection, mDBStatement); |
585 | 0 |
|
586 | 0 | if (srv != SQLITE_ROW && srv != SQLITE_DONE && MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) { |
587 | 0 | nsAutoCString errStr; |
588 | 0 | (void)mDBConnection->GetLastErrorString(errStr); |
589 | 0 | MOZ_LOG(gStorageLog, LogLevel::Debug, |
590 | 0 | ("Statement::ExecuteStep error: %s", errStr.get())); |
591 | 0 | } |
592 | 0 |
|
593 | 0 | // SQLITE_ROW and SQLITE_DONE are non-errors |
594 | 0 | if (srv == SQLITE_ROW) { |
595 | 0 | // we got a row back |
596 | 0 | mExecuting = true; |
597 | 0 | *_moreResults = true; |
598 | 0 | return NS_OK; |
599 | 0 | } |
600 | 0 | else if (srv == SQLITE_DONE) { |
601 | 0 | // statement is done (no row returned) |
602 | 0 | mExecuting = false; |
603 | 0 | *_moreResults = false; |
604 | 0 | return NS_OK; |
605 | 0 | } |
606 | 0 | else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) { |
607 | 0 | mExecuting = false; |
608 | 0 | } |
609 | 0 | else if (mExecuting) { |
610 | 0 | MOZ_LOG(gStorageLog, LogLevel::Error, |
611 | 0 | ("SQLite error after mExecuting was true!")); |
612 | 0 | mExecuting = false; |
613 | 0 | } |
614 | 0 |
|
615 | 0 | return convertResultCode(srv); |
616 | 0 | } |
617 | | |
618 | | NS_IMETHODIMP |
619 | | Statement::GetState(int32_t *_state) |
620 | 0 | { |
621 | 0 | if (!mDBStatement) |
622 | 0 | *_state = MOZ_STORAGE_STATEMENT_INVALID; |
623 | 0 | else if (mExecuting) |
624 | 0 | *_state = MOZ_STORAGE_STATEMENT_EXECUTING; |
625 | 0 | else |
626 | 0 | *_state = MOZ_STORAGE_STATEMENT_READY; |
627 | 0 |
|
628 | 0 | return NS_OK; |
629 | 0 | } |
630 | | |
631 | | //////////////////////////////////////////////////////////////////////////////// |
632 | | //// mozIStorageValueArray (now part of mozIStorageStatement too) |
633 | | |
634 | | NS_IMETHODIMP |
635 | | Statement::GetNumEntries(uint32_t *_length) |
636 | 0 | { |
637 | 0 | *_length = mResultColumnCount; |
638 | 0 | return NS_OK; |
639 | 0 | } |
640 | | |
641 | | NS_IMETHODIMP |
642 | | Statement::GetTypeOfIndex(uint32_t aIndex, |
643 | | int32_t *_type) |
644 | 0 | { |
645 | 0 | if (!mDBStatement) |
646 | 0 | return NS_ERROR_NOT_INITIALIZED; |
647 | 0 | |
648 | 0 | ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); |
649 | 0 |
|
650 | 0 | if (!mExecuting) |
651 | 0 | return NS_ERROR_UNEXPECTED; |
652 | 0 | |
653 | 0 | int t = ::sqlite3_column_type(mDBStatement, aIndex); |
654 | 0 | switch (t) { |
655 | 0 | case SQLITE_INTEGER: |
656 | 0 | *_type = mozIStorageStatement::VALUE_TYPE_INTEGER; |
657 | 0 | break; |
658 | 0 | case SQLITE_FLOAT: |
659 | 0 | *_type = mozIStorageStatement::VALUE_TYPE_FLOAT; |
660 | 0 | break; |
661 | 0 | case SQLITE_TEXT: |
662 | 0 | *_type = mozIStorageStatement::VALUE_TYPE_TEXT; |
663 | 0 | break; |
664 | 0 | case SQLITE_BLOB: |
665 | 0 | *_type = mozIStorageStatement::VALUE_TYPE_BLOB; |
666 | 0 | break; |
667 | 0 | case SQLITE_NULL: |
668 | 0 | *_type = mozIStorageStatement::VALUE_TYPE_NULL; |
669 | 0 | break; |
670 | 0 | default: |
671 | 0 | return NS_ERROR_FAILURE; |
672 | 0 | } |
673 | 0 | |
674 | 0 | return NS_OK; |
675 | 0 | } |
676 | | |
677 | | NS_IMETHODIMP |
678 | | Statement::GetInt32(uint32_t aIndex, |
679 | | int32_t *_value) |
680 | 0 | { |
681 | 0 | if (!mDBStatement) |
682 | 0 | return NS_ERROR_NOT_INITIALIZED; |
683 | 0 | |
684 | 0 | ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); |
685 | 0 |
|
686 | 0 | if (!mExecuting) |
687 | 0 | return NS_ERROR_UNEXPECTED; |
688 | 0 | |
689 | 0 | *_value = ::sqlite3_column_int(mDBStatement, aIndex); |
690 | 0 | return NS_OK; |
691 | 0 | } |
692 | | |
693 | | NS_IMETHODIMP |
694 | | Statement::GetInt64(uint32_t aIndex, |
695 | | int64_t *_value) |
696 | 0 | { |
697 | 0 | if (!mDBStatement) |
698 | 0 | return NS_ERROR_NOT_INITIALIZED; |
699 | 0 | |
700 | 0 | ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); |
701 | 0 |
|
702 | 0 | if (!mExecuting) |
703 | 0 | return NS_ERROR_UNEXPECTED; |
704 | 0 | |
705 | 0 | *_value = ::sqlite3_column_int64(mDBStatement, aIndex); |
706 | 0 |
|
707 | 0 | return NS_OK; |
708 | 0 | } |
709 | | |
710 | | NS_IMETHODIMP |
711 | | Statement::GetDouble(uint32_t aIndex, |
712 | | double *_value) |
713 | 0 | { |
714 | 0 | if (!mDBStatement) |
715 | 0 | return NS_ERROR_NOT_INITIALIZED; |
716 | 0 | |
717 | 0 | ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); |
718 | 0 |
|
719 | 0 | if (!mExecuting) |
720 | 0 | return NS_ERROR_UNEXPECTED; |
721 | 0 | |
722 | 0 | *_value = ::sqlite3_column_double(mDBStatement, aIndex); |
723 | 0 |
|
724 | 0 | return NS_OK; |
725 | 0 | } |
726 | | |
727 | | NS_IMETHODIMP |
728 | | Statement::GetUTF8String(uint32_t aIndex, |
729 | | nsACString &_value) |
730 | 0 | { |
731 | 0 | // Get type of Index will check aIndex for us, so we don't have to. |
732 | 0 | int32_t type; |
733 | 0 | nsresult rv = GetTypeOfIndex(aIndex, &type); |
734 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
735 | 0 | if (type == mozIStorageStatement::VALUE_TYPE_NULL) { |
736 | 0 | // NULL columns should have IsVoid set to distinguish them from the empty |
737 | 0 | // string. |
738 | 0 | _value.SetIsVoid(true); |
739 | 0 | } |
740 | 0 | else { |
741 | 0 | const char *value = |
742 | 0 | reinterpret_cast<const char *>(::sqlite3_column_text(mDBStatement, |
743 | 0 | aIndex)); |
744 | 0 | _value.Assign(value, ::sqlite3_column_bytes(mDBStatement, aIndex)); |
745 | 0 | } |
746 | 0 | return NS_OK; |
747 | 0 | } |
748 | | |
749 | | NS_IMETHODIMP |
750 | | Statement::GetString(uint32_t aIndex, |
751 | | nsAString &_value) |
752 | 0 | { |
753 | 0 | // Get type of Index will check aIndex for us, so we don't have to. |
754 | 0 | int32_t type; |
755 | 0 | nsresult rv = GetTypeOfIndex(aIndex, &type); |
756 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
757 | 0 | if (type == mozIStorageStatement::VALUE_TYPE_NULL) { |
758 | 0 | // NULL columns should have IsVoid set to distinguish them from the empty |
759 | 0 | // string. |
760 | 0 | _value.SetIsVoid(true); |
761 | 0 | } else { |
762 | 0 | const char16_t *value = |
763 | 0 | static_cast<const char16_t *>(::sqlite3_column_text16(mDBStatement, |
764 | 0 | aIndex)); |
765 | 0 | _value.Assign(value, ::sqlite3_column_bytes16(mDBStatement, aIndex) / 2); |
766 | 0 | } |
767 | 0 | return NS_OK; |
768 | 0 | } |
769 | | |
770 | | NS_IMETHODIMP |
771 | | Statement::GetBlob(uint32_t aIndex, |
772 | | uint32_t *_size, |
773 | | uint8_t **_blob) |
774 | 0 | { |
775 | 0 | if (!mDBStatement) |
776 | 0 | return NS_ERROR_NOT_INITIALIZED; |
777 | 0 | |
778 | 0 | ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); |
779 | 0 |
|
780 | 0 | if (!mExecuting) |
781 | 0 | return NS_ERROR_UNEXPECTED; |
782 | 0 | |
783 | 0 | int size = ::sqlite3_column_bytes(mDBStatement, aIndex); |
784 | 0 | void *blob = nullptr; |
785 | 0 | if (size) { |
786 | 0 | blob = moz_xmemdup(::sqlite3_column_blob(mDBStatement, aIndex), size); |
787 | 0 | } |
788 | 0 |
|
789 | 0 | *_blob = static_cast<uint8_t *>(blob); |
790 | 0 | *_size = size; |
791 | 0 | return NS_OK; |
792 | 0 | } |
793 | | |
794 | | NS_IMETHODIMP |
795 | | Statement::GetBlobAsString(uint32_t aIndex, nsAString& aValue) |
796 | 0 | { |
797 | 0 | return DoGetBlobAsString(this, aIndex, aValue); |
798 | 0 | } |
799 | | |
800 | | NS_IMETHODIMP |
801 | | Statement::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) |
802 | 0 | { |
803 | 0 | return DoGetBlobAsString(this, aIndex, aValue); |
804 | 0 | } |
805 | | |
806 | | NS_IMETHODIMP |
807 | | Statement::GetSharedUTF8String(uint32_t aIndex, |
808 | | uint32_t *_length, |
809 | | const char **_value) |
810 | 0 | { |
811 | 0 | if (_length) |
812 | 0 | *_length = ::sqlite3_column_bytes(mDBStatement, aIndex); |
813 | 0 |
|
814 | 0 | *_value = reinterpret_cast<const char *>(::sqlite3_column_text(mDBStatement, |
815 | 0 | aIndex)); |
816 | 0 | return NS_OK; |
817 | 0 | } |
818 | | |
819 | | NS_IMETHODIMP |
820 | | Statement::GetSharedString(uint32_t aIndex, |
821 | | uint32_t *_length, |
822 | | const char16_t **_value) |
823 | 0 | { |
824 | 0 | if (_length) |
825 | 0 | *_length = ::sqlite3_column_bytes16(mDBStatement, aIndex); |
826 | 0 |
|
827 | 0 | *_value = static_cast<const char16_t *>(::sqlite3_column_text16(mDBStatement, |
828 | 0 | aIndex)); |
829 | 0 | return NS_OK; |
830 | 0 | } |
831 | | |
832 | | NS_IMETHODIMP |
833 | | Statement::GetSharedBlob(uint32_t aIndex, |
834 | | uint32_t *_size, |
835 | | const uint8_t **_blob) |
836 | 0 | { |
837 | 0 | *_size = ::sqlite3_column_bytes(mDBStatement, aIndex); |
838 | 0 | *_blob = static_cast<const uint8_t *>(::sqlite3_column_blob(mDBStatement, |
839 | 0 | aIndex)); |
840 | 0 | return NS_OK; |
841 | 0 | } |
842 | | |
843 | | NS_IMETHODIMP |
844 | | Statement::GetIsNull(uint32_t aIndex, |
845 | | bool *_isNull) |
846 | 0 | { |
847 | 0 | // Get type of Index will check aIndex for us, so we don't have to. |
848 | 0 | int32_t type; |
849 | 0 | nsresult rv = GetTypeOfIndex(aIndex, &type); |
850 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
851 | 0 | *_isNull = (type == mozIStorageStatement::VALUE_TYPE_NULL); |
852 | 0 | return NS_OK; |
853 | 0 | } |
854 | | |
855 | | //////////////////////////////////////////////////////////////////////////////// |
856 | | //// mozIStorageBindingParams |
857 | | |
858 | | BOILERPLATE_BIND_PROXIES( |
859 | | Statement, |
860 | | if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED; |
861 | | ) |
862 | | |
863 | | } // namespace storage |
864 | | } // namespace mozilla |