kexi
mysqlconnection_p.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00071
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
00104 hostName = "127.0.0.1";
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();
00116 db_disconnect();
00117
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
00136 return executeSQL("USE " + dbName);
00137 }
00138
00141 bool MySqlConnectionInternal::executeSQL(const QString& statement) {
00142
00143
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
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
|