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 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 #include "mozStoragePrivateHelpers.h"
45 #include "mozStorageRow.h"
50 ////////////////////////////////////////////////////////////////////////////////
54 Row::initialize(sqlite3_stmt
*aStatement
)
56 // Initialize the hash table
57 NS_ENSURE_TRUE(mNameHashtable
.Init(), NS_ERROR_OUT_OF_MEMORY
);
59 // Get the number of results
60 mNumCols
= ::sqlite3_column_count(aStatement
);
62 // Start copying over values
63 for (PRUint32 i
= 0; i
< mNumCols
; i
++) {
65 nsIVariant
*variant
= nsnull
;
66 int type
= ::sqlite3_column_type(aStatement
, i
);
69 variant
= new IntegerVariant(::sqlite3_column_int64(aStatement
, i
));
72 variant
= new FloatVariant(::sqlite3_column_double(aStatement
, i
));
76 nsDependentString
str(
77 static_cast<const PRUnichar
*>(::sqlite3_column_text16(aStatement
, i
))
79 variant
= new TextVariant(str
);
83 variant
= new NullVariant();
87 int size
= ::sqlite3_column_bytes(aStatement
, i
);
88 const void *data
= ::sqlite3_column_blob(aStatement
, i
);
89 variant
= new BlobVariant(std::pair
<const void *, int>(data
, size
));
93 return NS_ERROR_UNEXPECTED
;
95 NS_ENSURE_TRUE(variant
, NS_ERROR_OUT_OF_MEMORY
);
97 // Insert into our storage array
98 NS_ENSURE_TRUE(mData
.InsertObjectAt(variant
, i
), NS_ERROR_OUT_OF_MEMORY
);
100 // Associate the name (if any) with the index
101 const char *name
= ::sqlite3_column_name(aStatement
, i
);
103 nsCAutoString
colName(name
);
104 mNameHashtable
.Put(colName
, i
);
111 * Note: This object is only ever accessed on one thread at a time. It it not
112 * threadsafe, but it does need threadsafe AddRef and Release.
114 NS_IMPL_THREADSAFE_ISUPPORTS2(
117 mozIStorageValueArray
120 ////////////////////////////////////////////////////////////////////////////////
124 Row::GetResultByIndex(PRUint32 aIndex
,
125 nsIVariant
**_result
)
127 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
128 NS_ADDREF(*_result
= mData
.ObjectAt(aIndex
));
133 Row::GetResultByName(const nsACString
&aName
,
134 nsIVariant
**_result
)
137 NS_ENSURE_TRUE(mNameHashtable
.Get(aName
, &index
), NS_ERROR_NOT_AVAILABLE
);
138 return GetResultByIndex(index
, _result
);
141 ////////////////////////////////////////////////////////////////////////////////
142 //// mozIStorageValueArray
145 Row::GetNumEntries(PRUint32
*_entries
)
147 *_entries
= mNumCols
;
152 Row::GetTypeOfIndex(PRUint32 aIndex
,
155 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
158 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
160 case nsIDataType::VTYPE_INT32
:
161 case nsIDataType::VTYPE_INT64
:
162 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
164 case nsIDataType::VTYPE_DOUBLE
:
165 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
167 case nsIDataType::VTYPE_ASTRING
:
168 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
170 case nsIDataType::VTYPE_ARRAY
:
171 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
174 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
181 Row::GetInt32(PRUint32 aIndex
,
184 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
185 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
189 Row::GetInt64(PRUint32 aIndex
,
192 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
193 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
197 Row::GetDouble(PRUint32 aIndex
,
200 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
201 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
205 Row::GetUTF8String(PRUint32 aIndex
,
208 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
209 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
213 Row::GetString(PRUint32 aIndex
,
216 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
217 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
221 Row::GetBlob(PRUint32 aIndex
,
225 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
229 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interfaceIID
, _size
,
230 reinterpret_cast<void **>(_blob
));
234 Row::GetIsNull(PRUint32 aIndex
,
237 ENSURE_INDEX_VALUE(aIndex
, mNumCols
);
238 NS_ENSURE_ARG_POINTER(_isNull
);
241 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
242 *_isNull
= type
== nsIDataType::VTYPE_EMPTY
;
247 Row::GetSharedUTF8String(PRUint32
,
251 return NS_ERROR_NOT_IMPLEMENTED
;
255 Row::GetSharedString(PRUint32
,
259 return NS_ERROR_NOT_IMPLEMENTED
;
263 Row::GetSharedBlob(PRUint32
,
267 return NS_ERROR_NOT_IMPLEMENTED
;
270 } // namespace storage
271 } // namespace mozilla