Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / storage / src / mozStorageStatementRow.cpp
blob92ec490fcc3f10cdad1af82e2155d3c4ea3a0581
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"
44 #include "mozStorageStatementRow.h"
45 #include "mozStorageStatement.h"
47 #include "jsapi.h"
48 #include "jsdate.h"
50 namespace mozilla {
51 namespace storage {
53 ////////////////////////////////////////////////////////////////////////////////
54 //// StatementRow
56 StatementRow::StatementRow(Statement *aStatement)
57 : mStatement(aStatement)
61 NS_IMPL_ISUPPORTS2(
62 StatementRow,
63 mozIStorageStatementRow,
64 nsIXPCScriptable
67 ////////////////////////////////////////////////////////////////////////////////
68 //// nsIXPCScriptable
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"
77 NS_IMETHODIMP
78 StatementRow::GetProperty(nsIXPConnectWrappedNative *aWrapper,
79 JSContext *aCtx,
80 JSObject *aScopeObj,
81 jsid aId,
82 jsval *_vp,
83 PRBool *_retval)
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());
92 PRUint32 idx;
93 nsresult rv = mStatement->GetColumnIndex(jsid, &idx);
94 NS_ENSURE_SUCCESS(rv, rv);
95 PRInt32 type;
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) {
101 double dval;
102 rv = mStatement->GetDouble(idx, &dval);
103 NS_ENSURE_SUCCESS(rv, rv);
104 if (!::JS_NewNumberValue(aCtx, dval, _vp)) {
105 *_retval = PR_FALSE;
106 return NS_OK;
109 else if (type == mozIStorageValueArray::VALUE_TYPE_TEXT) {
110 PRUint32 bytes;
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);
116 if (!str) {
117 *_retval = PR_FALSE;
118 return NS_OK;
120 *_vp = STRING_TO_JSVAL(str);
122 else if (type == mozIStorageValueArray::VALUE_TYPE_BLOB) {
123 PRUint32 length;
124 const PRUint8 *blob = static_cast<mozIStorageStatement *>(mStatement)->
125 AsSharedBlob(idx, &length);
126 JSObject *obj = ::JS_NewArrayObject(aCtx, length, nsnull);
127 if (!obj) {
128 *_retval = PR_FALSE;
129 return NS_OK;
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)) {
137 *_retval = PR_FALSE;
138 return NS_OK;
142 else if (type == mozIStorageValueArray::VALUE_TYPE_NULL) {
143 *_vp = JSVAL_NULL;
145 else {
146 NS_ERROR("unknown column type returned, what's going on?");
150 return NS_OK;
153 NS_IMETHODIMP
154 StatementRow::NewResolve(nsIXPConnectWrappedNative *aWrapper,
155 JSContext *aCtx,
156 JSObject *aScopeObj,
157 jsid aId,
158 PRUint32 aFlags,
159 JSObject **_objp,
160 PRBool *_retval)
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());
171 PRUint32 idx;
172 nsresult rv = mStatement->GetColumnIndex(name, &idx);
173 if (NS_FAILED(rv)) {
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
176 // either.
177 *_objp = NULL;
178 return NS_OK;
181 *_retval = ::JS_DefinePropertyById(aCtx, aScopeObj, aId, JSVAL_VOID,
182 nsnull, nsnull, 0);
183 *_objp = aScopeObj;
184 return NS_OK;
187 return NS_OK;
190 } // namespace storage
191 } // namescape mozilla