Bug 1853320 [wpt PR 41940] - [FLEDGE] Split up runs of /trusted-scoring-signals.https...
[gecko.git] / storage / mozStorageRow.cpp
blobbee49dfd85c58b3224d83d069b5225628470f484
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 "mozStorageRow.h"
9 #include "nsString.h"
11 #include "sqlite3.h"
12 #include "mozStoragePrivateHelpers.h"
13 #include "Variant.h"
15 namespace mozilla {
16 namespace storage {
18 ////////////////////////////////////////////////////////////////////////////////
19 //// Row
21 nsresult Row::initialize(sqlite3_stmt* aStatement) {
22 // Get the number of results
23 mNumCols = ::sqlite3_column_count(aStatement);
25 // Start copying over values
26 for (uint32_t i = 0; i < mNumCols; i++) {
27 // Store the value
28 nsIVariant* variant = nullptr;
29 int type = ::sqlite3_column_type(aStatement, i);
30 switch (type) {
31 case SQLITE_INTEGER:
32 variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
33 break;
34 case SQLITE_FLOAT:
35 variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
36 break;
37 case SQLITE_TEXT: {
38 const char16_t* value = static_cast<const char16_t*>(
39 ::sqlite3_column_text16(aStatement, i));
40 nsDependentString str(
41 value, ::sqlite3_column_bytes16(aStatement, i) / sizeof(char16_t));
42 variant = new TextVariant(str);
43 break;
45 case SQLITE_NULL:
46 variant = new NullVariant();
47 break;
48 case SQLITE_BLOB: {
49 const void* data = ::sqlite3_column_blob(aStatement, i);
50 int size = ::sqlite3_column_bytes(aStatement, i);
51 variant = new BlobVariant(std::pair<const void*, int>(data, size));
52 break;
54 default:
55 return NS_ERROR_UNEXPECTED;
57 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
59 // Insert into our storage array
60 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
62 // Associate the name (if any) with the index
63 const char* name = ::sqlite3_column_name(aStatement, i);
64 if (!name) break;
65 nsAutoCString colName(name);
66 mNameHashtable.InsertOrUpdate(colName, i);
69 return NS_OK;
72 /**
73 * Note: This object is only ever accessed on one thread at a time. It it not
74 * threadsafe, but it does need threadsafe AddRef and Release.
76 NS_IMPL_ISUPPORTS(Row, mozIStorageRow, mozIStorageValueArray)
78 ////////////////////////////////////////////////////////////////////////////////
79 //// mozIStorageRow
81 NS_IMETHODIMP
82 Row::GetResultByIndex(uint32_t aIndex, nsIVariant** _result) {
83 ENSURE_INDEX_VALUE(aIndex, mNumCols);
84 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
85 return NS_OK;
88 NS_IMETHODIMP
89 Row::GetResultByName(const nsACString& aName, nsIVariant** _result) {
90 uint32_t index;
91 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
92 return GetResultByIndex(index, _result);
95 ////////////////////////////////////////////////////////////////////////////////
96 //// mozIStorageValueArray
98 NS_IMETHODIMP
99 Row::GetNumEntries(uint32_t* _entries) {
100 *_entries = mNumCols;
101 return NS_OK;
104 NS_IMETHODIMP
105 Row::GetTypeOfIndex(uint32_t aIndex, int32_t* _type) {
106 ENSURE_INDEX_VALUE(aIndex, mNumCols);
108 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
109 switch (type) {
110 case nsIDataType::VTYPE_INT32:
111 case nsIDataType::VTYPE_INT64:
112 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
113 break;
114 case nsIDataType::VTYPE_DOUBLE:
115 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
116 break;
117 case nsIDataType::VTYPE_ASTRING:
118 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
119 break;
120 case nsIDataType::VTYPE_ARRAY:
121 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
122 break;
123 default:
124 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
125 break;
127 return NS_OK;
130 NS_IMETHODIMP
131 Row::GetInt32(uint32_t aIndex, int32_t* _value) {
132 ENSURE_INDEX_VALUE(aIndex, mNumCols);
133 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
136 NS_IMETHODIMP
137 Row::GetInt64(uint32_t aIndex, int64_t* _value) {
138 ENSURE_INDEX_VALUE(aIndex, mNumCols);
139 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
142 NS_IMETHODIMP
143 Row::GetDouble(uint32_t aIndex, double* _value) {
144 ENSURE_INDEX_VALUE(aIndex, mNumCols);
145 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
148 NS_IMETHODIMP
149 Row::GetUTF8String(uint32_t aIndex, nsACString& _value) {
150 ENSURE_INDEX_VALUE(aIndex, mNumCols);
151 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
154 NS_IMETHODIMP
155 Row::GetString(uint32_t aIndex, nsAString& _value) {
156 ENSURE_INDEX_VALUE(aIndex, mNumCols);
157 return mData.ObjectAt(aIndex)->GetAsAString(_value);
160 NS_IMETHODIMP
161 Row::GetBlob(uint32_t aIndex, uint32_t* _size, uint8_t** _blob) {
162 ENSURE_INDEX_VALUE(aIndex, mNumCols);
164 uint16_t type;
165 nsIID interfaceIID;
166 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
167 reinterpret_cast<void**>(_blob));
170 NS_IMETHODIMP
171 Row::GetBlobAsString(uint32_t aIndex, nsAString& aValue) {
172 return DoGetBlobAsString(this, aIndex, aValue);
175 NS_IMETHODIMP
176 Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) {
177 return DoGetBlobAsString(this, aIndex, aValue);
180 NS_IMETHODIMP
181 Row::GetIsNull(uint32_t aIndex, bool* _isNull) {
182 ENSURE_INDEX_VALUE(aIndex, mNumCols);
183 NS_ENSURE_ARG_POINTER(_isNull);
185 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
186 *_isNull = type == nsIDataType::VTYPE_EMPTY;
187 return NS_OK;
190 NS_IMETHODIMP
191 Row::GetSharedUTF8String(uint32_t, uint32_t*, char const**) {
192 return NS_ERROR_NOT_IMPLEMENTED;
195 NS_IMETHODIMP
196 Row::GetSharedString(uint32_t, uint32_t*, const char16_t**) {
197 return NS_ERROR_NOT_IMPLEMENTED;
200 NS_IMETHODIMP
201 Row::GetSharedBlob(uint32_t, uint32_t*, const uint8_t**) {
202 return NS_ERROR_NOT_IMPLEMENTED;
205 } // namespace storage
206 } // namespace mozilla