mysqlconnection_p.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
3 Copyright (C) 2004 Martin Ellis <martin.ellis@kdemail.net>
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public
7License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public License
16along with this program; see the file COPYING. If not, write to
17the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqcstring.h>
22#include <tqstring.h>
23#include <tqstringlist.h>
24#include <tqfile.h>
25
26#include <kdebug.h>
27
28#include "mysqlconnection_p.h"
29
30#include <kexidb/connectiondata.h>
31
32#ifdef MYSQLMIGRATE_H
33#define NAMESPACE KexiMigration
34#else
35#define NAMESPACE KexiDB
36#endif
37
38using namespace NAMESPACE;
39
40/* ************************************************************************** */
41MySqlConnectionInternal::MySqlConnectionInternal(KexiDB::Connection* connection)
42 : ConnectionInternal(connection)
43 , mysql(0)
44 , mysql_owned(true)
45 , res(0)
46{
47}
48
49MySqlConnectionInternal::~MySqlConnectionInternal()
50{
51 if (mysql_owned && mysql) {
52 mysql_close(mysql);
53 mysql = 0;
54 }
55}
56
57void MySqlConnectionInternal::storeResult()
58{
59 res = mysql_errno(mysql);
60 errmsg = mysql_error(mysql);
61}
62
63/* ************************************************************************** */
70//bool MySqlConnectionInternal::db_connect(TQCString host, TQCString user,
71// TQCString password, unsigned short int port, TQString socket)
72bool MySqlConnectionInternal::db_connect(const KexiDB::ConnectionData& data)
73{
74 if (!(mysql = mysql_init(mysql)))
75 return false;
76
77 KexiDBDrvDbg << "MySqlConnectionInternal::connect()" << endl;
78 TQCString localSocket;
79 TQString hostName = data.hostName;
80 if (hostName.isEmpty() || hostName.lower()=="localhost") {
81 if (data.useLocalSocketFile) {
82 if (data.localSocketFileName.isEmpty()) {
84 TQStringList sockets;
85 #ifndef TQ_WS_WIN
86 sockets.append("/var/lib/mysql/mysql.sock");
87 sockets.append("/var/run/mysqld/mysqld.sock");
88 sockets.append("/tmp/mysql.sock");
89
90 for(TQStringList::ConstIterator it = sockets.constBegin(); it != sockets.constEnd(); it++)
91 {
92 if(TQFile(*it).exists()) {
93 localSocket = ((TQString)(*it)).local8Bit();
94 break;
95 }
96 }
97 #endif
98 }
99 else
100 localSocket = TQFile::encodeName(data.localSocketFileName);
101 }
102 else {
103 //we're not using local socket
104 hostName = "127.0.0.1"; //this will force mysql to connect to localhost
105 }
106 }
107
109 const char *pwd = data.password.isNull() ? 0 : data.password.latin1();
110 mysql_real_connect(mysql, hostName.latin1(), data.userName.latin1(),
111 pwd, 0, data.port, localSocket, 0);
112 if(mysql_errno(mysql) == 0)
113 return true;
114
115 storeResult(); //store error msg, if any - can be destroyed after disconnect()
116 db_disconnect();
117// setError(ERR_DB_SPECIFIC,err);
118 return false;
119}
120
123bool MySqlConnectionInternal::db_disconnect()
124{
125 mysql_close(mysql);
126 mysql = 0;
127 KexiDBDrvDbg << "MySqlConnection::disconnect()" << endl;
128 return true;
129}
130
131/* ************************************************************************** */
134bool MySqlConnectionInternal::useDatabase(const TQString &dbName) {
135//TODO is here escaping needed?
136 return executeSQL("USE " + dbName);
137}
138
141bool MySqlConnectionInternal::executeSQL(const TQString& statement) {
142// KexiDBDrvDbg << "MySqlConnectionInternal::executeSQL: "
143// << statement << endl;
144 TQCString queryStr=statement.utf8();
145 const char *query=queryStr;
146 if(mysql_real_query(mysql, query, strlen(query)) == 0)
147 {
148 return true;
149 }
150
151 storeResult();
152// setError(ERR_DB_SPECIFIC,mysql_error(m_mysql));
153 return false;
154}
155
156TQString MySqlConnectionInternal::escapeIdentifier(const TQString& str) const {
157 return TQString(str).replace('`', "'");
158}
159
160//--------------------------------------
161
162MySqlCursorData::MySqlCursorData(KexiDB::Connection* connection)
163: MySqlConnectionInternal(connection)
164, mysqlres(0)
165, mysqlrow(0)
166, lengths(0)
167, numRows(0)
168{
169 mysql_owned = false;
170}
171
172MySqlCursorData::~MySqlCursorData()
173{
174}
175
TQString localSocketFileName
Name of local (named) socket file.
TQString userName
Username used for the connection.
TQString password
Password used for the connection.
unsigned short int port
Port used for the remote connection.
TQString hostName
Host name used for the remote connection.
bool useLocalSocketFile
True if local socket file should be used instead of TCP/IP port.
Database specific connection data, e.g. host, port.
Provides database connection, allowing queries and data modification.
Definition connection.h:62