Bug 1588538 - Add missing file.
[gecko.git] / storage / mozStorageRow.cpp
blobd470feaa33866eb528b4e96a1c4de98a2188c04d
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 const char16_t* value = static_cast<const char16_t*>(
38 ::sqlite3_column_text16(aStatement, i));
39 nsDependentString str(
40 value, ::sqlite3_column_bytes16(aStatement, i) / sizeof(char16_t));
41 variant = new TextVariant(str);
42 break;
44 case SQLITE_NULL:
45 variant = new NullVariant();
46 break;
47 case SQLITE_BLOB: {
48 const void* data = ::sqlite3_column_blob(aStatement, i);
49 int size = ::sqlite3_column_bytes(aStatement, i);
50 variant = new BlobVariant(std::pair<const void*, int>(data, size));
51 break;
53 default:
54 return NS_ERROR_UNEXPECTED;
56 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
58 // Insert into our storage array
59 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
61 // Associate the name (if any) with the index
62 const char* name = ::sqlite3_column_name(aStatement, i);
63 if (!name) break;
64 nsAutoCString colName(name);
65 mNameHashtable.Put(colName, i);
68 return NS_OK;
71 /**
72 * Note: This object is only ever accessed on one thread at a time. It it not
73 * threadsafe, but it does need threadsafe AddRef and Release.
75 NS_IMPL_ISUPPORTS(Row, mozIStorageRow, mozIStorageValueArray)
77 ////////////////////////////////////////////////////////////////////////////////
78 //// mozIStorageRow
80 NS_IMETHODIMP
81 Row::GetResultByIndex(uint32_t aIndex, nsIVariant** _result) {
82 ENSURE_INDEX_VALUE(aIndex, mNumCols);
83 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
84 return NS_OK;
87 NS_IMETHODIMP
88 Row::GetResultByName(const nsACString& aName, nsIVariant** _result) {
89 uint32_t index;
90 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
91 return GetResultByIndex(index, _result);
94 ////////////////////////////////////////////////////////////////////////////////
95 //// mozIStorageValueArray
97 NS_IMETHODIMP
98 Row::GetNumEntries(uint32_t* _entries) {
99 *_entries = mNumCols;
100 return NS_OK;
103 NS_IMETHODIMP
104 Row::GetTypeOfIndex(uint32_t aIndex, int32_t* _type) {
105 ENSURE_INDEX_VALUE(aIndex, mNumCols);
107 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
108 switch (type) {
109 case nsIDataType::VTYPE_INT32:
110 case nsIDataType::VTYPE_INT64:
111 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
112 break;
113 case nsIDataType::VTYPE_DOUBLE:
114 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
115 break;
116 case nsIDataType::VTYPE_ASTRING:
117 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
118 break;
119 case nsIDataType::VTYPE_ARRAY:
120 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
121 break;
122 default:
123 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
124 break;
126 return NS_OK;
129 NS_IMETHODIMP
130 Row::GetInt32(uint32_t aIndex, int32_t* _value) {
131 ENSURE_INDEX_VALUE(aIndex, mNumCols);
132 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
135 NS_IMETHODIMP
136 Row::GetInt64(uint32_t aIndex, int64_t* _value) {
137 ENSURE_INDEX_VALUE(aIndex, mNumCols);
138 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
141 NS_IMETHODIMP
142 Row::GetDouble(uint32_t aIndex, double* _value) {
143 ENSURE_INDEX_VALUE(aIndex, mNumCols);
144 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
147 NS_IMETHODIMP
148 Row::GetUTF8String(uint32_t aIndex, nsACString& _value) {
149 ENSURE_INDEX_VALUE(aIndex, mNumCols);
150 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
153 NS_IMETHODIMP
154 Row::GetString(uint32_t aIndex, nsAString& _value) {
155 ENSURE_INDEX_VALUE(aIndex, mNumCols);
156 return mData.ObjectAt(aIndex)->GetAsAString(_value);
159 NS_IMETHODIMP
160 Row::GetBlob(uint32_t aIndex, uint32_t* _size, uint8_t** _blob) {
161 ENSURE_INDEX_VALUE(aIndex, mNumCols);
163 uint16_t type;
164 nsIID interfaceIID;
165 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
166 reinterpret_cast<void**>(_blob));
169 NS_IMETHODIMP
170 Row::GetBlobAsString(uint32_t aIndex, nsAString& aValue) {
171 return DoGetBlobAsString(this, aIndex, aValue);
174 NS_IMETHODIMP
175 Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) {
176 return DoGetBlobAsString(this, aIndex, aValue);
179 NS_IMETHODIMP
180 Row::GetIsNull(uint32_t aIndex, bool* _isNull) {
181 ENSURE_INDEX_VALUE(aIndex, mNumCols);
182 NS_ENSURE_ARG_POINTER(_isNull);
184 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
185 *_isNull = type == nsIDataType::VTYPE_EMPTY;
186 return NS_OK;
189 NS_IMETHODIMP
190 Row::GetSharedUTF8String(uint32_t, uint32_t*, char const**) {
191 return NS_ERROR_NOT_IMPLEMENTED;
194 NS_IMETHODIMP
195 Row::GetSharedString(uint32_t, uint32_t*, const char16_t**) {
196 return NS_ERROR_NOT_IMPLEMENTED;
199 NS_IMETHODIMP
200 Row::GetSharedBlob(uint32_t, uint32_t*, const uint8_t**) {
201 return NS_ERROR_NOT_IMPLEMENTED;
204 } // namespace storage
205 } // namespace mozilla