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/. */
10 #include "mozilla/dom/MozStorageStatementRowBinding.h"
11 #include "mozStorageStatementRow.h"
12 #include "mozStorageStatement.h"
15 #include "js/Array.h" // JS::NewArrayObject
17 #include "xpc_make_class.h"
22 ////////////////////////////////////////////////////////////////////////////////
25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow
, mWindow
)
27 NS_INTERFACE_TABLE_HEAD(StatementRow
)
28 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
29 NS_INTERFACE_TABLE(StatementRow
, nsISupports
)
30 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow
)
33 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow
)
34 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow
)
36 StatementRow::StatementRow(nsPIDOMWindowInner
* aWindow
, Statement
* aStatement
)
37 : mWindow(aWindow
), mStatement(aStatement
) {}
39 JSObject
* StatementRow::WrapObject(JSContext
* aCx
,
40 JS::Handle
<JSObject
*> aGivenProto
) {
41 return dom::MozStorageStatementRow_Binding::Wrap(aCx
, this, aGivenProto
);
44 void StatementRow::NamedGetter(JSContext
* aCx
, const nsAString
& aName
,
46 JS::MutableHandle
<JS::Value
> aResult
,
47 mozilla::ErrorResult
& aRv
) {
49 aRv
.Throw(NS_ERROR_NOT_INITIALIZED
);
53 nsCString name
= NS_ConvertUTF16toUTF8(aName
);
57 nsresult rv
= mStatement
->GetColumnIndex(name
, &idx
);
59 // It's highly likely that the name doesn't exist, so let the JS engine
60 // check the prototype chain and throw if that doesn't have the property
68 aRv
= mStatement
->GetTypeOfIndex(idx
, &type
);
74 case mozIStorageValueArray::VALUE_TYPE_INTEGER
:
75 case mozIStorageValueArray::VALUE_TYPE_FLOAT
: {
77 aRv
= mStatement
->GetDouble(idx
, &dval
);
81 aResult
.set(::JS_NumberValue(dval
));
84 case mozIStorageValueArray::VALUE_TYPE_TEXT
: {
86 const char16_t
* sval
= reinterpret_cast<const char16_t
*>(
87 static_cast<mozIStorageStatement
*>(mStatement
)
88 ->AsSharedWString(idx
, &bytes
));
90 ::JS_NewUCStringCopyN(aCx
, sval
, bytes
/ sizeof(char16_t
));
92 aRv
.Throw(NS_ERROR_UNEXPECTED
);
95 aResult
.setString(str
);
98 case mozIStorageValueArray::VALUE_TYPE_BLOB
: {
100 const uint8_t* blob
= static_cast<mozIStorageStatement
*>(mStatement
)
101 ->AsSharedBlob(idx
, &length
);
102 JS::Rooted
<JSObject
*> obj(aCx
, JS::NewArrayObject(aCx
, length
));
104 aRv
.Throw(NS_ERROR_UNEXPECTED
);
107 aResult
.setObject(*obj
);
109 // Copy the blob over to the JS array.
110 for (uint32_t i
= 0; i
< length
; i
++) {
111 if (!::JS_DefineElement(aCx
, obj
, i
, blob
[i
], JSPROP_ENUMERATE
)) {
112 aRv
.Throw(NS_ERROR_UNEXPECTED
);
118 case mozIStorageValueArray::VALUE_TYPE_NULL
:
122 NS_ERROR("unknown column type returned, what's going on?");
128 void StatementRow::GetSupportedNames(nsTArray
<nsString
>& aNames
) {
133 uint32_t columnCount
;
134 nsresult rv
= mStatement
->GetColumnCount(&columnCount
);
135 if (NS_WARN_IF(NS_FAILED(rv
))) {
139 for (uint32_t i
= 0; i
< columnCount
; i
++) {
141 nsresult rv
= mStatement
->GetColumnName(i
, name
);
142 if (NS_WARN_IF(NS_FAILED(rv
))) {
145 aNames
.AppendElement(NS_ConvertUTF8toUTF16(name
));
149 } // namespace storage
150 } // namespace mozilla