Qt 4.8
Public Functions | Protected Slots | Protected Functions | Private Functions | Properties | Friends | List of all members
QODBCDriver Class Reference

#include <qsql_odbc.h>

Inheritance diagram for QODBCDriver:
QSqlDriver QObject

Public Functions

void close ()
 Derived classes must reimplement this pure virtual function in order to close the database connection. More...
 
QSqlResultcreateResult () const
 Creates an empty SQL result on the database. More...
 
QString escapeIdentifier (const QString &identifier, IdentifierType type) const
 Returns the identifier escaped according to the database rules. More...
 
QString formatValue (const QSqlField &field, bool trimStrings) const
 Returns a string representation of the field value for the database. More...
 
QVariant handle () const
 Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no handle. More...
 
bool hasFeature (DriverFeature f) const
 Returns true if the driver supports feature feature; otherwise returns false. More...
 
bool open (const QString &db, const QString &user, const QString &password, const QString &host, int port, const QString &connOpts)
 Derived classes must reimplement this pure virtual function to open a database connection on database db, using user name user, password password, host host, port port and connection options options. More...
 
QSqlIndex primaryIndex (const QString &tablename) const
 Returns the primary index for table tableName. More...
 
 QODBCDriver (QObject *parent=0)
 
 QODBCDriver (SQLHANDLE env, SQLHANDLE con, QObject *parent=0)
 
QSqlRecord record (const QString &tablename) const
 Returns a QSqlRecord populated with the names of the fields in table tableName. More...
 
QStringList tables (QSql::TableType) const
 Returns a list of the names of the tables in the database. More...
 
virtual ~QODBCDriver ()
 
- Public Functions inherited from QSqlDriver
bool isIdentifierEscaped (const QString &identifier, IdentifierType type) const
 Returns whether identifier is escaped according to the database rules. More...
 
virtual bool isOpen () const
 Returns true if the database connection is open; otherwise returns false. More...
 
bool isOpenError () const
 Returns true if the there was an error opening the database connection; otherwise returns false. More...
 
QSqlError lastError () const
 Returns a QSqlError object which contains information about the last error that occurred on the database. More...
 
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy () const
 Returns the current default precision policy for the database connection. More...
 
 QSqlDriver (QObject *parent=0)
 Constructs a new driver with the given parent. More...
 
void setNumericalPrecisionPolicy (QSql::NumericalPrecisionPolicy precisionPolicy)
 Sets the default numerical precision policy used by queries created by this driver to precisionPolicy. More...
 
virtual QString sqlStatement (StatementType type, const QString &tableName, const QSqlRecord &rec, bool preparedStatement) const
 Returns a SQL statement of type type for the table tableName with the values from rec. More...
 
QString stripDelimiters (const QString &identifier, IdentifierType type) const
 Returns the identifier with the leading and trailing delimiters removed, identifier can either be a table name or field name, dependent on type. More...
 
QStringList subscribedToNotifications () const
 Returns a list of the names of the event notifications that are currently subscribed to. More...
 
bool subscribeToNotification (const QString &name)
 This function is called to subscribe to event notifications from the database. More...
 
bool unsubscribeFromNotification (const QString &name)
 This function is called to unsubscribe from event notifications from the database. More...
 
 ~QSqlDriver ()
 Destroys the object and frees any allocated resources. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Slots

bool isIdentifierEscapedImplementation (const QString &identifier, IdentifierType type) const
 
- Protected Slots inherited from QSqlDriver
bool isIdentifierEscapedImplementation (const QString &identifier, IdentifierType type) const
 This slot returns whether identifier is escaped according to the database rules. More...
 
QString stripDelimitersImplementation (const QString &identifier, IdentifierType type) const
 This slot returns identifier with the leading and trailing delimiters removed, identifier can either be a tablename or field name, dependent on type. More...
 
QStringList subscribedToNotificationsImplementation () const
 Returns a list of the names of the event notifications that are currently subscribed to. More...
 
bool subscribeToNotificationImplementation (const QString &name)
 This slot is called to subscribe to event notifications from the database. More...
 
bool unsubscribeFromNotificationImplementation (const QString &name)
 This slot is called to unsubscribe from event notifications from the database. More...
 

Protected Functions

bool beginTransaction ()
 This function is called to begin a transaction. More...
 
bool commitTransaction ()
 This function is called to commit a transaction. More...
 
bool rollbackTransaction ()
 This function is called to rollback a transaction. More...
 
- Protected Functions inherited from QSqlDriver
virtual void setLastError (const QSqlError &e)
 This function is used to set the value of the last error, error, that occurred on the database. More...
 
virtual void setOpen (bool o)
 This function sets the open state of the database to open. More...
 
virtual void setOpenError (bool e)
 This function sets the open error state of the database to error. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Private Functions

void cleanup ()
 
bool endTrans ()
 
void init ()
 

Properties

QODBCDriverPrivated
 

Friends

class QODBCPrivate
 

Additional Inherited Members

- Public Types inherited from QSqlDriver
enum  DriverFeature {
  Transactions, QuerySize, BLOB, Unicode,
  PreparedQueries, NamedPlaceholders, PositionalPlaceholders, LastInsertId,
  BatchOperations, SimpleLocking, LowPrecisionNumbers, EventNotifications,
  FinishQuery, MultipleResultSets
}
 This enum contains a list of features a driver might support. More...
 
enum  IdentifierType { FieldName, TableName }
 This enum contains a list of SQL identifier types. More...
 
enum  StatementType {
  WhereStatement, SelectStatement, UpdateStatement, InsertStatement,
  DeleteStatement
}
 This enum contains a list of SQL statement (or clause) types the driver can create. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QSqlDriver
void notification (const QString &name)
 This signal is emitted when the database posts an event notification that the driver subscribes to. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 113 of file qsql_odbc.h.

Constructors and Destructors

◆ QODBCDriver() [1/2]

QODBCDriver::QODBCDriver ( QObject parent = 0)
explicit

Definition at line 1743 of file qsql_odbc.cpp.

1744  : QSqlDriver(parent)
1745 {
1746  init();
1747 }
QSqlDriver(QObject *parent=0)
Constructs a new driver with the given parent.
Definition: qsqldriver.cpp:112

◆ QODBCDriver() [2/2]

QODBCDriver::QODBCDriver ( SQLHANDLE  env,
SQLHANDLE  con,
QObject parent = 0 
)

Definition at line 1749 of file qsql_odbc.cpp.

1750  : QSqlDriver(parent)
1751 {
1752  init();
1753  d->hEnv = env;
1754  d->hDbc = con;
1755  if (env && con) {
1756  setOpen(true);
1757  setOpenError(false);
1758  }
1759 }
virtual void setOpen(bool o)
This function sets the open state of the database to open.
Definition: qsqldriver.cpp:283
virtual void setOpenError(bool e)
This function sets the open error state of the database to error.
Definition: qsqldriver.cpp:297
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
QSqlDriver(QObject *parent=0)
Constructs a new driver with the given parent.
Definition: qsqldriver.cpp:112

◆ ~QODBCDriver()

QODBCDriver::~QODBCDriver ( )
virtual

Definition at line 1766 of file qsql_odbc.cpp.

1767 {
1768  cleanup();
1769  delete d;
1770 }
void cleanup()
Definition: qsql_odbc.cpp:1917
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150

Functions

◆ beginTransaction()

bool QODBCDriver::beginTransaction ( )
protectedvirtual

This function is called to begin a transaction.

If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also
commitTransaction(), rollbackTransaction()

Reimplemented from QSqlDriver.

Definition at line 2164 of file qsql_odbc.cpp.

2165 {
2166  if (!isOpen()) {
2167  qWarning() << "QODBCDriver::beginTransaction: Database not open";
2168  return false;
2169  }
2170  SQLUINTEGER ac(SQL_AUTOCOMMIT_OFF);
2171  SQLRETURN r = SQLSetConnectAttr(d->hDbc,
2172  SQL_ATTR_AUTOCOMMIT,
2173  (SQLPOINTER)ac,
2174  sizeof(ac));
2175  if (r != SQL_SUCCESS) {
2176  setLastError(qMakeError(tr("Unable to disable autocommit"),
2178  return false;
2179  }
2180  return true;
2181 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
Q_CORE_EXPORT void qWarning(const char *,...)
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
Definition: qsql_odbc.cpp:277
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350

◆ cleanup()

void QODBCDriver::cleanup ( )
private

Definition at line 1917 of file qsql_odbc.cpp.

Referenced by close(), and ~QODBCDriver().

1918 {
1919  SQLRETURN r;
1920  if (!d)
1921  return;
1922 
1923  if(d->hDbc) {
1924  // Open statements/descriptors handles are automatically cleaned up by SQLDisconnect
1925  if (isOpen()) {
1926  r = SQLDisconnect(d->hDbc);
1927  if (r != SQL_SUCCESS)
1928  qSqlWarning(QLatin1String("QODBCDriver::disconnect: Unable to disconnect datasource"), d);
1929  else
1930  d->disconnectCount++;
1931  }
1932 
1933  r = SQLFreeHandle(SQL_HANDLE_DBC, d->hDbc);
1934  if (r != SQL_SUCCESS)
1935  qSqlWarning(QLatin1String("QODBCDriver::cleanup: Unable to free connection handle"), d);
1936  d->hDbc = 0;
1937  }
1938 
1939  if (d->hEnv) {
1940  r = SQLFreeHandle(SQL_HANDLE_ENV, d->hEnv);
1941  if (r != SQL_SUCCESS)
1942  qSqlWarning(QLatin1String("QODBCDriver::cleanup: Unable to free environment handle"), d);
1943  d->hEnv = 0;
1944  }
1945 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267

◆ close()

void QODBCDriver::close ( )
virtual

Derived classes must reimplement this pure virtual function in order to close the database connection.

Return true on success, false on failure.

See also
open(), setOpen()

Implements QSqlDriver.

Definition at line 1910 of file qsql_odbc.cpp.

Referenced by open().

1911 {
1912  cleanup();
1913  setOpen(false);
1914  setOpenError(false);
1915 }
void cleanup()
Definition: qsql_odbc.cpp:1917
virtual void setOpen(bool o)
This function sets the open state of the database to open.
Definition: qsqldriver.cpp:283
virtual void setOpenError(bool e)
This function sets the open error state of the database to error.
Definition: qsqldriver.cpp:297

◆ commitTransaction()

bool QODBCDriver::commitTransaction ( )
protectedvirtual

This function is called to commit a transaction.

If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also
beginTransaction(), rollbackTransaction()

Reimplemented from QSqlDriver.

Definition at line 2183 of file qsql_odbc.cpp.

2184 {
2185  if (!isOpen()) {
2186  qWarning() << "QODBCDriver::commitTransaction: Database not open";
2187  return false;
2188  }
2189  SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC,
2190  d->hDbc,
2191  SQL_COMMIT);
2192  if (r != SQL_SUCCESS) {
2193  setLastError(qMakeError(tr("Unable to commit transaction"),
2195  return false;
2196  }
2197  return endTrans();
2198 }
bool endTrans()
Definition: qsql_odbc.cpp:2217
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
Q_CORE_EXPORT void qWarning(const char *,...)
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
Definition: qsql_odbc.cpp:277
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350

◆ createResult()

QSqlResult * QODBCDriver::createResult ( ) const
virtual

Creates an empty SQL result on the database.

Derived classes must reimplement this function and return a QSqlResult object appropriate for their database to the caller.

Implements QSqlDriver.

Definition at line 2159 of file qsql_odbc.cpp.

Referenced by open().

2160 {
2161  return new QODBCResult(this, d);
2162 }
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150

◆ endTrans()

bool QODBCDriver::endTrans ( )
private

Definition at line 2217 of file qsql_odbc.cpp.

Referenced by commitTransaction(), and rollbackTransaction().

2218 {
2219  SQLUINTEGER ac(SQL_AUTOCOMMIT_ON);
2220  SQLRETURN r = SQLSetConnectAttr(d->hDbc,
2221  SQL_ATTR_AUTOCOMMIT,
2222  (SQLPOINTER)ac,
2223  sizeof(ac));
2224  if (r != SQL_SUCCESS) {
2225  setLastError(qMakeError(tr("Unable to enable autocommit"), QSqlError::TransactionError, d));
2226  return false;
2227  }
2228  return true;
2229 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
Definition: qsql_odbc.cpp:277
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350

◆ escapeIdentifier()

QString QODBCDriver::escapeIdentifier ( const QString identifier,
IdentifierType  type 
) const
virtual

Returns the identifier escaped according to the database rules.

identifier can either be a table name or field name, dependent on type.

The default implementation does nothing.

See also
isIdentifierEscaped()

Reimplemented from QSqlDriver.

Definition at line 2565 of file qsql_odbc.cpp.

2566 {
2567  QChar quote = d->quoteChar();
2568  QString res = identifier;
2569  if(!identifier.isEmpty() && !identifier.startsWith(quote) && !identifier.endsWith(quote) ) {
2570  res.replace(quote, QString(quote)+QString(quote));
2571  res.prepend(quote).append(quote);
2572  res.replace(QLatin1Char('.'), QString(quote)+QLatin1Char('.')+QString(quote));
2573  }
2574  return res;
2575 }
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
QString & prepend(QChar c)
Definition: qstring.h:261
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
QString & append(QChar c)
Definition: qstring.cpp:1777
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ formatValue()

QString QODBCDriver::formatValue ( const QSqlField field,
bool  trimStrings 
) const
virtual

Returns a string representation of the field value for the database.

Use the other formatValue() overload instead.

This is used, for example, when constructing INSERT and UPDATE statements.

The default implementation returns the value formatted as a string according to the following rules:

  • If field is character data, the value is returned enclosed in single quotation marks, which is appropriate for many SQL databases. Any embedded single-quote characters are escaped (replaced with two single-quote characters). If trimStrings is true (the default is false), all trailing whitespace is trimmed from the field.

  • If field is date/time data, the value is formatted in ISO format and enclosed in single quotation marks. If the date/time data is invalid, "NULL" is returned.

  • If field is bytearray data, and the driver can edit binary fields, the value is formatted as a hexadecimal string.

  • For any other field type, toString() is called on its value and the result of this is returned.

See also
QVariant::toString()

Reimplemented from QSqlDriver.

Definition at line 2522 of file qsql_odbc.cpp.

2524 {
2525  QString r;
2526  if (field.isNull()) {
2527  r = QLatin1String("NULL");
2528  } else if (field.type() == QVariant::DateTime) {
2529  // Use an escape sequence for the datetime fields
2530  if (field.value().toDateTime().isValid()){
2531  QDate dt = field.value().toDateTime().date();
2532  QTime tm = field.value().toDateTime().time();
2533  // Dateformat has to be "yyyy-MM-dd hh:mm:ss", with leading zeroes if month or day < 10
2534  r = QLatin1String("{ ts '") +
2535  QString::number(dt.year()) + QLatin1Char('-') +
2536  QString::number(dt.month()).rightJustified(2, QLatin1Char('0'), true) +
2537  QLatin1Char('-') +
2538  QString::number(dt.day()).rightJustified(2, QLatin1Char('0'), true) +
2539  QLatin1Char(' ') +
2540  tm.toString() +
2541  QLatin1String("' }");
2542  } else
2543  r = QLatin1String("NULL");
2544  } else if (field.type() == QVariant::ByteArray) {
2545  QByteArray ba = field.value().toByteArray();
2546  QString res;
2547  static const char hexchars[] = "0123456789abcdef";
2548  for (int i = 0; i < ba.size(); ++i) {
2549  uchar s = (uchar) ba[i];
2550  res += QLatin1Char(hexchars[s >> 4]);
2551  res += QLatin1Char(hexchars[s & 0x0f]);
2552  }
2553  r = QLatin1String("0x") + res;
2554  } else {
2555  r = QSqlDriver::formatValue(field, trimStrings);
2556  }
2557  return r;
2558 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QVariant value() const
Returns the value of the field as a QVariant.
Definition: qsqlfield.h:71
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
unsigned char uchar
Definition: qglobal.h:994
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the time as a string.
Definition: qdatetime.cpp:1653
bool isNull() const
Returns true if the field&#39;s value is NULL; otherwise returns false.
Definition: qsqlfield.cpp:428
virtual QString formatValue(const QSqlField &field, bool trimStrings=false) const
Returns a string representation of the field value for the database.
Definition: qsqldriver.cpp:597
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ handle()

QVariant QODBCDriver::handle ( ) const
virtual

Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no handle.

Warning
Use this with uttermost care and only if you know what you're doing.
The handle returned here can become a stale pointer if the connection is modified (for example, if you close the connection).
The handle can be NULL if the connection is not open yet.

The handle returned here is database-dependent, you should query the type name of the variant before accessing it.

This example retrieves the handle for a connection to sqlite:

QSqlDatabase db = ...;
QVariant v = db.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
// v.data() returns a pointer to the handle
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0) { // check that it is not NULL
...
}
}

This snippet returns the handle for PostgreSQL or MySQL:

if (qstrcmp(v.typeName(), "PGconn*") == 0) {
PGconn *handle = *static_cast<PGconn **>(v.data());
if (handle != 0) ...
}
if (qstrcmp(v.typeName(), "MYSQL*") == 0) {
MYSQL *handle = *static_cast<MYSQL **>(v.data());
if (handle != 0) ...
}
See also
QSqlResult::handle()

Reimplemented from QSqlDriver.

Definition at line 2560 of file qsql_odbc.cpp.

2561 {
2562  return QVariant(qRegisterMetaType<SQLHANDLE>("SQLHANDLE"), &d->hDbc);
2563 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150

◆ hasFeature()

bool QODBCDriver::hasFeature ( DriverFeature  feature) const
virtual

Returns true if the driver supports feature feature; otherwise returns false.

Note that some databases need to be open() before this can be determined.

See also
DriverFeature

Implements QSqlDriver.

Definition at line 1772 of file qsql_odbc.cpp.

1773 {
1774  switch (f) {
1775  case Transactions: {
1776  if (!d->hDbc)
1777  return false;
1778  SQLUSMALLINT txn;
1779  SQLSMALLINT t;
1780  int r = SQLGetInfo(d->hDbc,
1781  (SQLUSMALLINT)SQL_TXN_CAPABLE,
1782  &txn,
1783  sizeof(txn),
1784  &t);
1785  if (r != SQL_SUCCESS || txn == SQL_TC_NONE)
1786  return false;
1787  else
1788  return true;
1789  }
1790  case Unicode:
1791  return d->unicode;
1792  case PreparedQueries:
1794  case FinishQuery:
1795  case LowPrecisionNumbers:
1796  return true;
1797  case QuerySize:
1798  case NamedPlaceholders:
1799  case LastInsertId:
1800  case BatchOperations:
1801  case SimpleLocking:
1802  case EventNotifications:
1803  return false;
1804  case MultipleResultSets:
1805  return d->hasMultiResultSets;
1806  case BLOB: {
1807  if(d->isMySqlServer)
1808  return true;
1809  else
1810  return false;
1811  }
1812  }
1813  return false;
1814 }
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150

◆ init()

void QODBCDriver::init ( )
private

Definition at line 1761 of file qsql_odbc.cpp.

Referenced by QODBCDriver().

1762 {
1763  d = new QODBCDriverPrivate();
1764 }
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150

◆ isIdentifierEscapedImplementation

bool QODBCDriver::isIdentifierEscapedImplementation ( const QString identifier,
IdentifierType  type 
) const
protectedslot

Definition at line 2577 of file qsql_odbc.cpp.

2578 {
2579  QChar quote = d->quoteChar();
2580  return identifier.size() > 2
2581  && identifier.startsWith(quote) //left delimited
2582  && identifier.endsWith(quote); //right delimited
2583 }
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796

◆ open()

bool QODBCDriver::open ( const QString db,
const QString user,
const QString password,
const QString host,
int  port,
const QString options 
)
virtual

Derived classes must reimplement this pure virtual function to open a database connection on database db, using user name user, password password, host host, port port and connection options options.

The function must return true on success and false on failure.

See also
setOpen()

Implements QSqlDriver.

Definition at line 1816 of file qsql_odbc.cpp.

1822 {
1823  if (isOpen())
1824  close();
1825  SQLRETURN r;
1826  r = SQLAllocHandle(SQL_HANDLE_ENV,
1827  SQL_NULL_HANDLE,
1828  &d->hEnv);
1829  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) {
1830  qSqlWarning(QLatin1String("QODBCDriver::open: Unable to allocate environment"), d);
1831  setOpenError(true);
1832  return false;
1833  }
1834  r = SQLSetEnvAttr(d->hEnv,
1835  SQL_ATTR_ODBC_VERSION,
1836  (SQLPOINTER)qGetODBCVersion(connOpts),
1837  SQL_IS_UINTEGER);
1838  r = SQLAllocHandle(SQL_HANDLE_DBC,
1839  d->hEnv,
1840  &d->hDbc);
1841  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) {
1842  qSqlWarning(QLatin1String("QODBCDriver::open: Unable to allocate connection"), d);
1843  setOpenError(true);
1844  return false;
1845  }
1846 
1847  if (!d->setConnectionOptions(connOpts))
1848  return false;
1849 
1850  // Create the connection string
1851  QString connQStr;
1852  // support the "DRIVER={SQL SERVER};SERVER=blah" syntax
1853  if (db.contains(QLatin1String(".dsn"), Qt::CaseInsensitive))
1854  connQStr = QLatin1String("FILEDSN=") + db;
1855  else if (db.contains(QLatin1String("DRIVER="), Qt::CaseInsensitive)
1856  || db.contains(QLatin1String("SERVER="), Qt::CaseInsensitive))
1857  connQStr = db;
1858  else
1859  connQStr = QLatin1String("DSN=") + db;
1860 
1861  if (!user.isEmpty())
1862  connQStr += QLatin1String(";UID=") + user;
1863  if (!password.isEmpty())
1864  connQStr += QLatin1String(";PWD=") + password;
1865 
1866  SQLSMALLINT cb;
1867  QVarLengthArray<SQLTCHAR> connOut(1024);
1868  memset(connOut.data(), 0, connOut.size() * sizeof(SQLTCHAR));
1869  r = SQLDriverConnect(d->hDbc,
1870  NULL,
1871 #ifdef UNICODE
1872  toSQLTCHAR(connQStr).data(),
1873 #else
1874  (SQLCHAR*)connQStr.toUtf8().data(),
1875 #endif
1876  (SQLSMALLINT)connQStr.length(),
1877  connOut.data(),
1878  1024,
1879  &cb,
1880  /*SQL_DRIVER_NOPROMPT*/0);
1881 
1882  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) {
1883  setLastError(qMakeError(tr("Unable to connect"), QSqlError::ConnectionError, d));
1884  setOpenError(true);
1885  return false;
1886  }
1887 
1888  if (!d->checkDriver()) {
1889  setLastError(qMakeError(tr("Unable to connect - Driver doesn't support all "
1890  "functionality required"), QSqlError::ConnectionError, d));
1891  setOpenError(true);
1892  return false;
1893  }
1894 
1895  d->checkUnicode();
1896  d->checkSchemaUsage();
1897  d->checkSqlServer();
1898  d->checkHasSQLFetchScroll();
1899  d->checkHasMultiResults();
1900  d->checkDateTimePrecision();
1901  setOpen(true);
1902  setOpenError(false);
1903  if(d->isMSSqlServer) {
1904  QSqlQuery i(createResult());
1905  i.exec(QLatin1String("SET QUOTED_IDENTIFIER ON"));
1906  }
1907  return true;
1908 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
virtual void setOpen(bool o)
This function sets the open state of the database to open.
Definition: qsqldriver.cpp:283
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static int qGetODBCVersion(const QString &connOpts)
Definition: qsql_odbc.cpp:655
virtual void setOpenError(bool e)
This function sets the open error state of the database to error.
Definition: qsqldriver.cpp:297
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QVarLengthArray< SQLTCHAR > toSQLTCHAR(const QString &input)
Definition: qsql_odbc.cpp:93
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: qsqlquery.h:63
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
The QString class provides a Unicode character string.
Definition: qstring.h:83
void close()
Derived classes must reimplement this pure virtual function in order to close the database connection...
Definition: qsql_odbc.cpp:1910
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
static const char * data(const QByteArray &arr)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
#define UNICODE
Definition: qsql_db2.cpp:62
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267
QSqlResult * createResult() const
Creates an empty SQL result on the database.
Definition: qsql_odbc.cpp:2159
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
Definition: qsql_odbc.cpp:277
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350

◆ primaryIndex()

QSqlIndex QODBCDriver::primaryIndex ( const QString tableName) const
virtual

Returns the primary index for table tableName.

Returns an empty QSqlIndex if the table doesn't have a primary index. The default implementation returns an empty index.

Reimplemented from QSqlDriver.

Definition at line 2308 of file qsql_odbc.cpp.

2309 {
2310  QSqlIndex index(tablename);
2311  if (!isOpen())
2312  return index;
2313  bool usingSpecialColumns = false;
2314  QSqlRecord rec = record(tablename);
2315 
2316  SQLHANDLE hStmt;
2317  SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT,
2318  d->hDbc,
2319  &hStmt);
2320  if (r != SQL_SUCCESS) {
2321  qSqlWarning(QLatin1String("QODBCDriver::primaryIndex: Unable to list primary key"), d);
2322  return index;
2323  }
2324  QString catalog, schema, table;
2325  d->splitTableQualifier(tablename, catalog, schema, table);
2326 
2328  catalog = stripDelimiters(catalog, QSqlDriver::TableName);
2329  else
2330  catalog = d->adjustCase(catalog);
2331 
2333  schema = stripDelimiters(schema, QSqlDriver::TableName);
2334  else
2335  schema = d->adjustCase(schema);
2336 
2338  table = stripDelimiters(table, QSqlDriver::TableName);
2339  else
2340  table = d->adjustCase(table);
2341 
2342  r = SQLSetStmtAttr(hStmt,
2343  SQL_ATTR_CURSOR_TYPE,
2344  (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY,
2345  SQL_IS_UINTEGER);
2346  r = SQLPrimaryKeys(hStmt,
2347 #ifdef UNICODE
2348  catalog.length() == 0 ? NULL : toSQLTCHAR(catalog).data(),
2349 #else
2350  catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toUtf8().data(),
2351 #endif
2352  catalog.length(),
2353 #ifdef UNICODE
2354  schema.length() == 0 ? NULL : toSQLTCHAR(schema).data(),
2355 #else
2356  schema.length() == 0 ? NULL : (SQLCHAR*)schema.toUtf8().data(),
2357 #endif
2358  schema.length(),
2359 #ifdef UNICODE
2360  toSQLTCHAR(table).data(),
2361 #else
2362  (SQLCHAR*)table.toUtf8().data(),
2363 #endif
2364  table.length() /* in characters, not in bytes */);
2365 
2366  // if the SQLPrimaryKeys() call does not succeed (e.g the driver
2367  // does not support it) - try an alternative method to get hold of
2368  // the primary index (e.g MS Access and FoxPro)
2369  if (r != SQL_SUCCESS) {
2370  r = SQLSpecialColumns(hStmt,
2371  SQL_BEST_ROWID,
2372 #ifdef UNICODE
2373  catalog.length() == 0 ? NULL : toSQLTCHAR(catalog).data(),
2374 #else
2375  catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toUtf8().data(),
2376 #endif
2377  catalog.length(),
2378 #ifdef UNICODE
2379  schema.length() == 0 ? NULL : toSQLTCHAR(schema).data(),
2380 #else
2381  schema.length() == 0 ? NULL : (SQLCHAR*)schema.toUtf8().data(),
2382 #endif
2383  schema.length(),
2384 #ifdef UNICODE
2385  toSQLTCHAR(table).data(),
2386 #else
2387  (SQLCHAR*)table.toUtf8().data(),
2388 #endif
2389  table.length(),
2390  SQL_SCOPE_CURROW,
2391  SQL_NULLABLE);
2392 
2393  if (r != SQL_SUCCESS) {
2394  qSqlWarning(QLatin1String("QODBCDriver::primaryIndex: Unable to execute primary key list"), d);
2395  } else {
2396  usingSpecialColumns = true;
2397  }
2398  }
2399 
2400  if (d->hasSQLFetchScroll)
2401  r = SQLFetchScroll(hStmt,
2402  SQL_FETCH_NEXT,
2403  0);
2404  else
2405  r = SQLFetch(hStmt);
2406 
2407  int fakeId = 0;
2408  QString cName, idxName;
2409  // Store all fields in a StringList because some drivers can't detail fields in this FETCH loop
2410  while (r == SQL_SUCCESS) {
2411  if (usingSpecialColumns) {
2412  cName = qGetStringData(hStmt, 1, -1, d->unicode); // column name
2413  idxName = QString::number(fakeId++); // invent a fake index name
2414  } else {
2415  cName = qGetStringData(hStmt, 3, -1, d->unicode); // column name
2416  idxName = qGetStringData(hStmt, 5, -1, d->unicode); // pk index name
2417  }
2418  index.append(rec.field(cName));
2419  index.setName(idxName);
2420 
2421  if (d->hasSQLFetchScroll)
2422  r = SQLFetchScroll(hStmt,
2423  SQL_FETCH_NEXT,
2424  0);
2425  else
2426  r = SQLFetch(hStmt);
2427 
2428  }
2429  r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
2430  if (r!= SQL_SUCCESS)
2431  qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle") + QString::number(r), d);
2432  return index;
2433 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QSqlIndex class provides functions to manipulate and describe database indexes.
Definition: qsqlindex.h:55
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QVarLengthArray< SQLTCHAR > toSQLTCHAR(const QString &input)
Definition: qsql_odbc.cpp:93
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const
Returns whether identifier is escaped according to the database rules.
Definition: qsqldriver.cpp:429
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
QSqlRecord record(const QString &tablename) const
Returns a QSqlRecord populated with the names of the fields in table tableName.
Definition: qsql_odbc.cpp:2435
#define UNICODE
Definition: qsql_db2.cpp:62
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267
QSqlField field(int i) const
Returns the field at position index.
Definition: qsqlrecord.cpp:289
static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool unicode=false)
Definition: qsql_odbc.cpp:353
quint16 index
QString stripDelimiters(const QString &identifier, IdentifierType type) const
Returns the identifier with the leading and trailing delimiters removed, identifier can either be a t...
Definition: qsqldriver.cpp:455

◆ record()

QSqlRecord QODBCDriver::record ( const QString tableName) const
virtual

Returns a QSqlRecord populated with the names of the fields in table tableName.

Use query.

If no such table exists, an empty record is returned. The default implementation returns an empty record.

record() instead.

Reimplemented from QSqlDriver.

Definition at line 2435 of file qsql_odbc.cpp.

Referenced by primaryIndex().

2436 {
2437  QSqlRecord fil;
2438  if (!isOpen())
2439  return fil;
2440 
2441  SQLHANDLE hStmt;
2442  QString catalog, schema, table;
2443  d->splitTableQualifier(tablename, catalog, schema, table);
2444 
2446  catalog = stripDelimiters(catalog, QSqlDriver::TableName);
2447  else
2448  catalog = d->adjustCase(catalog);
2449 
2451  schema = stripDelimiters(schema, QSqlDriver::TableName);
2452  else
2453  schema = d->adjustCase(schema);
2454 
2456  table = stripDelimiters(table, QSqlDriver::TableName);
2457  else
2458  table = d->adjustCase(table);
2459 
2460  SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT,
2461  d->hDbc,
2462  &hStmt);
2463  if (r != SQL_SUCCESS) {
2464  qSqlWarning(QLatin1String("QODBCDriver::record: Unable to allocate handle"), d);
2465  return fil;
2466  }
2467  r = SQLSetStmtAttr(hStmt,
2468  SQL_ATTR_CURSOR_TYPE,
2469  (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY,
2470  SQL_IS_UINTEGER);
2471  r = SQLColumns(hStmt,
2472 #ifdef UNICODE
2473  catalog.length() == 0 ? NULL : toSQLTCHAR(catalog).data(),
2474 #else
2475  catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toUtf8().data(),
2476 #endif
2477  catalog.length(),
2478 #ifdef UNICODE
2479  schema.length() == 0 ? NULL : toSQLTCHAR(schema).data(),
2480 #else
2481  schema.length() == 0 ? NULL : (SQLCHAR*)schema.toUtf8().data(),
2482 #endif
2483  schema.length(),
2484 #ifdef UNICODE
2485  toSQLTCHAR(table).data(),
2486 #else
2487  (SQLCHAR*)table.toUtf8().data(),
2488 #endif
2489  table.length(),
2490  NULL,
2491  0);
2492  if (r != SQL_SUCCESS)
2493  qSqlWarning(QLatin1String("QODBCDriver::record: Unable to execute column list"), d);
2494 
2495  if (d->hasSQLFetchScroll)
2496  r = SQLFetchScroll(hStmt,
2497  SQL_FETCH_NEXT,
2498  0);
2499  else
2500  r = SQLFetch(hStmt);
2501 
2502  // Store all fields in a StringList because some drivers can't detail fields in this FETCH loop
2503  while (r == SQL_SUCCESS) {
2504 
2505  fil.append(qMakeFieldInfo(hStmt, d));
2506 
2507  if (d->hasSQLFetchScroll)
2508  r = SQLFetchScroll(hStmt,
2509  SQL_FETCH_NEXT,
2510  0);
2511  else
2512  r = SQLFetch(hStmt);
2513  }
2514 
2515  r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
2516  if (r!= SQL_SUCCESS)
2517  qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle ") + QString::number(r), d);
2518 
2519  return fil;
2520 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QVarLengthArray< SQLTCHAR > toSQLTCHAR(const QString &input)
Definition: qsql_odbc.cpp:93
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const
Returns whether identifier is escaped according to the database rules.
Definition: qsqldriver.cpp:429
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
#define UNICODE
Definition: qsql_db2.cpp:62
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
Definition: qsqlrecord.cpp:312
static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, const QODBCDriverPrivate *p)
Definition: qsql_odbc.cpp:574
QString stripDelimiters(const QString &identifier, IdentifierType type) const
Returns the identifier with the leading and trailing delimiters removed, identifier can either be a t...
Definition: qsqldriver.cpp:455

◆ rollbackTransaction()

bool QODBCDriver::rollbackTransaction ( )
protectedvirtual

This function is called to rollback a transaction.

If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also
beginTransaction(), commitTransaction()

Reimplemented from QSqlDriver.

Definition at line 2200 of file qsql_odbc.cpp.

2201 {
2202  if (!isOpen()) {
2203  qWarning() << "QODBCDriver::rollbackTransaction: Database not open";
2204  return false;
2205  }
2206  SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC,
2207  d->hDbc,
2208  SQL_ROLLBACK);
2209  if (r != SQL_SUCCESS) {
2210  setLastError(qMakeError(tr("Unable to rollback transaction"),
2212  return false;
2213  }
2214  return endTrans();
2215 }
bool endTrans()
Definition: qsql_odbc.cpp:2217
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
Q_CORE_EXPORT void qWarning(const char *,...)
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
Definition: qsql_odbc.cpp:277
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350

◆ tables()

QStringList QODBCDriver::tables ( QSql::TableType  tableType) const
virtual

Returns a list of the names of the tables in the database.

The default implementation returns an empty list.

The tableType argument describes what types of tables should be returned. Due to binary compatibility, the string contains the value of the enum QSql::TableTypes as text. An empty string should be treated as QSql::Tables for backward compatibility.

Reimplemented from QSqlDriver.

Definition at line 2231 of file qsql_odbc.cpp.

2232 {
2233  QStringList tl;
2234  if (!isOpen())
2235  return tl;
2236  SQLHANDLE hStmt;
2237 
2238  SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT,
2239  d->hDbc,
2240  &hStmt);
2241  if (r != SQL_SUCCESS) {
2242  qSqlWarning(QLatin1String("QODBCDriver::tables: Unable to allocate handle"), d);
2243  return tl;
2244  }
2245  r = SQLSetStmtAttr(hStmt,
2246  SQL_ATTR_CURSOR_TYPE,
2247  (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY,
2248  SQL_IS_UINTEGER);
2249  QStringList tableType;
2250  if (type & QSql::Tables)
2251  tableType += QLatin1String("TABLE");
2252  if (type & QSql::Views)
2253  tableType += QLatin1String("VIEW");
2254  if (type & QSql::SystemTables)
2255  tableType += QLatin1String("SYSTEM TABLE");
2256  if (tableType.isEmpty())
2257  return tl;
2258 
2259  QString joinedTableTypeString = tableType.join(QLatin1String(","));
2260 
2261  r = SQLTables(hStmt,
2262  NULL,
2263  0,
2264  NULL,
2265  0,
2266  NULL,
2267  0,
2268 #ifdef UNICODE
2269  toSQLTCHAR(joinedTableTypeString).data(),
2270 #else
2271  (SQLCHAR*)joinedTableTypeString.toUtf8().data(),
2272 #endif
2273  joinedTableTypeString.length() /* characters, not bytes */);
2274 
2275  if (r != SQL_SUCCESS)
2276  qSqlWarning(QLatin1String("QODBCDriver::tables Unable to execute table list"), d);
2277 
2278  if (d->hasSQLFetchScroll)
2279  r = SQLFetchScroll(hStmt,
2280  SQL_FETCH_NEXT,
2281  0);
2282  else
2283  r = SQLFetch(hStmt);
2284 
2285  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO && r != SQL_NO_DATA) {
2286  qWarning() << "QODBCDriver::tables failed to retrieve table/view list: (" << r << "," << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ")";
2287  return QStringList();
2288  }
2289 
2290  while (r == SQL_SUCCESS) {
2291  QString fieldVal = qGetStringData(hStmt, 2, -1, false);
2292  tl.append(fieldVal);
2293 
2294  if (d->hasSQLFetchScroll)
2295  r = SQLFetchScroll(hStmt,
2296  SQL_FETCH_NEXT,
2297  0);
2298  else
2299  r = SQLFetch(hStmt);
2300  }
2301 
2302  r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
2303  if (r!= SQL_SUCCESS)
2304  qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle") + QString::number(r), d);
2305  return tl;
2306 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
int type
Definition: qmetatype.cpp:239
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QVarLengthArray< SQLTCHAR > toSQLTCHAR(const QString &input)
Definition: qsql_odbc.cpp:93
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static QString qWarnODBCHandle(int handleType, SQLHANDLE handle, int *nativeCode=0)
Definition: qsql_odbc.cpp:202
QODBCDriverPrivate * d
Definition: qsql_odbc.h:150
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
#define UNICODE
Definition: qsql_db2.cpp:62
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267
QString join(const QString &sep) const
Joins all the string list&#39;s strings into a single string with each element separated by the given sep...
Definition: qstringlist.h:162
static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool unicode=false)
Definition: qsql_odbc.cpp:353

Friends and Related Functions

◆ QODBCPrivate

friend class QODBCPrivate
friend

Definition at line 151 of file qsql_odbc.h.

Properties

◆ d

QODBCDriverPrivate* QODBCDriver::d
private

The documentation for this class was generated from the following files: