Back to index

lightning-sunbird  0.9+nobinonly
mozSqlResultMysql.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 Neil Deakin
00017  * Portions created by the Initial Developer are Copyright (C) 2004
00018  * the Initial Developer. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *
00022  * Alternatively, the contents of this file may be used under the terms of
00023  * either the GNU General Public License Version 2 or later (the "GPL"), or
00024  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00025  * in which case the provisions of the GPL or the LGPL are applicable instead
00026  * of those above. If you wish to allow use of your version of this file only
00027  * under the terms of either the GPL or the LGPL, and not to allow others to
00028  * use your version of this file under the terms of the MPL, indicate your
00029  * decision by deleting the provisions above and replace them with the notice
00030  * and other provisions required by the GPL or the LGPL. If you do not delete
00031  * the provisions above, a recipient may use your version of this file under
00032  * the terms of any one of the MPL, the GPL or the LGPL.
00033  *
00034  * ***** END LICENSE BLOCK ***** */
00035 
00036 #include "prprf.h"
00037 #include "nsReadableUtils.h"
00038 #include "mozSqlResultMysql.h"
00039 
00040 mozSqlResultMysql::mozSqlResultMysql(mozISqlConnection* aConnection,
00041                                      const nsAString& aQuery)
00042   : mozSqlResult(aConnection, aQuery),
00043     mResult(nsnull)
00044 {
00045 }
00046 
00047 void
00048 mozSqlResultMysql::SetResult(MYSQL_RES *aResult)
00049 {
00050   if (mResult){
00051     mysql_free_result(mResult);
00052   }
00053 
00054   mResult = aResult;
00055 }
00056 
00057 mozSqlResultMysql::~mozSqlResultMysql()
00058 {
00059   ClearNativeResult();
00060 }
00061 
00062 NS_IMPL_ADDREF_INHERITED(mozSqlResultMysql, mozSqlResult)
00063 NS_IMPL_RELEASE_INHERITED(mozSqlResultMysql, mozSqlResult)
00064 
00065 // QueryInterface
00066 NS_INTERFACE_MAP_BEGIN(mozSqlResultMysql)
00067   NS_INTERFACE_MAP_ENTRY(mozISqlResultMysql)
00068 NS_INTERFACE_MAP_END_INHERITING(mozSqlResult)
00069 
00070 PRInt32
00071 mozSqlResultMysql::GetColType(MYSQL_FIELD *aField)
00072 {
00073   switch (aField->type){
00074     case FIELD_TYPE_TINY:
00075     case FIELD_TYPE_SHORT:
00076     case FIELD_TYPE_LONG:
00077     case FIELD_TYPE_INT24:
00078     case FIELD_TYPE_LONGLONG:
00079       return mozISqlResult::TYPE_INT;
00080 
00081     case FIELD_TYPE_DECIMAL:
00082       return mozISqlResult::TYPE_DECIMAL;
00083 
00084     case FIELD_TYPE_FLOAT:
00085     case FIELD_TYPE_DOUBLE:
00086       return mozISqlResult::TYPE_FLOAT;
00087 
00088     case FIELD_TYPE_DATE:
00089       return mozISqlResult::TYPE_DATE;
00090 
00091     case FIELD_TYPE_TIME:
00092       return mozISqlResult::TYPE_TIME;
00093 
00094     case FIELD_TYPE_DATETIME:
00095       return mozISqlResult::TYPE_DATETIME;
00096 
00097     default:
00098       // handles these types:
00099       //   FIELD_TYPE_TIMESTAMP, FIELD_TYPE_YEAR, FIELD_TYPE_STRING,
00100       //   FIELD_TYPE_VAR_STRING, FIELD_TYPE_BLOB, FIELD_TYPE_SET,
00101       //   FIELD_TYPE_ENUM, FIELD_TYPE_NULL, FIELD_TYPE_CHAR
00102 
00103       return mozISqlResult::TYPE_STRING;
00104   }
00105 }
00106 
00107 nsresult
00108 mozSqlResultMysql::BuildColumnInfo()
00109 {
00110   MYSQL_FIELD *field;
00111 
00112   while ((field = mysql_fetch_field(mResult))){
00113     PRUnichar* name = UTF8ToNewUnicode(nsDependentCString(field->name));
00114     PRInt32 type = GetColType(field);
00115 
00116     nsCAutoString uri(NS_LITERAL_CSTRING("http://www.mozilla.org/SQL-rdf#"));
00117     uri.Append(field->name);
00118 
00119     nsCOMPtr<nsIRDFResource> property;
00120     gRDFService->GetResource(uri, getter_AddRefs(property));
00121 
00122     ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type,
00123                                                 (PRInt32)field->length, (PRInt32)field->length,
00124                                                 (field->flags & PRI_KEY_FLAG), property);
00125     mColumnInfo.AppendElement(columnInfo); 
00126   }
00127 
00128   return NS_OK;
00129 }
00130 
00131 nsresult
00132 mozSqlResultMysql::BuildRows()
00133 {
00134   MYSQL_ROW resultrow;
00135   while ((resultrow = mysql_fetch_row(mResult))){
00136     nsCOMPtr<nsIRDFResource> resource;
00137     nsresult rv = gRDFService->GetAnonymousResource(getter_AddRefs(resource));
00138     if (NS_FAILED(rv)) return rv;
00139 
00140     Row* row = Row::Create(mAllocator, resource, mColumnInfo);
00141 
00142     for (PRInt32 j = 0; j < mColumnInfo.Count(); j++) {
00143       char* value = resultrow[j];
00144       if (value){
00145         Cell* cell = row->mCells[j];
00146         cell->SetNull(PR_FALSE);
00147         PRInt32 type = cell->GetType();
00148         if (type == mozISqlResult::TYPE_STRING)
00149           cell->SetString(UTF8ToNewUnicode(nsDependentCString(value)));
00150         else if (type == mozISqlResult::TYPE_INT)
00151           PR_sscanf(value, "%d", &cell->mInt);
00152         else if (type == mozISqlResult::TYPE_FLOAT)
00153           PR_sscanf(value, "%f", &cell->mFloat);
00154         else if (type == mozISqlResult::TYPE_DECIMAL)
00155           PR_sscanf(value, "%f", &cell->mFloat);
00156         else if (type == mozISqlResult::TYPE_DATE ||
00157                  type == mozISqlResult::TYPE_TIME ||
00158                  type == mozISqlResult::TYPE_DATETIME)
00159           PR_ParseTimeString(value, PR_FALSE, &cell->mDate);
00160         else if (type == mozISqlResult::TYPE_BOOL)
00161           cell->mBool = !strcmp(value, "t");
00162       }
00163     }
00164 
00165     mRows.AppendElement(row);
00166     nsVoidKey key(resource);
00167     mSources.Put(&key, row);
00168   }
00169 
00170   return NS_OK;
00171 }
00172 
00173 void
00174 mozSqlResultMysql::ClearNativeResult()
00175 {
00176   if (mResult) {
00177     mysql_free_result(mResult);
00178     mResult = nsnull;
00179   }
00180 }
00181 
00182 nsresult
00183 mozSqlResultMysql::EnsurePrimaryKeys()
00184 {
00185   return NS_OK;
00186 }
00187 
00188 nsresult
00189 mozSqlResultMysql::AppendKeys(Row* aRow, nsAutoString& aKeys)
00190 {
00191   PRInt32 i;
00192   for (i = 0; i < mColumnInfo.Count(); i++) {
00193     if (((ColumnInfo*)mColumnInfo[i])->mIsPrimaryKey){
00194       if (i){
00195         aKeys.Append(NS_LITERAL_STRING(" AND "));
00196       }
00197       aKeys.Append(((ColumnInfo*)mColumnInfo[i])->mName);
00198       aKeys.Append(PRUnichar('='));
00199 
00200       Cell* cell = aRow->mCells[i];
00201       AppendValue(cell, aKeys);
00202     }
00203   }
00204 
00205   return NS_OK;
00206 }
00207 
00208 nsresult
00209 mozSqlResultMysql::CanInsert(PRBool* _retval)
00210 {
00211   return PR_TRUE;
00212 }
00213 
00214 nsresult
00215 mozSqlResultMysql::CanUpdate(PRBool* _retval)
00216 {
00217   return PR_TRUE;
00218 }
00219 
00220 nsresult
00221 mozSqlResultMysql::CanDelete(PRBool* _retval)
00222 {
00223   return PR_TRUE;
00224 }