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/. */
10 #include "mozStoragePrivateHelpers.h"
12 #include "mozStorageRow.h"
17 ////////////////////////////////////////////////////////////////////////////////
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
++) {
27 nsIVariant
*variant
= nullptr;
28 int type
= ::sqlite3_column_type(aStatement
, i
);
31 variant
= new IntegerVariant(::sqlite3_column_int64(aStatement
, i
));
34 variant
= new FloatVariant(::sqlite3_column_double(aStatement
, i
));
37 nsDependentString
str(static_cast<const char16_t
*>(
38 ::sqlite3_column_text16(aStatement
, i
)));
39 variant
= new TextVariant(str
);
43 variant
= new NullVariant();
46 int size
= ::sqlite3_column_bytes(aStatement
, i
);
47 const void *data
= ::sqlite3_column_blob(aStatement
, i
);
48 variant
= new BlobVariant(std::pair
<const void *, int>(data
, size
));
52 return NS_ERROR_UNEXPECTED
;
54 NS_ENSURE_TRUE(variant
, NS_ERROR_OUT_OF_MEMORY
);
56 // Insert into our storage array
57 NS_ENSURE_TRUE(mData
.InsertObjectAt(variant
, i
), NS_ERROR_OUT_OF_MEMORY
);
59 // Associate the name (if any) with the index
60 const char *name
= ::sqlite3_column_name(aStatement
, i
);
62 nsAutoCString
colName(name
);
63 mNameHashtable
.Put(colName
, i
);
70 * Note: This object is only ever accessed on one thread at a time. It it not
71 * threadsafe, but it does need threadsafe AddRef and Release.
73 NS_IMPL_ISUPPORTS(Row
, mozIStorageRow
, mozIStorageValueArray
)
75 ////////////////////////////////////////////////////////////////////////////////
79 Row::GetResultByIndex(uint32_t aIndex
, nsIVariant
**_result
) {
80 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
81 NS_ADDREF(*_result
= mData
.ObjectAt(aIndex
));
86 Row::GetResultByName(const nsACString
&aName
, nsIVariant
**_result
) {
88 NS_ENSURE_TRUE(mNameHashtable
.Get(aName
, &index
), NS_ERROR_NOT_AVAILABLE
);
89 return GetResultByIndex(index
, _result
);
92 ////////////////////////////////////////////////////////////////////////////////
93 //// mozIStorageValueArray
96 Row::GetNumEntries(uint32_t *_entries
) {
102 Row::GetTypeOfIndex(uint32_t aIndex
, int32_t *_type
) {
103 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
105 uint16_t type
= mData
.ObjectAt(aIndex
)->GetDataType();
107 case nsIDataType::VTYPE_INT32
:
108 case nsIDataType::VTYPE_INT64
:
109 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
111 case nsIDataType::VTYPE_DOUBLE
:
112 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
114 case nsIDataType::VTYPE_ASTRING
:
115 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
117 case nsIDataType::VTYPE_ARRAY
:
118 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
121 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
128 Row::GetInt32(uint32_t aIndex
, int32_t *_value
) {
129 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
130 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
134 Row::GetInt64(uint32_t aIndex
, int64_t *_value
) {
135 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
136 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
140 Row::GetDouble(uint32_t aIndex
, double *_value
) {
141 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
142 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
146 Row::GetUTF8String(uint32_t aIndex
, nsACString
&_value
) {
147 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
148 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
152 Row::GetString(uint32_t aIndex
, nsAString
&_value
) {
153 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
154 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
158 Row::GetBlob(uint32_t aIndex
, uint32_t *_size
, uint8_t **_blob
) {
159 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
163 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interfaceIID
, _size
,
164 reinterpret_cast<void **>(_blob
));
168 Row::GetBlobAsString(uint32_t aIndex
, nsAString
&aValue
) {
169 return DoGetBlobAsString(this, aIndex
, aValue
);
173 Row::GetBlobAsUTF8String(uint32_t aIndex
, nsACString
&aValue
) {
174 return DoGetBlobAsString(this, aIndex
, aValue
);
178 Row::GetIsNull(uint32_t aIndex
, bool *_isNull
) {
179 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
180 NS_ENSURE_ARG_POINTER(_isNull
);
182 uint16_t type
= mData
.ObjectAt(aIndex
)->GetDataType();
183 *_isNull
= type
== nsIDataType::VTYPE_EMPTY
;
188 Row::GetSharedUTF8String(uint32_t, uint32_t *, char const **) {
189 return NS_ERROR_NOT_IMPLEMENTED
;
193 Row::GetSharedString(uint32_t, uint32_t *, const char16_t
**) {
194 return NS_ERROR_NOT_IMPLEMENTED
;
198 Row::GetSharedBlob(uint32_t, uint32_t *, const uint8_t **) {
199 return NS_ERROR_NOT_IMPLEMENTED
;
202 } // namespace storage
203 } // namespace mozilla