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 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/ErrorResult.h"
10 #include "mozilla/dom/MozStorageStatementRowBinding.h"
11 #include "mozStorageStatementRow.h"
12 #include "mozStorageStatement.h"
15 #include "js/Array.h" // JS::NewArrayObject
16 #include "js/PropertyAndElement.h" // JS_DefineElement
19 #include "xpc_make_class.h"
24 ////////////////////////////////////////////////////////////////////////////////
27 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow
, mWindow
)
29 NS_INTERFACE_TABLE_HEAD(StatementRow
)
30 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
31 NS_INTERFACE_TABLE(StatementRow
, nsISupports
)
32 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow
)
35 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow
)
36 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow
)
38 StatementRow::StatementRow(nsPIDOMWindowInner
* aWindow
, Statement
* aStatement
)
39 : mWindow(aWindow
), mStatement(aStatement
) {}
41 JSObject
* StatementRow::WrapObject(JSContext
* aCx
,
42 JS::Handle
<JSObject
*> aGivenProto
) {
43 return dom::MozStorageStatementRow_Binding::Wrap(aCx
, this, aGivenProto
);
46 void StatementRow::NamedGetter(JSContext
* aCx
, const nsAString
& aName
,
48 JS::MutableHandle
<JS::Value
> aResult
,
49 mozilla::ErrorResult
& aRv
) {
51 aRv
.Throw(NS_ERROR_NOT_INITIALIZED
);
55 nsCString name
= NS_ConvertUTF16toUTF8(aName
);
59 nsresult rv
= mStatement
->GetColumnIndex(name
, &idx
);
61 // It's highly likely that the name doesn't exist, so let the JS engine
62 // check the prototype chain and throw if that doesn't have the property
70 aRv
= mStatement
->GetTypeOfIndex(idx
, &type
);
76 case mozIStorageValueArray::VALUE_TYPE_INTEGER
:
77 case mozIStorageValueArray::VALUE_TYPE_FLOAT
: {
79 aRv
= mStatement
->GetDouble(idx
, &dval
);
83 aResult
.set(::JS_NumberValue(dval
));
86 case mozIStorageValueArray::VALUE_TYPE_TEXT
: {
88 const char16_t
* sval
= reinterpret_cast<const char16_t
*>(
89 static_cast<mozIStorageStatement
*>(mStatement
)
90 ->AsSharedWString(idx
, &bytes
));
92 ::JS_NewUCStringCopyN(aCx
, sval
, bytes
/ sizeof(char16_t
));
94 aRv
.Throw(NS_ERROR_UNEXPECTED
);
97 aResult
.setString(str
);
100 case mozIStorageValueArray::VALUE_TYPE_BLOB
: {
102 const uint8_t* blob
= static_cast<mozIStorageStatement
*>(mStatement
)
103 ->AsSharedBlob(idx
, &length
);
104 JS::Rooted
<JSObject
*> obj(aCx
, JS::NewArrayObject(aCx
, length
));
106 aRv
.Throw(NS_ERROR_UNEXPECTED
);
109 aResult
.setObject(*obj
);
111 // Copy the blob over to the JS array.
112 for (uint32_t i
= 0; i
< length
; i
++) {
113 if (!::JS_DefineElement(aCx
, obj
, i
, blob
[i
], JSPROP_ENUMERATE
)) {
114 aRv
.Throw(NS_ERROR_UNEXPECTED
);
120 case mozIStorageValueArray::VALUE_TYPE_NULL
:
124 NS_ERROR("unknown column type returned, what's going on?");
130 void StatementRow::GetSupportedNames(nsTArray
<nsString
>& aNames
) {
135 uint32_t columnCount
;
136 nsresult rv
= mStatement
->GetColumnCount(&columnCount
);
137 if (NS_WARN_IF(NS_FAILED(rv
))) {
141 for (uint32_t i
= 0; i
< columnCount
; i
++) {
143 nsresult rv
= mStatement
->GetColumnName(i
, name
);
144 if (NS_WARN_IF(NS_FAILED(rv
))) {
147 aNames
.AppendElement(NS_ConvertUTF8toUTF16(name
));
151 } // namespace storage
152 } // namespace mozilla