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.
|
|
|
|
*/
|
|
|
|
AsyncStatementFinalizer(StorageBaseStatementInternal *aStatement,
|
|
|
|
Connection *aConnection)
|
|
|
|
: mStatement(aStatement)
|
|
|
|
, mConnection(aConnection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
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);
|
|
|
|
NS_ProxyRelease(targetThread, mStatement.forget());
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2015-10-18 08:24:48 +03:00
|
|
|
LastDitchSqliteStatementFinalizer(RefPtr<Connection> &aConnection,
|
2011-01-20 05:16:42 +03:00
|
|
|
sqlite3_stmt *aStatement)
|
|
|
|
: mConnection(aConnection)
|
|
|
|
, mAsyncStatement(aStatement)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "You must provide a Connection");
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
(void)::NS_ProxyRelease(target, mConnection.forget());
|
2011-01-20 05:16:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
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()
|
2013-07-31 19:44:58 +04:00
|
|
|
: 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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-20 05:16:42 +03:00
|
|
|
StorageBaseStatementInternal::destructorAsyncFinalize()
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
2011-01-20 05:16:42 +03:00
|
|
|
if (!mAsyncStatement)
|
|
|
|
return;
|
|
|
|
|
2013-08-28 01:07:04 +04:00
|
|
|
// If we reach this point, our owner has not finalized this
|
|
|
|
// statement, yet we are being destructed. If possible, we want to
|
|
|
|
// auto-finalize it early, to release the resources early.
|
2011-01-20 05:16:42 +03:00
|
|
|
nsIEventTarget *target = mDBConnection->getAsyncExecutionTarget();
|
|
|
|
if (target) {
|
2013-08-28 01:07:04 +04:00
|
|
|
// If we can get the async execution target, we can indeed finalize
|
|
|
|
// the statement, as the connection is still open.
|
|
|
|
bool isAsyncThread = false;
|
|
|
|
(void)target->IsOnCurrentThread(&isAsyncThread);
|
|
|
|
|
2011-01-20 05:16:42 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement);
|
2013-08-28 01:07:04 +04:00
|
|
|
if (isAsyncThread) {
|
|
|
|
(void)event->Run();
|
|
|
|
} else {
|
|
|
|
(void)target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2011-01-20 05:16:42 +03:00
|
|
|
}
|
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);
|
|
|
|
NS_ENSURE_TRUE(stmts.AppendElement(data), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Dispatch to the background
|
2014-04-24 13:54:09 +04:00
|
|
|
return AsyncExecuteStatements::execute(stmts, mDBConnection,
|
|
|
|
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
|