Bug 1507748 - Add a pointer cursor to the flexbox inspector buttons. r=mtigley
[gecko.git] / storage / mozStorageRow.cpp
blob399344c57a84f5cc141762b5fbc73147ad025766
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
21 Row::initialize(sqlite3_stmt *aStatement)
23 // Get the number of results
24 mNumCols = ::sqlite3_column_count(aStatement);
26 // Start copying over values
27 for (uint32_t i = 0; i < mNumCols; i++) {
28 // Store the value
29 nsIVariant *variant = nullptr;
30 int type = ::sqlite3_column_type(aStatement, i);
31 switch (type) {
32 case SQLITE_INTEGER:
33 variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
34 break;
35 case SQLITE_FLOAT:
36 variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
37 break;
38 case SQLITE_TEXT:
40 nsDependentString str(
41 static_cast<const char16_t *>(::sqlite3_column_text16(aStatement, i))
43 variant = new TextVariant(str);
44 break;
46 case SQLITE_NULL:
47 variant = new NullVariant();
48 break;
49 case SQLITE_BLOB:
51 int size = ::sqlite3_column_bytes(aStatement, i);
52 const void *data = ::sqlite3_column_blob(aStatement, i);
53 variant = new BlobVariant(std::pair<const void *, int>(data, size));
54 break;
56 default:
57 return NS_ERROR_UNEXPECTED;
59 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
61 // Insert into our storage array
62 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
64 // Associate the name (if any) with the index
65 const char *name = ::sqlite3_column_name(aStatement, i);
66 if (!name) break;
67 nsAutoCString colName(name);
68 mNameHashtable.Put(colName, i);
71 return NS_OK;
74 /**
75 * Note: This object is only ever accessed on one thread at a time. It it not
76 * threadsafe, but it does need threadsafe AddRef and Release.
78 NS_IMPL_ISUPPORTS(
79 Row,
80 mozIStorageRow,
81 mozIStorageValueArray
84 ////////////////////////////////////////////////////////////////////////////////
85 //// mozIStorageRow
87 NS_IMETHODIMP
88 Row::GetResultByIndex(uint32_t aIndex,
89 nsIVariant **_result)
91 ENSURE_INDEX_VALUE(aIndex, mNumCols);
92 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
93 return NS_OK;
96 NS_IMETHODIMP
97 Row::GetResultByName(const nsACString &aName,
98 nsIVariant **_result)
100 uint32_t index;
101 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
102 return GetResultByIndex(index, _result);
105 ////////////////////////////////////////////////////////////////////////////////
106 //// mozIStorageValueArray
108 NS_IMETHODIMP
109 Row::GetNumEntries(uint32_t *_entries)
111 *_entries = mNumCols;
112 return NS_OK;
115 NS_IMETHODIMP
116 Row::GetTypeOfIndex(uint32_t aIndex,
117 int32_t *_type)
119 ENSURE_INDEX_VALUE(aIndex, mNumCols);
121 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
122 switch (type) {
123 case nsIDataType::VTYPE_INT32:
124 case nsIDataType::VTYPE_INT64:
125 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
126 break;
127 case nsIDataType::VTYPE_DOUBLE:
128 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
129 break;
130 case nsIDataType::VTYPE_ASTRING:
131 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
132 break;
133 case nsIDataType::VTYPE_ARRAY:
134 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
135 break;
136 default:
137 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
138 break;
140 return NS_OK;
143 NS_IMETHODIMP
144 Row::GetInt32(uint32_t aIndex,
145 int32_t *_value)
147 ENSURE_INDEX_VALUE(aIndex, mNumCols);
148 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
151 NS_IMETHODIMP
152 Row::GetInt64(uint32_t aIndex,
153 int64_t *_value)
155 ENSURE_INDEX_VALUE(aIndex, mNumCols);
156 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
159 NS_IMETHODIMP
160 Row::GetDouble(uint32_t aIndex,
161 double *_value)
163 ENSURE_INDEX_VALUE(aIndex, mNumCols);
164 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
167 NS_IMETHODIMP
168 Row::GetUTF8String(uint32_t aIndex,
169 nsACString &_value)
171 ENSURE_INDEX_VALUE(aIndex, mNumCols);
172 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
175 NS_IMETHODIMP
176 Row::GetString(uint32_t aIndex,
177 nsAString &_value)
179 ENSURE_INDEX_VALUE(aIndex, mNumCols);
180 return mData.ObjectAt(aIndex)->GetAsAString(_value);
183 NS_IMETHODIMP
184 Row::GetBlob(uint32_t aIndex,
185 uint32_t *_size,
186 uint8_t **_blob)
188 ENSURE_INDEX_VALUE(aIndex, mNumCols);
190 uint16_t type;
191 nsIID interfaceIID;
192 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
193 reinterpret_cast<void **>(_blob));
196 NS_IMETHODIMP
197 Row::GetBlobAsString(uint32_t aIndex, nsAString& aValue)
199 return DoGetBlobAsString(this, aIndex, aValue);
202 NS_IMETHODIMP
203 Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue)
205 return DoGetBlobAsString(this, aIndex, aValue);
208 NS_IMETHODIMP
209 Row::GetIsNull(uint32_t aIndex,
210 bool *_isNull)
212 ENSURE_INDEX_VALUE(aIndex, mNumCols);
213 NS_ENSURE_ARG_POINTER(_isNull);
215 uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
216 *_isNull = type == nsIDataType::VTYPE_EMPTY;
217 return NS_OK;
220 NS_IMETHODIMP
221 Row::GetSharedUTF8String(uint32_t,
222 uint32_t *,
223 char const **)
225 return NS_ERROR_NOT_IMPLEMENTED;
228 NS_IMETHODIMP
229 Row::GetSharedString(uint32_t,
230 uint32_t *,
231 const char16_t **)
233 return NS_ERROR_NOT_IMPLEMENTED;
236 NS_IMETHODIMP
237 Row::GetSharedBlob(uint32_t,
238 uint32_t *,
239 const uint8_t **)
241 return NS_ERROR_NOT_IMPLEMENTED;
244 } // namespace storage
245 } // namespace mozilla