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 ////////////////////////////////////////////////////////////////////////////////
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
++) {
29 nsIVariant
*variant
= nullptr;
30 int type
= ::sqlite3_column_type(aStatement
, i
);
33 variant
= new IntegerVariant(::sqlite3_column_int64(aStatement
, i
));
36 variant
= new FloatVariant(::sqlite3_column_double(aStatement
, i
));
40 nsDependentString
str(
41 static_cast<const char16_t
*>(::sqlite3_column_text16(aStatement
, i
))
43 variant
= new TextVariant(str
);
47 variant
= new NullVariant();
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
));
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
);
67 nsAutoCString
colName(name
);
68 mNameHashtable
.Put(colName
, i
);
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.
84 ////////////////////////////////////////////////////////////////////////////////
88 Row::GetResultByIndex(uint32_t aIndex
,
91 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
92 NS_ADDREF(*_result
= mData
.ObjectAt(aIndex
));
97 Row::GetResultByName(const nsACString
&aName
,
101 NS_ENSURE_TRUE(mNameHashtable
.Get(aName
, &index
), NS_ERROR_NOT_AVAILABLE
);
102 return GetResultByIndex(index
, _result
);
105 ////////////////////////////////////////////////////////////////////////////////
106 //// mozIStorageValueArray
109 Row::GetNumEntries(uint32_t *_entries
)
111 *_entries
= mNumCols
;
116 Row::GetTypeOfIndex(uint32_t aIndex
,
119 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
122 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
124 case nsIDataType::VTYPE_INT32
:
125 case nsIDataType::VTYPE_INT64
:
126 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
128 case nsIDataType::VTYPE_DOUBLE
:
129 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
131 case nsIDataType::VTYPE_ASTRING
:
132 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
134 case nsIDataType::VTYPE_ARRAY
:
135 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
138 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
145 Row::GetInt32(uint32_t aIndex
,
148 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
149 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
153 Row::GetInt64(uint32_t aIndex
,
156 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
157 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
161 Row::GetDouble(uint32_t aIndex
,
164 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
165 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
169 Row::GetUTF8String(uint32_t aIndex
,
172 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
173 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
177 Row::GetString(uint32_t aIndex
,
180 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
181 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
185 Row::GetBlob(uint32_t aIndex
,
189 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
193 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interfaceIID
, _size
,
194 reinterpret_cast<void **>(_blob
));
198 Row::GetBlobAsString(uint32_t aIndex
, nsAString
& aValue
)
200 return DoGetBlobAsString(this, aIndex
, aValue
);
204 Row::GetBlobAsUTF8String(uint32_t aIndex
, nsACString
& aValue
)
206 return DoGetBlobAsString(this, aIndex
, aValue
);
210 Row::GetIsNull(uint32_t aIndex
,
213 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
214 NS_ENSURE_ARG_POINTER(_isNull
);
217 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
218 *_isNull
= type
== nsIDataType::VTYPE_EMPTY
;
223 Row::GetSharedUTF8String(uint32_t,
227 return NS_ERROR_NOT_IMPLEMENTED
;
231 Row::GetSharedString(uint32_t,
235 return NS_ERROR_NOT_IMPLEMENTED
;
239 Row::GetSharedBlob(uint32_t,
243 return NS_ERROR_NOT_IMPLEMENTED
;
246 } // namespace storage
247 } // namespace mozilla