kexi

mysqlconnection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3 Daniel Molkentin <molkentin@kde.org>
4 Copyright (C) 2003 Joseph Wenninger<jowenn@kde.org>
5 Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU Library General Public
9License as published by the Free Software Foundation; either
10version 2 of the License, or (at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Library General Public License for more details.
16
17You should have received a copy of the GNU Library General Public License
18along with this program; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23#include <tqvariant.h>
24#include <tqfile.h>
25#include <tqdict.h>
26#include <tqregexp.h>
27
28#include <kgenericfactory.h>
29#include <kdebug.h>
30
31#include "mysqldriver.h"
32#include "mysqlconnection.h"
33#include "mysqlconnection_p.h"
34#include "mysqlcursor.h"
35#include "mysqlpreparedstatement.h"
36#include <kexidb/error.h>
37
38
39using namespace KexiDB;
40
41//--------------------------------------------------------------------------
42
44 :Connection(driver,conn_data)
45 ,d(new MySqlConnectionInternal(this))
46{
47}
48
49MySqlConnection::~MySqlConnection() {
50 destroy();
51}
52
54{
55 const bool ok = d->db_connect(*data());
56 if (!ok)
57 return false;
58
59 version.string = mysql_get_host_info(d->mysql);
60
61 //retrieve server version info
62#if 0 //this only works for client version >= 4.1 :(
63 unsigned long v = mysql_get_server_version(d->mysql);
64 // v - a number that represents the MySQL server version in this format
65 // = major_version*10000 + minor_version *100 + sub_version
66 version.major = v/10000;
67 version.minor = (v - version.major*10000)/100;
68 version.release = v - version.major*10000 - version.minor*100;
69#else //better way to get the version info: use 'version' built-in variable:
71 TQString versionString;
72 const tristate res = querySingleString("SELECT @@version", versionString, /*column*/0, false );
73 TQRegExp versionRe("(\\d+)\\.(\\d+)\\.(\\d+)");
74 if (res==true && versionRe.exactMatch(versionString)) { // (if querySingleString failed, the version will be 0.0.0...
75 version.major = versionRe.cap(1).toInt();
76 version.minor = versionRe.cap(2).toInt();
77 version.release = versionRe.cap(3).toInt();
78 }
79#endif
80 return true;
81}
82
84 return d->db_disconnect();
85}
86
87Cursor* MySqlConnection::prepareQuery(const TQString& statement, uint cursor_options) {
88 return new MySqlCursor(this,statement,cursor_options);
89}
90
91Cursor* MySqlConnection::prepareQuery( QuerySchema& query, uint cursor_options ) {
92 return new MySqlCursor( this, query, cursor_options );
93}
94
95bool MySqlConnection::drv_getDatabasesList( TQStringList &list ) {
96 KexiDBDrvDbg << "MySqlConnection::drv_getDatabasesList()" << endl;
97 list.clear();
98 MYSQL_RES *res;
99
100 if((res=mysql_list_dbs(d->mysql,0)) != 0) {
101 MYSQL_ROW row;
102 while ( (row = mysql_fetch_row(res))!=0) {
103 list<<TQString(row[0]);
104 }
105 mysql_free_result(res);
106 return true;
107 }
108
109 d->storeResult();
110// setError(ERR_DB_SPECIFIC,mysql_error(d->mysql));
111 return false;
112}
113
114bool MySqlConnection::drv_createDatabase( const TQString &dbName) {
115 KexiDBDrvDbg << "MySqlConnection::drv_createDatabase: " << dbName << endl;
116 // mysql_create_db deprecated, use SQL here.
117 if (drv_executeSQL("CREATE DATABASE " + (dbName)))
118 return true;
119 d->storeResult();
120 return false;
121}
122
123bool MySqlConnection::drv_useDatabase(const TQString &dbName, bool *cancelled, MessageHandler* msgHandler)
124{
125 Q_UNUSED(cancelled);
126 Q_UNUSED(msgHandler);
127//TODO is here escaping needed?
128 return d->useDatabase(dbName);
129}
130
132//TODO free resources
133//As far as I know, mysql doesn't support that
134 return true;
135}
136
137bool MySqlConnection::drv_dropDatabase( const TQString &dbName) {
138//TODO is here escaping needed
139 return drv_executeSQL("drop database "+dbName);
140}
141
142bool MySqlConnection::drv_executeSQL( const TQString& statement ) {
143 return d->executeSQL(statement);
144}
145
147{
149 return (TQ_ULLONG)mysql_insert_id(d->mysql);
150}
151
153{
154 return d->res;
155}
156
158{
159 return TQString();
160}
161
163{
164 if (!d)
165 return;
166 d->res = 0;
167}
168
170{
171 return d->errmsg;
172}
173
174bool MySqlConnection::drv_containsTable( const TQString &tableName )
175{
176 bool success;
177 return resultExists(TQString("show tables like %1")
178 .arg(driver()->escapeString(tableName)), success) && success;
179}
180
181bool MySqlConnection::drv_getTablesList( TQStringList &list )
182{
183 KexiDB::Cursor *cursor;
184 m_sql = "show tables";
185 if (!(cursor = executeQuery( m_sql ))) {
186 KexiDBDbg << "Connection::drv_getTablesList(): !executeQuery()" << endl;
187 return false;
188 }
189 list.clear();
190 cursor->moveFirst();
191 while (!cursor->eof() && !cursor->error()) {
192 list += cursor->value(0).toString();
193 cursor->moveNext();
194 }
195 if (cursor->error()) {
196 deleteCursor(cursor);
197 return false;
198 }
199 return deleteCursor(cursor);
200}
201
203 FieldList& fields)
204{
205 return new MySqlPreparedStatement(type, *d, fields);
206}
207
208#include "mysqlconnection.moc"
Database specific connection data, e.g. host, port.
bool deleteCursor(Cursor *cursor)
tristate querySingleString(const TQString &sql, TQString &value, uint column=0, bool addLimitTo1=true)
bool resultExists(const TQString &sql, bool &success, bool addLimitTo1=true)
Driver * driver() const
Definition connection.h:78
Connection(Driver *driver, ConnectionData &conn_data)
ConnectionData * data() const
Cursor * executeQuery(const TQString &statement, uint cursor_options=0)
Provides database cursor functionality.
Definition cursor.h:67
virtual bool moveNext()
Definition cursor.cpp:292
virtual TQVariant value(uint i)=0
bool eof() const
Definition cursor.cpp:339
bool moveFirst()
Definition cursor.cpp:205
virtual Cursor * prepareQuery(const TQString &statement=TQString(), uint cursor_options=0)
virtual bool drv_getTablesList(TQStringList &list)
virtual bool drv_connect(KexiDB::ServerVersionInfo &version)
virtual TQString serverErrorMsg()
virtual bool drv_executeSQL(const TQString &statement)
virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type, FieldList &fields)
virtual void drv_clearServerResult()
virtual bool drv_getDatabasesList(TQStringList &list)
virtual bool drv_dropDatabase(const TQString &dbName=TQString())
virtual bool drv_useDatabase(const TQString &dbName=TQString(), bool *cancelled=0, MessageHandler *msgHandler=0)
virtual bool drv_containsTable(const TQString &tableName)
virtual TQ_ULLONG drv_lastInsertRowID()
virtual bool drv_createDatabase(const TQString &dbName=TQString())
virtual TQString serverResultName()
MySqlConnection(Driver *driver, ConnectionData &conn_data)
bool error() const
Definition object.h:38
TQString m_sql
used to store of actually executed SQL statement
Definition object.h:161
StatementType
Defines type of the prepared statement.
KexiDB::QuerySchema provides information about database query.
Contains version information about a database backend.
Definition global.h:126
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT DatabaseVersionInfo version()
Definition global.cpp:55