Line data Source code
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/. */
6 :
7 : #include "nsString.h"
8 :
9 : #include "sqlite3.h"
10 : #include "mozStoragePrivateHelpers.h"
11 : #include "Variant.h"
12 : #include "mozStorageRow.h"
13 :
14 : namespace mozilla {
15 : namespace storage {
16 :
17 : ////////////////////////////////////////////////////////////////////////////////
18 : //// Row
19 :
20 : nsresult
21 4 : Row::initialize(sqlite3_stmt *aStatement)
22 : {
23 : // Get the number of results
24 4 : mNumCols = ::sqlite3_column_count(aStatement);
25 :
26 : // Start copying over values
27 15 : for (uint32_t i = 0; i < mNumCols; i++) {
28 : // Store the value
29 11 : nsIVariant *variant = nullptr;
30 11 : int type = ::sqlite3_column_type(aStatement, i);
31 11 : switch (type) {
32 : case SQLITE_INTEGER:
33 10 : variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
34 5 : break;
35 : case SQLITE_FLOAT:
36 0 : variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
37 0 : break;
38 : case SQLITE_TEXT:
39 : {
40 : nsDependentString str(
41 4 : static_cast<const char16_t *>(::sqlite3_column_text16(aStatement, i))
42 8 : );
43 4 : variant = new TextVariant(str);
44 4 : break;
45 : }
46 : case SQLITE_NULL:
47 2 : variant = new NullVariant();
48 2 : break;
49 : case SQLITE_BLOB:
50 : {
51 0 : int size = ::sqlite3_column_bytes(aStatement, i);
52 0 : const void *data = ::sqlite3_column_blob(aStatement, i);
53 0 : variant = new BlobVariant(std::pair<const void *, int>(data, size));
54 0 : break;
55 : }
56 : default:
57 0 : return NS_ERROR_UNEXPECTED;
58 : }
59 11 : NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
60 :
61 : // Insert into our storage array
62 11 : NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
63 :
64 : // Associate the name (if any) with the index
65 11 : const char *name = ::sqlite3_column_name(aStatement, i);
66 11 : if (!name) break;
67 22 : nsAutoCString colName(name);
68 11 : mNameHashtable.Put(colName, i);
69 : }
70 :
71 4 : return NS_OK;
72 : }
73 :
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.
77 : */
78 140 : NS_IMPL_ISUPPORTS(
79 : Row,
80 : mozIStorageRow,
81 : mozIStorageValueArray
82 : )
83 :
84 : ////////////////////////////////////////////////////////////////////////////////
85 : //// mozIStorageRow
86 :
87 : NS_IMETHODIMP
88 11 : Row::GetResultByIndex(uint32_t aIndex,
89 : nsIVariant **_result)
90 : {
91 11 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
92 11 : NS_ADDREF(*_result = mData.ObjectAt(aIndex));
93 11 : return NS_OK;
94 : }
95 :
96 : NS_IMETHODIMP
97 7 : Row::GetResultByName(const nsACString &aName,
98 : nsIVariant **_result)
99 : {
100 : uint32_t index;
101 7 : NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
102 7 : return GetResultByIndex(index, _result);
103 : }
104 :
105 : ////////////////////////////////////////////////////////////////////////////////
106 : //// mozIStorageValueArray
107 :
108 : NS_IMETHODIMP
109 0 : Row::GetNumEntries(uint32_t *_entries)
110 : {
111 0 : *_entries = mNumCols;
112 0 : return NS_OK;
113 : }
114 :
115 : NS_IMETHODIMP
116 0 : Row::GetTypeOfIndex(uint32_t aIndex,
117 : int32_t *_type)
118 : {
119 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
120 :
121 : uint16_t type;
122 0 : (void)mData.ObjectAt(aIndex)->GetDataType(&type);
123 0 : switch (type) {
124 : case nsIDataType::VTYPE_INT32:
125 : case nsIDataType::VTYPE_INT64:
126 0 : *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
127 0 : break;
128 : case nsIDataType::VTYPE_DOUBLE:
129 0 : *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
130 0 : break;
131 : case nsIDataType::VTYPE_ASTRING:
132 0 : *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
133 0 : break;
134 : case nsIDataType::VTYPE_ARRAY:
135 0 : *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
136 0 : break;
137 : default:
138 0 : *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
139 0 : break;
140 : }
141 0 : return NS_OK;
142 : }
143 :
144 : NS_IMETHODIMP
145 0 : Row::GetInt32(uint32_t aIndex,
146 : int32_t *_value)
147 : {
148 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
149 0 : return mData.ObjectAt(aIndex)->GetAsInt32(_value);
150 : }
151 :
152 : NS_IMETHODIMP
153 0 : Row::GetInt64(uint32_t aIndex,
154 : int64_t *_value)
155 : {
156 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
157 0 : return mData.ObjectAt(aIndex)->GetAsInt64(_value);
158 : }
159 :
160 : NS_IMETHODIMP
161 0 : Row::GetDouble(uint32_t aIndex,
162 : double *_value)
163 : {
164 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
165 0 : return mData.ObjectAt(aIndex)->GetAsDouble(_value);
166 : }
167 :
168 : NS_IMETHODIMP
169 0 : Row::GetUTF8String(uint32_t aIndex,
170 : nsACString &_value)
171 : {
172 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
173 0 : return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
174 : }
175 :
176 : NS_IMETHODIMP
177 0 : Row::GetString(uint32_t aIndex,
178 : nsAString &_value)
179 : {
180 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
181 0 : return mData.ObjectAt(aIndex)->GetAsAString(_value);
182 : }
183 :
184 : NS_IMETHODIMP
185 0 : Row::GetBlob(uint32_t aIndex,
186 : uint32_t *_size,
187 : uint8_t **_blob)
188 : {
189 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
190 :
191 : uint16_t type;
192 : nsIID interfaceIID;
193 0 : return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
194 0 : reinterpret_cast<void **>(_blob));
195 : }
196 :
197 : NS_IMETHODIMP
198 0 : Row::GetBlobAsString(uint32_t aIndex, nsAString& aValue)
199 : {
200 0 : return DoGetBlobAsString(this, aIndex, aValue);
201 : }
202 :
203 : NS_IMETHODIMP
204 0 : Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue)
205 : {
206 0 : return DoGetBlobAsString(this, aIndex, aValue);
207 : }
208 :
209 : NS_IMETHODIMP
210 0 : Row::GetIsNull(uint32_t aIndex,
211 : bool *_isNull)
212 : {
213 0 : ENSURE_INDEX_VALUE(aIndex, mNumCols);
214 0 : NS_ENSURE_ARG_POINTER(_isNull);
215 :
216 : uint16_t type;
217 0 : (void)mData.ObjectAt(aIndex)->GetDataType(&type);
218 0 : *_isNull = type == nsIDataType::VTYPE_EMPTY;
219 0 : return NS_OK;
220 : }
221 :
222 : NS_IMETHODIMP
223 0 : Row::GetSharedUTF8String(uint32_t,
224 : uint32_t *,
225 : char const **)
226 : {
227 0 : return NS_ERROR_NOT_IMPLEMENTED;
228 : }
229 :
230 : NS_IMETHODIMP
231 0 : Row::GetSharedString(uint32_t,
232 : uint32_t *,
233 : const char16_t **)
234 : {
235 0 : return NS_ERROR_NOT_IMPLEMENTED;
236 : }
237 :
238 : NS_IMETHODIMP
239 0 : Row::GetSharedBlob(uint32_t,
240 : uint32_t *,
241 : const uint8_t **)
242 : {
243 0 : return NS_ERROR_NOT_IMPLEMENTED;
244 : }
245 :
246 : } // namespace storage
247 : } // namespace mozilla
|