Bug 634734 - Fennec ASSERTION: mFUnitsConvFactor not valid: mFUnitsConvFactor > 0...
[mozilla-central.git] / storage / src / mozStorageArgValueArray.cpp
blob78716d7ce7137e647ee41987de9f59063a0ad71e
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
14 * License.
16 * The Original Code is Oracle Corporation code.
18 * The Initial Developer of the Original Code is
19 * Oracle Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
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 ***** */
40 #include "nsError.h"
41 #include "nsMemory.h"
42 #include "nsString.h"
44 #include "mozStoragePrivateHelpers.h"
45 #include "mozStorageArgValueArray.h"
47 namespace mozilla {
48 namespace storage {
50 ////////////////////////////////////////////////////////////////////////////////
51 //// ArgValueArray
53 ArgValueArray::ArgValueArray(PRInt32 aArgc,
54 sqlite3_value **aArgv)
55 : mArgc(aArgc)
56 , mArgv(aArgv)
60 NS_IMPL_ISUPPORTS1(
61 ArgValueArray,
62 mozIStorageValueArray
65 ////////////////////////////////////////////////////////////////////////////////
66 //// mozIStorageValueArray
68 NS_IMETHODIMP
69 ArgValueArray::GetNumEntries(PRUint32 *_size)
71 *_size = mArgc;
72 return NS_OK;
75 NS_IMETHODIMP
76 ArgValueArray::GetTypeOfIndex(PRUint32 aIndex,
77 PRInt32 *_type)
79 ENSURE_INDEX_VALUE(aIndex, mArgc);
81 int t = ::sqlite3_value_type(mArgv[aIndex]);
82 switch (t) {
83 case SQLITE_INTEGER:
84 *_type = VALUE_TYPE_INTEGER;
85 break;
86 case SQLITE_FLOAT:
87 *_type = VALUE_TYPE_FLOAT;
88 break;
89 case SQLITE_TEXT:
90 *_type = VALUE_TYPE_TEXT;
91 break;
92 case SQLITE_BLOB:
93 *_type = VALUE_TYPE_BLOB;
94 break;
95 case SQLITE_NULL:
96 *_type = VALUE_TYPE_NULL;
97 break;
98 default:
99 return NS_ERROR_FAILURE;
102 return NS_OK;
105 NS_IMETHODIMP
106 ArgValueArray::GetInt32(PRUint32 aIndex,
107 PRInt32 *_value)
109 ENSURE_INDEX_VALUE(aIndex, mArgc);
111 *_value = ::sqlite3_value_int(mArgv[aIndex]);
112 return NS_OK;
115 NS_IMETHODIMP
116 ArgValueArray::GetInt64(PRUint32 aIndex,
117 PRInt64 *_value)
119 ENSURE_INDEX_VALUE(aIndex, mArgc);
121 *_value = ::sqlite3_value_int64(mArgv[aIndex]);
122 return NS_OK;
125 NS_IMETHODIMP
126 ArgValueArray::GetDouble(PRUint32 aIndex,
127 double *_value)
129 ENSURE_INDEX_VALUE(aIndex, mArgc);
131 *_value = ::sqlite3_value_double(mArgv[aIndex]);
132 return NS_OK;
135 NS_IMETHODIMP
136 ArgValueArray::GetUTF8String(PRUint32 aIndex,
137 nsACString &_value)
139 ENSURE_INDEX_VALUE(aIndex, mArgc);
141 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
142 // NULL columns should have IsVoid set to distinguish them from an empty
143 // string.
144 _value.Truncate(0);
145 _value.SetIsVoid(PR_TRUE);
147 else {
148 _value.Assign(reinterpret_cast<const char *>(::sqlite3_value_text(mArgv[aIndex])),
149 ::sqlite3_value_bytes(mArgv[aIndex]));
151 return NS_OK;
154 NS_IMETHODIMP
155 ArgValueArray::GetString(PRUint32 aIndex,
156 nsAString &_value)
158 ENSURE_INDEX_VALUE(aIndex, mArgc);
160 if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) {
161 // NULL columns should have IsVoid set to distinguish them from an empty
162 // string.
163 _value.Truncate(0);
164 _value.SetIsVoid(PR_TRUE);
165 } else {
166 _value.Assign(static_cast<const PRUnichar *>(::sqlite3_value_text16(mArgv[aIndex])),
167 ::sqlite3_value_bytes16(mArgv[aIndex]) / 2);
169 return NS_OK;
172 NS_IMETHODIMP
173 ArgValueArray::GetBlob(PRUint32 aIndex,
174 PRUint32 *_size,
175 PRUint8 **_blob)
177 ENSURE_INDEX_VALUE(aIndex, mArgc);
179 int size = ::sqlite3_value_bytes(mArgv[aIndex]);
180 void *blob = nsMemory::Clone(::sqlite3_value_blob(mArgv[aIndex]), size);
181 NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY);
183 *_blob = static_cast<PRUint8 *>(blob);
184 *_size = size;
185 return NS_OK;
188 NS_IMETHODIMP
189 ArgValueArray::GetIsNull(PRUint32 aIndex,
190 PRBool *_isNull)
192 // GetTypeOfIndex will check aIndex for us, so we don't have to.
193 PRInt32 type;
194 nsresult rv = GetTypeOfIndex(aIndex, &type);
195 NS_ENSURE_SUCCESS(rv, rv);
197 *_isNull = (type == VALUE_TYPE_NULL);
198 return NS_OK;
201 NS_IMETHODIMP
202 ArgValueArray::GetSharedUTF8String(PRUint32 aIndex,
203 PRUint32 *_length,
204 const char **_string)
206 if (_length)
207 *_length = ::sqlite3_value_bytes(mArgv[aIndex]);
209 *_string = reinterpret_cast<const char *>(::sqlite3_value_text(mArgv[aIndex]));
210 return NS_OK;
213 NS_IMETHODIMP
214 ArgValueArray::GetSharedString(PRUint32 aIndex,
215 PRUint32 *_length,
216 const PRUnichar **_string)
218 if (_length)
219 *_length = ::sqlite3_value_bytes(mArgv[aIndex]);
221 *_string = static_cast<const PRUnichar *>(::sqlite3_value_text16(mArgv[aIndex]));
222 return NS_OK;
225 NS_IMETHODIMP
226 ArgValueArray::GetSharedBlob(PRUint32 aIndex,
227 PRUint32 *_size,
228 const PRUint8 **_blob)
230 *_size = ::sqlite3_value_bytes(mArgv[aIndex]);
231 *_blob = static_cast<const PRUint8 *>(::sqlite3_value_blob(mArgv[aIndex]));
232 return NS_OK;
235 } // namespace storage
236 } // namespace mozilla