1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 expandtab
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 "mozStorageVariant.h"
44 #include "mozStorageRow.h"
46 ////////////////////////////////////////////////////////////////////////////////
50 * Note: This object is only ever accessed on one thread at a time. It it not
51 * threadsafe, but it does need threadsafe AddRef and Release.
53 NS_IMPL_THREADSAFE_ISUPPORTS2(
60 mozStorageRow::initialize(sqlite3_stmt
*aStatement
)
62 // Initialize the hash table
63 NS_ENSURE_TRUE(mNameHashtable
.Init(), NS_ERROR_OUT_OF_MEMORY
);
65 // Get the number of results
66 mNumCols
= sqlite3_column_count(aStatement
);
68 // Start copying over values
69 for (PRUint32 i
= 0; i
< mNumCols
; i
++) {
71 nsIVariant
*variant
= nsnull
;
72 int type
= sqlite3_column_type(aStatement
, i
);
75 variant
= new mozStorageInteger(sqlite3_column_int64(aStatement
, i
));
78 variant
= new mozStorageFloat(sqlite3_column_double(aStatement
, i
));
83 static_cast<const PRUnichar
*>(sqlite3_column_text16(aStatement
, i
))
85 variant
= new mozStorageText(str
);
89 variant
= new mozStorageNull();
93 int size
= sqlite3_column_bytes(aStatement
, i
);
94 const void *data
= sqlite3_column_blob(aStatement
, i
);
95 variant
= new mozStorageBlob(std::pair
<const void *, int>(data
, size
));
99 return NS_ERROR_UNEXPECTED
;
101 NS_ENSURE_TRUE(variant
, NS_ERROR_OUT_OF_MEMORY
);
103 // Insert into our storage array
104 NS_ENSURE_TRUE(mData
.InsertObjectAt(variant
, i
), NS_ERROR_OUT_OF_MEMORY
);
106 // Associate the name (if any) with the index
107 const char *name
= sqlite3_column_name(aStatement
, i
);
109 nsCAutoString
colName(name
);
110 mNameHashtable
.Put(colName
, i
);
116 ////////////////////////////////////////////////////////////////////////////////
120 mozStorageRow::GetResultByIndex(PRUint32 aIndex
, nsIVariant
**_result
)
122 if (aIndex
>= mNumCols
)
123 return NS_ERROR_ILLEGAL_VALUE
;
125 NS_ADDREF(*_result
= mData
.ObjectAt(aIndex
));
130 mozStorageRow::GetResultByName(const nsACString
&aName
, nsIVariant
**_result
)
133 NS_ENSURE_TRUE(mNameHashtable
.Get(aName
, &index
), NS_ERROR_NOT_AVAILABLE
);
134 return GetResultByIndex(index
, _result
);
137 ////////////////////////////////////////////////////////////////////////////////
138 //// mozIStorageValueArray
141 mozStorageRow::GetNumEntries(PRUint32
*_entries
)
143 *_entries
= mNumCols
;
148 mozStorageRow::GetTypeOfIndex(PRUint32 aIndex
, PRInt32
*_type
)
150 if (aIndex
>= mNumCols
)
151 return NS_ERROR_ILLEGAL_VALUE
;
154 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
156 case nsIDataType::VTYPE_INT32
:
157 case nsIDataType::VTYPE_INT64
:
158 *_type
= mozIStorageValueArray::VALUE_TYPE_INTEGER
;
160 case nsIDataType::VTYPE_DOUBLE
:
161 *_type
= mozIStorageValueArray::VALUE_TYPE_FLOAT
;
163 case nsIDataType::VTYPE_ASTRING
:
164 *_type
= mozIStorageValueArray::VALUE_TYPE_TEXT
;
166 case nsIDataType::VTYPE_ARRAY
:
167 *_type
= mozIStorageValueArray::VALUE_TYPE_BLOB
;
170 *_type
= mozIStorageValueArray::VALUE_TYPE_NULL
;
177 mozStorageRow::GetInt32(PRUint32 aIndex
, PRInt32
*_value
)
179 if (aIndex
>= mNumCols
)
180 return NS_ERROR_ILLEGAL_VALUE
;
182 return mData
.ObjectAt(aIndex
)->GetAsInt32(_value
);
186 mozStorageRow::GetInt64(PRUint32 aIndex
, PRInt64
*_value
)
188 if (aIndex
>= mNumCols
)
189 return NS_ERROR_ILLEGAL_VALUE
;
191 return mData
.ObjectAt(aIndex
)->GetAsInt64(_value
);
195 mozStorageRow::GetDouble(PRUint32 aIndex
, double *_value
)
197 if (aIndex
>= mNumCols
)
198 return NS_ERROR_ILLEGAL_VALUE
;
200 return mData
.ObjectAt(aIndex
)->GetAsDouble(_value
);
204 mozStorageRow::GetUTF8String(PRUint32 aIndex
, nsACString
&_value
)
206 if (aIndex
>= mNumCols
)
207 return NS_ERROR_ILLEGAL_VALUE
;
209 return mData
.ObjectAt(aIndex
)->GetAsAUTF8String(_value
);
213 mozStorageRow::GetString(PRUint32 aIndex
, nsAString
&_value
)
215 if (aIndex
>= mNumCols
)
216 return NS_ERROR_ILLEGAL_VALUE
;
218 return mData
.ObjectAt(aIndex
)->GetAsAString(_value
);
222 mozStorageRow::GetBlob(PRUint32 aIndex
, PRUint32
*_size
, PRUint8
**_blob
)
224 if (aIndex
>= mNumCols
)
225 return NS_ERROR_ILLEGAL_VALUE
;
229 return mData
.ObjectAt(aIndex
)->GetAsArray(&type
, &interface
, _size
,
230 reinterpret_cast<void **>(_blob
));
234 mozStorageRow::GetIsNull(PRUint32 aIndex
, PRBool
*_isNull
)
236 if (aIndex
>= mNumCols
)
237 return NS_ERROR_ILLEGAL_VALUE
;
240 (void)mData
.ObjectAt(aIndex
)->GetDataType(&type
);
241 *_isNull
= type
== nsIDataType::VTYPE_VOID
;
246 mozStorageRow::GetSharedUTF8String(PRUint32
, PRUint32
*, char const **)
248 return NS_ERROR_NOT_IMPLEMENTED
;
252 mozStorageRow::GetSharedString(PRUint32
, PRUint32
*, const PRUnichar
**)
254 return NS_ERROR_NOT_IMPLEMENTED
;
258 mozStorageRow::GetSharedBlob(PRUint32
, PRUint32
*, const PRUint8
**)
260 return NS_ERROR_NOT_IMPLEMENTED
;