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 et lcs=trail\:.,tab\:>~ :
|
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 <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "Variant.h"
|
|
|
|
|
|
|
|
#include "mozIStorageError.h"
|
|
|
|
|
|
|
|
#include "mozStorageBindingParams.h"
|
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "mozStorageAsyncStatementJSHelper.h"
|
|
|
|
#include "mozStorageAsyncStatementParams.h"
|
|
|
|
#include "mozStoragePrivateHelpers.h"
|
|
|
|
#include "mozStorageStatementRow.h"
|
|
|
|
#include "mozStorageStatement.h"
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2016-01-28 21:35:00 +03:00
|
|
|
extern mozilla::LazyLogModule gStorageLog;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIClassInfo
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(AsyncStatement, mozIStorageAsyncStatement,
|
|
|
|
mozIStorageBaseStatement, mozIStorageBindingParams,
|
|
|
|
mozilla::storage::StorageBaseStatementInternal)
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
class AsyncStatementClassInfo : public nsIClassInfo {
|
|
|
|
public:
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr AsyncStatementClassInfo() {}
|
2013-11-22 23:45:50 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetInterfaces(nsTArray<nsIID>& _array) override {
|
2019-02-14 00:42:08 +03:00
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(AsyncStatement)(_array);
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetScriptableHelper(nsIXPCScriptable** _helper) override {
|
2015-03-29 17:52:54 +03:00
|
|
|
static AsyncStatementJSHelper sJSHelper;
|
|
|
|
*_helper = &sJSHelper;
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetContractID(nsACString& aContractID) override {
|
2017-10-18 05:17:26 +03:00
|
|
|
aContractID.SetIsVoid(true);
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetClassDescription(nsACString& aDesc) override {
|
2017-10-18 05:17:26 +03:00
|
|
|
aDesc.SetIsVoid(true);
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetClassID(nsCID** _id) override {
|
2012-07-30 18:20:58 +04:00
|
|
|
*_id = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetFlags(uint32_t* _flags) override {
|
2012-07-20 15:16:17 +04:00
|
|
|
*_flags = 0;
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-05-01 11:47:10 +03:00
|
|
|
GetClassIDNoAlloc(nsCID* _cid) override { return NS_ERROR_NOT_AVAILABLE; }
|
2010-03-24 10:32:40 +03:00
|
|
|
};
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementClassInfo::AddRef() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementClassInfo::Release() {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(AsyncStatementClassInfo, nsIClassInfo)
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
static AsyncStatementClassInfo sAsyncStatementClassInfo;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// AsyncStatement
|
|
|
|
|
|
|
|
AsyncStatement::AsyncStatement()
|
|
|
|
: StorageBaseStatementInternal(), mFinalized(false) {}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult AsyncStatement::initialize(Connection* aDBConnection,
|
|
|
|
sqlite3* aNativeConnection,
|
|
|
|
const nsACString& aSQLStatement) {
|
2014-04-24 13:54:09 +04:00
|
|
|
MOZ_ASSERT(aDBConnection, "No database connection given!");
|
2017-06-16 18:43:23 +03:00
|
|
|
MOZ_ASSERT(aDBConnection->isConnectionReadyOnThisThread(),
|
|
|
|
"Database connection should be valid");
|
2014-04-24 13:54:09 +04:00
|
|
|
MOZ_ASSERT(aNativeConnection, "No native connection given!");
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
mDBConnection = aDBConnection;
|
2014-04-24 13:54:09 +04:00
|
|
|
mNativeConnection = aNativeConnection;
|
2010-03-24 10:32:40 +03:00
|
|
|
mSQLString = aSQLStatement;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
|
|
|
("Inited async statement '%s' (0x%p)", mSQLString.get(), this));
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// We want to try and test for LIKE and that consumers are using
|
|
|
|
// escapeStringForLIKE instead of just trusting user input. The idea to
|
|
|
|
// check to see if they are binding a parameter after like instead of just
|
|
|
|
// using a string. We only do this in debug builds because it's expensive!
|
|
|
|
const nsCaseInsensitiveCStringComparator c;
|
|
|
|
nsACString::const_iterator start, end, e;
|
|
|
|
aSQLStatement.BeginReading(start);
|
|
|
|
aSQLStatement.EndReading(end);
|
|
|
|
e = end;
|
|
|
|
while (::FindInReadable(NS_LITERAL_CSTRING(" LIKE"), start, e, c)) {
|
|
|
|
// We have a LIKE in here, so we perform our tests
|
|
|
|
// FindInReadable moves the iterator, so we have to get a new one for
|
|
|
|
// each test we perform.
|
|
|
|
nsACString::const_iterator s1, s2, s3;
|
|
|
|
s1 = s2 = s3 = start;
|
|
|
|
|
|
|
|
if (!(::FindInReadable(NS_LITERAL_CSTRING(" LIKE ?"), s1, end, c) ||
|
|
|
|
::FindInReadable(NS_LITERAL_CSTRING(" LIKE :"), s2, end, c) ||
|
|
|
|
::FindInReadable(NS_LITERAL_CSTRING(" LIKE @"), s3, end, c))) {
|
|
|
|
// At this point, we didn't find a LIKE statement followed by ?, :,
|
|
|
|
// or @, all of which are valid characters for binding a parameter.
|
|
|
|
// We will warn the consumer that they may not be safely using LIKE.
|
|
|
|
NS_WARNING(
|
|
|
|
"Unsafe use of LIKE detected! Please ensure that you "
|
|
|
|
"are using mozIStorageAsyncStatement::escapeStringForLIKE "
|
|
|
|
"and that you are binding that result to the statement "
|
|
|
|
"to prevent SQL injection attacks.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetting start and e
|
|
|
|
start = e;
|
|
|
|
e = end;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
mozIStorageBindingParams* AsyncStatement::getParams() {
|
2010-03-24 10:32:40 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// If we do not have an array object yet, make it.
|
|
|
|
if (!mParamsArray) {
|
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> array;
|
|
|
|
rv = NewBindingParamsArray(getter_AddRefs(array));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
mParamsArray = static_cast<BindingParamsArray*>(array.get());
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't already any rows added, we'll have to add one to use.
|
|
|
|
if (mParamsArray->length() == 0) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncBindingParams> params(new AsyncBindingParams(mParamsArray));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(params, nullptr);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
rv = mParamsArray->AddParams(params);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// We have to unlock our params because AddParams locks them. This is safe
|
|
|
|
// because no reference to the params object was, or ever will be given out.
|
2012-07-30 18:20:58 +04:00
|
|
|
params->unlock(nullptr);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// We also want to lock our array at this point - we don't want anything to
|
|
|
|
// be added to it.
|
|
|
|
mParamsArray->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *mParamsArray->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we are here then we know there are no pending async executions relying on
|
|
|
|
* us (StatementData holds a reference to us; this also goes for our own
|
|
|
|
* AsyncStatementFinalizer which proxies its release to the calling thread) and
|
|
|
|
* so it is always safe to destroy our sqlite3_stmt if one exists. We can be
|
|
|
|
* destroyed on the caller thread by garbage-collection/reference counting or on
|
|
|
|
* the async thread by the last execution of a statement that already lost its
|
|
|
|
* main-thread refs.
|
|
|
|
*/
|
|
|
|
AsyncStatement::~AsyncStatement() {
|
2011-01-20 05:16:42 +03:00
|
|
|
destructorAsyncFinalize();
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// If we are getting destroyed on the wrong thread, proxy the connection
|
|
|
|
// release to the right thread. I'm not sure why we do this.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool onCallingThread = false;
|
2010-03-24 10:32:40 +03:00
|
|
|
(void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&onCallingThread);
|
|
|
|
if (!onCallingThread) {
|
|
|
|
// NS_ProxyRelase only magic forgets for us if mDBConnection is an
|
|
|
|
// nsCOMPtr. Which it is not; it's an nsRefPtr.
|
2016-02-10 10:23:00 +03:00
|
|
|
nsCOMPtr<nsIThread> targetThread(mDBConnection->threadOpenedOn);
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("AsyncStatement::mDBConnection", targetThread,
|
|
|
|
mDBConnection.forget());
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsISupports
|
|
|
|
|
2013-07-19 06:24:15 +04:00
|
|
|
NS_IMPL_ADDREF(AsyncStatement)
|
|
|
|
NS_IMPL_RELEASE(AsyncStatement)
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(AsyncStatement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncStatement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBaseStatement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBindingParams)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::storage::StorageBaseStatementInternal)
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
|
2019-05-01 11:47:10 +03:00
|
|
|
foundInterface = static_cast<nsIClassInfo*>(&sAsyncStatementClassInfo);
|
2010-03-24 10:32:40 +03:00
|
|
|
} else
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageAsyncStatement)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StorageBaseStatementInternal
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Connection* AsyncStatement::getOwner() { return mDBConnection; }
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int AsyncStatement::getAsyncStatement(sqlite3_stmt** _stmt) {
|
2010-03-24 10:32:40 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Make sure we are never called on the connection's owning thread.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool onOpenedThread = false;
|
2010-03-24 10:32:40 +03:00
|
|
|
(void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
|
|
|
|
NS_ASSERTION(!onOpenedThread,
|
|
|
|
"We should only be called on the async thread!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!mAsyncStatement) {
|
2014-04-24 13:54:12 +04:00
|
|
|
int rc = mDBConnection->prepareStatement(mNativeConnection, mSQLString,
|
|
|
|
&mAsyncStatement);
|
2010-03-24 10:32:40 +03:00
|
|
|
if (rc != SQLITE_OK) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2010-03-24 10:32:40 +03:00
|
|
|
("Sqlite statement prepare error: %d '%s'", rc,
|
2014-04-24 13:54:09 +04:00
|
|
|
::sqlite3_errmsg(mNativeConnection)));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2010-03-24 10:32:40 +03:00
|
|
|
("Statement was: '%s'", mSQLString.get()));
|
2012-07-30 18:20:58 +04:00
|
|
|
*_stmt = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
return rc;
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
|
|
|
("Initialized statement '%s' (0x%p)", mSQLString.get(),
|
2010-03-24 10:32:40 +03:00
|
|
|
mAsyncStatement));
|
|
|
|
}
|
|
|
|
|
|
|
|
*_stmt = mAsyncStatement;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult AsyncStatement::getAsynchronousStatementData(StatementData& _data) {
|
2010-03-24 10:32:40 +03:00
|
|
|
if (mFinalized) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// Pass null for the sqlite3_stmt; it will be requested on demand from the
|
|
|
|
// async thread.
|
2012-07-30 18:20:58 +04:00
|
|
|
_data = StatementData(nullptr, bindingParamsArray(), this);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageBindingParams> AsyncStatement::newBindingParams(
|
2019-05-01 11:47:10 +03:00
|
|
|
mozIStorageBindingParamsArray* aOwner) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mFinalized) return nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageBindingParams> params(new AsyncBindingParams(aOwner));
|
|
|
|
return params.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageAsyncStatement
|
|
|
|
|
|
|
|
// (nothing is specific to mozIStorageAsyncStatement)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StorageBaseStatementInternal
|
|
|
|
|
|
|
|
// proxy to StorageBaseStatementInternal using its define helper.
|
|
|
|
MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(
|
|
|
|
AsyncStatement, if (mFinalized) return NS_ERROR_UNEXPECTED;)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncStatement::Finalize() {
|
|
|
|
if (mFinalized) return NS_OK;
|
|
|
|
|
|
|
|
mFinalized = true;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
|
|
|
("Finalizing statement '%s'", mSQLString.get()));
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
asyncFinalize();
|
2015-03-12 19:55:56 +03:00
|
|
|
|
|
|
|
// Release the params holder, so it can release the reference to us.
|
|
|
|
mStatementParamsHolder = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
AsyncStatement::BindParameters(mozIStorageBindingParamsArray* aParameters) {
|
2010-03-24 10:32:40 +03:00
|
|
|
if (mFinalized) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
BindingParamsArray* array = static_cast<BindingParamsArray*>(aParameters);
|
2010-03-24 10:32:40 +03:00
|
|
|
if (array->getOwner() != this) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (array->length() == 0) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
mParamsArray = array;
|
|
|
|
mParamsArray->lock();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
AsyncStatement::GetState(int32_t* _state) {
|
2010-03-24 10:32:40 +03:00
|
|
|
if (mFinalized)
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_INVALID;
|
|
|
|
else
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_READY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageBindingParams
|
|
|
|
|
|
|
|
BOILERPLATE_BIND_PROXIES(AsyncStatement,
|
|
|
|
if (mFinalized) return NS_ERROR_UNEXPECTED;)
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|