Bug 1885565 - Part 4: Implement skeleton bottom sheet menu r=android-reviewers,007
[gecko.git] / storage / mozStorageArgValueArray.cpp
blob56e97aaa8b14b1cd6b5e49778a66ebf08107608b
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 "nsString.h"
10 #include "mozStoragePrivateHelpers.h"
11 #include "mozStorageArgValueArray.h"
13 namespace mozilla {
14 namespace storage {
16 ////////////////////////////////////////////////////////////////////////////////
17 //// ArgValueArray
19 ArgValueArray::ArgValueArray(int32_t aArgc, sqlite3_value** aArgv)
20 : mArgc(aArgc), mArgv(aArgv) {}
22 NS_IMPL_ISUPPORTS(ArgValueArray, mozIStorageValueArray)
24 ////////////////////////////////////////////////////////////////////////////////
25 //// mozIStorageValueArray
27 NS_IMETHODIMP
28 ArgValueArray::GetNumEntries(uint32_t* _size) {
29 *_size = mArgc;
30 return NS_OK;
33 NS_IMETHODIMP
34 ArgValueArray::GetTypeOfIndex(uint32_t aIndex, int32_t* _type) {
35 ENSURE_INDEX_VALUE(aIndex, mArgc);
37 int t = ::sqlite3_value_type(mArgv[aIndex]);
38 switch (t) {
39 case SQLITE_INTEGER:
40 *_type = VALUE_TYPE_INTEGER;
41 break;
42 case SQLITE_FLOAT:
43 *_type = VALUE_TYPE_FLOAT;
44 break;
45 case SQLITE_TEXT:
46 *_type = VALUE_TYPE_TEXT;
47 break;
48 case SQLITE_BLOB:
49 *_type = VALUE_TYPE_BLOB;
50 break;
51 case SQLITE_NULL:
52 *_type = VALUE_TYPE_NULL;
53 break;
54 default:
55 return NS_ERROR_FAILURE;
58 return NS_OK;
61 NS_IMETHODIMP
62 ArgValueArray::GetInt32(uint32_t aIndex, int32_t* _value) {
63 ENSURE_INDEX_VALUE(aIndex, mArgc);
65 *_value = ::sqlite3_value_int(mArgv[aIndex]);
66 return NS_OK;
69 NS_IMETHODIMP
70 ArgValueArray::GetInt64(uint32_t aIndex, int64_t* _value) {
71 ENSURE_INDEX_VALUE(aIndex, mArgc);
73 *_value = ::sqlite3_value_int64(mArgv[aIndex]);
74 return NS_OK;
77 NS_IMETHODIMP
78 ArgValueArray::GetDouble(uint32_t aIndex, double* _value) {
79 ENSURE_INDEX_VALUE(aIndex, mArgc);
81 *_value = ::sqlite3_value_double(mArgv[aIndex]);
82 return NS_OK;
85 NS_IMETHODIMP
86 ArgValueArray::GetUTF8String(uint32_t aIndex, nsACString& _value) {
87 ENSURE_INDEX_VALUE(aIndex, mArgc);
89 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
90 // NULL columns should have IsVoid set to distinguish them from an empty
91 // string.
92 _value.SetIsVoid(true);
93 } else {
94 _value.Assign(
95 reinterpret_cast<const char*>(::sqlite3_value_text(mArgv[aIndex])),
96 ::sqlite3_value_bytes(mArgv[aIndex]));
98 return NS_OK;
101 NS_IMETHODIMP
102 ArgValueArray::GetString(uint32_t aIndex, nsAString& _value) {
103 ENSURE_INDEX_VALUE(aIndex, mArgc);
105 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
106 // NULL columns should have IsVoid set to distinguish them from an empty
107 // string.
108 _value.SetIsVoid(true);
109 } else {
110 const char16_t* string =
111 static_cast<const char16_t*>(::sqlite3_value_text16(mArgv[aIndex]));
112 _value.Assign(string,
113 ::sqlite3_value_bytes16(mArgv[aIndex]) / sizeof(char16_t));
115 return NS_OK;
118 NS_IMETHODIMP
119 ArgValueArray::GetBlob(uint32_t aIndex, uint32_t* _size, uint8_t** _blob) {
120 ENSURE_INDEX_VALUE(aIndex, mArgc);
122 int size = ::sqlite3_value_bytes(mArgv[aIndex]);
123 void* blob = moz_xmemdup(::sqlite3_value_blob(mArgv[aIndex]), size);
124 *_blob = static_cast<uint8_t*>(blob);
125 *_size = size;
126 return NS_OK;
129 NS_IMETHODIMP
130 ArgValueArray::GetBlobAsString(uint32_t aIndex, nsAString& aValue) {
131 return DoGetBlobAsString(this, aIndex, aValue);
134 NS_IMETHODIMP
135 ArgValueArray::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) {
136 return DoGetBlobAsString(this, aIndex, aValue);
139 NS_IMETHODIMP
140 ArgValueArray::GetIsNull(uint32_t aIndex, bool* _isNull) {
141 // GetTypeOfIndex will check aIndex for us, so we don't have to.
142 int32_t type;
143 nsresult rv = GetTypeOfIndex(aIndex, &type);
144 NS_ENSURE_SUCCESS(rv, rv);
146 *_isNull = (type == VALUE_TYPE_NULL);
147 return NS_OK;
150 NS_IMETHODIMP
151 ArgValueArray::GetSharedUTF8String(uint32_t aIndex, uint32_t* _byteLength,
152 const char** _string) {
153 *_string = reinterpret_cast<const char*>(::sqlite3_value_text(mArgv[aIndex]));
154 if (_byteLength) {
155 *_byteLength = ::sqlite3_value_bytes(mArgv[aIndex]);
157 return NS_OK;
160 NS_IMETHODIMP
161 ArgValueArray::GetSharedString(uint32_t aIndex, uint32_t* _byteLength,
162 const char16_t** _string) {
163 *_string =
164 static_cast<const char16_t*>(::sqlite3_value_text16(mArgv[aIndex]));
165 if (_byteLength) {
166 *_byteLength = ::sqlite3_value_bytes16(mArgv[aIndex]);
168 return NS_OK;
171 NS_IMETHODIMP
172 ArgValueArray::GetSharedBlob(uint32_t aIndex, uint32_t* _byteLength,
173 const uint8_t** _blob) {
174 *_blob = static_cast<const uint8_t*>(::sqlite3_value_blob(mArgv[aIndex]));
175 if (_byteLength) {
176 *_byteLength = ::sqlite3_value_bytes(mArgv[aIndex]);
178 return NS_OK;
181 } // namespace storage
182 } // namespace mozilla