Back to index

lightning-sunbird  0.9+nobinonly
mozSqlResultSqlite.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is Jan Varga
00017  * Portions created by the Initial Developer are Copyright (C) 2003
00018  * the Initial Developer. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *   Valia Vaneeva <fattie@altlinux.ru>
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsReadableUtils.h"
00038 #include "mozSqlResultSqlite.h"
00039 
00040 mozSqlResultSqlite::mozSqlResultSqlite(mozISqlConnection* aConnection,
00041                                        const nsAString& aQuery)
00042   : mozSqlResult(aConnection, aQuery),
00043     mResult(nsnull),
00044     mColumnCount(0),
00045     mRowCount(0),
00046     mWritable(PR_FALSE)
00047 {
00048 }
00049 
00050 void
00051 mozSqlResultSqlite::SetResult(char** aResult, PRInt32 aRowCount,
00052                               PRInt32 aColumnCount, PRBool aWritable)
00053 {
00054   mResult = aResult;
00055   mColumnCount = aColumnCount;
00056   mRowCount = aRowCount;
00057   mWritable = aWritable;
00058 }
00059 
00060 mozSqlResultSqlite::~mozSqlResultSqlite()
00061 {
00062   ClearNativeResult();
00063 }
00064 
00065 NS_IMPL_ADDREF_INHERITED(mozSqlResultSqlite, mozSqlResult)
00066 NS_IMPL_RELEASE_INHERITED(mozSqlResultSqlite, mozSqlResult)
00067 
00068 // QueryInterface
00069 NS_INTERFACE_MAP_BEGIN(mozSqlResultSqlite)
00070   NS_INTERFACE_MAP_ENTRY(mozISqlResultSqlite)
00071 NS_INTERFACE_MAP_END_INHERITING(mozSqlResult)
00072 
00073 PRInt32
00074 mozSqlResultSqlite::GetColType(PRInt32 aColumnIndex)
00075 {
00076   return mozISqlResult::TYPE_STRING;
00077 }
00078 
00079 nsresult
00080 mozSqlResultSqlite::BuildColumnInfo()
00081 {
00082   for (PRInt32 i = 0; i < mColumnCount; i++) {
00083     char* n = mResult[i];
00084     PRUnichar* name = UTF8ToNewUnicode(nsDependentCString(n));
00085     PRInt32 type = GetColType(i);
00086     PRInt32 size = -1;
00087     PRInt32 mod = -1;
00088 
00089     nsCAutoString uri(NS_LITERAL_CSTRING("http://www.mozilla.org/SQL-rdf#"));
00090     uri.Append(n);
00091     nsCOMPtr<nsIRDFResource> property;
00092     gRDFService->GetResource(uri, getter_AddRefs(property));
00093 
00094     ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type, size,
00095                                                 mod, PR_FALSE, property);
00096     mColumnInfo.AppendElement(columnInfo); 
00097   }
00098 
00099   return NS_OK;
00100 }
00101 
00102 nsresult
00103 mozSqlResultSqlite::BuildRows()
00104 {
00105   for(PRInt32 i = 1; i <= mRowCount; i++) {
00106     nsCOMPtr<nsIRDFResource> resource;
00107     nsresult rv = gRDFService->GetAnonymousResource(getter_AddRefs(resource));
00108     if (NS_FAILED(rv)) return rv;
00109 
00110     Row* row = Row::Create(mAllocator, resource, mColumnInfo);
00111 
00112     for (PRInt32 j = 0; j < mColumnInfo.Count(); j++) {
00113       if (mResult[j+i*mColumnCount]) {
00114         char* value = mResult[j+i*mColumnCount];
00115         Cell* cell = row->mCells[j];
00116         cell->SetNull(PR_FALSE);
00117         cell->SetString(UTF8ToNewUnicode(nsDependentCString(value)));
00118       }
00119     }
00120 
00121     mRows.AppendElement(row);
00122     nsVoidKey key(resource);
00123     mSources.Put(&key, row);
00124   }
00125 
00126   return NS_OK;
00127 }
00128 
00129 void
00130 mozSqlResultSqlite::ClearNativeResult()
00131 {
00132   if (mResult) {
00133     sqlite3_free_table(mResult);
00134     mResult = nsnull;
00135   }
00136 }
00137 
00138 nsresult
00139 mozSqlResultSqlite::CanInsert(PRBool* _retval)
00140 {
00141   *_retval = mWritable;
00142 
00143   return NS_OK;
00144 }
00145 
00146 nsresult
00147 mozSqlResultSqlite::CanUpdate(PRBool* _retval)
00148 {
00149   *_retval = mWritable;
00150 
00151   return NS_OK;
00152 }
00153 
00154 nsresult
00155 mozSqlResultSqlite::CanDelete(PRBool* _retval)
00156 {
00157   *_retval = mWritable;
00158 
00159   return NS_OK;
00160 }