2010-03-24 10:32:40 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 sts=2 expandtab
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
#include "StorageBaseStatementInternal.h"
|
|
|
|
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
|
|
|
|
#include "mozStorageBindingParamsArray.h"
|
|
|
|
#include "mozStorageStatementData.h"
|
|
|
|
#include "mozStorageAsyncStatementExecution.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Local Classes
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to finalize an asynchronous statement on the background thread.
|
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncStatementFinalizer : public Runnable {
|
2010-03-24 10:32:40 +03:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor for the event.
|
|
|
|
*
|
|
|
|
* @param aStatement
|
|
|
|
* We need the AsyncStatement to be able to get at the sqlite3_stmt;
|
|
|
|
* we only access/create it on the async thread.
|
|
|
|
* @param aConnection
|
|
|
|
* We need the connection to know what thread to release the statement
|
|
|
|
* on. We release the statement on that thread since releasing the
|
|
|
|
* statement might end up releasing the connection too.
|
|
|
|
*/
|
2017-06-12 22:34:10 +03:00
|
|
|
AsyncStatementFinalizer(StorageBaseStatementInternal* aStatement,
|
|
|
|
Connection* aConnection)
|
|
|
|
: Runnable("storage::AsyncStatementFinalizer"),
|
|
|
|
mStatement(aStatement),
|
|
|
|
mConnection(aConnection) {}
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2011-01-20 05:16:42 +03:00
|
|
|
if (mStatement->mAsyncStatement) {
|
2016-02-10 10:23:00 +03:00
|
|
|
sqlite3_finalize(mStatement->mAsyncStatement);
|
2012-07-30 18:20:58 +04:00
|
|
|
mStatement->mAsyncStatement = nullptr;
|
2011-01-20 05:16:42 +03:00
|
|
|
}
|
2016-02-10 10:23:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> targetThread(mConnection->threadOpenedOn);
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("AsyncStatementFinalizer::mStatement", targetThread,
|
|
|
|
mStatement.forget());
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StorageBaseStatementInternal> mStatement;
|
|
|
|
RefPtr<Connection> mConnection;
|
2011-01-20 05:16:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize a sqlite3_stmt on the background thread for a statement whose
|
|
|
|
* destructor was invoked and the statement was non-null.
|
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class LastDitchSqliteStatementFinalizer : public Runnable {
|
2011-01-20 05:16:42 +03:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Event constructor.
|
|
|
|
*
|
|
|
|
* @param aConnection
|
|
|
|
* Used to keep the connection alive. If we failed to do this, it
|
|
|
|
* is possible that the statement going out of scope invoking us
|
|
|
|
* might have the last reference to the connection and so trigger
|
|
|
|
* an attempt to close the connection which is doomed to fail
|
|
|
|
* (because the asynchronous execution thread must exist which will
|
|
|
|
* trigger the failure case).
|
|
|
|
* @param aStatement
|
|
|
|
* The sqlite3_stmt to finalize. This object takes ownership /
|
|
|
|
* responsibility for the instance and all other references to it
|
|
|
|
* should be forgotten.
|
|
|
|
*/
|
2017-06-12 22:34:10 +03:00
|
|
|
LastDitchSqliteStatementFinalizer(RefPtr<Connection>& aConnection,
|
|
|
|
sqlite3_stmt* aStatement)
|
|
|
|
: Runnable("storage::LastDitchSqliteStatementFinalizer"),
|
|
|
|
mConnection(aConnection),
|
|
|
|
mAsyncStatement(aStatement) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aConnection, "You must provide a Connection");
|
2011-01-20 05:16:42 +03:00
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2011-01-20 05:16:42 +03:00
|
|
|
(void)::sqlite3_finalize(mAsyncStatement);
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncStatement = nullptr;
|
2011-01-20 05:16:42 +03:00
|
|
|
|
2016-02-10 10:23:00 +03:00
|
|
|
nsCOMPtr<nsIThread> target(mConnection->threadOpenedOn);
|
2017-06-14 04:27:17 +03:00
|
|
|
(void)::NS_ProxyRelease("LastDitchSqliteStatementFinalizer::mConnection",
|
|
|
|
target, mConnection.forget());
|
2011-01-20 05:16:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-01-20 05:16:42 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Connection> mConnection;
|
2011-01-20 05:16:42 +03:00
|
|
|
sqlite3_stmt* mAsyncStatement;
|
2010-03-24 10:32:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StorageBaseStatementInternal
|
|
|
|
|
|
|
|
StorageBaseStatementInternal::StorageBaseStatementInternal()
|
2018-06-15 10:42:19 +03:00
|
|
|
: mNativeConnection(nullptr), mAsyncStatement(nullptr) {}
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
void StorageBaseStatementInternal::asyncFinalize() {
|
|
|
|
nsIEventTarget* target = mDBConnection->getAsyncExecutionTarget();
|
2013-08-28 01:07:04 +04:00
|
|
|
if (target) {
|
|
|
|
// Attempt to finalize asynchronously
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new AsyncStatementFinalizer(this, mDBConnection);
|
|
|
|
|
2013-08-28 01:07:04 +04:00
|
|
|
// Dispatch. Note that dispatching can fail, typically if
|
|
|
|
// we have a race condition with asyncClose(). It's ok,
|
|
|
|
// let asyncClose() win.
|
|
|
|
(void)target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
2013-08-28 01:07:04 +04:00
|
|
|
// If we cannot get the background thread,
|
|
|
|
// mozStorageConnection::AsyncClose() has already been called and
|
|
|
|
// the statement either has been or will be cleaned up by
|
|
|
|
// internalClose().
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
2011-01-20 05:16:42 +03:00
|
|
|
void StorageBaseStatementInternal::destructorAsyncFinalize() {
|
|
|
|
if (!mAsyncStatement) return;
|
|
|
|
|
2017-06-02 00:46:53 +03:00
|
|
|
bool isOwningThread = false;
|
|
|
|
(void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&isOwningThread);
|
|
|
|
if (isOwningThread) {
|
|
|
|
// If we are the owning thread (currently that means we're also the
|
|
|
|
// main thread), then we can get the async target and just dispatch
|
|
|
|
// to it.
|
|
|
|
nsIEventTarget* target = mDBConnection->getAsyncExecutionTarget();
|
|
|
|
if (target) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement);
|
2013-08-28 01:07:04 +04:00
|
|
|
(void)target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2011-01-20 05:16:42 +03:00
|
|
|
}
|
2017-06-02 00:46:53 +03:00
|
|
|
} else {
|
|
|
|
// If we're not the owning thread, assume we're the async thread, and
|
|
|
|
// just run the statement.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement);
|
|
|
|
(void)event->Run();
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
2013-08-28 01:07:04 +04:00
|
|
|
|
|
|
|
// We might not be able to dispatch to the background thread,
|
|
|
|
// presumably because it is being shutdown. Since said shutdown will
|
|
|
|
// finalize the statement, we just need to clean-up around here.
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncStatement = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StorageBaseStatementInternal::NewBindingParamsArray(
|
|
|
|
mozIStorageBindingParamsArray** _array) {
|
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> array = new BindingParamsArray(this);
|
|
|
|
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
array.forget(_array);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StorageBaseStatementInternal::ExecuteAsync(
|
|
|
|
mozIStorageStatementCallback* aCallback,
|
|
|
|
mozIStoragePendingStatement** _stmt) {
|
|
|
|
// We used to call Connection::ExecuteAsync but it takes a
|
|
|
|
// mozIStorageBaseStatement signature because it is also a public API. Since
|
|
|
|
// our 'this' has no static concept of mozIStorageBaseStatement and Connection
|
|
|
|
// would just QI it back across to a StorageBaseStatementInternal and the
|
|
|
|
// actual logic is very simple, we now roll our own.
|
|
|
|
nsTArray<StatementData> stmts(1);
|
|
|
|
StatementData data;
|
|
|
|
nsresult rv = getAsynchronousStatementData(data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2020-04-24 16:35:27 +03:00
|
|
|
stmts.AppendElement(data);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// Dispatch to the background
|
2020-08-04 14:27:07 +03:00
|
|
|
return AsyncExecuteStatements::execute(std::move(stmts), mDBConnection,
|
2014-04-24 13:54:09 +04:00
|
|
|
mNativeConnection, aCallback, _stmt);
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StorageBaseStatementInternal::EscapeStringForLIKE(const nsAString& aValue,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t aEscapeChar,
|
2010-03-24 10:32:40 +03:00
|
|
|
nsAString& _escapedString) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t MATCH_ALL('%');
|
|
|
|
const char16_t MATCH_ONE('_');
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
_escapedString.Truncate(0);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aValue.Length(); i++) {
|
2010-03-24 10:32:40 +03:00
|
|
|
if (aValue[i] == aEscapeChar || aValue[i] == MATCH_ALL ||
|
|
|
|
aValue[i] == MATCH_ONE) {
|
|
|
|
_escapedString += aEscapeChar;
|
|
|
|
}
|
|
|
|
_escapedString += aValue[i];
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|