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
);
121 uint16_t type
= mData
.ObjectAt(aIndex
)->GetDataType();
123 case nsIDataType::VTYPE_INT32
:
124 case nsIDataType::VTYPE_INT64
:
125 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
127 case nsIDataType::VTYPE_DOUBLE
:
128 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
130 case nsIDataType::VTYPE_ASTRING
:
131 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
133 case nsIDataType::VTYPE_ARRAY
:
134 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
137 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
144 Row::GetInt32(uint32_t aIndex
,
147 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
148 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
152 Row::GetInt64(uint32_t aIndex
,
155 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
156 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
160 Row::GetDouble(uint32_t aIndex
,
163 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
164 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
168 Row::GetUTF8String(uint32_t aIndex
,
171 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
172 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
176 Row::GetString(uint32_t aIndex
,
179 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
180 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
184 Row::GetBlob(uint32_t aIndex
,
188 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
192 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interfaceIID
, _size
,
193 reinterpret_cast<void **>(_blob
));
197 Row::GetBlobAsString(uint32_t aIndex
, nsAString
& aValue
)
199 return DoGetBlobAsString(this, aIndex
, aValue
);
203 Row::GetBlobAsUTF8String(uint32_t aIndex
, nsACString
& aValue
)
205 return DoGetBlobAsString(this, aIndex
, aValue
);
209 Row::GetIsNull(uint32_t aIndex
,
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
;
221 Row::GetSharedUTF8String(uint32_t,
225 return NS_ERROR_NOT_IMPLEMENTED
;
229 Row::GetSharedString(uint32_t,
233 return NS_ERROR_NOT_IMPLEMENTED
;
237 Row::GetSharedBlob(uint32_t,
241 return NS_ERROR_NOT_IMPLEMENTED
;
244 } // namespace storage
245 } // namespace mozilla