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