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 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 
00027 #include <kgenericfactory.h>
00028 #include <kdebug.h>
00029 
00030 #include "mysqldriver.h"
00031 #include "mysqlconnection.h"
00032 #include "mysqlconnection_p.h"
00033 #include "mysqlcursor.h"
00034 #include "mysqlpreparedstatement.h"
00035 #include <kexidb/error.h>
00036 
00037 
00038 using namespace KexiDB;
00039 
00040 //--------------------------------------------------------------------------
00041 
00042 MySqlConnection::MySqlConnection( Driver *driver, ConnectionData &conn_data )
00043     :Connection(driver,conn_data)
00044     ,d(new MySqlConnectionInternal(this))
00045 {
00046 }
00047 
00048 MySqlConnection::~MySqlConnection() {
00049     destroy();
00050 }
00051 
00052 bool MySqlConnection::drv_connect() {
00053   return d->db_connect(*m_data);
00054 }
00055 
00056 bool MySqlConnection::drv_disconnect() {
00057   return d->db_disconnect();
00058 }
00059 
00060 Cursor* MySqlConnection::prepareQuery(const QString& statement, uint cursor_options) {
00061     return new MySqlCursor(this,statement,cursor_options);
00062 }
00063 
00064 Cursor* MySqlConnection::prepareQuery( QuerySchema& query, uint cursor_options ) {
00065     return new MySqlCursor( this, query, cursor_options );
00066 }
00067 
00068 bool MySqlConnection::drv_getDatabasesList( QStringList &list ) {
00069     KexiDBDrvDbg << "MySqlConnection::drv_getDatabasesList()" << endl;
00070     list.clear();
00071     MYSQL_RES *res;
00072 
00073     if((res=mysql_list_dbs(d->mysql,0)) != 0) {
00074         MYSQL_ROW  row;
00075         while ( (row = mysql_fetch_row(res))!=0) {
00076             list<<QString(row[0]);
00077         }
00078         mysql_free_result(res);
00079         return true;
00080     }
00081 
00082     d->storeResult();
00083 //  setError(ERR_DB_SPECIFIC,mysql_error(d->mysql));
00084     return false;
00085 }
00086 
00087 bool MySqlConnection::drv_createDatabase( const QString &dbName) {
00088     KexiDBDrvDbg << "MySqlConnection::drv_createDatabase: " << dbName << endl;
00089     // mysql_create_db deprecated, use SQL here. 
00090     if (drv_executeSQL("CREATE DATABASE " + (dbName)))
00091         return true;
00092     d->storeResult();
00093     return false;
00094 }
00095 
00096 bool MySqlConnection::drv_useDatabase(const QString &dbName, bool *cancelled, MessageHandler* msgHandler)
00097 {
00098     Q_UNUSED(cancelled);
00099     Q_UNUSED(msgHandler);
00100 //TODO is here escaping needed?
00101     return d->useDatabase(dbName);
00102 }
00103 
00104 bool MySqlConnection::drv_closeDatabase() {
00105 //TODO free resources 
00106 //As far as I know, mysql doesn't support that
00107     return true;
00108 }
00109 
00110 bool MySqlConnection::drv_dropDatabase( const QString &dbName) {
00111 //TODO is here escaping needed
00112     return drv_executeSQL("drop database "+dbName);
00113 }
00114                 
00115 bool MySqlConnection::drv_executeSQL( const QString& statement ) {
00116   return d->executeSQL(statement);
00117 }
00118 
00119 Q_ULLONG MySqlConnection::drv_lastInsertRowID()
00120 {
00122     return (Q_ULLONG)mysql_insert_id(d->mysql);
00123 }
00124 
00125 int MySqlConnection::serverResult()
00126 {
00127     return d->res;
00128 }
00129 
00130 QString MySqlConnection::serverResultName()
00131 {
00132     return QString::null;
00133 }
00134 
00135 void MySqlConnection::drv_clearServerResult()
00136 {
00137     if (!d)
00138         return;
00139     d->res = 0;
00140 }
00141 
00142 QString MySqlConnection::serverErrorMsg()
00143 {
00144     return d->errmsg;
00145 }
00146 
00147 bool MySqlConnection::drv_containsTable( const QString &tableName )
00148 {
00149     bool success;
00150     return resultExists(QString("show tables like %1")
00151         .arg(driver()->escapeString(tableName)), success) && success;
00152 }
00153 
00154 bool MySqlConnection::drv_getTablesList( QStringList &list )
00155 {
00156     KexiDB::Cursor *cursor;
00157     m_sql = "show tables";
00158     if (!(cursor = executeQuery( m_sql ))) {
00159         KexiDBDbg << "Connection::drv_getTablesList(): !executeQuery()" << endl;
00160         return false;
00161     }
00162     list.clear();
00163     cursor->moveFirst();
00164     while (!cursor->eof() && !cursor->error()) {
00165         list += cursor->value(0).toString();
00166         cursor->moveNext();
00167     }
00168     if (cursor->error()) {
00169         deleteCursor(cursor);
00170         return false;
00171     }
00172     return deleteCursor(cursor);
00173 }
00174 
00175 PreparedStatement::Ptr MySqlConnection::prepareStatement(PreparedStatement::StatementType type, 
00176     FieldList& fields)
00177 {
00178     return new MySqlPreparedStatement(type, *d, fields);
00179 }
00180 
00181 #include "mysqlconnection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys