bool QMYSQLDriver::open(const QString& db,
const QString& user,
const QString& password,
const QString& host,
int port,
const QString& connOpts)
{
if (isOpen())
close();
/* This is a hack to get MySQL's stored procedure support working.
Since a stored procedure _may_ return multiple result sets,
we have to enable CLIEN_MULTI_STATEMENTS here, otherwise _any_
stored procedure call will fail.
*/
unsigned int optionFlags = Q_CLIENT_MULTI_STATEMENTS;
// -------------------code added here----------------------- //
unsigned int connectTimeout=0;
//------------------------------------------------------------------- //
#if MYSQL_VERSION_ID >= 50000
my_bool reconnect=false;
#endif
// extract the real options from the string
for (int i = 0; i < opts.count(); ++i) {
QString tmp
(opts.
at(i
).
simplified());
int idx;
QString val
= tmp.
mid(idx
+ 1).
simplified();
QString opt
= tmp.
left(idx
).
simplified();
unixSocket = val;
// -------------------code added here----------------------- //
{
bool ok;
connectTimeout=val.toUInt(&ok);
if (!ok) {
connectTimeout=0;
qWarning("QMYSQLmodDriver Invalid timeout value %s", val.toLocal8Bit().constData());
}
}
//------------------------------------------------------------------- //
#if MYSQL_VERSION_ID >= 50000
reconnect = true;
}
#endif
setOptionFlag(optionFlags, tmp.left(idx).simplified());
else
qWarning("QMYSQLDriver::open: Illegal connect option value '%s'",
tmp.toLocal8Bit().constData());
} else {
setOptionFlag(optionFlags, tmp);
}
}
// -------------------code added here----------------------- //
if (!(d->mysql = mysql_init((MYSQL*) 0)))
{
setLastError(qMakeError(tr("Unable to initialize connection"),
mysql_close(d->mysql);
d->mysql = NULL;
setOpenError(true);
return false;
}
if (connectTimeout>0)
mysql_options(d->mysql, MYSQL_OPT_CONNECT_TIMEOUT , &connectTimeout);
//------------------------------------------------------------------- //
//--------------------------modified----------------------------- //
// if ((d->mysql = mysql_init((MYSQL*) 0)) &&
// mysql_real_connect(d->mysql,
if(mysql_real_connect(d->mysql,
host.isNull() ? static_cast<const char *>(0)
: host.toLocal8Bit().constData(),
user.isNull() ? static_cast<const char *>(0)
: user.toLocal8Bit().constData(),
password.isNull() ? static_cast<const char *>(0)
: password.toLocal8Bit().constData(),
db.isNull() ? static_cast<const char *>(0)
: db.toLocal8Bit().constData(),
(port > -1) ? port : 0,
unixSocket.isNull() ? static_cast<const char *>(0)
: unixSocket.toLocal8Bit().constData(),
optionFlags))
{
if (!db.isEmpty() && mysql_select_db(d->mysql, db.toLocal8Bit().constData())) {
setLastError(qMakeError(tr("Unable to open database '") + db +
mysql_close(d->mysql);
setOpenError(true);
return false;
}
#if MYSQL_VERSION_ID >= 50000
if(reconnect)
mysql_options(d->mysql, MYSQL_OPT_RECONNECT, &reconnect);
#endif
} else {
setLastError(qMakeError(tr("Unable to connect"),
mysql_close(d->mysql);
d->mysql = NULL;
setOpenError(true);
return false;
}
#if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007
// force the communication to be utf8
mysql_set_character_set(d->mysql, "utf8");
#endif
#ifndef QT_NO_TEXTCODEC
d->tc = codec(d->mysql);
#endif
#if MYSQL_VERSION_ID >= 40108
d->preparedQuerysEnabled = mysql_get_client_version() >= 40108
&& mysql_get_server_version(d->mysql) >= 40100;
#else
d->preparedQuerysEnabled = false;
#endif
#ifndef QT_NO_THREAD
mysql_thread_init();
#endif
setOpen(true);
setOpenError(false);
return true;
}
bool QMYSQLDriver::open(const QString& db,
const QString& user,
const QString& password,
const QString& host,
int port,
const QString& connOpts)
{
if (isOpen())
close();
/* This is a hack to get MySQL's stored procedure support working.
Since a stored procedure _may_ return multiple result sets,
we have to enable CLIEN_MULTI_STATEMENTS here, otherwise _any_
stored procedure call will fail.
*/
unsigned int optionFlags = Q_CLIENT_MULTI_STATEMENTS;
const QStringList opts(connOpts.split(QLatin1Char(';'), QString::SkipEmptyParts));
QString unixSocket;
// -------------------code added here----------------------- //
unsigned int connectTimeout=0;
//------------------------------------------------------------------- //
#if MYSQL_VERSION_ID >= 50000
my_bool reconnect=false;
#endif
// extract the real options from the string
for (int i = 0; i < opts.count(); ++i) {
QString tmp(opts.at(i).simplified());
int idx;
if ((idx = tmp.indexOf(QLatin1Char('='))) != -1) {
QString val = tmp.mid(idx + 1).simplified();
QString opt = tmp.left(idx).simplified();
if (opt == QLatin1String("UNIX_SOCKET"))
unixSocket = val;
// -------------------code added here----------------------- //
else if( opt == QLatin1String("MYSQL_OPT_CONNECT_TIMEOUT"))
{
bool ok;
connectTimeout=val.toUInt(&ok);
if (!ok) {
connectTimeout=0;
qWarning("QMYSQLmodDriver Invalid timeout value %s", val.toLocal8Bit().constData());
}
}
//------------------------------------------------------------------- //
#if MYSQL_VERSION_ID >= 50000
else if (opt == QLatin1String("MYSQL_OPT_RECONNECT")) {
if (val == QLatin1String("TRUE") || val == QLatin1String("1") || val.isEmpty())
reconnect = true;
}
#endif
else if (val == QLatin1String("TRUE") || val == QLatin1String("1"))
setOptionFlag(optionFlags, tmp.left(idx).simplified());
else
qWarning("QMYSQLDriver::open: Illegal connect option value '%s'",
tmp.toLocal8Bit().constData());
} else {
setOptionFlag(optionFlags, tmp);
}
}
// -------------------code added here----------------------- //
if (!(d->mysql = mysql_init((MYSQL*) 0)))
{
setLastError(qMakeError(tr("Unable to initialize connection"),
QSqlError::ConnectionError, d));
mysql_close(d->mysql);
d->mysql = NULL;
setOpenError(true);
return false;
}
if (connectTimeout>0)
mysql_options(d->mysql, MYSQL_OPT_CONNECT_TIMEOUT , &connectTimeout);
//------------------------------------------------------------------- //
//--------------------------modified----------------------------- //
// if ((d->mysql = mysql_init((MYSQL*) 0)) &&
// mysql_real_connect(d->mysql,
if(mysql_real_connect(d->mysql,
host.isNull() ? static_cast<const char *>(0)
: host.toLocal8Bit().constData(),
user.isNull() ? static_cast<const char *>(0)
: user.toLocal8Bit().constData(),
password.isNull() ? static_cast<const char *>(0)
: password.toLocal8Bit().constData(),
db.isNull() ? static_cast<const char *>(0)
: db.toLocal8Bit().constData(),
(port > -1) ? port : 0,
unixSocket.isNull() ? static_cast<const char *>(0)
: unixSocket.toLocal8Bit().constData(),
optionFlags))
{
if (!db.isEmpty() && mysql_select_db(d->mysql, db.toLocal8Bit().constData())) {
setLastError(qMakeError(tr("Unable to open database '") + db +
QLatin1Char('\''), QSqlError::ConnectionError, d));
mysql_close(d->mysql);
setOpenError(true);
return false;
}
#if MYSQL_VERSION_ID >= 50000
if(reconnect)
mysql_options(d->mysql, MYSQL_OPT_RECONNECT, &reconnect);
#endif
} else {
setLastError(qMakeError(tr("Unable to connect"),
QSqlError::ConnectionError, d));
mysql_close(d->mysql);
d->mysql = NULL;
setOpenError(true);
return false;
}
#if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007
// force the communication to be utf8
mysql_set_character_set(d->mysql, "utf8");
#endif
#ifndef QT_NO_TEXTCODEC
d->tc = codec(d->mysql);
#endif
#if MYSQL_VERSION_ID >= 40108
d->preparedQuerysEnabled = mysql_get_client_version() >= 40108
&& mysql_get_server_version(d->mysql) >= 40100;
#else
d->preparedQuerysEnabled = false;
#endif
#ifndef QT_NO_THREAD
mysql_thread_init();
#endif
setOpen(true);
setOpenError(false);
return true;
}
To copy to clipboard, switch view to plain text mode
In .../src/plugins/sqldrivers/mysqlmod/main.cpp change the code to
#include <qsqldriverplugin.h>
#include <qstringlist.h>
#include "../../../sql/drivers/mysqlmod/qsql_mysql.h"
QT_BEGIN_NAMESPACE
{
public:
QMYSQLmodDriverPlugin();
};
QMYSQLmodDriverPlugin::QMYSQLmodDriverPlugin()
{
}
{
QMYSQLDriver* driver = new QMYSQLDriver();
return driver;
}
return 0;
}
{
return l;
}
Q_EXPORT_STATIC_PLUGIN(QMYSQLmodDriverPlugin)
Q_EXPORT_PLUGIN2(qsqlmysql, QMYSQLmodDriverPlugin)
QT_END_NAMESPACE
#include <qsqldriverplugin.h>
#include <qstringlist.h>
#include "../../../sql/drivers/mysqlmod/qsql_mysql.h"
QT_BEGIN_NAMESPACE
class QMYSQLmodDriverPlugin : public QSqlDriverPlugin
{
public:
QMYSQLmodDriverPlugin();
QSqlDriver* create(const QString &);
QStringList keys() const;
};
QMYSQLmodDriverPlugin::QMYSQLmodDriverPlugin()
: QSqlDriverPlugin()
{
}
QSqlDriver* QMYSQLmodDriverPlugin::create(const QString &name)
{
if (name == QLatin1String("QMYSQLmod") || name == QLatin1String("QMYSQL3mod")) {
QMYSQLDriver* driver = new QMYSQLDriver();
return driver;
}
return 0;
}
QStringList QMYSQLmodDriverPlugin::keys() const
{
QStringList l;
l << QLatin1String("QMYSQL3mod");
l << QLatin1String("QMYSQLmod");
return l;
}
Q_EXPORT_STATIC_PLUGIN(QMYSQLmodDriverPlugin)
Q_EXPORT_PLUGIN2(qsqlmysql, QMYSQLmodDriverPlugin)
QT_END_NAMESPACE
To copy to clipboard, switch view to plain text mode
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QElapsedTimer>
int main(int argc, char *argv[])
{
qDebug()<<QSqlDatabase::drivers();
QElapsedTimer timer;
timer.start();
db.setHostName("www.google.com");
db.setPort(3306);
db.setUserName("someone");
db.setPassword("pass");
db.setConnectOptions("MYSQL_OPT_CONNECT_TIMEOUT=4");
if (!db.open())
qDebug()<<db.lastError().text();
qDebug()<<"time elapsed: "<<timer.elapsed()/1000;
return 0;
}
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QElapsedTimer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<QSqlDatabase::drivers();
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQLmod");
QElapsedTimer timer;
timer.start();
db.setHostName("www.google.com");
db.setPort(3306);
db.setUserName("someone");
db.setPassword("pass");
db.setConnectOptions("MYSQL_OPT_CONNECT_TIMEOUT=4");
if (!db.open())
qDebug()<<db.lastError().text();
qDebug()<<"time elapsed: "<<timer.elapsed()/1000;
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks