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 Oracle Corporation code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
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 ***** */
44 #include "mozStorageStatementRow.h"
45 #include "mozStorageStatement.h"
53 ////////////////////////////////////////////////////////////////////////////////
56 StatementRow::StatementRow(Statement
*aStatement
)
57 : mStatement(aStatement
)
63 mozIStorageStatementRow
,
67 ////////////////////////////////////////////////////////////////////////////////
70 #define XPC_MAP_CLASSNAME StatementRow
71 #define XPC_MAP_QUOTED_CLASSNAME "StatementRow"
72 #define XPC_MAP_WANT_GETPROPERTY
73 #define XPC_MAP_WANT_NEWRESOLVE
74 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
75 #include "xpc_map_end.h"
78 StatementRow::GetProperty(nsIXPConnectWrappedNative
*aWrapper
,
85 NS_ENSURE_TRUE(mStatement
, NS_ERROR_NOT_INITIALIZED
);
87 if (JSID_IS_STRING(aId
)) {
88 ::JSAutoByteString
idBytes(aCtx
, JSID_TO_STRING(aId
));
89 NS_ENSURE_TRUE(!!idBytes
, NS_ERROR_OUT_OF_MEMORY
);
90 nsDependentCString
jsid(idBytes
.ptr());
93 nsresult rv
= mStatement
->GetColumnIndex(jsid
, &idx
);
94 NS_ENSURE_SUCCESS(rv
, rv
);
96 rv
= mStatement
->GetTypeOfIndex(idx
, &type
);
97 NS_ENSURE_SUCCESS(rv
, rv
);
99 if (type
== mozIStorageValueArray::VALUE_TYPE_INTEGER
||
100 type
== mozIStorageValueArray::VALUE_TYPE_FLOAT
) {
102 rv
= mStatement
->GetDouble(idx
, &dval
);
103 NS_ENSURE_SUCCESS(rv
, rv
);
104 if (!::JS_NewNumberValue(aCtx
, dval
, _vp
)) {
109 else if (type
== mozIStorageValueArray::VALUE_TYPE_TEXT
) {
111 const jschar
*sval
= reinterpret_cast<const jschar
*>(
112 static_cast<mozIStorageStatement
*>(mStatement
)->
113 AsSharedWString(idx
, &bytes
)
115 JSString
*str
= ::JS_NewUCStringCopyN(aCtx
, sval
, bytes
/ 2);
120 *_vp
= STRING_TO_JSVAL(str
);
122 else if (type
== mozIStorageValueArray::VALUE_TYPE_BLOB
) {
124 const PRUint8
*blob
= static_cast<mozIStorageStatement
*>(mStatement
)->
125 AsSharedBlob(idx
, &length
);
126 JSObject
*obj
= ::JS_NewArrayObject(aCtx
, length
, nsnull
);
131 *_vp
= OBJECT_TO_JSVAL(obj
);
133 // Copy the blob over to the JS array.
134 for (PRUint32 i
= 0; i
< length
; i
++) {
135 jsval val
= INT_TO_JSVAL(blob
[i
]);
136 if (!::JS_SetElement(aCtx
, aScopeObj
, i
, &val
)) {
142 else if (type
== mozIStorageValueArray::VALUE_TYPE_NULL
) {
146 NS_ERROR("unknown column type returned, what's going on?");
154 StatementRow::NewResolve(nsIXPConnectWrappedNative
*aWrapper
,
162 NS_ENSURE_TRUE(mStatement
, NS_ERROR_NOT_INITIALIZED
);
163 // We do not throw at any point after this because we want to allow the
164 // prototype chain to be checked for the property.
166 if (JSID_IS_STRING(aId
)) {
167 ::JSAutoByteString
idBytes(aCtx
, JSID_TO_STRING(aId
));
168 NS_ENSURE_TRUE(!!idBytes
, NS_ERROR_OUT_OF_MEMORY
);
169 nsDependentCString
name(idBytes
.ptr());
172 nsresult rv
= mStatement
->GetColumnIndex(name
, &idx
);
174 // It's highly likely that the name doesn't exist, so let the JS engine
175 // check the prototype chain and throw if that doesn't have the property
181 *_retval
= ::JS_DefinePropertyById(aCtx
, aScopeObj
, aId
, JSVAL_VOID
,
190 } // namespace storage
191 } // namescape mozilla