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"
16 #include "xpc_make_class.h"
21 ////////////////////////////////////////////////////////////////////////////////
24 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow
, mWindow
)
26 NS_INTERFACE_TABLE_HEAD(StatementRow
)
27 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
28 NS_INTERFACE_TABLE(StatementRow
, nsISupports
)
29 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow
)
32 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow
)
33 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow
)
35 StatementRow::StatementRow(nsPIDOMWindowInner
* aWindow
, Statement
* aStatement
)
36 : mWindow(aWindow
), mStatement(aStatement
) {}
38 JSObject
* StatementRow::WrapObject(JSContext
* aCx
,
39 JS::Handle
<JSObject
*> aGivenProto
) {
40 return dom::MozStorageStatementRow_Binding::Wrap(aCx
, this, aGivenProto
);
43 void StatementRow::NamedGetter(JSContext
* aCx
, const nsAString
& aName
,
45 JS::MutableHandle
<JS::Value
> aResult
,
46 mozilla::ErrorResult
& aRv
) {
48 aRv
.Throw(NS_ERROR_NOT_INITIALIZED
);
52 nsCString name
= NS_ConvertUTF16toUTF8(aName
);
56 nsresult rv
= mStatement
->GetColumnIndex(name
, &idx
);
58 // It's highly likely that the name doesn't exist, so let the JS engine
59 // check the prototype chain and throw if that doesn't have the property
67 aRv
= mStatement
->GetTypeOfIndex(idx
, &type
);
73 case mozIStorageValueArray::VALUE_TYPE_INTEGER
:
74 case mozIStorageValueArray::VALUE_TYPE_FLOAT
: {
76 aRv
= mStatement
->GetDouble(idx
, &dval
);
80 aResult
.set(::JS_NumberValue(dval
));
83 case mozIStorageValueArray::VALUE_TYPE_TEXT
: {
85 const char16_t
* sval
= reinterpret_cast<const char16_t
*>(
86 static_cast<mozIStorageStatement
*>(mStatement
)
87 ->AsSharedWString(idx
, &bytes
));
89 ::JS_NewUCStringCopyN(aCx
, sval
, bytes
/ sizeof(char16_t
));
91 aRv
.Throw(NS_ERROR_UNEXPECTED
);
94 aResult
.setString(str
);
97 case mozIStorageValueArray::VALUE_TYPE_BLOB
: {
99 const uint8_t* blob
= static_cast<mozIStorageStatement
*>(mStatement
)
100 ->AsSharedBlob(idx
, &length
);
101 JS::Rooted
<JSObject
*> obj(aCx
, ::JS_NewArrayObject(aCx
, length
));
103 aRv
.Throw(NS_ERROR_UNEXPECTED
);
106 aResult
.setObject(*obj
);
108 // Copy the blob over to the JS array.
109 for (uint32_t i
= 0; i
< length
; i
++) {
110 if (!::JS_DefineElement(aCx
, obj
, i
, blob
[i
], JSPROP_ENUMERATE
)) {
111 aRv
.Throw(NS_ERROR_UNEXPECTED
);
117 case mozIStorageValueArray::VALUE_TYPE_NULL
:
121 NS_ERROR("unknown column type returned, what's going on?");
127 void StatementRow::GetSupportedNames(nsTArray
<nsString
>& aNames
) {
132 uint32_t columnCount
;
133 nsresult rv
= mStatement
->GetColumnCount(&columnCount
);
134 if (NS_WARN_IF(NS_FAILED(rv
))) {
138 for (uint32_t i
= 0; i
< columnCount
; i
++) {
140 nsresult rv
= mStatement
->GetColumnName(i
, name
);
141 if (NS_WARN_IF(NS_FAILED(rv
))) {
144 aNames
.AppendElement(NS_ConvertUTF8toUTF16(name
));
148 } // namespace storage
149 } // namespace mozilla