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
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"
42 #include "mozStorageService.h"
46 #include "nsServiceManagerUtils.h"
48 #include "mozStorageStatementJSHelper.h"
50 #include "mozStorageStatementRow.h"
51 #include "mozStorageStatementParams.h"
58 ////////////////////////////////////////////////////////////////////////////////
63 stepFunc(JSContext
*aCtx
,
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
)
73 ::JS_ReportError(aCtx
, "mozIStorageStatement::step() could not obtain native statement");
79 nsCOMPtr
<mozIStorageStatement
> isStatement(
80 do_QueryInterface(wrapper
->Native())
82 NS_ASSERTION(isStatement
, "How is this not a statement?!");
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
) {
99 ::JS_ReportError(aCtx
, "mozIStorageStatement::step() returned an error");
103 *_vp
= BOOLEAN_TO_JSVAL(hasMore
);
107 ////////////////////////////////////////////////////////////////////////////////
108 //// StatementJSHelper
111 StatementJSHelper::getRow(Statement
*aStatement
,
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!");
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(
132 ::JS_GetGlobalForObject(aCtx
, aScopeObj
),
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
);
149 StatementJSHelper::getParams(Statement
*aStatement
,
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!");
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(
171 ::JS_GetGlobalForObject(aCtx
, aScopeObj
),
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
);
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
)
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"
205 StatementJSHelper::GetProperty(nsIXPConnectWrappedNative
*aWrapper
,
212 if (!JSID_IS_STRING(aId
))
217 nsCOMPtr
<mozIStorageStatement
> isStatement(
218 do_QueryInterface(aWrapper
->Native()));
219 NS_ASSERTION(isStatement
, "How is this not a statement?!");
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
);
239 StatementJSHelper::NewResolve(nsIXPConnectWrappedNative
*aWrapper
,
247 if (!JSID_IS_STRING(aId
))
250 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId
), "step")) {
251 *_retval
= ::JS_DefineFunction(aCtx
, aScopeObj
, "step", stepFunc
,
259 } // namespace storage
260 } // namespace mozilla