Bug 1527719 [wpt PR 15359] - KV storage: make backingStore return the same frozen...
[gecko.git] / storage / mozStorageRow.cpp
blobde77f22d503642e1fa18cf47bff6641d63092716
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 "nsString.h"
9 #include "sqlite3.h"
10 #include "mozStoragePrivateHelpers.h"
11 #include "Variant.h"
12 #include "mozStorageRow.h"
14 namespace mozilla {
15 namespace storage {
17 ////////////////////////////////////////////////////////////////////////////////
18 //// Row
20 nsresult Row::initialize(sqlite3_stmt *aStatement) {
21 // Get the number of results
22 mNumCols = ::sqlite3_column_count(aStatement);
24 // Start copying over values
25 for (uint32_t i = 0; i < mNumCols; i++) {
26 // Store the value
27 nsIVariant *variant = nullptr;
28 int type = ::sqlite3_column_type(aStatement, i);
29 switch (type) {
30 case SQLITE_INTEGER:
31 variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
32 break;
33 case SQLITE_FLOAT:
34 variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
35 break;
36 case SQLITE_TEXT: {
37 nsDependentString str(static_cast<const char16_t *>(
38 ::sqlite3_column_text16(aStatement, i)));
39 variant = new TextVariant(str);
40 break;
42 case SQLITE_NULL:
43 variant = new NullVariant();
44 break;
45 case SQLITE_BLOB: {
46 int size = ::sqlite3_column_bytes(aStatement, i);
47 const void *data = ::sqlite3_column_blob(aStatement, i);
48 variant = new BlobVariant(std::pair<const void *, int>(data, size));
49 break;
51 default:
52 return NS_ERROR_UNEXPECTED;
54 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
56 // Insert into our storage array
57 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
59 // Associate the name (if any) with the index
60 const char *name = ::sqlite3_column_name(aStatement, i);
61 if (!name) break;
62 nsAutoCString colName(name);
63 mNameHashtable.Put(colName, i);
66 return NS_OK;
69 /**
70 * Note: This object is only ever accessed on one thread at a time. It it not
71 * threadsafe, but it does need threadsafe AddRef and Release.
73 NS_IMPL_ISUPPORTS(Row, mozIStorageRow, mozIStorageValueArray)
75 ////////////////////////////////////////////////////////////////////////////////
76 //// mozIStorageRow
78 NS_IMETHODIMP
79 Row::GetResultByIndex(uint32_t aIndex, nsIVariant **_result) {
80 ENSURE_INDEX_VALUE(aIndex, mNumCols);
81 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
82 return NS_OK;
85 NS_IMETHODIMP
86 Row::GetResultByName(const nsACString &aName, nsIVariant **_result) {
87 uint32_t index;
88 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
89 return GetResultByIndex(index, _result);
92 ////////////////////////////////////////////////////////////////////////////////
93 //// mozIStorageValueArray
95 NS_IMETHODIMP
96 Row::GetNumEntries(uint32_t *_entries) {
97 *_entries = mNumCols;
98 return NS_OK;
101 NS_IMETHODIMP
102 Row::GetTypeOfIndex(uint32_t aIndex, int32_t *_type) {
103 ENSURE_INDEX_VALUE(aIndex, mNumCols);
105 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
106 switch (type) {
107 case nsIDataType::VTYPE_INT32:
108 case nsIDataType::VTYPE_INT64:
109 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
110 break;
111 case nsIDataType::VTYPE_DOUBLE:
112 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
113 break;
114 case nsIDataType::VTYPE_ASTRING:
115 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
116 break;
117 case nsIDataType::VTYPE_ARRAY:
118 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
119 break;
120 default:
121 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
122 break;
124 return NS_OK;
127 NS_IMETHODIMP
128 Row::GetInt32(uint32_t aIndex, int32_t *_value) {
129 ENSURE_INDEX_VALUE(aIndex, mNumCols);
130 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
133 NS_IMETHODIMP
134 Row::GetInt64(uint32_t aIndex, int64_t *_value) {
135 ENSURE_INDEX_VALUE(aIndex, mNumCols);
136 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
139 NS_IMETHODIMP
140 Row::GetDouble(uint32_t aIndex, double *_value) {
141 ENSURE_INDEX_VALUE(aIndex, mNumCols);
142 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
145 NS_IMETHODIMP
146 Row::GetUTF8String(uint32_t aIndex, nsACString &_value) {
147 ENSURE_INDEX_VALUE(aIndex, mNumCols);
148 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
151 NS_IMETHODIMP
152 Row::GetString(uint32_t aIndex, nsAString &_value) {
153 ENSURE_INDEX_VALUE(aIndex, mNumCols);
154 return mData.ObjectAt(aIndex)->GetAsAString(_value);
157 NS_IMETHODIMP
158 Row::GetBlob(uint32_t aIndex, uint32_t *_size, uint8_t **_blob) {
159 ENSURE_INDEX_VALUE(aIndex, mNumCols);
161 uint16_t type;
162 nsIID interfaceIID;
163 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
164 reinterpret_cast<void **>(_blob));
167 NS_IMETHODIMP
168 Row::GetBlobAsString(uint32_t aIndex, nsAString &aValue) {
169 return DoGetBlobAsString(this, aIndex, aValue);
172 NS_IMETHODIMP
173 Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString &aValue) {
174 return DoGetBlobAsString(this, aIndex, aValue);
177 NS_IMETHODIMP
178 Row::GetIsNull(uint32_t aIndex, bool *_isNull) {
179 ENSURE_INDEX_VALUE(aIndex, mNumCols);
180 NS_ENSURE_ARG_POINTER(_isNull);
182 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
183 *_isNull = type == nsIDataType::VTYPE_EMPTY;
184 return NS_OK;
187 NS_IMETHODIMP
188 Row::GetSharedUTF8String(uint32_t, uint32_t *, char const **) {
189 return NS_ERROR_NOT_IMPLEMENTED;
192 NS_IMETHODIMP
193 Row::GetSharedString(uint32_t, uint32_t *, const char16_t **) {
194 return NS_ERROR_NOT_IMPLEMENTED;
197 NS_IMETHODIMP
198 Row::GetSharedBlob(uint32_t, uint32_t *, const uint8_t **) {
199 return NS_ERROR_NOT_IMPLEMENTED;
202 } // namespace storage
203 } // namespace mozilla