1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 expandtab
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
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.
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"
45 #include "nsServiceManagerUtils.h"
47 #include "mozStorageStatementJSHelper.h"
49 #include "mozStorageStatementRow.h"
50 #include "mozStorageStatementParams.h"
54 static nsIXPConnect
*sXPConnect
= nsnull
;
60 (void)CallGetService(nsIXPConnect::GetCID(), &sXPConnect
);
61 NS_ASSERTION(sXPConnect
, "Could not get XPConnect!");
68 stepFunc(JSContext
*aCtx
, PRUint32
, jsval
*_vp
)
70 nsCOMPtr
<nsIXPConnectWrappedNative
> wrapper
;
71 nsresult rv
= XPConnect()->GetWrappedNativeOfJSObject(
72 aCtx
, JS_THIS_OBJECT(aCtx
, _vp
), getter_AddRefs(wrapper
)
75 JS_ReportError(aCtx
, "mozIStorageStatement::step() could not obtain native statement");
79 mozStorageStatement
*stmt
=
80 static_cast<mozStorageStatement
*>(wrapper
->Native());
84 nsCOMPtr
<mozIStorageStatement
> isStatement(do_QueryInterface(stmt
));
85 NS_ASSERTION(isStatement
, "How is this not a statement?!");
89 PRBool hasMore
= PR_FALSE
;
90 rv
= stmt
->ExecuteStep(&hasMore
);
91 if (NS_SUCCEEDED(rv
) && !hasMore
) {
98 JS_ReportError(aCtx
, "mozIStorageStatement::step() returned an error");
102 *_vp
= BOOLEAN_TO_JSVAL(hasMore
);
107 mozStorageStatementJSHelper::getRow(mozStorageStatement
*aStatement
,
108 JSContext
*aCtx
, JSObject
*aScopeObj
,
114 (void)aStatement
->GetState(&state
);
115 if (state
!= mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING
)
116 return NS_ERROR_UNEXPECTED
;
118 if (!aStatement
->mStatementRowHolder
) {
119 nsCOMPtr
<mozIStorageStatementRow
> row
=
120 new mozStorageStatementRow(aStatement
);
121 NS_ENSURE_TRUE(row
, NS_ERROR_OUT_OF_MEMORY
);
123 rv
= XPConnect()->WrapNative(aCtx
, ::JS_GetGlobalForObject(aCtx
, aScopeObj
),
124 row
, NS_GET_IID(mozIStorageStatementRow
),
125 getter_AddRefs(aStatement
->mStatementRowHolder
));
126 NS_ENSURE_SUCCESS(rv
, rv
);
129 JSObject
*obj
= nsnull
;
130 rv
= aStatement
->mStatementRowHolder
->GetJSObject(&obj
);
131 NS_ENSURE_SUCCESS(rv
, rv
);
133 *_row
= OBJECT_TO_JSVAL(obj
);
138 mozStorageStatementJSHelper::getParams(mozStorageStatement
*aStatement
,
139 JSContext
*aCtx
, JSObject
*aScopeObj
,
145 (void)aStatement
->GetState(&state
);
146 if (state
!= mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY
)
147 return NS_ERROR_UNEXPECTED
;
149 if (!aStatement
->mStatementParamsHolder
) {
150 nsCOMPtr
<mozIStorageStatementParams
> params
=
151 new mozStorageStatementParams(aStatement
);
152 NS_ENSURE_TRUE(params
, NS_ERROR_OUT_OF_MEMORY
);
154 rv
= XPConnect()->WrapNative(aCtx
, ::JS_GetGlobalForObject(aCtx
, aScopeObj
),
155 params
, NS_GET_IID(mozIStorageStatementParams
),
156 getter_AddRefs(aStatement
->mStatementParamsHolder
));
157 NS_ENSURE_SUCCESS(rv
, rv
);
160 JSObject
*obj
= nsnull
;
161 rv
= aStatement
->mStatementParamsHolder
->GetJSObject(&obj
);
162 NS_ENSURE_SUCCESS(rv
, rv
);
164 *_params
= OBJECT_TO_JSVAL(obj
);
168 NS_IMETHODIMP_(nsrefcnt
) mozStorageStatementJSHelper::AddRef() { return 2; }
169 NS_IMETHODIMP_(nsrefcnt
) mozStorageStatementJSHelper::Release() { return 1; }
170 NS_INTERFACE_MAP_BEGIN(mozStorageStatementJSHelper
)
171 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable
)
172 NS_INTERFACE_MAP_ENTRY(nsISupports
)
175 ////////////////////////////////////////////////////////////////////////////////
176 //// nsIXPCScriptable
178 #define XPC_MAP_CLASSNAME mozStorageStatementJSHelper
179 #define XPC_MAP_QUOTED_CLASSNAME "mozStorageStatementJSHelper"
180 #define XPC_MAP_WANT_GETPROPERTY
181 #define XPC_MAP_WANT_NEWRESOLVE
182 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
183 #include "xpc_map_end.h"
186 mozStorageStatementJSHelper::GetProperty(nsIXPConnectWrappedNative
*aWrapper
,
187 JSContext
*aCtx
, JSObject
*aScopeObj
,
188 jsval aId
, jsval
*_result
,
191 if (!JSVAL_IS_STRING(aId
))
194 mozStorageStatement
*stmt
=
195 static_cast<mozStorageStatement
*>(aWrapper
->Native());
199 nsCOMPtr
<mozIStorageStatement
> isStatement(do_QueryInterface(stmt
));
200 NS_ASSERTION(isStatement
, "How is this not a statement?!");
204 const char *propName
= JS_GetStringBytes(JSVAL_TO_STRING(aId
));
205 if (strcmp(propName
, "row") == 0)
206 return getRow(stmt
, aCtx
, aScopeObj
, _result
);
208 if (strcmp(propName
, "params") == 0)
209 return getParams(stmt
, aCtx
, aScopeObj
, _result
);
216 mozStorageStatementJSHelper::NewResolve(nsIXPConnectWrappedNative
*aWrapper
,
217 JSContext
*aCtx
, JSObject
*aScopeObj
,
218 jsval aId
, PRUint32 aFlags
,
219 JSObject
**_objp
, PRBool
*_retval
)
221 if (!JSVAL_IS_STRING(aId
))
224 const char *name
= JS_GetStringBytes(JSVAL_TO_STRING(aId
));
225 if (strcmp(name
, "step") == 0) {
226 *_retval
= JS_DefineFunction(aCtx
, aScopeObj
, "step", (JSNative
)stepFunc
, 0,
227 JSFUN_FAST_NATIVE
) != nsnull
;