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 "mozStorageRow.h"
12 #include "mozStoragePrivateHelpers.h"
18 ////////////////////////////////////////////////////////////////////////////////
21 nsresult
Row::initialize(sqlite3_stmt
* aStatement
) {
22 // Get the number of results
23 mNumCols
= ::sqlite3_column_count(aStatement
);
25 // Start copying over values
26 for (uint32_t i
= 0; i
< mNumCols
; i
++) {
28 nsIVariant
* variant
= nullptr;
29 int type
= ::sqlite3_column_type(aStatement
, i
);
32 variant
= new IntegerVariant(::sqlite3_column_int64(aStatement
, i
));
35 variant
= new FloatVariant(::sqlite3_column_double(aStatement
, i
));
38 const char16_t
* value
= static_cast<const char16_t
*>(
39 ::sqlite3_column_text16(aStatement
, i
));
40 nsDependentString
str(
41 value
, ::sqlite3_column_bytes16(aStatement
, i
) / sizeof(char16_t
));
42 variant
= new TextVariant(str
);
46 variant
= new NullVariant();
49 const void* data
= ::sqlite3_column_blob(aStatement
, i
);
50 int size
= ::sqlite3_column_bytes(aStatement
, i
);
51 variant
= new BlobVariant(std::pair
<const void*, int>(data
, size
));
55 return NS_ERROR_UNEXPECTED
;
57 NS_ENSURE_TRUE(variant
, NS_ERROR_OUT_OF_MEMORY
);
59 // Insert into our storage array
60 NS_ENSURE_TRUE(mData
.InsertObjectAt(variant
, i
), NS_ERROR_OUT_OF_MEMORY
);
62 // Associate the name (if any) with the index
63 const char* name
= ::sqlite3_column_name(aStatement
, i
);
65 nsAutoCString
colName(name
);
66 mNameHashtable
.InsertOrUpdate(colName
, i
);
73 * Note: This object is only ever accessed on one thread at a time. It it not
74 * threadsafe, but it does need threadsafe AddRef and Release.
76 NS_IMPL_ISUPPORTS(Row
, mozIStorageRow
, mozIStorageValueArray
)
78 ////////////////////////////////////////////////////////////////////////////////
82 Row::GetResultByIndex(uint32_t aIndex
, nsIVariant
** _result
) {
83 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
84 NS_ADDREF(*_result
= mData
.ObjectAt(aIndex
));
89 Row::GetResultByName(const nsACString
& aName
, nsIVariant
** _result
) {
91 NS_ENSURE_TRUE(mNameHashtable
.Get(aName
, &index
), NS_ERROR_NOT_AVAILABLE
);
92 return GetResultByIndex(index
, _result
);
95 ////////////////////////////////////////////////////////////////////////////////
96 //// mozIStorageValueArray
99 Row::GetNumEntries(uint32_t* _entries
) {
100 *_entries
= mNumCols
;
105 Row::GetTypeOfIndex(uint32_t aIndex
, int32_t* _type
) {
106 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
108 uint16_t type
= mData
.ObjectAt(aIndex
)->GetDataType();
110 case nsIDataType::VTYPE_INT32
:
111 case nsIDataType::VTYPE_INT64
:
112 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
114 case nsIDataType::VTYPE_DOUBLE
:
115 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
117 case nsIDataType::VTYPE_ASTRING
:
118 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
120 case nsIDataType::VTYPE_ARRAY
:
121 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
124 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
131 Row::GetInt32(uint32_t aIndex
, int32_t* _value
) {
132 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
133 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
137 Row::GetInt64(uint32_t aIndex
, int64_t* _value
) {
138 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
139 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
143 Row::GetDouble(uint32_t aIndex
, double* _value
) {
144 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
145 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
149 Row::GetUTF8String(uint32_t aIndex
, nsACString
& _value
) {
150 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
151 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
155 Row::GetString(uint32_t aIndex
, nsAString
& _value
) {
156 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
157 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
161 Row::GetBlob(uint32_t aIndex
, uint32_t* _size
, uint8_t** _blob
) {
162 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
166 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interfaceIID
, _size
,
167 reinterpret_cast<void**>(_blob
));
171 Row::GetBlobAsString(uint32_t aIndex
, nsAString
& aValue
) {
172 return DoGetBlobAsString(this, aIndex
, aValue
);
176 Row::GetBlobAsUTF8String(uint32_t aIndex
, nsACString
& aValue
) {
177 return DoGetBlobAsString(this, aIndex
, aValue
);
181 Row::GetIsNull(uint32_t aIndex
, bool* _isNull
) {
182 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
183 NS_ENSURE_ARG_POINTER(_isNull
);
185 uint16_t type
= mData
.ObjectAt(aIndex
)->GetDataType();
186 *_isNull
= type
== nsIDataType::VTYPE_EMPTY
;
191 Row::GetSharedUTF8String(uint32_t, uint32_t*, char const**) {
192 return NS_ERROR_NOT_IMPLEMENTED
;
196 Row::GetSharedString(uint32_t, uint32_t*, const char16_t
**) {
197 return NS_ERROR_NOT_IMPLEMENTED
;
201 Row::GetSharedBlob(uint32_t, uint32_t*, const uint8_t**) {
202 return NS_ERROR_NOT_IMPLEMENTED
;
205 } // namespace storage
206 } // namespace mozilla