Bug 481045. <svg:use> shouldn't paint its kids. r+sr=roc
[mozilla-central.git] / storage / src / mozStorageValueArray.cpp
blobf105dd4c99f1630e862cc5fed9c25a30e0b1caef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is
18 * Oracle Corporation
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsError.h"
40 #include "nsMemory.h"
41 #include "nsString.h"
43 #include "mozStorageValueArray.h"
45 /***
46 *** mozStorageStatementRowValueArray
47 ***/
49 /* Implementation file */
50 NS_IMPL_ISUPPORTS1(mozStorageStatementRowValueArray, mozIStorageValueArray)
52 mozStorageStatementRowValueArray::mozStorageStatementRowValueArray(sqlite3_stmt *aSqliteStmt)
54 mSqliteStatement = aSqliteStmt;
55 mNumEntries = sqlite3_data_count (aSqliteStmt);
58 mozStorageStatementRowValueArray::~mozStorageStatementRowValueArray()
60 /* do nothing, we don't own the stmt */
63 /* readonly attribute unsigned long numEntries; */
64 NS_IMETHODIMP
65 mozStorageStatementRowValueArray::GetNumEntries(PRUint32 *aLength)
67 *aLength = mNumEntries;
68 return NS_OK;
71 /* long getTypeOfIndex (in unsigned long aIndex); */
72 NS_IMETHODIMP
73 mozStorageStatementRowValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
75 if (aIndex < 0 || aIndex >= mNumEntries)
76 return NS_ERROR_ILLEGAL_VALUE;
78 int t = sqlite3_column_type (mSqliteStatement, aIndex);
79 switch (t) {
80 case SQLITE_INTEGER:
81 *_retval = VALUE_TYPE_INTEGER;
82 break;
83 case SQLITE_FLOAT:
84 *_retval = VALUE_TYPE_FLOAT;
85 break;
86 case SQLITE_TEXT:
87 *_retval = VALUE_TYPE_TEXT;
88 break;
89 case SQLITE_BLOB:
90 *_retval = VALUE_TYPE_BLOB;
91 break;
92 case SQLITE_NULL:
93 *_retval = VALUE_TYPE_NULL;
94 break;
95 default:
96 // ???
97 return NS_ERROR_FAILURE;
100 return NS_OK;
103 NS_IMETHODIMP
104 mozStorageStatementRowValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
106 if (aIndex < 0 || aIndex >= mNumEntries)
107 return NS_ERROR_ILLEGAL_VALUE;
109 *_retval = sqlite3_column_int (mSqliteStatement, aIndex);
111 return NS_OK;
114 NS_IMETHODIMP
115 mozStorageStatementRowValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
117 if (aIndex < 0 || aIndex >= mNumEntries)
118 return NS_ERROR_ILLEGAL_VALUE;
120 *_retval = sqlite3_column_int64 (mSqliteStatement, aIndex);
122 return NS_OK;
125 NS_IMETHODIMP
126 mozStorageStatementRowValueArray::GetDouble(PRUint32 aIndex, double *_retval)
128 if (aIndex < 0 || aIndex >= mNumEntries)
129 return NS_ERROR_ILLEGAL_VALUE;
131 *_retval = sqlite3_column_double (mSqliteStatement, aIndex);
133 return NS_OK;
136 NS_IMETHODIMP
137 mozStorageStatementRowValueArray::GetUTF8String(PRUint32 aIndex, nsACString &_retval)
139 // GetTypeOfIndex will check aIndex for us, so we don't have to.
140 PRInt32 type;
141 nsresult rv = GetTypeOfIndex (aIndex, &type);
142 NS_ENSURE_SUCCESS(rv, rv);
143 if (type == SQLITE_NULL) {
144 // null columns get IsVoid set to distinguish them from empty strings
145 _retval.Truncate(0);
146 _retval.SetIsVoid(PR_TRUE);
147 } else {
148 PRUint32 slen = (PRUint32) sqlite3_column_bytes (mSqliteStatement, aIndex);
149 const char *cstr = (const char *) sqlite3_column_text (mSqliteStatement, aIndex);
150 _retval.Assign(cstr, slen);
152 return NS_OK;
155 NS_IMETHODIMP
156 mozStorageStatementRowValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
158 // GetTypeOfIndex will check aIndex for us, so we don't have to.
159 PRInt32 type;
160 nsresult rv = GetTypeOfIndex (aIndex, &type);
161 NS_ENSURE_SUCCESS(rv, rv);
162 if (type == SQLITE_NULL) {
163 // null columns get IsVoid set to distinguish them from empty strings
164 _retval.Truncate(0);
165 _retval.SetIsVoid(PR_TRUE);
166 } else {
167 int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex);
168 const PRUnichar *wstr = (const PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex);
169 _retval.Assign (wstr, slen/2);
171 return NS_OK;
174 NS_IMETHODIMP
175 mozStorageStatementRowValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
177 if (aIndex < 0 || aIndex >= mNumEntries)
178 return NS_ERROR_ILLEGAL_VALUE;
180 int blobsize = sqlite3_column_bytes (mSqliteStatement, aIndex);
181 const void *blob = sqlite3_column_blob (mSqliteStatement, aIndex);
183 void *blobcopy = nsMemory::Clone(blob, blobsize);
184 if (blobcopy == NULL)
185 return NS_ERROR_OUT_OF_MEMORY;
187 *aData = (PRUint8*) blobcopy;
188 *aDataSize = blobsize;
190 return NS_OK;
193 NS_IMETHODIMP
194 mozStorageStatementRowValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
196 // GetTypeOfIndex will check aIndex for us, so we don't have to.
197 PRInt32 t;
198 nsresult rv = GetTypeOfIndex (aIndex, &t);
199 NS_ENSURE_SUCCESS(rv, rv);
201 if (t == VALUE_TYPE_NULL)
202 *_retval = PR_TRUE;
203 else
204 *_retval = PR_FALSE;
206 return NS_OK;
209 NS_IMETHODIMP
210 mozStorageStatementRowValueArray::GetSharedUTF8String(PRUint32 aIndex, PRUint32 *aLength, const char **_retval)
212 if (aLength) {
213 int slen = sqlite3_column_bytes (mSqliteStatement, aIndex);
214 *aLength = slen;
217 *_retval = (const char*) sqlite3_column_text (mSqliteStatement, aIndex);
218 return NS_OK;
221 NS_IMETHODIMP
222 mozStorageStatementRowValueArray::GetSharedString(PRUint32 aIndex, PRUint32 *aLength, const PRUnichar **_retval)
224 if (aLength) {
225 int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex);
226 *aLength = slen;
229 *_retval = (const PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex);
230 return NS_OK;
233 NS_IMETHODIMP
234 mozStorageStatementRowValueArray::GetSharedBlob(PRUint32 aIndex, PRUint32 *aDataSize, const PRUint8 **aData)
236 *aDataSize = sqlite3_column_bytes (mSqliteStatement, aIndex);
237 *aData = (const PRUint8*) sqlite3_column_blob (mSqliteStatement, aIndex);
239 return NS_OK;
243 /***
244 *** mozStorageArgvValueArray
245 ***/
247 /* Implementation file */
248 NS_IMPL_ISUPPORTS1(mozStorageArgvValueArray, mozIStorageValueArray)
250 mozStorageArgvValueArray::mozStorageArgvValueArray(PRInt32 aArgc, sqlite3_value **aArgv)
251 : mArgc(aArgc), mArgv(aArgv)
255 mozStorageArgvValueArray::~mozStorageArgvValueArray()
257 /* do nothing, we don't own the array */
260 /* readonly attribute unsigned long length; */
261 NS_IMETHODIMP
262 mozStorageArgvValueArray::GetNumEntries(PRUint32 *aLength)
264 *aLength = mArgc;
265 return NS_OK;
268 /* long getTypeOfIndex (in unsigned long aIndex); */
269 NS_IMETHODIMP
270 mozStorageArgvValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
272 if (aIndex < 0 || aIndex >= mArgc)
273 return NS_ERROR_ILLEGAL_VALUE;
275 int t = sqlite3_value_type (mArgv[aIndex]);
276 switch (t) {
277 case SQLITE_INTEGER:
278 *_retval = VALUE_TYPE_INTEGER;
279 break;
280 case SQLITE_FLOAT:
281 *_retval = VALUE_TYPE_FLOAT;
282 break;
283 case SQLITE_TEXT:
284 *_retval = VALUE_TYPE_TEXT;
285 break;
286 case SQLITE_BLOB:
287 *_retval = VALUE_TYPE_BLOB;
288 break;
289 case SQLITE_NULL:
290 *_retval = VALUE_TYPE_NULL;
291 break;
292 default:
293 // ???
294 return NS_ERROR_FAILURE;
297 return NS_OK;
300 NS_IMETHODIMP
301 mozStorageArgvValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
303 if (aIndex < 0 || aIndex >= mArgc)
304 return NS_ERROR_ILLEGAL_VALUE;
306 *_retval = sqlite3_value_int (mArgv[aIndex]);
308 return NS_OK;
311 NS_IMETHODIMP
312 mozStorageArgvValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
314 if (aIndex < 0 || aIndex >= mArgc)
315 return NS_ERROR_ILLEGAL_VALUE;
317 *_retval = sqlite3_value_int64 (mArgv[aIndex]);
319 return NS_OK;
322 NS_IMETHODIMP
323 mozStorageArgvValueArray::GetDouble(PRUint32 aIndex, double *_retval)
325 if (aIndex < 0 || aIndex >= mArgc)
326 return NS_ERROR_ILLEGAL_VALUE;
328 *_retval = sqlite3_value_double (mArgv[aIndex]);
330 return NS_OK;
333 NS_IMETHODIMP
334 mozStorageArgvValueArray::GetUTF8String(PRUint32 aIndex, nsACString & _retval)
336 if (aIndex < 0 || aIndex >= mArgc)
337 return NS_ERROR_ILLEGAL_VALUE;
339 if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
340 // null columns get IsVoid set to distinguish them from empty strings
341 _retval.Truncate(0);
342 _retval.SetIsVoid(PR_TRUE);
343 } else {
344 int slen = sqlite3_value_bytes (mArgv[aIndex]);
345 const unsigned char *cstr = sqlite3_value_text (mArgv[aIndex]);
346 _retval.Assign ((char *) cstr, slen);
348 return NS_OK;
351 NS_IMETHODIMP
352 mozStorageArgvValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
354 if (aIndex < 0 || aIndex >= mArgc)
355 return NS_ERROR_ILLEGAL_VALUE;
357 if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
358 // null columns get IsVoid set to distinguish them from empty strings
359 _retval.Truncate(0);
360 _retval.SetIsVoid(PR_TRUE);
361 } else {
362 int slen = sqlite3_value_bytes16 (mArgv[aIndex]);
363 const PRUnichar *wstr = (const PRUnichar *) sqlite3_value_text16 (mArgv[aIndex]);
364 _retval.Assign (wstr, slen/2);
366 return NS_OK;
369 NS_IMETHODIMP
370 mozStorageArgvValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
372 if (aIndex < 0 || aIndex >= mArgc)
373 return NS_ERROR_ILLEGAL_VALUE;
375 int blobsize = sqlite3_value_bytes (mArgv[aIndex]);
376 const void *blob = sqlite3_value_blob (mArgv[aIndex]);
378 void *blobcopy = nsMemory::Clone(blob, blobsize);
379 if (blobcopy == NULL)
380 return NS_ERROR_OUT_OF_MEMORY;
382 *aData = (PRUint8*) blobcopy;
383 *aDataSize = blobsize;
385 return NS_OK;
388 /* boolean getIsNull (in unsigned long aIndex); */
389 NS_IMETHODIMP
390 mozStorageArgvValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
392 // GetTypeOfIndex will check aIndex for us, so we don't have to.
393 PRInt32 t;
394 nsresult rv = GetTypeOfIndex (aIndex, &t);
395 NS_ENSURE_SUCCESS(rv, rv);
397 if (t == VALUE_TYPE_NULL)
398 *_retval = PR_TRUE;
399 else
400 *_retval = PR_FALSE;
402 return NS_OK;
405 NS_IMETHODIMP
406 mozStorageArgvValueArray::GetSharedUTF8String(PRUint32 aIndex, PRUint32 *aLength, const char **_retval)
408 if (aLength) {
409 int slen = sqlite3_value_bytes (mArgv[aIndex]);
410 *aLength = slen;
413 *_retval = (const char*) sqlite3_value_text (mArgv[aIndex]);
414 return NS_OK;
417 NS_IMETHODIMP
418 mozStorageArgvValueArray::GetSharedString(PRUint32 aIndex, PRUint32 *aLength, const PRUnichar **_retval)
420 if (aLength) {
421 int slen = sqlite3_value_bytes16 (mArgv[aIndex]);
422 *aLength = slen;
425 *_retval = (const PRUnichar*) sqlite3_value_text16 (mArgv[aIndex]);
426 return NS_OK;
429 NS_IMETHODIMP
430 mozStorageArgvValueArray::GetSharedBlob(PRUint32 aIndex, PRUint32 *aDataSize, const PRUint8 **aData)
432 *aDataSize = sqlite3_value_bytes (mArgv[aIndex]);
433 *aData = (const PRUint8*) sqlite3_value_blob (mArgv[aIndex]);
435 return NS_OK;