Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/storage/mozStorageStatementRow.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "nsMemory.h"
8
#include "nsString.h"
9
10
#include "mozilla/dom/MozStorageStatementRowBinding.h"
11
#include "mozStorageStatementRow.h"
12
#include "mozStorageStatement.h"
13
14
#include "jsapi.h"
15
16
#include "xpc_make_class.h"
17
18
namespace mozilla {
19
namespace storage {
20
21
////////////////////////////////////////////////////////////////////////////////
22
//// StatementRow
23
24
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow, mWindow)
25
26
0
NS_INTERFACE_TABLE_HEAD(StatementRow)
27
0
  NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
28
0
  NS_INTERFACE_TABLE(StatementRow, nsISupports)
29
0
  NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow)
30
0
NS_INTERFACE_MAP_END
31
32
NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow)
33
NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow)
34
35
StatementRow::StatementRow(nsPIDOMWindowInner* aWindow, Statement *aStatement)
36
: mWindow(aWindow),
37
  mStatement(aStatement)
38
0
{
39
0
}
40
41
JSObject*
42
StatementRow::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
43
0
{
44
0
  return dom::MozStorageStatementRow_Binding::Wrap(aCx, this, aGivenProto);
45
0
}
46
47
void
48
StatementRow::NamedGetter(JSContext* aCx,
49
                          const nsAString& aName,
50
                          bool& aFound,
51
                          JS::MutableHandle<JS::Value> aResult,
52
                          mozilla::ErrorResult& aRv)
53
0
{
54
0
  if (!mStatement) {
55
0
    aRv.Throw(NS_ERROR_NOT_INITIALIZED);
56
0
    return;
57
0
  }
58
0
59
0
  nsCString name = NS_ConvertUTF16toUTF8(aName);
60
0
61
0
  uint32_t idx;
62
0
  {
63
0
    nsresult rv = mStatement->GetColumnIndex(name, &idx);
64
0
    if (NS_FAILED(rv)) {
65
0
      // It's highly likely that the name doesn't exist, so let the JS engine
66
0
      // check the prototype chain and throw if that doesn't have the property
67
0
      // either.
68
0
      aFound = false;
69
0
      return;
70
0
    }
71
0
  }
72
0
73
0
  int32_t type;
74
0
  aRv = mStatement->GetTypeOfIndex(idx, &type);
75
0
  if (aRv.Failed()) {
76
0
    return;
77
0
  }
78
0
79
0
  switch (type) {
80
0
  case mozIStorageValueArray::VALUE_TYPE_INTEGER:
81
0
  case mozIStorageValueArray::VALUE_TYPE_FLOAT: {
82
0
    double dval;
83
0
    aRv = mStatement->GetDouble(idx, &dval);
84
0
    if (aRv.Failed()) {
85
0
      return;
86
0
    }
87
0
    aResult.set(::JS_NumberValue(dval));
88
0
    break;
89
0
  }
90
0
  case mozIStorageValueArray::VALUE_TYPE_TEXT: {
91
0
    uint32_t bytes;
92
0
    const char16_t *sval = reinterpret_cast<const char16_t *>(
93
0
        static_cast<mozIStorageStatement *>(mStatement)->
94
0
          AsSharedWString(idx, &bytes)
95
0
      );
96
0
    JSString *str = ::JS_NewUCStringCopyN(aCx, sval, bytes / 2);
97
0
    if (!str) {
98
0
      aRv.Throw(NS_ERROR_UNEXPECTED);
99
0
      return;
100
0
    }
101
0
    aResult.setString(str);
102
0
    break;
103
0
  }
104
0
  case mozIStorageValueArray::VALUE_TYPE_BLOB: {
105
0
    uint32_t length;
106
0
    const uint8_t *blob = static_cast<mozIStorageStatement *>(mStatement)->
107
0
      AsSharedBlob(idx, &length);
108
0
    JS::Rooted<JSObject*> obj(aCx, ::JS_NewArrayObject(aCx, length));
109
0
    if (!obj) {
110
0
      aRv.Throw(NS_ERROR_UNEXPECTED);
111
0
      return;
112
0
    }
113
0
    aResult.setObject(*obj);
114
0
115
0
    // Copy the blob over to the JS array.
116
0
    for (uint32_t i = 0; i < length; i++) {
117
0
      if (!::JS_DefineElement(aCx, obj, i, blob[i], JSPROP_ENUMERATE)) {
118
0
        aRv.Throw(NS_ERROR_UNEXPECTED);
119
0
        return;
120
0
      }
121
0
    }
122
0
    break;
123
0
  }
124
0
  case mozIStorageValueArray::VALUE_TYPE_NULL:
125
0
    aResult.setNull();
126
0
    break;
127
0
  default:
128
0
    NS_ERROR("unknown column type returned, what's going on?");
129
0
    break;
130
0
  }
131
0
  aFound = true;
132
0
}
133
134
void
135
StatementRow::GetSupportedNames(nsTArray<nsString>& aNames)
136
0
{
137
0
  if (!mStatement) {
138
0
    return;
139
0
  }
140
0
141
0
  uint32_t columnCount;
142
0
  nsresult rv = mStatement->GetColumnCount(&columnCount);
143
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
144
0
    return;
145
0
  }
146
0
147
0
  for (uint32_t i = 0; i < columnCount; i++) {
148
0
    nsAutoCString name;
149
0
    nsresult rv = mStatement->GetColumnName(i, name);
150
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
151
0
      return;
152
0
    }
153
0
    aNames.AppendElement(NS_ConvertUTF8toUTF16(name));
154
0
  }
155
0
}
156
157
} // namespace storage
158
} // namespace mozilla