Back to index

lightning-sunbird  0.9+nobinonly
mozSqlConnectionMysql.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 "nsReadableUtils.h"
00037 #include "mozSqlConnectionMysql.h"
00038 #include "mozSqlResultMysql.h"
00039 
00040 mozSqlConnectionMysql::mozSqlConnectionMysql()
00041   : mConnection(nsnull)
00042 {
00043 }
00044 
00045 mozSqlConnectionMysql::~mozSqlConnectionMysql()
00046 {
00047   if (mConnection)
00048     mysql_close(mConnection);
00049 }
00050 
00051 NS_IMPL_ADDREF_INHERITED(mozSqlConnectionMysql, mozSqlConnection)
00052 NS_IMPL_RELEASE_INHERITED(mozSqlConnectionMysql, mozSqlConnection)
00053 
00054 // QueryInterface
00055 NS_INTERFACE_MAP_BEGIN(mozSqlConnectionMysql)
00056   NS_INTERFACE_MAP_ENTRY(mozISqlConnectionMysql)
00057 NS_INTERFACE_MAP_END_INHERITING(mozSqlConnection)
00058 
00059 NS_IMETHODIMP
00060 mozSqlConnectionMysql::Init(const nsAString &aHost, PRInt32 aPort,
00061                             const nsAString &aDatabase, const nsAString &aUsername,
00062                             const nsAString &aPassword)
00063 {
00064   if (mConnection)
00065     return NS_OK;
00066 
00067   if (aPort == -1)
00068     aPort = 0;
00069 
00070   mConnection = mysql_init((MYSQL *) nsnull);
00071   if (!mConnection){
00072     return NS_ERROR_FAILURE;
00073   }
00074 
00075   if (!mysql_real_connect(mConnection, 
00076                           NS_ConvertUCS2toUTF8(aHost).get(),
00077                           NS_ConvertUCS2toUTF8(aUsername).get(),
00078                           NS_ConvertUCS2toUTF8(aPassword).get(),
00079                           NS_ConvertUCS2toUTF8(aDatabase).get(),
00080                           aPort, nsnull, 0)){
00081     mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection)));
00082     mConnection = nsnull;
00083 
00084     return NS_ERROR_FAILURE;
00085   }
00086 
00087   return NS_OK;
00088 }
00089 
00090 NS_IMETHODIMP
00091 mozSqlConnectionMysql::GetServerVersion(nsAString &aServerVersion)
00092 {
00093   if (!mConnection)
00094     return NS_ERROR_NOT_INITIALIZED;
00095 
00096   if (mServerVersion.IsEmpty()){
00097     mServerVersion.Assign(NS_ConvertUTF8toUCS2(mysql_get_server_info(mConnection)));
00098   }
00099   aServerVersion.Assign(mServerVersion);
00100 
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 mozSqlConnectionMysql::GetPrimaryKeys(const nsAString& aSchema,
00106                                       const nsAString& aTable,
00107                                       mozISqlResult** aResult)
00108 {
00109   // XXX this can be done with 'show columns from <table>', but it can't be
00110   //     filtered just for primary keys, which are listed in column 3
00111 
00112   return NS_ERROR_NOT_IMPLEMENTED;
00113 }
00114 
00115 nsresult
00116 mozSqlConnectionMysql::RealExec(const nsAString& aQuery,
00117                                 mozISqlResult** aResult, PRInt32* aAffectedRows)
00118 {
00119   if (!mConnection)
00120     return NS_ERROR_NOT_INITIALIZED;
00121 
00122   if (mysql_query(mConnection, NS_ConvertUCS2toUTF8(aQuery).get())){
00123     mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection)));
00124 
00125     return NS_ERROR_FAILURE;
00126   }
00127 
00128   if (!aResult){
00129     if (!aAffectedRows)
00130       return NS_ERROR_NULL_POINTER;
00131 
00132     my_ulonglong numrows = (PRInt32)mysql_affected_rows(mConnection);
00133     *aAffectedRows = ((numrows == (my_ulonglong)-1) ? 0 : (PRInt32)numrows);
00134 
00135     return NS_OK;
00136   }
00137   
00138   *aResult = nsnull;
00139 
00140   MYSQL_RES *rowresults = mysql_store_result(mConnection);
00141   if (!rowresults){
00142     mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection)));
00143 
00144     return NS_ERROR_FAILURE;
00145   }
00146 
00147   mozSqlResult *mozresult = new mozSqlResultMysql(this, aQuery);
00148   if (!mozresult){
00149     mysql_free_result(rowresults);
00150     return NS_ERROR_OUT_OF_MEMORY;
00151   }
00152 
00153   ((mozSqlResultMysql*)mozresult)->SetResult(rowresults);
00154   nsresult rv = mozresult->Init();
00155   if (NS_FAILED(rv)){
00156     delete mozresult;
00157     return rv;
00158   }
00159 
00160   *aResult = mozresult;
00161   NS_ADDREF(*aResult);
00162 
00163   return NS_OK;
00164 }
00165 
00166 nsresult
00167 mozSqlConnectionMysql::CancelExec()
00168 {
00169   unsigned long id = mysql_thread_id(mConnection);
00170   mysql_kill(mConnection, id);
00171 
00172   if (mysql_errno(mConnection)) {
00173     mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection)));
00174     return NS_ERROR_FAILURE;
00175   }
00176 
00177   mysql_ping(mConnection);
00178 
00179   return NS_OK;
00180 }
00181 
00182 nsresult
00183 mozSqlConnectionMysql::GetIDName(nsAString& aIDName)
00184 {
00185   aIDName.Truncate();
00186 
00187   return NS_OK;
00188 }