Bug 1568860 - Part 1: Use PageStyle from the currently selected element in the fonts...
[gecko.git] / storage / mozStorageStatementRow.cpp
blobb6b445b6cfaaa015232bb5d4ed3196d243e5f198
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/. */
7 #include "nsMemory.h"
8 #include "nsString.h"
10 #include "mozilla/dom/MozStorageStatementRowBinding.h"
11 #include "mozStorageStatementRow.h"
12 #include "mozStorageStatement.h"
14 #include "jsapi.h"
16 #include "xpc_make_class.h"
18 namespace mozilla {
19 namespace storage {
21 ////////////////////////////////////////////////////////////////////////////////
22 //// StatementRow
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)
30 NS_INTERFACE_MAP_END
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,
44 bool& aFound,
45 JS::MutableHandle<JS::Value> aResult,
46 mozilla::ErrorResult& aRv) {
47 if (!mStatement) {
48 aRv.Throw(NS_ERROR_NOT_INITIALIZED);
49 return;
52 nsCString name = NS_ConvertUTF16toUTF8(aName);
54 uint32_t idx;
56 nsresult rv = mStatement->GetColumnIndex(name, &idx);
57 if (NS_FAILED(rv)) {
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
60 // either.
61 aFound = false;
62 return;
66 int32_t type;
67 aRv = mStatement->GetTypeOfIndex(idx, &type);
68 if (aRv.Failed()) {
69 return;
72 switch (type) {
73 case mozIStorageValueArray::VALUE_TYPE_INTEGER:
74 case mozIStorageValueArray::VALUE_TYPE_FLOAT: {
75 double dval;
76 aRv = mStatement->GetDouble(idx, &dval);
77 if (aRv.Failed()) {
78 return;
80 aResult.set(::JS_NumberValue(dval));
81 break;
83 case mozIStorageValueArray::VALUE_TYPE_TEXT: {
84 uint32_t bytes;
85 const char16_t* sval = reinterpret_cast<const char16_t*>(
86 static_cast<mozIStorageStatement*>(mStatement)
87 ->AsSharedWString(idx, &bytes));
88 JSString* str =
89 ::JS_NewUCStringCopyN(aCx, sval, bytes / sizeof(char16_t));
90 if (!str) {
91 aRv.Throw(NS_ERROR_UNEXPECTED);
92 return;
94 aResult.setString(str);
95 break;
97 case mozIStorageValueArray::VALUE_TYPE_BLOB: {
98 uint32_t length;
99 const uint8_t* blob = static_cast<mozIStorageStatement*>(mStatement)
100 ->AsSharedBlob(idx, &length);
101 JS::Rooted<JSObject*> obj(aCx, ::JS_NewArrayObject(aCx, length));
102 if (!obj) {
103 aRv.Throw(NS_ERROR_UNEXPECTED);
104 return;
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);
112 return;
115 break;
117 case mozIStorageValueArray::VALUE_TYPE_NULL:
118 aResult.setNull();
119 break;
120 default:
121 NS_ERROR("unknown column type returned, what's going on?");
122 break;
124 aFound = true;
127 void StatementRow::GetSupportedNames(nsTArray<nsString>& aNames) {
128 if (!mStatement) {
129 return;
132 uint32_t columnCount;
133 nsresult rv = mStatement->GetColumnCount(&columnCount);
134 if (NS_WARN_IF(NS_FAILED(rv))) {
135 return;
138 for (uint32_t i = 0; i < columnCount; i++) {
139 nsAutoCString name;
140 nsresult rv = mStatement->GetColumnName(i, name);
141 if (NS_WARN_IF(NS_FAILED(rv))) {
142 return;
144 aNames.AppendElement(NS_ConvertUTF8toUTF16(name));
148 } // namespace storage
149 } // namespace mozilla