Bug 460546 - nsBrowserGlue should use a smart getter for the pref service. r=gavin
[wine-gecko.git] / storage / src / mozStorageStatementParams.cpp
blobe9390e1342ed3f0706b57800f6a3f60ec69af419
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: sw=4 ts=4 sts=4
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 Oracle Corporation code.
18 * The Initial Developer of the Original Code is
19 * Oracle Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Shawn Wilsher <me@shawnwilsher.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsMemory.h"
42 #include "nsString.h"
44 #include "mozStorageStatementParams.h"
46 #include "sqlite3.h"
48 /*************************************************************************
49 ****
50 **** mozStorageStatementParams
51 ****
52 *************************************************************************/
54 NS_IMPL_ISUPPORTS2(mozStorageStatementParams, mozIStorageStatementParams, nsIXPCScriptable)
56 mozStorageStatementParams::mozStorageStatementParams(mozIStorageStatement *aStatement)
57 : mStatement(aStatement)
59 NS_ASSERTION(mStatement != nsnull, "mStatement is null");
60 mStatement->GetParameterCount(&mParamCount);
64 * nsIXPCScriptable impl
67 /* readonly attribute string className; */
68 NS_IMETHODIMP
69 mozStorageStatementParams::GetClassName(char * *aClassName)
71 NS_ENSURE_ARG_POINTER(aClassName);
72 *aClassName = (char *) nsMemory::Clone("mozStorageStatementParams", 26);
73 if (!*aClassName)
74 return NS_ERROR_OUT_OF_MEMORY;
75 return NS_OK;
78 /* readonly attribute PRUint32 scriptableFlags; */
79 NS_IMETHODIMP
80 mozStorageStatementParams::GetScriptableFlags(PRUint32 *aScriptableFlags)
82 *aScriptableFlags =
83 nsIXPCScriptable::WANT_SETPROPERTY |
84 nsIXPCScriptable::WANT_NEWRESOLVE |
85 nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE;
86 return NS_OK;
89 /* PRBool getProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in JSValPtr vp); */
90 NS_IMETHODIMP
91 mozStorageStatementParams::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
92 JSObject * obj, jsval id, jsval * vp, PRBool *_retval)
94 return NS_ERROR_NOT_IMPLEMENTED;
98 /* PRBool setProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in JSValPtr vp); */
99 NS_IMETHODIMP
100 mozStorageStatementParams::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
101 JSObject * obj, jsval id, jsval * vp, PRBool *_retval)
103 if (JSVAL_IS_INT(id)) {
104 int idx = JSVAL_TO_INT(id);
106 *_retval = JSValStorageStatementBinder (cx, mStatement, idx, *vp);
107 } else if (JSVAL_IS_STRING(id)) {
108 sqlite3_stmt *stmt = mStatement->GetNativeStatementPointer();
110 JSString *str = JSVAL_TO_STRING(id);
111 nsCAutoString name(":");
112 name.Append(NS_ConvertUTF16toUTF8(nsDependentString((PRUnichar *)::JS_GetStringChars(str),
113 ::JS_GetStringLength(str))));
115 // check to see if there's a parameter with this name
116 if (sqlite3_bind_parameter_index(stmt, name.get()) == 0) {
117 *_retval = PR_FALSE;
118 return NS_ERROR_INVALID_ARG;
121 *_retval = PR_TRUE;
122 // You can use a named parameter more than once in a statement...
123 int count = sqlite3_bind_parameter_count(stmt);
124 for (int i = 0; (i < count) && (*_retval); i++) {
125 // sqlite indices start at 1
126 const char *pName = sqlite3_bind_parameter_name(stmt, i + 1);
127 if (name.Equals(pName))
128 *_retval = JSValStorageStatementBinder(cx, mStatement, i, *vp);
130 } else {
131 *_retval = PR_FALSE;
134 return (*_retval) ? NS_OK : NS_ERROR_INVALID_ARG;
137 /* void preCreate (in nsISupports nativeObj, in JSContextPtr cx, in JSObjectPtr globalObj, out JSObjectPtr parentObj); */
138 NS_IMETHODIMP
139 mozStorageStatementParams::PreCreate(nsISupports *nativeObj, JSContext * cx,
140 JSObject * globalObj, JSObject * *parentObj)
142 return NS_ERROR_NOT_IMPLEMENTED;
145 /* void create (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
146 NS_IMETHODIMP
147 mozStorageStatementParams::Create(nsIXPConnectWrappedNative *wrapper, JSContext * cx, JSObject * obj)
149 return NS_ERROR_NOT_IMPLEMENTED;
152 /* void postCreate (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
153 NS_IMETHODIMP
154 mozStorageStatementParams::PostCreate(nsIXPConnectWrappedNative *wrapper, JSContext * cx, JSObject * obj)
156 return NS_ERROR_NOT_IMPLEMENTED;
159 /* PRBool addProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in JSValPtr vp); */
160 NS_IMETHODIMP
161 mozStorageStatementParams::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
162 JSObject * obj, jsval id, jsval * vp, PRBool *_retval)
164 return NS_ERROR_NOT_IMPLEMENTED;
167 /* PRBool delProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in JSValPtr vp); */
168 NS_IMETHODIMP
169 mozStorageStatementParams::DelProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
170 JSObject * obj, jsval id, jsval * vp, PRBool *_retval)
172 return NS_ERROR_NOT_IMPLEMENTED;
175 /* PRBool enumerate (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
176 NS_IMETHODIMP
177 mozStorageStatementParams::Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
178 JSObject * obj, PRBool *_retval)
180 return NS_ERROR_NOT_IMPLEMENTED;
183 /* PRBool newEnumerate (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in PRUint32 enum_op, in JSValPtr statep, out JSID idp); */
184 NS_IMETHODIMP
185 mozStorageStatementParams::NewEnumerate(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
186 JSObject * obj, PRUint32 enum_op, jsval * statep, jsid *idp, PRBool *_retval)
188 return NS_ERROR_NOT_IMPLEMENTED;
191 /* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
192 NS_IMETHODIMP
193 mozStorageStatementParams::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
194 JSObject * obj, jsval id, PRUint32 flags, JSObject * *objp, PRBool *_retval)
196 int idx = -1;
198 if (JSVAL_IS_INT(id)) {
199 idx = JSVAL_TO_INT(id);
200 } else if (JSVAL_IS_STRING(id)) {
201 JSString *str = JSVAL_TO_STRING(id);
202 nsCAutoString name(":");
203 name.Append(NS_ConvertUTF16toUTF8(nsDependentString((PRUnichar *)::JS_GetStringChars(str),
204 ::JS_GetStringLength(str))));
206 // check to see if there's a parameter with this name
207 idx = sqlite3_bind_parameter_index(mStatement->GetNativeStatementPointer(), name.get());
208 if (idx == 0) {
209 // nope.
210 fprintf (stderr, "********** mozStorageStatementWrapper: Couldn't find parameter %s\n", name.get());
211 *_retval = PR_FALSE;
212 return NS_OK;
213 } else {
214 // set idx, so that the numbered property also gets defined
215 idx = idx - 1;
218 PRBool success = ::JS_DefineUCProperty(cx, obj, ::JS_GetStringChars(str),
219 ::JS_GetStringLength(str),
220 JSVAL_VOID,
221 nsnull, nsnull, 0);
222 if (!success) {
223 *_retval = PR_FALSE;
224 return NS_ERROR_FAILURE;
228 if (idx == -1) {
229 *_retval = PR_FALSE;
230 return NS_ERROR_FAILURE;
233 // is it out of range?
234 if (idx < 0 || idx >= (int)mParamCount) {
235 *_retval = PR_FALSE;
236 return NS_OK;
239 *_retval = ::JS_DefineElement(cx, obj, idx, JSVAL_VOID, nsnull, nsnull, 0);
240 if (*_retval)
241 *objp = obj;
242 return NS_OK;
245 /* PRBool convert (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in PRUint32 type, in JSValPtr vp); */
246 NS_IMETHODIMP
247 mozStorageStatementParams::Convert(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
248 JSObject * obj, PRUint32 type, jsval * vp, PRBool *_retval)
250 return NS_ERROR_NOT_IMPLEMENTED;
253 /* void finalize (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
254 NS_IMETHODIMP
255 mozStorageStatementParams::Finalize(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
256 JSObject * obj)
258 return NS_ERROR_NOT_IMPLEMENTED;
261 /* PRBool checkAccess (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 mode, in JSValPtr vp); */
262 NS_IMETHODIMP
263 mozStorageStatementParams::CheckAccess(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
264 JSObject * obj, jsval id, PRUint32 mode, jsval * vp, PRBool *_retval)
266 return NS_ERROR_NOT_IMPLEMENTED;
269 /* PRBool call (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in PRUint32 argc, in JSValPtr argv, in JSValPtr vp); */
270 NS_IMETHODIMP
271 mozStorageStatementParams::Call(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
272 JSObject * obj, PRUint32 argc, jsval * argv, jsval * vp, PRBool *_retval)
274 return NS_ERROR_NOT_IMPLEMENTED;
277 /* PRBool construct (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in PRUint32 argc, in JSValPtr argv, in JSValPtr vp); */
278 NS_IMETHODIMP
279 mozStorageStatementParams::Construct(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
280 JSObject * obj, PRUint32 argc, jsval * argv, jsval * vp, PRBool *_retval)
282 return NS_ERROR_NOT_IMPLEMENTED;
285 /* PRBool hasInstance (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal val, out PRBool bp); */
286 NS_IMETHODIMP
287 mozStorageStatementParams::HasInstance(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
288 JSObject * obj, jsval val, PRBool *bp, PRBool *_retval)
290 return NS_ERROR_NOT_IMPLEMENTED;
293 /* void trace (in nsIXPConnectWrappedNative wrapper, in JSTracerPtr trc, in JSObjectPtr obj); */
294 NS_IMETHODIMP
295 mozStorageStatementParams::Trace(nsIXPConnectWrappedNative *wrapper,
296 JSTracer *trc, JSObject * obj)
298 return NS_ERROR_NOT_IMPLEMENTED;
301 /* PRBool equality(in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal val); */
302 NS_IMETHODIMP
303 mozStorageStatementParams::Equality(nsIXPConnectWrappedNative *wrapper,
304 JSContext *cx, JSObject *obj, jsval val,
305 PRBool *_retval)
307 return NS_ERROR_NOT_IMPLEMENTED;
310 /* JSObjectPtr outerObject(in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
311 NS_IMETHODIMP
312 mozStorageStatementParams::OuterObject(nsIXPConnectWrappedNative *wrapper,
313 JSContext *cx, JSObject *obj,
314 JSObject **_retval)
316 return NS_ERROR_NOT_IMPLEMENTED;
319 /* JSObjectPtr innerObject(in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
320 NS_IMETHODIMP
321 mozStorageStatementParams::InnerObject(nsIXPConnectWrappedNative *wrapper,
322 JSContext *cx, JSObject *obj,
323 JSObject **_retval)
325 return NS_ERROR_NOT_IMPLEMENTED;
328 /* void postCreatePrototype (in JSContextPtr cx, in JSObjectPtr proto); */
329 NS_IMETHODIMP
330 mozStorageStatementParams::PostCreatePrototype(JSContext * cx, JSObject * proto)
332 return NS_OK;