Bug 1610307 [wpt PR 21266] - Update wpt metadata, a=testonly
[gecko.git] / storage / mozStorageArgValueArray.cpp
blob5526786cf95ac5ebbd88ac1bdd8ba0ff7d68a2fb
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 "nsError.h"
8 #include "nsMemory.h"
9 #include "nsString.h"
11 #include "mozStoragePrivateHelpers.h"
12 #include "mozStorageArgValueArray.h"
14 namespace mozilla {
15 namespace storage {
17 ////////////////////////////////////////////////////////////////////////////////
18 //// ArgValueArray
20 ArgValueArray::ArgValueArray(int32_t aArgc, sqlite3_value** aArgv)
21 : mArgc(aArgc), mArgv(aArgv) {}
23 NS_IMPL_ISUPPORTS(ArgValueArray, mozIStorageValueArray)
25 ////////////////////////////////////////////////////////////////////////////////
26 //// mozIStorageValueArray
28 NS_IMETHODIMP
29 ArgValueArray::GetNumEntries(uint32_t* _size) {
30 *_size = mArgc;
31 return NS_OK;
34 NS_IMETHODIMP
35 ArgValueArray::GetTypeOfIndex(uint32_t aIndex, int32_t* _type) {
36 ENSURE_INDEX_VALUE(aIndex, mArgc);
38 int t = ::sqlite3_value_type(mArgv[aIndex]);
39 switch (t) {
40 case SQLITE_INTEGER:
41 *_type = VALUE_TYPE_INTEGER;
42 break;
43 case SQLITE_FLOAT:
44 *_type = VALUE_TYPE_FLOAT;
45 break;
46 case SQLITE_TEXT:
47 *_type = VALUE_TYPE_TEXT;
48 break;
49 case SQLITE_BLOB:
50 *_type = VALUE_TYPE_BLOB;
51 break;
52 case SQLITE_NULL:
53 *_type = VALUE_TYPE_NULL;
54 break;
55 default:
56 return NS_ERROR_FAILURE;
59 return NS_OK;
62 NS_IMETHODIMP
63 ArgValueArray::GetInt32(uint32_t aIndex, int32_t* _value) {
64 ENSURE_INDEX_VALUE(aIndex, mArgc);
66 *_value = ::sqlite3_value_int(mArgv[aIndex]);
67 return NS_OK;
70 NS_IMETHODIMP
71 ArgValueArray::GetInt64(uint32_t aIndex, int64_t* _value) {
72 ENSURE_INDEX_VALUE(aIndex, mArgc);
74 *_value = ::sqlite3_value_int64(mArgv[aIndex]);
75 return NS_OK;
78 NS_IMETHODIMP
79 ArgValueArray::GetDouble(uint32_t aIndex, double* _value) {
80 ENSURE_INDEX_VALUE(aIndex, mArgc);
82 *_value = ::sqlite3_value_double(mArgv[aIndex]);
83 return NS_OK;
86 NS_IMETHODIMP
87 ArgValueArray::GetUTF8String(uint32_t aIndex, nsACString& _value) {
88 ENSURE_INDEX_VALUE(aIndex, mArgc);
90 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
91 // NULL columns should have IsVoid set to distinguish them from an empty
92 // string.
93 _value.SetIsVoid(true);
94 } else {
95 _value.Assign(
96 reinterpret_cast<const char*>(::sqlite3_value_text(mArgv[aIndex])),
97 ::sqlite3_value_bytes(mArgv[aIndex]));
99 return NS_OK;
102 NS_IMETHODIMP
103 ArgValueArray::GetString(uint32_t aIndex, nsAString& _value) {
104 ENSURE_INDEX_VALUE(aIndex, mArgc);
106 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
107 // NULL columns should have IsVoid set to distinguish them from an empty
108 // string.
109 _value.SetIsVoid(true);
110 } else {
111 const char16_t* string =
112 static_cast<const char16_t*>(::sqlite3_value_text16(mArgv[aIndex]));
113 _value.Assign(string,
114 ::sqlite3_value_bytes16(mArgv[aIndex]) / sizeof(char16_t));
116 return NS_OK;
119 NS_IMETHODIMP
120 ArgValueArray::GetBlob(uint32_t aIndex, uint32_t* _size, uint8_t** _blob) {
121 ENSURE_INDEX_VALUE(aIndex, mArgc);
123 int size = ::sqlite3_value_bytes(mArgv[aIndex]);
124 void* blob = moz_xmemdup(::sqlite3_value_blob(mArgv[aIndex]), size);
125 *_blob = static_cast<uint8_t*>(blob);
126 *_size = size;
127 return NS_OK;
130 NS_IMETHODIMP
131 ArgValueArray::GetBlobAsString(uint32_t aIndex, nsAString& aValue) {
132 return DoGetBlobAsString(this, aIndex, aValue);
135 NS_IMETHODIMP
136 ArgValueArray::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) {
137 return DoGetBlobAsString(this, aIndex, aValue);
140 NS_IMETHODIMP
141 ArgValueArray::GetIsNull(uint32_t aIndex, bool* _isNull) {
142 // GetTypeOfIndex will check aIndex for us, so we don't have to.
143 int32_t type;
144 nsresult rv = GetTypeOfIndex(aIndex, &type);
145 NS_ENSURE_SUCCESS(rv, rv);
147 *_isNull = (type == VALUE_TYPE_NULL);
148 return NS_OK;
151 NS_IMETHODIMP
152 ArgValueArray::GetSharedUTF8String(uint32_t aIndex, uint32_t* _byteLength,
153 const char** _string) {
154 *_string = reinterpret_cast<const char*>(::sqlite3_value_text(mArgv[aIndex]));
155 if (_byteLength) {
156 *_byteLength = ::sqlite3_value_bytes(mArgv[aIndex]);
158 return NS_OK;
161 NS_IMETHODIMP
162 ArgValueArray::GetSharedString(uint32_t aIndex, uint32_t* _byteLength,
163 const char16_t** _string) {
164 *_string =
165 static_cast<const char16_t*>(::sqlite3_value_text16(mArgv[aIndex]));
166 if (_byteLength) {
167 *_byteLength = ::sqlite3_value_bytes16(mArgv[aIndex]);
169 return NS_OK;
172 NS_IMETHODIMP
173 ArgValueArray::GetSharedBlob(uint32_t aIndex, uint32_t* _byteLength,
174 const uint8_t** _blob) {
175 *_blob = static_cast<const uint8_t*>(::sqlite3_value_blob(mArgv[aIndex]));
176 if (_byteLength) {
177 *_byteLength = ::sqlite3_value_bytes(mArgv[aIndex]);
179 return NS_OK;
182 } // namespace storage
183 } // namespace mozilla