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;
}