QSqlDriver: use Q_DECLARE_PUBLIC/Q_DECLARE_PRIVATE

406c8ef6e6 introduced deriving the private SQL driver classes from
QSqlDriverPrivate. However, the drivers continued to keep their own
pointer to the private class, even though QObject provides the same
pointer. Worse yet, the private class is allocated too late and not
even passed to QSqlDriver. The result is that QSqlDriver allocates
a separate instance of QSqlDriverPrivate. This is likely to cause
all kinds of chaos.

The private class needs to be allocated in time pass it to QSqlDriver
which passes it on to QObject.

This commit covers the the base class and drivers:
    ibase
    mysql
    odbc
    psql
    sqlite
    tds

Fixes for the remaining drivers will follow.

Change-Id: Id8e7ec4205b0ca6cd00bd022c9cd24f137089245
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
Mark Brand 2013-04-03 21:44:30 +02:00 committed by The Qt Project
parent 708d39fa31
commit 0bdc86d9ef
15 changed files with 154 additions and 106 deletions

View File

@ -312,11 +312,13 @@ struct QIBaseEventBuffer {
class QIBaseDriverPrivate : public QSqlDriverPrivate
{
Q_DECLARE_PUBLIC(QIBaseDriver)
public:
QIBaseDriverPrivate(QIBaseDriver *d) : QSqlDriverPrivate(), q(d), ibase(0), trans(0), tc(0) { dbmsType = Interbase; }
QIBaseDriverPrivate() : QSqlDriverPrivate(), ibase(0), trans(0), tc(0) { dbmsType = Interbase; }
bool isError(const char *msg, QSqlError::ErrorType typ = QSqlError::UnknownError)
{
Q_Q(QIBaseDriver);
QString imsg;
ISC_LONG sqlcode;
if (!getIBaseError(imsg, status, sqlcode, tc))
@ -328,7 +330,6 @@ public:
}
public:
QIBaseDriver* q;
isc_db_handle ibase;
isc_tr_handle trans;
QTextCodec *tc;
@ -418,9 +419,9 @@ public:
QIBaseResultPrivate::QIBaseResultPrivate(QIBaseResult *d, const QIBaseDriver *ddb):
q(d), db(ddb), trans(0), stmt(0), ibase(ddb->d->ibase), sqlda(0), inda(0), queryType(-1), tc(ddb->d->tc)
q(d), db(ddb), trans(0), stmt(0), ibase(ddb->d_func()->ibase), sqlda(0), inda(0), queryType(-1), tc(ddb->d_func()->tc)
{
localTransaction = (ddb->d->ibase == 0);
localTransaction = (ddb->d_func()->ibase == 0);
}
void QIBaseResultPrivate::cleanup()
@ -861,9 +862,9 @@ bool QIBaseResultPrivate::transaction()
{
if (trans)
return true;
if (db->d->trans) {
if (db->d_func()->trans) {
localTransaction = false;
trans = db->d->trans;
trans = db->d_func()->trans;
return true;
}
localTransaction = true;
@ -1396,15 +1397,14 @@ QVariant QIBaseResult::handle() const
/*********************************/
QIBaseDriver::QIBaseDriver(QObject * parent)
: QSqlDriver(parent)
: QSqlDriver(*new QIBaseDriverPrivate, parent)
{
d = new QIBaseDriverPrivate(this);
}
QIBaseDriver::QIBaseDriver(isc_db_handle connection, QObject *parent)
: QSqlDriver(parent)
: QSqlDriver(*new QIBaseDriverPrivate, parent)
{
d = new QIBaseDriverPrivate(this);
Q_D(QIBaseDriver);
d->ibase = connection;
setOpen(true);
setOpenError(false);
@ -1444,6 +1444,7 @@ bool QIBaseDriver::open(const QString & db,
int /*port*/,
const QString & connOpts)
{
Q_D(QIBaseDriver);
if (isOpen())
close();
@ -1526,6 +1527,7 @@ bool QIBaseDriver::open(const QString & db,
void QIBaseDriver::close()
{
Q_D(QIBaseDriver);
if (isOpen()) {
if (d->eventBuffers.size()) {
@ -1562,6 +1564,7 @@ QSqlResult *QIBaseDriver::createResult() const
bool QIBaseDriver::beginTransaction()
{
Q_D(QIBaseDriver);
if (!isOpen() || isOpenError())
return false;
if (d->trans)
@ -1574,6 +1577,7 @@ bool QIBaseDriver::beginTransaction()
bool QIBaseDriver::commitTransaction()
{
Q_D(QIBaseDriver);
if (!isOpen() || isOpenError())
return false;
if (!d->trans)
@ -1587,6 +1591,7 @@ bool QIBaseDriver::commitTransaction()
bool QIBaseDriver::rollbackTransaction()
{
Q_D(QIBaseDriver);
if (!isOpen() || isOpenError())
return false;
if (!d->trans)
@ -1749,6 +1754,7 @@ QString QIBaseDriver::formatValue(const QSqlField &field, bool trimStrings) cons
QVariant QIBaseDriver::handle() const
{
Q_D(const QIBaseDriver);
return QVariant(qRegisterMetaType<isc_db_handle>("isc_db_handle"), &d->ibase);
}
@ -1777,6 +1783,7 @@ static isc_callback qEventCallback(char *result, short length, char *updated)
bool QIBaseDriver::subscribeToNotification(const QString &name)
{
Q_D(QIBaseDriver);
if (!isOpen()) {
qWarning("QIBaseDriver::subscribeFromNotificationImplementation: database not open.");
return false;
@ -1826,6 +1833,7 @@ bool QIBaseDriver::subscribeToNotification(const QString &name)
bool QIBaseDriver::unsubscribeFromNotification(const QString &name)
{
Q_D(QIBaseDriver);
if (!isOpen()) {
qWarning("QIBaseDriver::unsubscribeFromNotificationImplementation: database not open.");
return false;
@ -1855,11 +1863,13 @@ bool QIBaseDriver::unsubscribeFromNotification(const QString &name)
QStringList QIBaseDriver::subscribedToNotifications() const
{
Q_D(const QIBaseDriver);
return QStringList(d->eventBuffers.keys());
}
void QIBaseDriver::qHandleEventNotification(void *updatedResultBuffer)
{
Q_D(QIBaseDriver);
QMap<QString, QIBaseEventBuffer *>::const_iterator i;
for (i = d->eventBuffers.constBegin(); i != d->eventBuffers.constEnd(); ++i) {
QIBaseEventBuffer* eBuffer = i.value();

View File

@ -64,9 +64,9 @@ class QIBaseDriver;
class QIBaseDriver : public QSqlDriver
{
Q_OBJECT
friend class QIBaseDriverPrivate;
friend class QIBaseResultPrivate;
Q_DECLARE_PRIVATE(QIBaseDriver)
Q_OBJECT
public:
explicit QIBaseDriver(QObject *parent = 0);
explicit QIBaseDriver(isc_db_handle connection, QObject *parent = 0);
@ -104,9 +104,6 @@ public:
private Q_SLOTS:
void qHandleEventNotification(void* updatedResultBuffer);
private:
QIBaseDriverPrivate* d;
};
QT_END_NAMESPACE

View File

@ -435,8 +435,8 @@ void QMYSQLResult::cleanup()
// must iterate trough leftover result sets from multi-selects or stored procedures
// if this isn't done subsequent queries will fail with "Commands out of sync"
#if MYSQL_VERSION_ID >= 40100
while (d->driver && d->driver->d->mysql && mysql_next_result(d->driver->d->mysql) == 0) {
MYSQL_RES *res = mysql_store_result(d->driver->d->mysql);
while (d->driver && d->driver->d_func()->mysql && mysql_next_result(d->driver->d_func()->mysql) == 0) {
MYSQL_RES *res = mysql_store_result(d->driver->d_func()->mysql);
if (res)
mysql_free_result(res);
}
@ -606,7 +606,7 @@ QVariant QMYSQLResult::data(int field)
return QVariant(f.type);
if (f.type != QVariant::ByteArray)
val = toUnicode(d->driver->d->tc, f.outField, f.bufLength);
val = toUnicode(d->driver->d_func()->tc, f.outField, f.bufLength);
} else {
if (d->row[field] == NULL) {
// NULL value
@ -614,7 +614,7 @@ QVariant QMYSQLResult::data(int field)
}
fieldLength = mysql_fetch_lengths(d->result)[field];
if (f.type != QVariant::ByteArray)
val = toUnicode(d->driver->d->tc, d->row[field], fieldLength);
val = toUnicode(d->driver->d_func()->tc, d->row[field], fieldLength);
}
switch(f.type) {
@ -693,22 +693,22 @@ bool QMYSQLResult::reset (const QString& query)
cleanup();
const QByteArray encQuery(fromUnicode(d->driver->d->tc, query));
if (mysql_real_query(d->driver->d->mysql, encQuery.data(), encQuery.length())) {
const QByteArray encQuery(fromUnicode(d->driver->d_func()->tc, query));
if (mysql_real_query(d->driver->d_func()->mysql, encQuery.data(), encQuery.length())) {
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute query"),
QSqlError::StatementError, d->driver->d));
QSqlError::StatementError, d->driver->d_func()));
return false;
}
d->result = mysql_store_result(d->driver->d->mysql);
if (!d->result && mysql_field_count(d->driver->d->mysql) > 0) {
d->result = mysql_store_result(d->driver->d_func()->mysql);
if (!d->result && mysql_field_count(d->driver->d_func()->mysql) > 0) {
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store result"),
QSqlError::StatementError, d->driver->d));
QSqlError::StatementError, d->driver->d_func()));
return false;
}
int numFields = mysql_field_count(d->driver->d->mysql);
int numFields = mysql_field_count(d->driver->d_func()->mysql);
setSelect(numFields != 0);
d->fields.resize(numFields);
d->rowsAffected = mysql_affected_rows(d->driver->d->mysql);
d->rowsAffected = mysql_affected_rows(d->driver->d_func()->mysql);
if (isSelect()) {
for(int i = 0; i < numFields; i++) {
@ -753,7 +753,7 @@ QVariant QMYSQLResult::lastInsertId() const
return QVariant(id);
#endif
} else {
quint64 id = mysql_insert_id(d->driver->d->mysql);
quint64 id = mysql_insert_id(d->driver->d_func()->mysql);
if (id)
return QVariant(id);
}
@ -773,11 +773,11 @@ QSqlRecord QMYSQLResult::record() const
res = d->result;
#endif
if (!mysql_errno(d->driver->d->mysql)) {
if (!mysql_errno(d->driver->d_func()->mysql)) {
mysql_field_seek(res, 0);
MYSQL_FIELD* field = mysql_fetch_field(res);
while(field) {
info.append(qToField(field, d->driver->d->tc));
info.append(qToField(field, d->driver->d_func()->tc));
field = mysql_fetch_field(res);
}
}
@ -802,26 +802,26 @@ bool QMYSQLResult::nextResult()
delete[] d->fields[i].outField;
d->fields.clear();
int status = mysql_next_result(d->driver->d->mysql);
int status = mysql_next_result(d->driver->d_func()->mysql);
if (status > 0) {
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute next query"),
QSqlError::StatementError, d->driver->d));
QSqlError::StatementError, d->driver->d_func()));
return false;
} else if (status == -1) {
return false; // No more result sets
}
d->result = mysql_store_result(d->driver->d->mysql);
int numFields = mysql_field_count(d->driver->d->mysql);
d->result = mysql_store_result(d->driver->d_func()->mysql);
int numFields = mysql_field_count(d->driver->d_func()->mysql);
if (!d->result && numFields > 0) {
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store next result"),
QSqlError::StatementError, d->driver->d));
QSqlError::StatementError, d->driver->d_func()));
return false;
}
setSelect(numFields > 0);
d->fields.resize(numFields);
d->rowsAffected = mysql_affected_rows(d->driver->d->mysql);
d->rowsAffected = mysql_affected_rows(d->driver->d_func()->mysql);
if (isSelect()) {
for (int i = 0; i < numFields; i++) {
@ -874,7 +874,7 @@ bool QMYSQLResult::prepare(const QString& query)
return false;
#if MYSQL_VERSION_ID >= 40108
cleanup();
if (!d->driver->d->preparedQuerysEnabled)
if (!d->driver->d_func()->preparedQuerysEnabled)
return QSqlResult::prepare(query);
int r;
@ -883,14 +883,14 @@ bool QMYSQLResult::prepare(const QString& query)
return false;
if (!d->stmt)
d->stmt = mysql_stmt_init(d->driver->d->mysql);
d->stmt = mysql_stmt_init(d->driver->d_func()->mysql);
if (!d->stmt) {
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to prepare statement"),
QSqlError::StatementError, d->driver->d));
QSqlError::StatementError, d->driver->d_func()));
return false;
}
const QByteArray encQuery(fromUnicode(d->driver->d->tc, query));
const QByteArray encQuery(fromUnicode(d->driver->d_func()->tc, query));
r = mysql_stmt_prepare(d->stmt, encQuery.constData(), encQuery.length());
if (r != 0) {
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult",
@ -1010,7 +1010,7 @@ bool QMYSQLResult::exec()
break;
case QVariant::String:
default: {
QByteArray ba = fromUnicode(d->driver->d->tc, val.toString());
QByteArray ba = fromUnicode(d->driver->d_func()->tc, val.toString());
stringVector.append(ba);
currBind->buffer_type = MYSQL_TYPE_STRING;
currBind->buffer = const_cast<char *>(ba.constData());
@ -1115,7 +1115,7 @@ static void qLibraryEnd()
}
QMYSQLDriver::QMYSQLDriver(QObject * parent)
: QSqlDriver(parent)
: QSqlDriver(*new QMYSQLDriverPrivate, parent)
{
init();
qLibraryInit();
@ -1127,8 +1127,9 @@ QMYSQLDriver::QMYSQLDriver(QObject * parent)
*/
QMYSQLDriver::QMYSQLDriver(MYSQL * con, QObject * parent)
: QSqlDriver(parent)
: QSqlDriver(*new QMYSQLDriverPrivate, parent)
{
Q_D(QMYSQLDriver);
init();
if (con) {
d->mysql = (MYSQL *) con;
@ -1146,7 +1147,7 @@ QMYSQLDriver::QMYSQLDriver(MYSQL * con, QObject * parent)
void QMYSQLDriver::init()
{
d = new QMYSQLDriverPrivate();
Q_D(QMYSQLDriver);
d->mysql = 0;
qMySqlConnectionCount++;
}
@ -1160,6 +1161,7 @@ QMYSQLDriver::~QMYSQLDriver()
bool QMYSQLDriver::hasFeature(DriverFeature f) const
{
Q_D(const QMYSQLDriver);
switch (f) {
case Transactions:
// CLIENT_TRANSACTION should be defined in all recent mysql client libs > 3.23.34
@ -1227,6 +1229,7 @@ bool QMYSQLDriver::open(const QString& db,
int port,
const QString& connOpts)
{
Q_D(QMYSQLDriver);
if (isOpen())
close();
@ -1328,6 +1331,7 @@ bool QMYSQLDriver::open(const QString& db,
void QMYSQLDriver::close()
{
Q_D(QMYSQLDriver);
if (isOpen()) {
#ifndef QT_NO_THREAD
mysql_thread_end();
@ -1346,6 +1350,7 @@ QSqlResult *QMYSQLDriver::createResult() const
QStringList QMYSQLDriver::tables(QSql::TableType type) const
{
Q_D(const QMYSQLDriver);
QStringList tl;
#if MYSQL_VERSION_ID >= 40100
if( mysql_get_server_version(d->mysql) < 50000)
@ -1413,6 +1418,7 @@ QSqlIndex QMYSQLDriver::primaryIndex(const QString& tablename) const
QSqlRecord QMYSQLDriver::record(const QString& tablename) const
{
Q_D(const QMYSQLDriver);
QString table=tablename;
if(isIdentifierEscaped(table, QSqlDriver::TableName))
table = stripDelimiters(table, QSqlDriver::TableName);
@ -1434,11 +1440,13 @@ QSqlRecord QMYSQLDriver::record(const QString& tablename) const
QVariant QMYSQLDriver::handle() const
{
Q_D(const QMYSQLDriver);
return QVariant::fromValue(d->mysql);
}
bool QMYSQLDriver::beginTransaction()
{
Q_D(QMYSQLDriver);
#ifndef CLIENT_TRANSACTIONS
return false;
#endif
@ -1456,6 +1464,7 @@ bool QMYSQLDriver::beginTransaction()
bool QMYSQLDriver::commitTransaction()
{
Q_D(QMYSQLDriver);
#ifndef CLIENT_TRANSACTIONS
return false;
#endif
@ -1473,6 +1482,7 @@ bool QMYSQLDriver::commitTransaction()
bool QMYSQLDriver::rollbackTransaction()
{
Q_D(QMYSQLDriver);
#ifndef CLIENT_TRANSACTIONS
return false;
#endif
@ -1490,6 +1500,7 @@ bool QMYSQLDriver::rollbackTransaction()
QString QMYSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const
{
Q_D(const QMYSQLDriver);
QString r;
if (field.isNull()) {
r = QLatin1String("NULL");

View File

@ -110,8 +110,9 @@ private:
class Q_EXPORT_SQLDRIVER_MYSQL QMYSQLDriver : public QSqlDriver
{
Q_OBJECT
friend class QMYSQLResult;
Q_DECLARE_PRIVATE(QMYSQLDriver)
Q_OBJECT
public:
explicit QMYSQLDriver(QObject *parent=0);
explicit QMYSQLDriver(MYSQL *con, QObject * parent=0);
@ -141,7 +142,6 @@ protected:
bool rollbackTransaction();
private:
void init();
QMYSQLDriverPrivate* d;
};
QT_END_NAMESPACE

View File

@ -188,13 +188,13 @@ public:
bool QODBCPrivate::isStmtHandleValid(const QSqlDriver *driver)
{
const QODBCDriver *odbcdriver = static_cast<const QODBCDriver*> (driver);
return disconnectCount == odbcdriver->d->disconnectCount;
return disconnectCount == odbcdriver->d_func()->disconnectCount;
}
void QODBCPrivate::updateStmtHandleState(const QSqlDriver *driver)
{
const QODBCDriver *odbcdriver = static_cast<const QODBCDriver*> (driver);
disconnectCount = odbcdriver->d->disconnectCount;
disconnectCount = odbcdriver->d_func()->disconnectCount;
}
static QString qWarnODBCHandle(int handleType, SQLHANDLE handle, int *nativeCode = 0)
@ -1767,15 +1767,14 @@ void QODBCResult::setForwardOnly(bool forward)
QODBCDriver::QODBCDriver(QObject *parent)
: QSqlDriver(parent)
: QSqlDriver(*new QODBCDriverPrivate, parent)
{
init();
}
QODBCDriver::QODBCDriver(SQLHANDLE env, SQLHANDLE con, QObject * parent)
: QSqlDriver(parent)
QODBCDriver::QODBCDriver(SQLHANDLE env, SQLHANDLE con, QObject *parent)
: QSqlDriver(*new QODBCDriverPrivate, parent)
{
init();
Q_D(QODBCDriver);
d->hEnv = env;
d->hDbc = con;
if (env && con) {
@ -1784,11 +1783,6 @@ QODBCDriver::QODBCDriver(SQLHANDLE env, SQLHANDLE con, QObject * parent)
}
}
void QODBCDriver::init()
{
d = new QODBCDriverPrivate();
}
QODBCDriver::~QODBCDriver()
{
cleanup();
@ -1796,6 +1790,7 @@ QODBCDriver::~QODBCDriver()
bool QODBCDriver::hasFeature(DriverFeature f) const
{
Q_D(const QODBCDriver);
switch (f) {
case Transactions: {
if (!d->hDbc)
@ -1850,6 +1845,7 @@ bool QODBCDriver::open(const QString & db,
int,
const QString& connOpts)
{
Q_D(QODBCDriver);
if (isOpen())
close();
SQLRETURN r;
@ -1946,9 +1942,8 @@ void QODBCDriver::close()
void QODBCDriver::cleanup()
{
Q_D(QODBCDriver);
SQLRETURN r;
if (!d)
return;
if(d->hDbc) {
// Open statements/descriptors handles are automatically cleaned up by SQLDisconnect
@ -2196,11 +2191,13 @@ void QODBCDriverPrivate::checkDateTimePrecision()
QSqlResult *QODBCDriver::createResult() const
{
return new QODBCResult(this, d);
Q_D(const QODBCDriver);
return new QODBCResult(this, const_cast<QODBCDriverPrivate*>(d));
}
bool QODBCDriver::beginTransaction()
{
Q_D(QODBCDriver);
if (!isOpen()) {
qWarning() << "QODBCDriver::beginTransaction: Database not open";
return false;
@ -2220,6 +2217,7 @@ bool QODBCDriver::beginTransaction()
bool QODBCDriver::commitTransaction()
{
Q_D(QODBCDriver);
if (!isOpen()) {
qWarning() << "QODBCDriver::commitTransaction: Database not open";
return false;
@ -2237,6 +2235,7 @@ bool QODBCDriver::commitTransaction()
bool QODBCDriver::rollbackTransaction()
{
Q_D(QODBCDriver);
if (!isOpen()) {
qWarning() << "QODBCDriver::rollbackTransaction: Database not open";
return false;
@ -2254,6 +2253,7 @@ bool QODBCDriver::rollbackTransaction()
bool QODBCDriver::endTrans()
{
Q_D(QODBCDriver);
SQLUINTEGER ac(SQL_AUTOCOMMIT_ON);
SQLRETURN r = SQLSetConnectAttr(d->hDbc,
SQL_ATTR_AUTOCOMMIT,
@ -2268,6 +2268,7 @@ bool QODBCDriver::endTrans()
QStringList QODBCDriver::tables(QSql::TableType type) const
{
Q_D(const QODBCDriver);
QStringList tl;
if (!isOpen())
return tl;
@ -2345,6 +2346,7 @@ QStringList QODBCDriver::tables(QSql::TableType type) const
QSqlIndex QODBCDriver::primaryIndex(const QString& tablename) const
{
Q_D(const QODBCDriver);
QSqlIndex index(tablename);
if (!isOpen())
return index;
@ -2360,7 +2362,7 @@ QSqlIndex QODBCDriver::primaryIndex(const QString& tablename) const
return index;
}
QString catalog, schema, table;
d->splitTableQualifier(tablename, catalog, schema, table);
const_cast<QODBCDriverPrivate*>(d)->splitTableQualifier(tablename, catalog, schema, table);
if (isIdentifierEscaped(catalog, QSqlDriver::TableName))
catalog = stripDelimiters(catalog, QSqlDriver::TableName);
@ -2472,13 +2474,14 @@ QSqlIndex QODBCDriver::primaryIndex(const QString& tablename) const
QSqlRecord QODBCDriver::record(const QString& tablename) const
{
Q_D(const QODBCDriver);
QSqlRecord fil;
if (!isOpen())
return fil;
SQLHANDLE hStmt;
QString catalog, schema, table;
d->splitTableQualifier(tablename, catalog, schema, table);
const_cast<QODBCDriverPrivate*>(d)->splitTableQualifier(tablename, catalog, schema, table);
if (isIdentifierEscaped(catalog, QSqlDriver::TableName))
catalog = stripDelimiters(catalog, QSqlDriver::TableName);
@ -2597,12 +2600,14 @@ QString QODBCDriver::formatValue(const QSqlField &field,
QVariant QODBCDriver::handle() const
{
Q_D(const QODBCDriver);
return QVariant(qRegisterMetaType<SQLHANDLE>("SQLHANDLE"), &d->hDbc);
}
QString QODBCDriver::escapeIdentifier(const QString &identifier, IdentifierType) const
{
QChar quote = d->quoteChar();
Q_D(const QODBCDriver);
QChar quote = const_cast<QODBCDriverPrivate*>(d)->quoteChar();
QString res = identifier;
if(!identifier.isEmpty() && !identifier.startsWith(quote) && !identifier.endsWith(quote) ) {
res.replace(quote, QString(quote)+QString(quote));
@ -2614,7 +2619,8 @@ QString QODBCDriver::escapeIdentifier(const QString &identifier, IdentifierType)
bool QODBCDriver::isIdentifierEscaped(const QString &identifier, IdentifierType) const
{
QChar quote = d->quoteChar();
Q_D(const QODBCDriver);
QChar quote = const_cast<QODBCDriverPrivate*>(d)->quoteChar();
return identifier.size() > 2
&& identifier.startsWith(quote) //left delimited
&& identifier.endsWith(quote); //right delimited

View File

@ -123,6 +123,7 @@ private:
class Q_EXPORT_SQLDRIVER_ODBC QODBCDriver : public QSqlDriver
{
Q_DECLARE_PRIVATE(QODBCDriver)
Q_OBJECT
public:
explicit QODBCDriver(QObject *parent=0);
@ -154,10 +155,8 @@ protected:
bool rollbackTransaction();
private:
void init();
bool endTrans();
void cleanup();
QODBCDriverPrivate* d;
friend class QODBCPrivate;
};

View File

@ -124,9 +124,9 @@ inline void qPQfreemem(void *buffer)
class QPSQLDriverPrivate : public QSqlDriverPrivate
{
Q_DECLARE_PUBLIC(QPSQLDriver)
public:
QPSQLDriverPrivate(QPSQLDriver *qq) : QSqlDriverPrivate(),
q(qq),
QPSQLDriverPrivate() : QSqlDriverPrivate(),
connection(0),
isUtf8(false),
pro(QPSQLDriver::Version6),
@ -135,7 +135,6 @@ public:
hasBackslashEscape(false)
{ dbmsType = PostgreSQL; }
QPSQLDriver *q;
PGconn *connection;
bool isUtf8;
QPSQLDriver::Protocol pro;
@ -179,10 +178,11 @@ void QPSQLDriverPrivate::appendTables(QStringList &tl, QSqlQuery &t, QChar type)
PGresult * QPSQLDriverPrivate::exec(const char * stmt) const
{
Q_Q(const QPSQLDriver);
PGresult *result = PQexec(connection, stmt);
if (seid.size() && !pendingNotifyCheck) {
pendingNotifyCheck = true;
QMetaObject::invokeMethod(q, "_q_handleNotification", Qt::QueuedConnection, Q_ARG(int,0));
QMetaObject::invokeMethod(const_cast<QPSQLDriver*>(q), "_q_handleNotification", Qt::QueuedConnection, Q_ARG(int,0));
}
return result;
}
@ -205,7 +205,11 @@ public:
QString fieldSerial(int i) const { return QLatin1Char('$') + QString::number(i + 1); }
void deallocatePreparedStmt();
const QPSQLDriverPrivate * privDriver() const {Q_Q(const QPSQLResult); return reinterpret_cast<const QPSQLDriver *>(q->driver())->d; }
const QPSQLDriverPrivate * privDriver() const
{
Q_Q(const QPSQLResult);
return reinterpret_cast<const QPSQLDriver *>(q->driver())->d_func();
}
PGresult *result;
int currentSize;
@ -767,15 +771,14 @@ QPSQLDriver::Protocol QPSQLDriverPrivate::getPSQLVersion()
}
QPSQLDriver::QPSQLDriver(QObject *parent)
: QSqlDriver(parent)
: QSqlDriver(*new QPSQLDriverPrivate, parent)
{
init();
}
QPSQLDriver::QPSQLDriver(PGconn *conn, QObject *parent)
: QSqlDriver(parent)
: QSqlDriver(*new QPSQLDriverPrivate, parent)
{
init();
Q_D(QPSQLDriver);
d->connection = conn;
if (conn) {
d->pro = d->getPSQLVersion();
@ -785,24 +788,22 @@ QPSQLDriver::QPSQLDriver(PGconn *conn, QObject *parent)
}
}
void QPSQLDriver::init()
{
d = new QPSQLDriverPrivate(this);
}
QPSQLDriver::~QPSQLDriver()
{
Q_D(QPSQLDriver);
if (d->connection)
PQfinish(d->connection);
}
QVariant QPSQLDriver::handle() const
{
Q_D(const QPSQLDriver);
return QVariant::fromValue(d->connection);
}
bool QPSQLDriver::hasFeature(DriverFeature f) const
{
Q_D(const QPSQLDriver);
switch (f) {
case Transactions:
case QuerySize:
@ -849,6 +850,7 @@ bool QPSQLDriver::open(const QString & db,
int port,
const QString& connOpts)
{
Q_D(QPSQLDriver);
if (isOpen())
close();
QString connectString;
@ -891,6 +893,7 @@ bool QPSQLDriver::open(const QString & db,
void QPSQLDriver::close()
{
Q_D(QPSQLDriver);
if (isOpen()) {
d->seid.clear();
@ -915,6 +918,7 @@ QSqlResult *QPSQLDriver::createResult() const
bool QPSQLDriver::beginTransaction()
{
Q_D(const QPSQLDriver);
if (!isOpen()) {
qWarning("QPSQLDriver::beginTransaction: Database not open");
return false;
@ -932,6 +936,7 @@ bool QPSQLDriver::beginTransaction()
bool QPSQLDriver::commitTransaction()
{
Q_D(QPSQLDriver);
if (!isOpen()) {
qWarning("QPSQLDriver::commitTransaction: Database not open");
return false;
@ -965,6 +970,7 @@ bool QPSQLDriver::commitTransaction()
bool QPSQLDriver::rollbackTransaction()
{
Q_D(QPSQLDriver);
if (!isOpen()) {
qWarning("QPSQLDriver::rollbackTransaction: Database not open");
return false;
@ -982,6 +988,7 @@ bool QPSQLDriver::rollbackTransaction()
QStringList QPSQLDriver::tables(QSql::TableType type) const
{
Q_D(const QPSQLDriver);
QStringList tl;
if (!isOpen())
return tl;
@ -989,9 +996,9 @@ QStringList QPSQLDriver::tables(QSql::TableType type) const
t.setForwardOnly(true);
if (type & QSql::Tables)
d->appendTables(tl, t, QLatin1Char('r'));
const_cast<QPSQLDriverPrivate*>(d)->appendTables(tl, t, QLatin1Char('r'));
if (type & QSql::Views)
d->appendTables(tl, t, QLatin1Char('v'));
const_cast<QPSQLDriverPrivate*>(d)->appendTables(tl, t, QLatin1Char('v'));
if (type & QSql::SystemTables) {
t.exec(QLatin1String("select relname from pg_class where (relkind = 'r') "
"and (relname like 'pg_%') "));
@ -1013,6 +1020,7 @@ static void qSplitTableName(QString &tablename, QString &schema)
QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const
{
Q_D(const QPSQLDriver);
QSqlIndex idx(tablename);
if (!isOpen())
return idx;
@ -1094,6 +1102,7 @@ QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const
QSqlRecord QPSQLDriver::record(const QString& tablename) const
{
Q_D(const QPSQLDriver);
QSqlRecord info;
if (!isOpen())
return info;
@ -1231,6 +1240,7 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const
QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const
{
Q_D(const QPSQLDriver);
QString r;
if (field.isNull()) {
r = QLatin1String("NULL");
@ -1326,16 +1336,19 @@ QString QPSQLDriver::escapeIdentifier(const QString &identifier, IdentifierType)
bool QPSQLDriver::isOpen() const
{
Q_D(const QPSQLDriver);
return PQstatus(d->connection) == CONNECTION_OK;
}
QPSQLDriver::Protocol QPSQLDriver::protocol() const
{
Q_D(const QPSQLDriver);
return d->pro;
}
bool QPSQLDriver::subscribeToNotification(const QString &name)
{
Q_D(QPSQLDriver);
if (!isOpen()) {
qWarning("QPSQLDriver::subscribeToNotificationImplementation: database not open.");
return false;
@ -1373,6 +1386,7 @@ bool QPSQLDriver::subscribeToNotification(const QString &name)
bool QPSQLDriver::unsubscribeFromNotification(const QString &name)
{
Q_D(QPSQLDriver);
if (!isOpen()) {
qWarning("QPSQLDriver::unsubscribeFromNotificationImplementation: database not open.");
return false;
@ -1404,11 +1418,13 @@ bool QPSQLDriver::unsubscribeFromNotification(const QString &name)
QStringList QPSQLDriver::subscribedToNotifications() const
{
Q_D(const QPSQLDriver);
return d->seid;
}
void QPSQLDriver::_q_handleNotification(int)
{
Q_D(QPSQLDriver);
d->pendingNotifyCheck = false;
PQconsumeInput(d->connection);

View File

@ -103,6 +103,7 @@ class QPSQLDriverPrivate;
class Q_EXPORT_SQLDRIVER_PSQL QPSQLDriver : public QSqlDriver
{
friend class QPSQLResultPrivate;
Q_DECLARE_PRIVATE(QPSQLDriver)
Q_OBJECT
public:
@ -155,10 +156,6 @@ protected:
private Q_SLOTS:
void _q_handleNotification(int);
private:
void init();
QPSQLDriverPrivate *d;
};
QT_END_NAMESPACE

View File

@ -346,15 +346,15 @@ QSQLiteResult::QSQLiteResult(const QSQLiteDriver* db)
: QSqlCachedResult(db)
{
d = new QSQLiteResultPrivate(this);
d->access = db->d->access;
db->d->results.append(this);
d->access = db->d_func()->access;
const_cast<QSQLiteDriverPrivate*>(db->d_func())->results.append(this);
}
QSQLiteResult::~QSQLiteResult()
{
const QSqlDriver *sqlDriver = driver();
if (sqlDriver)
qobject_cast<const QSQLiteDriver *>(sqlDriver)->d->results.removeOne(this);
const_cast<QSQLiteDriverPrivate*>(qobject_cast<const QSQLiteDriver *>(sqlDriver)->d_func())->results.removeOne(this);
d->cleanup();
delete d;
}
@ -530,15 +530,14 @@ QVariant QSQLiteResult::handle() const
/////////////////////////////////////////////////////////
QSQLiteDriver::QSQLiteDriver(QObject * parent)
: QSqlDriver(parent)
: QSqlDriver(*new QSQLiteDriverPrivate, parent)
{
d = new QSQLiteDriverPrivate();
}
QSQLiteDriver::QSQLiteDriver(sqlite3 *connection, QObject *parent)
: QSqlDriver(parent)
: QSqlDriver(*new QSQLiteDriverPrivate, parent)
{
d = new QSQLiteDriverPrivate();
Q_D(QSQLiteDriver);
d->access = connection;
setOpen(true);
setOpenError(false);
@ -579,6 +578,7 @@ bool QSQLiteDriver::hasFeature(DriverFeature f) const
*/
bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)
{
Q_D(QSQLiteDriver);
if (isOpen())
close();
@ -622,6 +622,7 @@ bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, c
void QSQLiteDriver::close()
{
Q_D(QSQLiteDriver);
if (isOpen()) {
foreach (QSQLiteResult *result, d->results) {
result->d->finalize();
@ -778,6 +779,7 @@ QSqlRecord QSQLiteDriver::record(const QString &tbl) const
QVariant QSQLiteDriver::handle() const
{
Q_D(const QSQLiteDriver);
return QVariant::fromValue(d->access);
}

View File

@ -71,6 +71,7 @@ class QSQLiteDriver;
class Q_EXPORT_SQLDRIVER_SQLITE QSQLiteDriver : public QSqlDriver
{
Q_DECLARE_PRIVATE(QSQLiteDriver)
Q_OBJECT
friend class QSQLiteResult;
public:
@ -95,9 +96,6 @@ public:
QSqlIndex primaryIndex(const QString &table) const;
QVariant handle() const;
QString escapeIdentifier(const QString &identifier, IdentifierType) const;
private:
QSQLiteDriverPrivate* d;
};
QT_END_NAMESPACE

View File

@ -327,12 +327,12 @@ QTDSResult::QTDSResult(const QTDSDriver* db)
: QSqlCachedResult(db)
{
d = new QTDSResultPrivate();
d->login = db->d->login;
d->login = db->d_func()->login;
d->dbproc = dbopen(d->login, const_cast<char*>(db->d->hostName.toLatin1().constData()));
d->dbproc = dbopen(d->login, const_cast<char*>(db->d_func()->hostName.toLatin1().constData()));
if (!d->dbproc)
return;
if (dbuse(d->dbproc, const_cast<char*>(db->d->db.toLatin1().constData())) == FAIL)
if (dbuse(d->dbproc, const_cast<char*>(db->d_func()->db.toLatin1().constData())) == FAIL)
return;
// insert d in error handler dict
@ -541,14 +541,15 @@ QSqlRecord QTDSResult::record() const
///////////////////////////////////////////////////////////////////
QTDSDriver::QTDSDriver(QObject* parent)
: QSqlDriver(parent)
: QSqlDriver(*new QTDSDriverPrivate, parent)
{
init();
}
QTDSDriver::QTDSDriver(LOGINREC* rec, const QString& host, const QString &db, QObject* parent)
: QSqlDriver(parent)
: QSqlDriver(*new QTDSDriverPrivate, parent)
{
Q_D(QTDSDriver);
init();
d->login = rec;
d->hostName = host;
@ -561,12 +562,13 @@ QTDSDriver::QTDSDriver(LOGINREC* rec, const QString& host, const QString &db, QO
QVariant QTDSDriver::handle() const
{
Q_D(const QTDSDriver);
return QVariant(qRegisterMetaType<LOGINREC *>("LOGINREC*"), &d->login);
}
void QTDSDriver::init()
{
d = new QTDSDriverPrivate();
Q_D(QTDSDriver);
d->initialized = (dbinit() == SUCCEED);
// the following two code-lines will fail compilation on some FreeTDS versions
// just comment them out if you have FreeTDS (you won't get any errors and warnings then)
@ -606,6 +608,7 @@ bool QTDSDriver::open(const QString & db,
int /*port*/,
const QString& /*connOpts*/)
{
Q_D(QTDSDriver);
if (isOpen())
close();
if (!d->initialized) {
@ -645,6 +648,7 @@ bool QTDSDriver::open(const QString & db,
void QTDSDriver::close()
{
Q_D(QTDSDriver);
if (isOpen()) {
#ifdef Q_USE_SYBASE
dbloginfree(d->login);

View File

@ -84,6 +84,7 @@ class QTDSDriver;
class Q_EXPORT_SQLDRIVER_TDS QTDSDriver : public QSqlDriver
{
Q_DECLARE_PRIVATE(QTDSDriver)
Q_OBJECT
friend class QTDSResult;
public:
@ -115,7 +116,6 @@ protected:
bool rollbackTransaction();
private:
void init();
QTDSDriverPrivate *d;
};
QT_END_NAMESPACE

View File

@ -88,6 +88,13 @@ QSqlDriver::QSqlDriver(QObject *parent)
{
}
/*! \internal
*/
QSqlDriver::QSqlDriver(QSqlDriverPrivate &dd, QObject *parent)
: QObject(dd, parent)
{
}
/*!
Destroys the object and frees any allocated resources.
*/

View File

@ -126,6 +126,7 @@ Q_SIGNALS:
void notification(const QString &name, QSqlDriver::NotificationSource source, const QVariant &payload);
protected:
QSqlDriver(QSqlDriverPrivate &dd, QObject *parent = 0);
virtual void setOpen(bool o);
virtual void setOpenError(bool e);
virtual void setLastError(const QSqlError& e);

View File

@ -61,6 +61,8 @@ QT_BEGIN_NAMESPACE
class QSqlDriverPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QSqlDriver)
public:
enum DBMSType {UnknownDB, MSSqlServer, MySqlServer, PostgreSQL, Oracle, Sybase, SQLite, Interbase, DB2};
@ -72,8 +74,6 @@ public:
dbmsType(UnknownDB)
{ }
// @CHECK: this member is never used. It was named q, which expanded to q_func().
QSqlDriver *q_func();
uint isOpen;
uint isOpenError;
QSqlError error;