Back to index

lightning-sunbird  0.9+nobinonly
mozStorageValueArray.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Oracle Corporation code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  *  Oracle Corporation
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsError.h"
00040 #include "nsMemory.h"
00041 #include "nsString.h"
00042 
00043 #include "mozStorageValueArray.h"
00044 
00045 /***
00046  *** mozStorageStatementRowValueArray
00047  ***/
00048 
00049 /* Implementation file */
00050 NS_IMPL_ISUPPORTS1(mozStorageStatementRowValueArray, mozIStorageValueArray)
00051 
00052 mozStorageStatementRowValueArray::mozStorageStatementRowValueArray(sqlite3_stmt *aSqliteStmt)
00053 {
00054     mSqliteStatement = aSqliteStmt;
00055     mNumEntries = sqlite3_data_count (aSqliteStmt);
00056 }
00057 
00058 mozStorageStatementRowValueArray::~mozStorageStatementRowValueArray()
00059 {
00060     /* do nothing, we don't own the stmt */
00061 }
00062 
00063 /* readonly attribute unsigned long numEntries; */
00064 NS_IMETHODIMP
00065 mozStorageStatementRowValueArray::GetNumEntries(PRUint32 *aLength)
00066 {
00067     *aLength = mNumEntries;
00068     return NS_OK;
00069 }
00070 
00071 /* long getTypeOfIndex (in unsigned long aIndex); */
00072 NS_IMETHODIMP
00073 mozStorageStatementRowValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
00074 {
00075     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00076 
00077     int t = sqlite3_column_type (mSqliteStatement, aIndex);
00078     switch (t) {
00079         case SQLITE_INTEGER:
00080             *_retval = VALUE_TYPE_INTEGER;
00081             break;
00082         case SQLITE_FLOAT:
00083             *_retval = VALUE_TYPE_FLOAT;
00084             break;
00085         case SQLITE_TEXT:
00086             *_retval = VALUE_TYPE_TEXT;
00087             break;
00088         case SQLITE_BLOB:
00089             *_retval = VALUE_TYPE_BLOB;
00090             break;
00091         case SQLITE_NULL:
00092             *_retval = VALUE_TYPE_NULL;
00093             break;
00094         default:
00095             // ???
00096             return NS_ERROR_FAILURE;
00097     }
00098 
00099     return NS_OK;
00100 }
00101 
00102 NS_IMETHODIMP
00103 mozStorageStatementRowValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
00104 {
00105     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00106 
00107     *_retval = sqlite3_column_int (mSqliteStatement, aIndex);
00108 
00109     return NS_OK;
00110 }
00111 
00112 NS_IMETHODIMP
00113 mozStorageStatementRowValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
00114 {
00115     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00116 
00117     *_retval = sqlite3_column_int64 (mSqliteStatement, aIndex);
00118 
00119     return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 mozStorageStatementRowValueArray::GetDouble(PRUint32 aIndex, double *_retval)
00124 {
00125     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00126 
00127     *_retval = sqlite3_column_double (mSqliteStatement, aIndex);
00128 
00129     return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP
00133 mozStorageStatementRowValueArray::GetUTF8String(PRUint32 aIndex, nsACString &_retval)
00134 {
00135     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00136 
00137     PRInt32 type;
00138     nsresult rv = GetTypeOfIndex (aIndex, &type);
00139     NS_ENSURE_SUCCESS(rv, rv);
00140     if (type == SQLITE_NULL) {
00141         // null columns get IsVoid set to distinguish them from empty strings
00142         _retval.Truncate(0);
00143         _retval.SetIsVoid(PR_TRUE);
00144     } else {
00145         PRUint32 slen = (PRUint32) sqlite3_column_bytes (mSqliteStatement, aIndex);
00146         const char *cstr = (const char *) sqlite3_column_text (mSqliteStatement, aIndex);
00147         _retval.Assign(cstr, slen);
00148     }
00149     return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 mozStorageStatementRowValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
00154 {
00155     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00156 
00157     PRInt32 type;
00158     nsresult rv = GetTypeOfIndex (aIndex, &type);
00159     NS_ENSURE_SUCCESS(rv, rv);
00160     if (type == SQLITE_NULL) {
00161         // null columns get IsVoid set to distinguish them from empty strings
00162         _retval.Truncate(0);
00163         _retval.SetIsVoid(PR_TRUE);
00164     } else {
00165         int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex);
00166         const PRUnichar *wstr = (const PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex);
00167         _retval.Assign (wstr, slen/2);
00168     }
00169     return NS_OK;
00170 }
00171 
00172 NS_IMETHODIMP
00173 mozStorageStatementRowValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
00174 {
00175     NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
00176 
00177     int blobsize = sqlite3_column_bytes (mSqliteStatement, aIndex);
00178     const void *blob = sqlite3_column_blob (mSqliteStatement, aIndex);
00179 
00180     void *blobcopy = nsMemory::Clone(blob, blobsize);
00181     if (blobcopy == NULL)
00182         return NS_ERROR_OUT_OF_MEMORY;
00183 
00184     *aData = (PRUint8*) blobcopy;
00185     *aDataSize = blobsize;
00186 
00187     return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP
00191 mozStorageStatementRowValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
00192 {
00193     PRInt32 t;
00194     nsresult rv = GetTypeOfIndex (aIndex, &t);
00195     if (NS_FAILED(rv))
00196         return rv;
00197 
00198     if (t == VALUE_TYPE_NULL)
00199         *_retval = PR_TRUE;
00200     else
00201         *_retval = PR_FALSE;
00202 
00203     return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 mozStorageStatementRowValueArray::GetSharedUTF8String(PRUint32 aIndex, PRUint32 *aLength, const char **_retval)
00208 {
00209     if (aLength) {
00210         int slen = sqlite3_column_bytes (mSqliteStatement, aIndex);
00211         *aLength = slen;
00212     }
00213 
00214     *_retval = (const char*) sqlite3_column_text (mSqliteStatement, aIndex);
00215     return NS_OK;
00216 }
00217 
00218 NS_IMETHODIMP
00219 mozStorageStatementRowValueArray::GetSharedString(PRUint32 aIndex, PRUint32 *aLength, const PRUnichar **_retval)
00220 {
00221     if (aLength) {
00222         int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex);
00223         *aLength = slen;
00224     }
00225 
00226     *_retval = (const PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex);
00227     return NS_OK;
00228 }
00229 
00230 NS_IMETHODIMP
00231 mozStorageStatementRowValueArray::GetSharedBlob(PRUint32 aIndex, PRUint32 *aDataSize, const PRUint8 **aData)
00232 {
00233     *aDataSize = sqlite3_column_bytes (mSqliteStatement, aIndex);
00234     *aData = (const PRUint8*) sqlite3_column_blob (mSqliteStatement, aIndex);
00235 
00236     return NS_OK;
00237 }
00238 
00239 
00240 /***
00241  *** mozStorageArgvValueArray
00242  ***/
00243 
00244 /* Implementation file */
00245 NS_IMPL_ISUPPORTS1(mozStorageArgvValueArray, mozIStorageValueArray)
00246 
00247 mozStorageArgvValueArray::mozStorageArgvValueArray(PRInt32 aArgc, sqlite3_value **aArgv)
00248     : mArgc(aArgc), mArgv(aArgv)
00249 {
00250 }
00251 
00252 mozStorageArgvValueArray::~mozStorageArgvValueArray()
00253 {
00254     /* do nothing, we don't own the array */
00255 }
00256 
00257 /* readonly attribute unsigned long length; */
00258 NS_IMETHODIMP
00259 mozStorageArgvValueArray::GetNumEntries(PRUint32 *aLength)
00260 {
00261     *aLength = mArgc;
00262     return NS_OK;
00263 }
00264 
00265 /* long getTypeOfIndex (in unsigned long aIndex); */
00266 NS_IMETHODIMP
00267 mozStorageArgvValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
00268 {
00269     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00270 
00271     int t = sqlite3_value_type (mArgv[aIndex]);
00272     switch (t) {
00273         case SQLITE_INTEGER:
00274             *_retval = VALUE_TYPE_INTEGER;
00275             break;
00276         case SQLITE_FLOAT:
00277             *_retval = VALUE_TYPE_FLOAT;
00278             break;
00279         case SQLITE_TEXT:
00280             *_retval = VALUE_TYPE_TEXT;
00281             break;
00282         case SQLITE_BLOB:
00283             *_retval = VALUE_TYPE_BLOB;
00284             break;
00285         case SQLITE_NULL:
00286             *_retval = VALUE_TYPE_NULL;
00287             break;
00288         default:
00289             // ???
00290             return NS_ERROR_FAILURE;
00291     }
00292 
00293     return NS_OK;
00294 }
00295 
00296 NS_IMETHODIMP
00297 mozStorageArgvValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
00298 {
00299     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00300 
00301     *_retval = sqlite3_value_int (mArgv[aIndex]);
00302 
00303     return NS_OK;
00304 }
00305 
00306 NS_IMETHODIMP
00307 mozStorageArgvValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
00308 {
00309     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00310 
00311     *_retval = sqlite3_value_int64 (mArgv[aIndex]);
00312 
00313     return NS_OK;
00314 }
00315 
00316 NS_IMETHODIMP
00317 mozStorageArgvValueArray::GetDouble(PRUint32 aIndex, double *_retval)
00318 {
00319     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00320 
00321     *_retval = sqlite3_value_double (mArgv[aIndex]);
00322 
00323     return NS_OK;
00324 }
00325 
00326 NS_IMETHODIMP
00327 mozStorageArgvValueArray::GetUTF8String(PRUint32 aIndex, nsACString & _retval)
00328 {
00329     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00330 
00331     if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
00332         // null columns get IsVoid set to distinguish them from empty strings
00333         _retval.Truncate(0);
00334         _retval.SetIsVoid(PR_TRUE);
00335     } else {
00336         int slen = sqlite3_value_bytes (mArgv[aIndex]);
00337         const unsigned char *cstr = sqlite3_value_text (mArgv[aIndex]);
00338         _retval.Assign ((char *) cstr, slen);
00339     }
00340     return NS_OK;
00341 }
00342 
00343 NS_IMETHODIMP
00344 mozStorageArgvValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
00345 {
00346     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00347 
00348     if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
00349         // null columns get IsVoid set to distinguish them from empty strings
00350         _retval.Truncate(0);
00351         _retval.SetIsVoid(PR_TRUE);
00352     } else {
00353         int slen = sqlite3_value_bytes16 (mArgv[aIndex]);
00354         const PRUnichar *wstr = (const PRUnichar *) sqlite3_value_text16 (mArgv[aIndex]);
00355         _retval.Assign (wstr, slen);
00356     }
00357     return NS_OK;
00358 }
00359 
00360 NS_IMETHODIMP
00361 mozStorageArgvValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
00362 {
00363     NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
00364 
00365     int blobsize = sqlite3_value_bytes (mArgv[aIndex]);
00366     const void *blob = sqlite3_value_blob (mArgv[aIndex]);
00367 
00368     void *blobcopy = nsMemory::Clone(blob, blobsize);
00369     if (blobcopy == NULL)
00370         return NS_ERROR_OUT_OF_MEMORY;
00371 
00372     *aData = (PRUint8*) blobcopy;
00373     *aDataSize = blobsize;
00374 
00375     return NS_OK;
00376 }
00377 
00378 /* boolean getIsNull (in unsigned long aIndex); */
00379 NS_IMETHODIMP
00380 mozStorageArgvValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
00381 {
00382     PRInt32 t;
00383     nsresult rv = GetTypeOfIndex (aIndex, &t);
00384     if (NS_FAILED(rv))
00385         return rv;
00386 
00387     if (t == VALUE_TYPE_NULL)
00388         *_retval = PR_TRUE;
00389     else
00390         *_retval = PR_FALSE;
00391 
00392     return NS_OK;
00393 }
00394 
00395 NS_IMETHODIMP
00396 mozStorageArgvValueArray::GetSharedUTF8String(PRUint32 aIndex, PRUint32 *aLength, const char **_retval)
00397 {
00398     if (aLength) {
00399         int slen = sqlite3_value_bytes (mArgv[aIndex]);
00400         *aLength = slen;
00401     }
00402 
00403     *_retval = (const char*) sqlite3_value_text (mArgv[aIndex]);
00404     return NS_OK;
00405 }
00406 
00407 NS_IMETHODIMP
00408 mozStorageArgvValueArray::GetSharedString(PRUint32 aIndex, PRUint32 *aLength, const PRUnichar **_retval)
00409 {
00410     if (aLength) {
00411         int slen = sqlite3_value_bytes16 (mArgv[aIndex]);
00412         *aLength = slen;
00413     }
00414 
00415     *_retval = (const PRUnichar*) sqlite3_value_text16 (mArgv[aIndex]);
00416     return NS_OK;
00417 }
00418 
00419 NS_IMETHODIMP
00420 mozStorageArgvValueArray::GetSharedBlob(PRUint32 aIndex, PRUint32 *aDataSize, const PRUint8 **aData)
00421 {
00422     *aDataSize = sqlite3_value_bytes (mArgv[aIndex]);
00423     *aData = (const PRUint8*) sqlite3_value_blob (mArgv[aIndex]);
00424 
00425     return NS_OK;
00426 }