kexi

mysqlconnection_p.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00003    Copyright (C) 2004 Martin Ellis <martin.ellis@kdemail.net>
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public License
00016 along with this program; see the file COPYING.  If not, write to
00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qcstring.h>
00022 #include <qstring.h>
00023 #include <qstringlist.h>
00024 #include <qfile.h>
00025 
00026 #include <kdebug.h>
00027 
00028 #include "mysqlconnection_p.h"
00029 
00030 #include <kexidb/connectiondata.h>
00031 
00032 #ifdef MYSQLMIGRATE_H
00033 #define NAMESPACE KexiMigration
00034 #else
00035 #define NAMESPACE KexiDB
00036 #endif
00037 
00038 using namespace NAMESPACE;
00039 
00040 /* ************************************************************************** */
00041 MySqlConnectionInternal::MySqlConnectionInternal(KexiDB::Connection* connection)
00042     : ConnectionInternal(connection)
00043     , mysql(0)
00044     , mysql_owned(true)
00045     , res(0)
00046 {
00047 }
00048 
00049 MySqlConnectionInternal::~MySqlConnectionInternal()
00050 {
00051     if (mysql_owned && mysql) {
00052         mysql_close(mysql);
00053         mysql = 0;
00054     }
00055 }
00056 
00057 void MySqlConnectionInternal::storeResult()
00058 {
00059     res = mysql_errno(mysql);
00060     errmsg = mysql_error(mysql);
00061 }
00062 
00063 /* ************************************************************************** */
00070 //bool MySqlConnectionInternal::db_connect(QCString host, QCString user,
00071 //  QCString password, unsigned short int port, QString socket)
00072 bool MySqlConnectionInternal::db_connect(const KexiDB::ConnectionData& data)
00073 {
00074     if (!(mysql = mysql_init(mysql)))
00075         return false;
00076 
00077     KexiDBDrvDbg << "MySqlConnectionInternal::connect()" << endl;
00078     QCString localSocket;
00079     QString hostName = data.hostName;
00080     if (hostName.isEmpty() || hostName.lower()=="localhost") {
00081         if (data.useLocalSocketFile) {
00082             if (data.localSocketFileName.isEmpty()) {
00084                 QStringList sockets;
00085     #ifndef Q_WS_WIN
00086                 sockets.append("/var/lib/mysql/mysql.sock");
00087                 sockets.append("/var/run/mysqld/mysqld.sock");
00088                 sockets.append("/tmp/mysql.sock");
00089         
00090                 for(QStringList::ConstIterator it = sockets.constBegin(); it != sockets.constEnd(); it++)
00091                 {
00092                     if(QFile(*it).exists()) {
00093                         localSocket = ((QString)(*it)).local8Bit();
00094                         break;
00095                     }
00096                 }
00097     #endif
00098             }
00099             else
00100                 localSocket = QFile::encodeName(data.localSocketFileName);
00101         }
00102         else {
00103             //we're not using local socket
00104             hostName = "127.0.0.1"; //this will force mysql to connect to localhost
00105         }
00106     }
00107 
00109     const char *pwd = data.password.isNull() ? 0 : data.password.latin1();
00110     mysql_real_connect(mysql, hostName.latin1(), data.userName.latin1(), 
00111         pwd, 0, data.port, localSocket, 0);
00112     if(mysql_errno(mysql) == 0)
00113         return true;
00114 
00115     storeResult(); //store error msg, if any - can be destroyed after disconnect()
00116     db_disconnect();
00117 //  setError(ERR_DB_SPECIFIC,err);
00118     return false;
00119 }
00120 
00123 bool MySqlConnectionInternal::db_disconnect()
00124 {
00125     mysql_close(mysql);
00126     mysql = 0;
00127     KexiDBDrvDbg << "MySqlConnection::disconnect()" << endl;
00128     return true;
00129 }
00130 
00131 /* ************************************************************************** */
00134 bool MySqlConnectionInternal::useDatabase(const QString &dbName) {
00135 //TODO is here escaping needed?
00136     return executeSQL("USE " + dbName);
00137 }
00138 
00141 bool MySqlConnectionInternal::executeSQL(const QString& statement) {
00142 //  KexiDBDrvDbg << "MySqlConnectionInternal::executeSQL: "
00143 //               << statement << endl;
00144     QCString queryStr=statement.utf8();
00145     const char *query=queryStr;
00146     if(mysql_real_query(mysql, query, strlen(query)) == 0)
00147     {
00148         return true;
00149     }
00150 
00151     storeResult();
00152 //  setError(ERR_DB_SPECIFIC,mysql_error(m_mysql));
00153     return false;
00154 }
00155 
00156 QString MySqlConnectionInternal::escapeIdentifier(const QString& str) const {
00157     return QString(str).replace('`', "'");
00158 }
00159 
00160 //--------------------------------------
00161 
00162 MySqlCursorData::MySqlCursorData(KexiDB::Connection* connection)
00163 : MySqlConnectionInternal(connection)
00164 , mysqlres(0)
00165 , mysqlrow(0)
00166 , lengths(0)
00167 , numRows(0)
00168 {
00169     mysql_owned = false;
00170 }
00171 
00172 MySqlCursorData::~MySqlCursorData()
00173 {
00174 }
00175 
KDE Home | KDE Accessibility Home | Description of Access Keys