kexi

mysqlconnection.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
00003                       Daniel Molkentin <molkentin@kde.org>
00004    Copyright (C) 2003 Joseph Wenninger<jowenn@kde.org>
00005    Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
00006 
00007 This program is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Library General Public
00009 License as published by the Free Software Foundation; either
00010 version 2 of the License, or (at your option) any later version.
00011 
00012 This program is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Library General Public License for more details.
00016 
00017 You should have received a copy of the GNU Library General Public License
00018 along with this program; see the file COPYING.  If not, write to
00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <qvariant.h>
00024 #include <qfile.h>
00025 #include <qdict.h>
00026 #include <qregexp.h>
00027 
00028 #include <kgenericfactory.h>
00029 #include <kdebug.h>
00030 
00031 #include "mysqldriver.h"
00032 #include "mysqlconnection.h"
00033 #include "mysqlconnection_p.h"
00034 #include "mysqlcursor.h"
00035 #include "mysqlpreparedstatement.h"
00036 #include <kexidb/error.h>
00037 
00038 
00039 using namespace KexiDB;
00040 
00041 //--------------------------------------------------------------------------
00042 
00043 MySqlConnection::MySqlConnection( Driver *driver, ConnectionData &conn_data )
00044     :Connection(driver,conn_data)
00045     ,d(new MySqlConnectionInternal(this))
00046 {
00047 }
00048 
00049 MySqlConnection::~MySqlConnection() {
00050     destroy();
00051 }
00052 
00053 bool MySqlConnection::drv_connect(KexiDB::ServerVersionInfo& version)
00054 {
00055     const bool ok = d->db_connect(*data());
00056     if (!ok)
00057         return false;
00058 
00059     version.string = mysql_get_host_info(d->mysql);
00060 
00061     //retrieve server version info
00062 #if 0 //this only works for client version >= 4.1 :(
00063     unsigned long v = mysql_get_server_version(d->mysql);
00064     // v - a number that represents the MySQL server version in this format
00065     // = major_version*10000 + minor_version *100 + sub_version
00066     version.major = v/10000;
00067     version.minor = (v - version.major*10000)/100;
00068     version.release = v - version.major*10000 - version.minor*100;
00069 #else //better way to get the version info: use 'version' built-in variable:
00071     QString versionString;
00072     const tristate res = querySingleString("SELECT @@version", versionString, /*column*/0, false );
00073     QRegExp versionRe("(\\d+)\\.(\\d+)\\.(\\d+)");
00074     if (res==true && versionRe.exactMatch(versionString)) { // (if querySingleString failed, the version will be 0.0.0...
00075         version.major = versionRe.cap(1).toInt();
00076         version.minor = versionRe.cap(2).toInt();
00077         version.release = versionRe.cap(3).toInt();
00078     }
00079 #endif
00080     return true;
00081 }
00082 
00083 bool MySqlConnection::drv_disconnect() {
00084   return d->db_disconnect();
00085 }
00086 
00087 Cursor* MySqlConnection::prepareQuery(const QString& statement, uint cursor_options) {
00088     return new MySqlCursor(this,statement,cursor_options);
00089 }
00090 
00091 Cursor* MySqlConnection::prepareQuery( QuerySchema& query, uint cursor_options ) {
00092     return new MySqlCursor( this, query, cursor_options );
00093 }
00094 
00095 bool MySqlConnection::drv_getDatabasesList( QStringList &list ) {
00096     KexiDBDrvDbg << "MySqlConnection::drv_getDatabasesList()" << endl;
00097     list.clear();
00098     MYSQL_RES *res;
00099 
00100     if((res=mysql_list_dbs(d->mysql,0)) != 0) {
00101         MYSQL_ROW  row;
00102         while ( (row = mysql_fetch_row(res))!=0) {
00103             list<<QString(row[0]);
00104         }
00105         mysql_free_result(res);
00106         return true;
00107     }
00108 
00109     d->storeResult();
00110 //  setError(ERR_DB_SPECIFIC,mysql_error(d->mysql));
00111     return false;
00112 }
00113 
00114 bool MySqlConnection::drv_createDatabase( const QString &dbName) {
00115     KexiDBDrvDbg << "MySqlConnection::drv_createDatabase: " << dbName << endl;
00116     // mysql_create_db deprecated, use SQL here. 
00117     if (drv_executeSQL("CREATE DATABASE " + (dbName)))
00118         return true;
00119     d->storeResult();
00120     return false;
00121 }
00122 
00123 bool MySqlConnection::drv_useDatabase(const QString &dbName, bool *cancelled, MessageHandler* msgHandler)
00124 {
00125     Q_UNUSED(cancelled);
00126     Q_UNUSED(msgHandler);
00127 //TODO is here escaping needed?
00128     return d->useDatabase(dbName);
00129 }
00130 
00131 bool MySqlConnection::drv_closeDatabase() {
00132 //TODO free resources 
00133 //As far as I know, mysql doesn't support that
00134     return true;
00135 }
00136 
00137 bool MySqlConnection::drv_dropDatabase( const QString &dbName) {
00138 //TODO is here escaping needed
00139     return drv_executeSQL("drop database "+dbName);
00140 }
00141                 
00142 bool MySqlConnection::drv_executeSQL( const QString& statement ) {
00143   return d->executeSQL(statement);
00144 }
00145 
00146 Q_ULLONG MySqlConnection::drv_lastInsertRowID()
00147 {
00149     return (Q_ULLONG)mysql_insert_id(d->mysql);
00150 }
00151 
00152 int MySqlConnection::serverResult()
00153 {
00154     return d->res;
00155 }
00156 
00157 QString MySqlConnection::serverResultName()
00158 {
00159     return QString::null;
00160 }
00161 
00162 void MySqlConnection::drv_clearServerResult()
00163 {
00164     if (!d)
00165         return;
00166     d->res = 0;
00167 }
00168 
00169 QString MySqlConnection::serverErrorMsg()
00170 {
00171     return d->errmsg;
00172 }
00173 
00174 bool MySqlConnection::drv_containsTable( const QString &tableName )
00175 {
00176     bool success;
00177     return resultExists(QString("show tables like %1")
00178         .arg(driver()->escapeString(tableName)), success) && success;
00179 }
00180 
00181 bool MySqlConnection::drv_getTablesList( QStringList &list )
00182 {
00183     KexiDB::Cursor *cursor;
00184     m_sql = "show tables";
00185     if (!(cursor = executeQuery( m_sql ))) {
00186         KexiDBDbg << "Connection::drv_getTablesList(): !executeQuery()" << endl;
00187         return false;
00188     }
00189     list.clear();
00190     cursor->moveFirst();
00191     while (!cursor->eof() && !cursor->error()) {
00192         list += cursor->value(0).toString();
00193         cursor->moveNext();
00194     }
00195     if (cursor->error()) {
00196         deleteCursor(cursor);
00197         return false;
00198     }
00199     return deleteCursor(cursor);
00200 }
00201 
00202 PreparedStatement::Ptr MySqlConnection::prepareStatement(PreparedStatement::StatementType type, 
00203     FieldList& fields)
00204 {
00205     return new MySqlPreparedStatement(type, *d, fields);
00206 }
00207 
00208 #include "mysqlconnection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys