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/ErrorResult.h"
11 #include "mozilla/dom/MozStorageStatementRowBinding.h"
12 #include "mozStorageStatementRow.h"
13 #include "mozStorageStatement.h"
16 #include "js/Array.h" // JS::NewArrayObject
17 #include "js/PropertyAndElement.h" // JS_DefineElement
20 #include "xpc_make_class.h"
25 ////////////////////////////////////////////////////////////////////////////////
28 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow
, mWindow
)
30 NS_INTERFACE_TABLE_HEAD(StatementRow
)
31 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
32 NS_INTERFACE_TABLE(StatementRow
, nsISupports
)
33 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow
)
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow
)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow
)
39 StatementRow::StatementRow(nsPIDOMWindowInner
* aWindow
, Statement
* aStatement
)
40 : mWindow(aWindow
), mStatement(aStatement
) {}
42 JSObject
* StatementRow::WrapObject(JSContext
* aCx
,
43 JS::Handle
<JSObject
*> aGivenProto
) {
44 return dom::MozStorageStatementRow_Binding::Wrap(aCx
, this, aGivenProto
);
47 void StatementRow::NamedGetter(JSContext
* aCx
, const nsAString
& aName
,
49 JS::MutableHandle
<JS::Value
> aResult
,
50 mozilla::ErrorResult
& aRv
) {
52 aRv
.Throw(NS_ERROR_NOT_INITIALIZED
);
56 nsCString name
= NS_ConvertUTF16toUTF8(aName
);
60 nsresult rv
= mStatement
->GetColumnIndex(name
, &idx
);
62 // It's highly likely that the name doesn't exist, so let the JS engine
63 // check the prototype chain and throw if that doesn't have the property
71 aRv
= mStatement
->GetTypeOfIndex(idx
, &type
);
77 case mozIStorageValueArray::VALUE_TYPE_INTEGER
:
78 case mozIStorageValueArray::VALUE_TYPE_FLOAT
: {
80 aRv
= mStatement
->GetDouble(idx
, &dval
);
84 aResult
.set(::JS_NumberValue(dval
));
87 case mozIStorageValueArray::VALUE_TYPE_TEXT
: {
89 const char16_t
* sval
= reinterpret_cast<const char16_t
*>(
90 static_cast<mozIStorageStatement
*>(mStatement
)
91 ->AsSharedWString(idx
, &bytes
));
93 ::JS_NewUCStringCopyN(aCx
, sval
, bytes
/ sizeof(char16_t
));
95 aRv
.Throw(NS_ERROR_UNEXPECTED
);
98 aResult
.setString(str
);
101 case mozIStorageValueArray::VALUE_TYPE_BLOB
: {
103 const uint8_t* blob
= static_cast<mozIStorageStatement
*>(mStatement
)
104 ->AsSharedBlob(idx
, &length
);
105 JS::Rooted
<JSObject
*> obj(aCx
, JS::NewArrayObject(aCx
, length
));
107 aRv
.Throw(NS_ERROR_UNEXPECTED
);
110 aResult
.setObject(*obj
);
112 // Copy the blob over to the JS array.
113 for (uint32_t i
= 0; i
< length
; i
++) {
114 if (!::JS_DefineElement(aCx
, obj
, i
, blob
[i
], JSPROP_ENUMERATE
)) {
115 aRv
.Throw(NS_ERROR_UNEXPECTED
);
121 case mozIStorageValueArray::VALUE_TYPE_NULL
:
125 NS_ERROR("unknown column type returned, what's going on?");
131 void StatementRow::GetSupportedNames(nsTArray
<nsString
>& aNames
) {
136 uint32_t columnCount
;
137 nsresult rv
= mStatement
->GetColumnCount(&columnCount
);
138 if (NS_WARN_IF(NS_FAILED(rv
))) {
142 for (uint32_t i
= 0; i
< columnCount
; i
++) {
144 nsresult rv
= mStatement
->GetColumnName(i
, name
);
145 if (NS_WARN_IF(NS_FAILED(rv
))) {
148 aNames
.AppendElement(NS_ConvertUTF8toUTF16(name
));
152 } // namespace storage
153 } // namespace mozilla