Bug 634734 - Fennec ASSERTION: mFUnitsConvFactor not valid: mFUnitsConvFactor > 0...
[mozilla-central.git] / storage / src / mozStorageAsyncStatementParams.cpp
blob7f44702b5d130818d3edf89f0516cf02d8a65989
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 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"
43 #include "nsCOMPtr.h"
45 #include "mozStoragePrivateHelpers.h"
46 #include "mozStorageAsyncStatement.h"
47 #include "mozStorageAsyncStatementParams.h"
48 #include "mozIStorageStatement.h"
50 namespace mozilla {
51 namespace storage {
53 ////////////////////////////////////////////////////////////////////////////////
54 //// AsyncStatementParams
56 AsyncStatementParams::AsyncStatementParams(AsyncStatement *aStatement)
57 : mStatement(aStatement)
59 NS_ASSERTION(mStatement != nsnull, "mStatement is null");
62 NS_IMPL_ISUPPORTS2(
63 AsyncStatementParams
64 , mozIStorageStatementParams
65 , nsIXPCScriptable
68 ////////////////////////////////////////////////////////////////////////////////
69 //// nsIXPCScriptable
71 #define XPC_MAP_CLASSNAME AsyncStatementParams
72 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementParams"
73 #define XPC_MAP_WANT_SETPROPERTY
74 #define XPC_MAP_WANT_NEWRESOLVE
75 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
76 #include "xpc_map_end.h"
78 NS_IMETHODIMP
79 AsyncStatementParams::SetProperty(
80 nsIXPConnectWrappedNative *aWrapper,
81 JSContext *aCtx,
82 JSObject *aScopeObj,
83 jsid aId,
84 jsval *_vp,
85 PRBool *_retval
88 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
90 if (JSID_IS_INT(aId)) {
91 int idx = JSID_TO_INT(aId);
93 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
94 NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
95 nsresult rv = mStatement->BindByIndex(idx, variant);
96 NS_ENSURE_SUCCESS(rv, rv);
98 else if (JSID_IS_STRING(aId)) {
99 JSString *str = JSID_TO_STRING(aId);
100 size_t length;
101 const jschar *chars = JS_GetInternedStringCharsAndLength(str, &length);
102 NS_ConvertUTF16toUTF8 name(chars, length);
104 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
105 NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
106 nsresult rv = mStatement->BindByName(name, variant);
107 NS_ENSURE_SUCCESS(rv, rv);
109 else {
110 return NS_ERROR_INVALID_ARG;
113 *_retval = PR_TRUE;
114 return NS_OK;
117 NS_IMETHODIMP
118 AsyncStatementParams::NewResolve(
119 nsIXPConnectWrappedNative *aWrapper,
120 JSContext *aCtx,
121 JSObject *aScopeObj,
122 jsid aId,
123 PRUint32 aFlags,
124 JSObject **_objp,
125 PRBool *_retval
128 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
129 // We do not throw at any point after this because we want to allow the
130 // prototype chain to be checked for the property.
132 bool resolved = false;
133 PRBool ok = PR_TRUE;
134 if (JSID_IS_INT(aId)) {
135 PRUint32 idx = JSID_TO_INT(aId);
136 // All indexes are good because we don't know how many parameters there
137 // really are.
138 ok = ::JS_DefineElement(aCtx, aScopeObj, idx, JSVAL_VOID, nsnull,
139 nsnull, 0);
140 resolved = true;
142 else if (JSID_IS_STRING(aId)) {
143 JSString *str = JSID_TO_STRING(aId);
144 size_t nameLength;
145 const jschar *nameChars = ::JS_GetInternedStringCharsAndLength(str, &nameLength);
147 // We are unable to tell if there's a parameter with this name and so
148 // we must assume that there is. This screws the rest of the prototype
149 // chain, but people really shouldn't be depending on this anyways.
150 ok = ::JS_DefineUCProperty(aCtx, aScopeObj, nameChars, nameLength,
151 JSVAL_VOID, nsnull, nsnull, 0);
152 resolved = true;
155 *_retval = ok;
156 *_objp = resolved && ok ? aScopeObj : nsnull;
157 return NS_OK;
160 } // namespace storage
161 } // namespace mozilla