kexi
mysqlconnection.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00062 #if 0 //this only works for client version >= 4.1 :(
00063 unsigned long v = mysql_get_server_version(d->mysql);
00064
00065
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, 0, false );
00073 QRegExp versionRe("(\\d+)\\.(\\d+)\\.(\\d+)");
00074 if (res==true && versionRe.exactMatch(versionString)) {
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
00111 return false;
00112 }
00113
00114 bool MySqlConnection::drv_createDatabase( const QString &dbName) {
00115 KexiDBDrvDbg << "MySqlConnection::drv_createDatabase: " << dbName << endl;
00116
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
00128 return d->useDatabase(dbName);
00129 }
00130
00131 bool MySqlConnection::drv_closeDatabase() {
00132
00133
00134 return true;
00135 }
00136
00137 bool MySqlConnection::drv_dropDatabase( const QString &dbName) {
00138
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"
|