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
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
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 ***** */
43 #include "mozStorageValueArray.h"
46 *** mozStorageStatementRowValueArray
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; */
65 mozStorageStatementRowValueArray::GetNumEntries(PRUint32
*aLength
)
67 *aLength
= mNumEntries
;
71 /* long getTypeOfIndex (in unsigned long aIndex); */
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
);
81 *_retval
= VALUE_TYPE_INTEGER
;
84 *_retval
= VALUE_TYPE_FLOAT
;
87 *_retval
= VALUE_TYPE_TEXT
;
90 *_retval
= VALUE_TYPE_BLOB
;
93 *_retval
= VALUE_TYPE_NULL
;
97 return NS_ERROR_FAILURE
;
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
);
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
);
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
);
137 mozStorageStatementRowValueArray::GetUTF8String(PRUint32 aIndex
, nsACString
&_retval
)
139 // GetTypeOfIndex will check aIndex for us, so we don't have to.
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
146 _retval
.SetIsVoid(PR_TRUE
);
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
);
156 mozStorageStatementRowValueArray::GetString(PRUint32 aIndex
, nsAString
& _retval
)
158 // GetTypeOfIndex will check aIndex for us, so we don't have to.
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
165 _retval
.SetIsVoid(PR_TRUE
);
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);
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
;
194 mozStorageStatementRowValueArray::GetIsNull(PRUint32 aIndex
, PRBool
*_retval
)
196 // GetTypeOfIndex will check aIndex for us, so we don't have to.
198 nsresult rv
= GetTypeOfIndex (aIndex
, &t
);
199 NS_ENSURE_SUCCESS(rv
, rv
);
201 if (t
== VALUE_TYPE_NULL
)
210 mozStorageStatementRowValueArray::GetSharedUTF8String(PRUint32 aIndex
, PRUint32
*aLength
, const char **_retval
)
213 int slen
= sqlite3_column_bytes (mSqliteStatement
, aIndex
);
217 *_retval
= (const char*) sqlite3_column_text (mSqliteStatement
, aIndex
);
222 mozStorageStatementRowValueArray::GetSharedString(PRUint32 aIndex
, PRUint32
*aLength
, const PRUnichar
**_retval
)
225 int slen
= sqlite3_column_bytes16 (mSqliteStatement
, aIndex
);
229 *_retval
= (const PRUnichar
*) sqlite3_column_text16 (mSqliteStatement
, aIndex
);
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
);
244 *** mozStorageArgvValueArray
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; */
262 mozStorageArgvValueArray::GetNumEntries(PRUint32
*aLength
)
268 /* long getTypeOfIndex (in unsigned long aIndex); */
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
]);
278 *_retval
= VALUE_TYPE_INTEGER
;
281 *_retval
= VALUE_TYPE_FLOAT
;
284 *_retval
= VALUE_TYPE_TEXT
;
287 *_retval
= VALUE_TYPE_BLOB
;
290 *_retval
= VALUE_TYPE_NULL
;
294 return NS_ERROR_FAILURE
;
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
]);
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
]);
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
]);
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
342 _retval
.SetIsVoid(PR_TRUE
);
344 int slen
= sqlite3_value_bytes (mArgv
[aIndex
]);
345 const unsigned char *cstr
= sqlite3_value_text (mArgv
[aIndex
]);
346 _retval
.Assign ((char *) cstr
, slen
);
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
360 _retval
.SetIsVoid(PR_TRUE
);
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);
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
;
388 /* boolean getIsNull (in unsigned long aIndex); */
390 mozStorageArgvValueArray::GetIsNull(PRUint32 aIndex
, PRBool
*_retval
)
392 // GetTypeOfIndex will check aIndex for us, so we don't have to.
394 nsresult rv
= GetTypeOfIndex (aIndex
, &t
);
395 NS_ENSURE_SUCCESS(rv
, rv
);
397 if (t
== VALUE_TYPE_NULL
)
406 mozStorageArgvValueArray::GetSharedUTF8String(PRUint32 aIndex
, PRUint32
*aLength
, const char **_retval
)
409 int slen
= sqlite3_value_bytes (mArgv
[aIndex
]);
413 *_retval
= (const char*) sqlite3_value_text (mArgv
[aIndex
]);
418 mozStorageArgvValueArray::GetSharedString(PRUint32 aIndex
, PRUint32
*aLength
, const PRUnichar
**_retval
)
421 int slen
= sqlite3_value_bytes16 (mArgv
[aIndex
]);
425 *_retval
= (const PRUnichar
*) sqlite3_value_text16 (mArgv
[aIndex
]);
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
]);