Bug 628922 - layout should use cached nsIAccessibilityService, r=davidb, sr=roc,...
[mozilla-central.git] / storage / src / mozStorageStatementJSHelper.cpp
blob6add7e4e568f33745c78b49f20e6aec55b856002
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 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozStorage code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsIXPConnect.h"
41 #include "mozStorageStatement.h"
42 #include "mozStorageService.h"
44 #include "nsMemory.h"
45 #include "nsString.h"
46 #include "nsServiceManagerUtils.h"
48 #include "mozStorageStatementJSHelper.h"
50 #include "mozStorageStatementRow.h"
51 #include "mozStorageStatementParams.h"
53 #include "jsapi.h"
55 namespace mozilla {
56 namespace storage {
58 ////////////////////////////////////////////////////////////////////////////////
59 //// Global Functions
61 static
62 JSBool
63 stepFunc(JSContext *aCtx,
64 PRUint32,
65 jsval *_vp)
67 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
68 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
69 nsresult rv = xpc->GetWrappedNativeOfJSObject(
70 aCtx, JS_THIS_OBJECT(aCtx, _vp), getter_AddRefs(wrapper)
72 if (NS_FAILED(rv)) {
73 ::JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement");
74 return JS_FALSE;
77 #ifdef DEBUG
79 nsCOMPtr<mozIStorageStatement> isStatement(
80 do_QueryInterface(wrapper->Native())
82 NS_ASSERTION(isStatement, "How is this not a statement?!");
84 #endif
86 Statement *stmt = static_cast<Statement *>(
87 static_cast<mozIStorageStatement *>(wrapper->Native())
90 PRBool hasMore = PR_FALSE;
91 rv = stmt->ExecuteStep(&hasMore);
92 if (NS_SUCCEEDED(rv) && !hasMore) {
93 *_vp = JSVAL_FALSE;
94 (void)stmt->Reset();
95 return JS_TRUE;
98 if (NS_FAILED(rv)) {
99 ::JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error");
100 return JS_FALSE;
103 *_vp = BOOLEAN_TO_JSVAL(hasMore);
104 return JS_TRUE;
107 ////////////////////////////////////////////////////////////////////////////////
108 //// StatementJSHelper
110 nsresult
111 StatementJSHelper::getRow(Statement *aStatement,
112 JSContext *aCtx,
113 JSObject *aScopeObj,
114 jsval *_row)
116 nsresult rv;
118 #ifdef DEBUG
119 PRInt32 state;
120 (void)aStatement->GetState(&state);
121 NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING,
122 "Invalid state to get the row object - all calls will fail!");
123 #endif
125 if (!aStatement->mStatementRowHolder) {
126 nsCOMPtr<mozIStorageStatementRow> row(new StatementRow(aStatement));
127 NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
129 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
130 rv = xpc->WrapNative(
131 aCtx,
132 ::JS_GetGlobalForObject(aCtx, aScopeObj),
133 row,
134 NS_GET_IID(mozIStorageStatementRow),
135 getter_AddRefs(aStatement->mStatementRowHolder)
137 NS_ENSURE_SUCCESS(rv, rv);
140 JSObject *obj = nsnull;
141 rv = aStatement->mStatementRowHolder->GetJSObject(&obj);
142 NS_ENSURE_SUCCESS(rv, rv);
144 *_row = OBJECT_TO_JSVAL(obj);
145 return NS_OK;
148 nsresult
149 StatementJSHelper::getParams(Statement *aStatement,
150 JSContext *aCtx,
151 JSObject *aScopeObj,
152 jsval *_params)
154 nsresult rv;
156 #ifdef DEBUG
157 PRInt32 state;
158 (void)aStatement->GetState(&state);
159 NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY,
160 "Invalid state to get the params object - all calls will fail!");
161 #endif
163 if (!aStatement->mStatementParamsHolder) {
164 nsCOMPtr<mozIStorageStatementParams> params =
165 new StatementParams(aStatement);
166 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
168 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
169 rv = xpc->WrapNative(
170 aCtx,
171 ::JS_GetGlobalForObject(aCtx, aScopeObj),
172 params,
173 NS_GET_IID(mozIStorageStatementParams),
174 getter_AddRefs(aStatement->mStatementParamsHolder)
176 NS_ENSURE_SUCCESS(rv, rv);
179 JSObject *obj = nsnull;
180 rv = aStatement->mStatementParamsHolder->GetJSObject(&obj);
181 NS_ENSURE_SUCCESS(rv, rv);
183 *_params = OBJECT_TO_JSVAL(obj);
184 return NS_OK;
187 NS_IMETHODIMP_(nsrefcnt) StatementJSHelper::AddRef() { return 2; }
188 NS_IMETHODIMP_(nsrefcnt) StatementJSHelper::Release() { return 1; }
189 NS_INTERFACE_MAP_BEGIN(StatementJSHelper)
190 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
191 NS_INTERFACE_MAP_ENTRY(nsISupports)
192 NS_INTERFACE_MAP_END
194 ////////////////////////////////////////////////////////////////////////////////
195 //// nsIXPCScriptable
197 #define XPC_MAP_CLASSNAME StatementJSHelper
198 #define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
199 #define XPC_MAP_WANT_GETPROPERTY
200 #define XPC_MAP_WANT_NEWRESOLVE
201 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
202 #include "xpc_map_end.h"
204 NS_IMETHODIMP
205 StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
206 JSContext *aCtx,
207 JSObject *aScopeObj,
208 jsid aId,
209 jsval *_result,
210 PRBool *_retval)
212 if (!JSID_IS_STRING(aId))
213 return NS_OK;
215 #ifdef DEBUG
217 nsCOMPtr<mozIStorageStatement> isStatement(
218 do_QueryInterface(aWrapper->Native()));
219 NS_ASSERTION(isStatement, "How is this not a statement?!");
221 #endif
223 Statement *stmt = static_cast<Statement *>(
224 static_cast<mozIStorageStatement *>(aWrapper->Native())
227 JSFlatString *str = JSID_TO_FLAT_STRING(aId);
228 if (::JS_FlatStringEqualsAscii(str, "row"))
229 return getRow(stmt, aCtx, aScopeObj, _result);
231 if (::JS_FlatStringEqualsAscii(str, "params"))
232 return getParams(stmt, aCtx, aScopeObj, _result);
234 return NS_OK;
238 NS_IMETHODIMP
239 StatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper,
240 JSContext *aCtx,
241 JSObject *aScopeObj,
242 jsid aId,
243 PRUint32 aFlags,
244 JSObject **_objp,
245 PRBool *_retval)
247 if (!JSID_IS_STRING(aId))
248 return NS_OK;
250 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) {
251 *_retval = ::JS_DefineFunction(aCtx, aScopeObj, "step", stepFunc,
252 0, 0) != nsnull;
253 *_objp = aScopeObj;
254 return NS_OK;
256 return NS_OK;
259 } // namespace storage
260 } // namespace mozilla