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/. */
7 #include "nsIXPConnect.h"
8 #include "mozStorageStatement.h"
9 #include "mozStorageService.h"
13 #include "nsServiceManagerUtils.h"
15 #include "mozStorageStatementJSHelper.h"
17 #include "mozStorageStatementRow.h"
18 #include "mozStorageStatementParams.h"
25 ////////////////////////////////////////////////////////////////////////////////
30 stepFunc(JSContext
*aCtx
,
34 nsCOMPtr
<nsIXPConnect
> xpc(Service::getXPConnect());
35 nsCOMPtr
<nsIXPConnectWrappedNative
> wrapper
;
36 JSObject
*obj
= JS_THIS_OBJECT(aCtx
, _vp
);
42 xpc
->GetWrappedNativeOfJSObject(aCtx
, obj
, getter_AddRefs(wrapper
));
44 ::JS_ReportError(aCtx
, "mozIStorageStatement::step() could not obtain native statement");
50 nsCOMPtr
<mozIStorageStatement
> isStatement(
51 do_QueryInterface(wrapper
->Native())
53 NS_ASSERTION(isStatement
, "How is this not a statement?!");
57 Statement
*stmt
= static_cast<Statement
*>(
58 static_cast<mozIStorageStatement
*>(wrapper
->Native())
62 rv
= stmt
->ExecuteStep(&hasMore
);
63 if (NS_SUCCEEDED(rv
) && !hasMore
) {
70 ::JS_ReportError(aCtx
, "mozIStorageStatement::step() returned an error");
74 *_vp
= BOOLEAN_TO_JSVAL(hasMore
);
78 ////////////////////////////////////////////////////////////////////////////////
79 //// StatementJSHelper
82 StatementJSHelper::getRow(Statement
*aStatement
,
91 (void)aStatement
->GetState(&state
);
92 NS_ASSERTION(state
== mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING
,
93 "Invalid state to get the row object - all calls will fail!");
96 if (!aStatement
->mStatementRowHolder
) {
97 JS::RootedObject
scope(aCtx
, aScopeObj
);
98 nsCOMPtr
<mozIStorageStatementRow
> row(new StatementRow(aStatement
));
99 NS_ENSURE_TRUE(row
, NS_ERROR_OUT_OF_MEMORY
);
101 nsCOMPtr
<nsIXPConnect
> xpc(Service::getXPConnect());
102 rv
= xpc
->WrapNative(
104 ::JS_GetGlobalForObject(aCtx
, scope
),
106 NS_GET_IID(mozIStorageStatementRow
),
107 getter_AddRefs(aStatement
->mStatementRowHolder
)
109 NS_ENSURE_SUCCESS(rv
, rv
);
112 JS::Rooted
<JSObject
*> obj(aCtx
);
113 obj
= aStatement
->mStatementRowHolder
->GetJSObject();
114 NS_ENSURE_STATE(obj
);
116 *_row
= OBJECT_TO_JSVAL(obj
);
121 StatementJSHelper::getParams(Statement
*aStatement
,
130 (void)aStatement
->GetState(&state
);
131 NS_ASSERTION(state
== mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY
,
132 "Invalid state to get the params object - all calls will fail!");
135 if (!aStatement
->mStatementParamsHolder
) {
136 JS::RootedObject
scope(aCtx
, aScopeObj
);
137 nsCOMPtr
<mozIStorageStatementParams
> params
=
138 new StatementParams(aStatement
);
139 NS_ENSURE_TRUE(params
, NS_ERROR_OUT_OF_MEMORY
);
141 nsCOMPtr
<nsIXPConnect
> xpc(Service::getXPConnect());
142 rv
= xpc
->WrapNative(
144 ::JS_GetGlobalForObject(aCtx
, scope
),
146 NS_GET_IID(mozIStorageStatementParams
),
147 getter_AddRefs(aStatement
->mStatementParamsHolder
)
149 NS_ENSURE_SUCCESS(rv
, rv
);
152 JS::Rooted
<JSObject
*> obj(aCtx
);
153 obj
= aStatement
->mStatementParamsHolder
->GetJSObject();
154 NS_ENSURE_STATE(obj
);
156 *_params
= OBJECT_TO_JSVAL(obj
);
160 NS_IMETHODIMP_(MozExternalRefCountType
) StatementJSHelper::AddRef() { return 2; }
161 NS_IMETHODIMP_(MozExternalRefCountType
) StatementJSHelper::Release() { return 1; }
162 NS_INTERFACE_MAP_BEGIN(StatementJSHelper
)
163 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable
)
164 NS_INTERFACE_MAP_ENTRY(nsISupports
)
167 ////////////////////////////////////////////////////////////////////////////////
168 //// nsIXPCScriptable
170 #define XPC_MAP_CLASSNAME StatementJSHelper
171 #define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
172 #define XPC_MAP_WANT_GETPROPERTY
173 #define XPC_MAP_WANT_NEWRESOLVE
174 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
175 #include "xpc_map_end.h"
178 StatementJSHelper::GetProperty(nsIXPConnectWrappedNative
*aWrapper
,
185 if (!JSID_IS_STRING(aId
))
188 JS::Rooted
<JSObject
*> scope(aCtx
, aScopeObj
);
189 JS::Rooted
<jsid
> id(aCtx
, aId
);
193 nsCOMPtr
<mozIStorageStatement
> isStatement(
194 do_QueryInterface(aWrapper
->Native()));
195 NS_ASSERTION(isStatement
, "How is this not a statement?!");
199 Statement
*stmt
= static_cast<Statement
*>(
200 static_cast<mozIStorageStatement
*>(aWrapper
->Native())
203 JSFlatString
*str
= JSID_TO_FLAT_STRING(id
);
204 if (::JS_FlatStringEqualsAscii(str
, "row"))
205 return getRow(stmt
, aCtx
, scope
, _result
);
207 if (::JS_FlatStringEqualsAscii(str
, "params"))
208 return getParams(stmt
, aCtx
, scope
, _result
);
215 StatementJSHelper::NewResolve(nsIXPConnectWrappedNative
*aWrapper
,
222 if (!JSID_IS_STRING(aId
))
225 JS::RootedObject
scope(aCtx
, aScopeObj
);
226 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId
), "step")) {
227 *_retval
= ::JS_DefineFunction(aCtx
, scope
, "step", stepFunc
,
229 *_objp
= scope
.get();
235 } // namespace storage
236 } // namespace mozilla