2008-09-12 21:30:41 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 16:29:41 +00:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 11:12:37 +00: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/. */
|
2008-09-12 21:30:41 +00:00
|
|
|
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "mozStorageStatement.h"
|
2008-11-19 06:11:30 +00:00
|
|
|
#include "mozStorageService.h"
|
2008-09-12 21:30:41 +00:00
|
|
|
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#include "mozStorageStatementJSHelper.h"
|
|
|
|
|
|
|
|
#include "mozStorageStatementRow.h"
|
|
|
|
#include "mozStorageStatementParams.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
2016-12-13 21:33:46 +00:00
|
|
|
#include "xpc_make_class.h"
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Global Functions
|
2009-03-31 14:26:16 +00:00
|
|
|
|
2008-09-12 21:30:41 +00:00
|
|
|
static
|
2013-08-02 07:41:57 +00:00
|
|
|
bool
|
2009-04-08 16:24:25 +00:00
|
|
|
stepFunc(JSContext *aCtx,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t,
|
2015-07-18 19:45:35 +00:00
|
|
|
JS::Value *_vp)
|
2008-09-12 21:30:41 +00:00
|
|
|
{
|
2009-03-31 14:26:16 +00:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
|
2008-09-12 21:30:41 +00:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
|
2011-12-01 21:30:28 +00:00
|
|
|
JSObject *obj = JS_THIS_OBJECT(aCtx, _vp);
|
|
|
|
if (!obj) {
|
2013-08-02 07:41:57 +00:00
|
|
|
return false;
|
2011-12-01 21:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper));
|
2008-09-12 21:30:41 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-08-14 11:39:28 +00:00
|
|
|
::JS_ReportErrorASCII(aCtx, "mozIStorageStatement::step() could not obtain native statement");
|
2013-08-02 07:41:57 +00:00
|
|
|
return false;
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2010-03-24 07:32:40 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> isStatement(
|
|
|
|
do_QueryInterface(wrapper->Native())
|
|
|
|
);
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ASSERTION(isStatement, "How is this not a statement?!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 07:32:40 +00:00
|
|
|
Statement *stmt = static_cast<Statement *>(
|
|
|
|
static_cast<mozIStorageStatement *>(wrapper->Native())
|
|
|
|
);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasMore = false;
|
2008-09-12 21:30:41 +00:00
|
|
|
rv = stmt->ExecuteStep(&hasMore);
|
|
|
|
if (NS_SUCCEEDED(rv) && !hasMore) {
|
2015-06-30 01:36:44 +00:00
|
|
|
_vp->setBoolean(false);
|
2008-09-12 21:30:41 +00:00
|
|
|
(void)stmt->Reset();
|
2013-08-02 07:41:57 +00:00
|
|
|
return true;
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-08-14 11:39:28 +00:00
|
|
|
::JS_ReportErrorASCII(aCtx, "mozIStorageStatement::step() returned an error");
|
2013-08-02 07:41:57 +00:00
|
|
|
return false;
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 18:20:56 +00:00
|
|
|
_vp->setBoolean(hasMore);
|
2013-08-02 07:41:57 +00:00
|
|
|
return true;
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StatementJSHelper
|
|
|
|
|
2008-09-12 21:30:41 +00:00
|
|
|
nsresult
|
2009-05-09 00:29:56 +00:00
|
|
|
StatementJSHelper::getRow(Statement *aStatement,
|
2009-04-08 16:24:25 +00:00
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2015-07-18 19:45:35 +00:00
|
|
|
JS::Value *_row)
|
2008-09-12 21:30:41 +00:00
|
|
|
{
|
2015-03-12 16:55:56 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-09-12 21:30:41 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2009-06-11 20:18:58 +00:00
|
|
|
#ifdef DEBUG
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t state;
|
2008-09-12 21:30:41 +00:00
|
|
|
(void)aStatement->GetState(&state);
|
2009-06-11 20:18:58 +00:00
|
|
|
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING,
|
|
|
|
"Invalid state to get the row object - all calls will fail!");
|
|
|
|
#endif
|
2008-09-12 21:30:41 +00:00
|
|
|
|
|
|
|
if (!aStatement->mStatementRowHolder) {
|
2013-05-12 13:10:39 +00:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
2009-04-08 16:24:25 +00:00
|
|
|
nsCOMPtr<mozIStorageStatementRow> row(new StatementRow(aStatement));
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-03-12 16:55:56 +00:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2009-03-31 14:26:16 +00:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
|
2015-07-01 18:17:17 +00:00
|
|
|
rv = xpc->WrapNativeHolder(
|
2008-11-19 06:11:30 +00:00
|
|
|
aCtx,
|
2013-05-12 13:10:39 +00:00
|
|
|
::JS_GetGlobalForObject(aCtx, scope),
|
2008-11-19 06:11:30 +00:00
|
|
|
row,
|
|
|
|
NS_GET_IID(mozIStorageStatementRow),
|
2015-03-12 16:55:56 +00:00
|
|
|
getter_AddRefs(holder)
|
2008-11-19 06:11:30 +00:00
|
|
|
);
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<StatementRowHolder> rowHolder = new StatementRowHolder(holder);
|
2015-03-12 16:55:56 +00:00
|
|
|
aStatement->mStatementRowHolder =
|
|
|
|
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(rowHolder);
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 02:02:40 +00:00
|
|
|
JS::Rooted<JSObject*> obj(aCtx);
|
2013-05-20 12:44:18 +00:00
|
|
|
obj = aStatement->mStatementRowHolder->GetJSObject();
|
|
|
|
NS_ENSURE_STATE(obj);
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2015-07-01 04:09:46 +00:00
|
|
|
_row->setObject(*obj);
|
2008-09-12 21:30:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-05-09 00:29:56 +00:00
|
|
|
StatementJSHelper::getParams(Statement *aStatement,
|
2009-04-08 16:24:25 +00:00
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2015-07-18 19:45:35 +00:00
|
|
|
JS::Value *_params)
|
2008-09-12 21:30:41 +00:00
|
|
|
{
|
2015-03-12 16:55:56 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-09-12 21:30:41 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2009-06-11 20:18:58 +00:00
|
|
|
#ifdef DEBUG
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t state;
|
2008-09-12 21:30:41 +00:00
|
|
|
(void)aStatement->GetState(&state);
|
2009-06-11 20:18:58 +00:00
|
|
|
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY,
|
|
|
|
"Invalid state to get the params object - all calls will fail!");
|
|
|
|
#endif
|
2008-09-12 21:30:41 +00:00
|
|
|
|
|
|
|
if (!aStatement->mStatementParamsHolder) {
|
2013-05-12 13:10:39 +00:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
2008-09-12 21:30:41 +00:00
|
|
|
nsCOMPtr<mozIStorageStatementParams> params =
|
2009-04-08 16:24:25 +00:00
|
|
|
new StatementParams(aStatement);
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-03-12 16:55:56 +00:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2009-03-31 14:26:16 +00:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
|
2015-07-01 18:17:17 +00:00
|
|
|
rv = xpc->WrapNativeHolder(
|
2008-11-19 06:11:30 +00:00
|
|
|
aCtx,
|
2013-05-12 13:10:39 +00:00
|
|
|
::JS_GetGlobalForObject(aCtx, scope),
|
2008-11-19 06:11:30 +00:00
|
|
|
params,
|
|
|
|
NS_GET_IID(mozIStorageStatementParams),
|
2015-03-12 16:55:56 +00:00
|
|
|
getter_AddRefs(holder)
|
2008-11-19 06:11:30 +00:00
|
|
|
);
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<StatementParamsHolder> paramsHolder =
|
2015-03-12 16:55:56 +00:00
|
|
|
new StatementParamsHolder(holder);
|
|
|
|
aStatement->mStatementParamsHolder =
|
|
|
|
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(paramsHolder);
|
2008-09-12 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 02:02:40 +00:00
|
|
|
JS::Rooted<JSObject*> obj(aCtx);
|
2013-05-20 12:44:18 +00:00
|
|
|
obj = aStatement->mStatementParamsHolder->GetJSObject();
|
|
|
|
NS_ENSURE_STATE(obj);
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2015-07-01 04:09:46 +00:00
|
|
|
_params->setObject(*obj);
|
2008-09-12 21:30:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() { return 2; }
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() { return 1; }
|
2009-04-08 16:24:25 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(StatementJSHelper)
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIXPCScriptable
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
#define XPC_MAP_CLASSNAME StatementJSHelper
|
|
|
|
#define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
|
2008-09-12 21:30:41 +00:00
|
|
|
#define XPC_MAP_WANT_GETPROPERTY
|
2014-11-08 00:07:12 +00:00
|
|
|
#define XPC_MAP_WANT_RESOLVE
|
2008-09-12 21:30:41 +00:00
|
|
|
#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
|
|
|
|
#include "xpc_map_end.h"
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-08 16:24:25 +00:00
|
|
|
StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
|
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2010-07-15 06:19:36 +00:00
|
|
|
jsid aId,
|
2015-07-18 19:45:35 +00:00
|
|
|
JS::Value *_result,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *_retval)
|
2008-09-12 21:30:41 +00:00
|
|
|
{
|
2010-07-15 06:19:36 +00:00
|
|
|
if (!JSID_IS_STRING(aId))
|
2008-09-12 21:30:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2013-05-12 22:32:13 +00:00
|
|
|
JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
|
|
|
|
JS::Rooted<jsid> id(aCtx, aId);
|
|
|
|
|
2008-09-12 21:30:41 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2010-03-24 07:32:40 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> isStatement(
|
|
|
|
do_QueryInterface(aWrapper->Native()));
|
2008-09-12 21:30:41 +00:00
|
|
|
NS_ASSERTION(isStatement, "How is this not a statement?!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 07:32:40 +00:00
|
|
|
Statement *stmt = static_cast<Statement *>(
|
|
|
|
static_cast<mozIStorageStatement *>(aWrapper->Native())
|
|
|
|
);
|
|
|
|
|
2013-05-12 13:10:39 +00:00
|
|
|
JSFlatString *str = JSID_TO_FLAT_STRING(id);
|
2010-12-03 08:24:17 +00:00
|
|
|
if (::JS_FlatStringEqualsAscii(str, "row"))
|
2013-05-12 13:10:39 +00:00
|
|
|
return getRow(stmt, aCtx, scope, _result);
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2010-12-03 08:24:17 +00:00
|
|
|
if (::JS_FlatStringEqualsAscii(str, "params"))
|
2013-05-12 13:10:39 +00:00
|
|
|
return getParams(stmt, aCtx, scope, _result);
|
2008-09-12 21:30:41 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-11-08 00:07:12 +00:00
|
|
|
StatementJSHelper::Resolve(nsIXPConnectWrappedNative *aWrapper,
|
|
|
|
JSContext *aCtx, JSObject *aScopeObj,
|
|
|
|
jsid aId, bool *aResolvedp,
|
|
|
|
bool *_retval)
|
2008-09-12 21:30:41 +00:00
|
|
|
{
|
2010-07-15 06:19:36 +00:00
|
|
|
if (!JSID_IS_STRING(aId))
|
2008-09-12 21:30:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2013-05-12 13:10:39 +00:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
2010-12-03 08:24:17 +00:00
|
|
|
if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) {
|
2013-05-12 13:10:39 +00:00
|
|
|
*_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc,
|
2015-05-01 19:03:17 +00:00
|
|
|
0, JSPROP_RESOLVING) != nullptr;
|
2014-11-08 00:07:12 +00:00
|
|
|
*aResolvedp = true;
|
2008-09-12 21:30:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-08 16:24:25 +00:00
|
|
|
|
2015-03-12 16:55:56 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StatementJSObjectHolder
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(StatementJSObjectHolder, nsIXPConnectJSObjectHolder);
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
StatementJSObjectHolder::GetJSObject()
|
|
|
|
{
|
|
|
|
return mHolder->GetJSObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatementJSObjectHolder::StatementJSObjectHolder(nsIXPConnectJSObjectHolder* aHolder)
|
|
|
|
: mHolder(aHolder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mHolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
StatementParamsHolder::~StatementParamsHolder()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// We are considered dead at this point, so any wrappers for row or params
|
|
|
|
// need to lose their reference to the statement.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper = do_QueryInterface(mHolder);
|
|
|
|
nsCOMPtr<mozIStorageStatementParams> iObj = do_QueryWrappedNative(wrapper);
|
|
|
|
StatementParams *obj = static_cast<StatementParams *>(iObj.get());
|
|
|
|
obj->mStatement = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatementRowHolder::~StatementRowHolder()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// We are considered dead at this point, so any wrappers for row or params
|
|
|
|
// need to lose their reference to the statement.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper = do_QueryInterface(mHolder);
|
|
|
|
nsCOMPtr<mozIStorageStatementRow> iObj = do_QueryWrappedNative(wrapper);
|
|
|
|
StatementRow *obj = static_cast<StatementRow *>(iObj.get());
|
|
|
|
obj->mStatement = nullptr;
|
|
|
|
}
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|