Qt 4.8
Classes | Macros | Functions | Variables
qsql_odbc.cpp File Reference
#include "qsql_odbc.h"
#include <qsqlrecord.h>
#include <qcoreapplication.h>
#include <qvariant.h>
#include <qdatetime.h>
#include <qsqlerror.h>
#include <qsqlfield.h>
#include <qsqlindex.h>
#include <qstringlist.h>
#include <qvarlengtharray.h>
#include <qvector.h>
#include <qmath.h>
#include <QDebug>
#include <QSqlQuery>

Go to the source code of this file.

Classes

class  QODBCDriverPrivate
 
class  QODBCPrivate
 

Macros

#define ODBC_CHECK_DRIVER
 

Functions

static QString fromSQLTCHAR (const QVarLengthArray< SQLTCHAR > &input, int size=-1)
 
template<class T >
static QVariant::Type qDecodeODBCType (SQLSMALLINT sqltype, const T *p, bool isSigned=true)
 
static QVariant qGetBigIntData (SQLHANDLE hStmt, int column, bool isSigned=true)
 
static QVariant qGetBinaryData (SQLHANDLE hStmt, int column)
 
static QVariant qGetDoubleData (SQLHANDLE hStmt, int column)
 
static QVariant qGetIntData (SQLHANDLE hStmt, int column, bool isSigned=true)
 
static int qGetODBCVersion (const QString &connOpts)
 
static QString qGetStringData (SQLHANDLE hStmt, int column, int colSize, bool unicode=false)
 
static QSqlError qMakeError (const QString &err, QSqlError::ErrorType type, const QODBCPrivate *p)
 
static QSqlError qMakeError (const QString &err, QSqlError::ErrorType type, const QODBCDriverPrivate *p)
 
static QSqlField qMakeFieldInfo (const SQLHANDLE hStmt, const QODBCDriverPrivate *p)
 
static QSqlField qMakeFieldInfo (const QODBCPrivate *p, int i)
 
static QString qODBCWarn (const QODBCPrivate *odbc, int *nativeCode=0)
 
static QString qODBCWarn (const QODBCDriverPrivate *odbc, int *nativeCode=0)
 
static void qSqlWarning (const QString &message, const QODBCPrivate *odbc)
 
static void qSqlWarning (const QString &message, const QODBCDriverPrivate *odbc)
 
static QString qWarnODBCHandle (int handleType, SQLHANDLE handle, int *nativeCode=0)
 
static QVarLengthArray< SQLTCHAR > toSQLTCHAR (const QString &input)
 

Variables

static const int COLNAMESIZE = 256
 
static const SQLSMALLINT qParamType [4] = { SQL_PARAM_INPUT, SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT }
 

Macro Definition Documentation

◆ ODBC_CHECK_DRIVER

#define ODBC_CHECK_DRIVER

Definition at line 64 of file qsql_odbc.cpp.

Function Documentation

◆ fromSQLTCHAR()

static QString fromSQLTCHAR ( const QVarLengthArray< SQLTCHAR > &  input,
int  size = -1 
)
inlinestatic

Definition at line 70 of file qsql_odbc.cpp.

Referenced by QODBCDriver::cleanup(), QODBCResult::exec(), qGetStringData(), qMakeFieldInfo(), and qWarnODBCHandle().

71 {
72  QString result;
73 
74  int realsize = qMin(size, input.size());
75  if(realsize > 0 && input[realsize-1] == 0)
76  realsize--;
77  switch(sizeof(SQLTCHAR)) {
78  case 1:
79  result=QString::fromUtf8((const char *)input.constData(), realsize);
80  break;
81  case 2:
82  result=QString::fromUtf16((const ushort *)input.constData(), realsize);
83  break;
84  case 4:
85  result=QString::fromUcs4((const uint *)input.constData(), realsize);
86  break;
87  default:
88  qCritical() << "sizeof(SQLTCHAR) is " << sizeof(SQLTCHAR) << "Don't know how to handle this";
89  }
90  return result;
91 }
const T * constData() const
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
unsigned int uint
Definition: qglobal.h:996
unsigned short ushort
Definition: qglobal.h:995
Q_CORE_EXPORT void qCritical(const char *,...)
int size() const
static QString fromUtf16(const ushort *, int size=-1)
Returns a QString initialized with the first size characters of the Unicode string unicode (ISO-10646...
Definition: qstring.cpp:4329
static QString fromUcs4(const uint *, int size=-1)
Returns a QString initialized with the first size characters of the Unicode string unicode (ISO-10646...
Definition: qstring.cpp:4356

◆ qDecodeODBCType()

template<class T >
static QVariant::Type qDecodeODBCType ( SQLSMALLINT  sqltype,
const T *  p,
bool  isSigned = true 
)
static

Definition at line 293 of file qsql_odbc.cpp.

Referenced by qMakeFieldInfo().

294 {
295  Q_UNUSED(p);
297  switch (sqltype) {
298  case SQL_DECIMAL:
299  case SQL_NUMERIC:
300  case SQL_REAL:
301  case SQL_FLOAT:
302  case SQL_DOUBLE:
303  type = QVariant::Double;
304  break;
305  case SQL_SMALLINT:
306  case SQL_INTEGER:
307  case SQL_BIT:
308  type = isSigned ? QVariant::Int : QVariant::UInt;
309  break;
310  case SQL_TINYINT:
311  type = QVariant::UInt;
312  break;
313  case SQL_BIGINT:
314  type = isSigned ? QVariant::LongLong : QVariant::ULongLong;
315  break;
316  case SQL_BINARY:
317  case SQL_VARBINARY:
318  case SQL_LONGVARBINARY:
319  type = QVariant::ByteArray;
320  break;
321  case SQL_DATE:
322  case SQL_TYPE_DATE:
323  type = QVariant::Date;
324  break;
325  case SQL_TIME:
326  case SQL_TYPE_TIME:
327  type = QVariant::Time;
328  break;
329  case SQL_TIMESTAMP:
330  case SQL_TYPE_TIMESTAMP:
331  type = QVariant::DateTime;
332  break;
333  case SQL_WCHAR:
334  case SQL_WVARCHAR:
335  case SQL_WLONGVARCHAR:
336  type = QVariant::String;
337  break;
338  case SQL_CHAR:
339  case SQL_VARCHAR:
340 #if (ODBCVER >= 0x0350)
341  case SQL_GUID:
342 #endif
343  case SQL_LONGVARCHAR:
344  type = QVariant::String;
345  break;
346  default:
347  type = QVariant::ByteArray;
348  break;
349  }
350  return type;
351 }
int type
Definition: qmetatype.cpp:239
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ qGetBigIntData()

static QVariant qGetBigIntData ( SQLHANDLE  hStmt,
int  column,
bool  isSigned = true 
)
static

Definition at line 551 of file qsql_odbc.cpp.

Referenced by QODBCResult::data().

552 {
553  SQLBIGINT lngbuf = 0;
554  SQLLEN lengthIndicator = 0;
555  SQLRETURN r = SQLGetData(hStmt,
556  column+1,
557  isSigned ? SQL_C_SBIGINT : SQL_C_UBIGINT,
558  (SQLPOINTER) &lngbuf,
559  sizeof(lngbuf),
560  &lengthIndicator);
561  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO)
562  return QVariant(QVariant::Invalid);
563  if (lengthIndicator == SQL_NULL_DATA)
565 
566  if (isSigned)
567  return qint64(lngbuf);
568  else
569  return quint64(lngbuf);
570 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
unsigned __int64 quint64
Definition: qglobal.h:943
__int64 qint64
Definition: qglobal.h:942

◆ qGetBinaryData()

static QVariant qGetBinaryData ( SQLHANDLE  hStmt,
int  column 
)
static

Definition at line 454 of file qsql_odbc.cpp.

Referenced by QODBCResult::data().

455 {
456  QByteArray fieldVal;
457  SQLSMALLINT colNameLen;
458  SQLSMALLINT colType;
459  SQLULEN colSize;
460  SQLSMALLINT colScale;
461  SQLSMALLINT nullable;
462  SQLLEN lengthIndicator = 0;
463  SQLRETURN r = SQL_ERROR;
464 
466 
467  r = SQLDescribeCol(hStmt,
468  column + 1,
469  colName.data(),
470  COLNAMESIZE,
471  &colNameLen,
472  &colType,
473  &colSize,
474  &colScale,
475  &nullable);
476  if (r != SQL_SUCCESS)
477  qWarning() << "qGetBinaryData: Unable to describe column" << column;
478  // SQLDescribeCol may return 0 if size cannot be determined
479  if (!colSize)
480  colSize = 255;
481  else if (colSize > 65536) // read the field in 64 KB chunks
482  colSize = 65536;
483  fieldVal.resize(colSize);
484  ulong read = 0;
485  while (true) {
486  r = SQLGetData(hStmt,
487  column+1,
488  SQL_C_BINARY,
489  (SQLPOINTER)(fieldVal.constData() + read),
490  colSize,
491  &lengthIndicator);
492  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO)
493  break;
494  if (lengthIndicator == SQL_NULL_DATA)
496  if (lengthIndicator > SQLLEN(colSize) || lengthIndicator == SQL_NO_TOTAL) {
497  read += colSize;
498  colSize = 65536;
499  } else {
500  read += lengthIndicator;
501  }
502  if (r == SQL_SUCCESS) { // the whole field was read in one chunk
503  fieldVal.resize(read);
504  break;
505  }
506  fieldVal.resize(fieldVal.size() + colSize);
507  }
508  return fieldVal;
509 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static const int COLNAMESIZE
Definition: qsql_odbc.cpp:66
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned long ulong
Definition: qglobal.h:997
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402

◆ qGetDoubleData()

static QVariant qGetDoubleData ( SQLHANDLE  hStmt,
int  column 
)
static

Definition at line 531 of file qsql_odbc.cpp.

Referenced by QODBCResult::data().

532 {
533  SQLDOUBLE dblbuf;
534  SQLLEN lengthIndicator = 0;
535  SQLRETURN r = SQLGetData(hStmt,
536  column+1,
537  SQL_C_DOUBLE,
538  (SQLPOINTER) &dblbuf,
539  0,
540  &lengthIndicator);
541  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) {
542  return QVariant(QVariant::Invalid);
543  }
544  if(lengthIndicator == SQL_NULL_DATA)
545  return QVariant(QVariant::Double);
546 
547  return (double) dblbuf;
548 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ qGetIntData()

static QVariant qGetIntData ( SQLHANDLE  hStmt,
int  column,
bool  isSigned = true 
)
static

Definition at line 511 of file qsql_odbc.cpp.

Referenced by QODBCResult::data(), and qMakeFieldInfo().

512 {
513  SQLINTEGER intbuf = 0;
514  SQLLEN lengthIndicator = 0;
515  SQLRETURN r = SQLGetData(hStmt,
516  column+1,
517  isSigned ? SQL_C_SLONG : SQL_C_ULONG,
518  (SQLPOINTER)&intbuf,
519  sizeof(intbuf),
520  &lengthIndicator);
521  if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO)
522  return QVariant(QVariant::Invalid);
523  if (lengthIndicator == SQL_NULL_DATA)
524  return QVariant(QVariant::Int);
525  if (isSigned)
526  return int(intbuf);
527  else
528  return uint(intbuf);
529 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
unsigned int uint
Definition: qglobal.h:996

◆ qGetODBCVersion()

static int qGetODBCVersion ( const QString connOpts)
static

Definition at line 655 of file qsql_odbc.cpp.

Referenced by QODBCDriver::open().

656 {
657  if (connOpts.contains(QLatin1String("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3"), Qt::CaseInsensitive))
658  return SQL_OV_ODBC3;
659  return SQL_OV_ODBC2;
660 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654

◆ qGetStringData()

static QString qGetStringData ( SQLHANDLE  hStmt,
int  column,
int  colSize,
bool  unicode = false 
)
static

Definition at line 353 of file qsql_odbc.cpp.

Referenced by QODBCResult::data(), QODBCDriver::primaryIndex(), qMakeFieldInfo(), and QODBCDriver::tables().

354 {
355  QString fieldVal;
356  SQLRETURN r = SQL_ERROR;
357  SQLLEN lengthIndicator = 0;
358 
359  // NB! colSize must be a multiple of 2 for unicode enabled DBs
360  if (colSize <= 0) {
361  colSize = 256;
362  } else if (colSize > 65536) { // limit buffer size to 64 KB
363  colSize = 65536;
364  } else {
365  colSize++; // make sure there is room for more than the 0 termination
366  }
367  if(unicode) {
368  r = SQLGetData(hStmt,
369  column+1,
370  SQL_C_TCHAR,
371  NULL,
372  0,
373  &lengthIndicator);
374  if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && lengthIndicator > 0)
375  colSize = lengthIndicator/sizeof(SQLTCHAR) + 1;
376  QVarLengthArray<SQLTCHAR> buf(colSize);
377  memset(buf.data(), 0, colSize*sizeof(SQLTCHAR));
378  while (true) {
379  r = SQLGetData(hStmt,
380  column+1,
381  SQL_C_TCHAR,
382  (SQLPOINTER)buf.data(),
383  colSize*sizeof(SQLTCHAR),
384  &lengthIndicator);
385  if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) {
386  if (lengthIndicator == SQL_NULL_DATA || lengthIndicator == SQL_NO_TOTAL) {
387  fieldVal.clear();
388  break;
389  }
390  // if SQL_SUCCESS_WITH_INFO is returned, indicating that
391  // more data can be fetched, the length indicator does NOT
392  // contain the number of bytes returned - it contains the
393  // total number of bytes that CAN be fetched
394  // colSize-1: remove 0 termination when there is more data to fetch
395  int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator/sizeof(SQLTCHAR);
396  fieldVal += fromSQLTCHAR(buf, rSize);
397  if (lengthIndicator < SQLLEN(colSize*sizeof(SQLTCHAR))) {
398  // workaround for Drivermanagers that don't return SQL_NO_DATA
399  break;
400  }
401  } else if (r == SQL_NO_DATA) {
402  break;
403  } else {
404  qWarning() << "qGetStringData: Error while fetching data (" << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ')';
405  fieldVal.clear();
406  break;
407  }
408  }
409  } else {
410  r = SQLGetData(hStmt,
411  column+1,
412  SQL_C_CHAR,
413  NULL,
414  0,
415  &lengthIndicator);
416  if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && lengthIndicator > 0)
417  colSize = lengthIndicator + 1;
418  QVarLengthArray<SQLCHAR> buf(colSize);
419  while (true) {
420  r = SQLGetData(hStmt,
421  column+1,
422  SQL_C_CHAR,
423  (SQLPOINTER)buf.data(),
424  colSize,
425  &lengthIndicator);
426  if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) {
427  if (lengthIndicator == SQL_NULL_DATA || lengthIndicator == SQL_NO_TOTAL) {
428  fieldVal.clear();
429  break;
430  }
431  // if SQL_SUCCESS_WITH_INFO is returned, indicating that
432  // more data can be fetched, the length indicator does NOT
433  // contain the number of bytes returned - it contains the
434  // total number of bytes that CAN be fetched
435  // colSize-1: remove 0 termination when there is more data to fetch
436  int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator;
437  fieldVal += QString::fromUtf8((const char *)buf.constData(), rSize);
438  if (lengthIndicator < SQLLEN(colSize)) {
439  // workaround for Drivermanagers that don't return SQL_NO_DATA
440  break;
441  }
442  } else if (r == SQL_NO_DATA) {
443  break;
444  } else {
445  qWarning() << "qGetStringData: Error while fetching data (" << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ')';
446  fieldVal.clear();
447  break;
448  }
449  }
450  }
451  return fieldVal;
452 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qWarnODBCHandle(int handleType, SQLHANDLE handle, int *nativeCode=0)
Definition: qsql_odbc.cpp:202
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString fromSQLTCHAR(const QVarLengthArray< SQLTCHAR > &input, int size=-1)
Definition: qsql_odbc.cpp:70

◆ qMakeError() [1/2]

static QSqlError qMakeError ( const QString err,
QSqlError::ErrorType  type,
const QODBCPrivate p 
)
static

Definition at line 277 of file qsql_odbc.cpp.

Referenced by QODBCDriver::beginTransaction(), QODBCDriver::commitTransaction(), QODBCDriver::endTrans(), QODBCResult::exec(), QODBCResult::fetch(), QODBCResult::fetchFirst(), QODBCResult::fetchLast(), QODBCResult::fetchNext(), QODBCResult::fetchPrevious(), QODBCResult::nextResult(), QODBCDriver::open(), QODBCResult::prepare(), QODBCResult::reset(), and QODBCDriver::rollbackTransaction().

278 {
279  int nativeCode = -1;
280  QString message = qODBCWarn(p, &nativeCode);
281  return QSqlError(QLatin1String("QODBC3: ") + err, message, type, nativeCode);
282 }
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
static QString qODBCWarn(const QODBCPrivate *odbc, int *nativeCode=0)
Definition: qsql_odbc.cpp:254
int type
Definition: qmetatype.cpp:239
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ qMakeError() [2/2]

static QSqlError qMakeError ( const QString err,
QSqlError::ErrorType  type,
const QODBCDriverPrivate p 
)
static

Definition at line 284 of file qsql_odbc.cpp.

286 {
287  int nativeCode = -1;
288  QString message = qODBCWarn(p, &nativeCode);
289  return QSqlError(QLatin1String("QODBC3: ") + err, qODBCWarn(p), type, nativeCode);
290 }
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
static QString qODBCWarn(const QODBCPrivate *odbc, int *nativeCode=0)
Definition: qsql_odbc.cpp:254
int type
Definition: qmetatype.cpp:239
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ qMakeFieldInfo() [1/2]

static QSqlField qMakeFieldInfo ( const SQLHANDLE  hStmt,
const QODBCDriverPrivate p 
)
static

Definition at line 574 of file qsql_odbc.cpp.

Referenced by QODBCResult::exec(), QODBCResult::nextResult(), QODBCDriver::record(), and QODBCResult::reset().

575 {
576  QString fname = qGetStringData(hStmt, 3, -1, p->unicode);
577  int type = qGetIntData(hStmt, 4).toInt(); // column type
578  QSqlField f(fname, qDecodeODBCType(type, p));
579  int required = qGetIntData(hStmt, 10).toInt(); // nullable-flag
580  // required can be SQL_NO_NULLS, SQL_NULLABLE or SQL_NULLABLE_UNKNOWN
581  if (required == SQL_NO_NULLS)
582  f.setRequired(true);
583  else if (required == SQL_NULLABLE)
584  f.setRequired(false);
585  // else we don't know
586  QVariant var = qGetIntData(hStmt, 6);
587  f.setLength(var.isNull() ? -1 : var.toInt()); // column size
588  var = qGetIntData(hStmt, 8).toInt();
589  f.setPrecision(var.isNull() ? -1 : var.toInt()); // precision
590  f.setSqlType(type);
591  return f;
592 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
int type
Definition: qmetatype.cpp:239
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
static QVariant::Type qDecodeODBCType(SQLSMALLINT sqltype, const T *p, bool isSigned=true)
Definition: qsql_odbc.cpp:293
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QVariant qGetIntData(SQLHANDLE hStmt, int column, bool isSigned=true)
Definition: qsql_odbc.cpp:511
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool unicode=false)
Definition: qsql_odbc.cpp:353
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56

◆ qMakeFieldInfo() [2/2]

static QSqlField qMakeFieldInfo ( const QODBCPrivate p,
int  i 
)
static

Definition at line 594 of file qsql_odbc.cpp.

595 {
596  SQLSMALLINT colNameLen;
597  SQLSMALLINT colType;
598  SQLULEN colSize;
599  SQLSMALLINT colScale;
600  SQLSMALLINT nullable;
601  SQLRETURN r = SQL_ERROR;
603  r = SQLDescribeCol(p->hStmt,
604  i+1,
605  colName.data(),
606  (SQLSMALLINT)COLNAMESIZE,
607  &colNameLen,
608  &colType,
609  &colSize,
610  &colScale,
611  &nullable);
612 
613  if (r != SQL_SUCCESS) {
614  qSqlWarning(QString::fromLatin1("qMakeField: Unable to describe column %1").arg(i), p);
615  return QSqlField();
616  }
617 
618  SQLLEN unsignedFlag = SQL_FALSE;
619  r = SQLColAttribute (p->hStmt,
620  i + 1,
621  SQL_DESC_UNSIGNED,
622  0,
623  0,
624  0,
625  &unsignedFlag);
626  if (r != SQL_SUCCESS) {
627  qSqlWarning(QString::fromLatin1("qMakeField: Unable to get column attributes for column %1").arg(i), p);
628  }
629 
630 #ifdef UNICODE
631  QString qColName(fromSQLTCHAR(colName, colNameLen));
632 #else
633  QString qColName = QString::fromUtf8((const char *)colName.constData());
634 #endif
635  // nullable can be SQL_NO_NULLS, SQL_NULLABLE or SQL_NULLABLE_UNKNOWN
636  int required = -1;
637  if (nullable == SQL_NO_NULLS) {
638  required = 1;
639  } else if (nullable == SQL_NULLABLE) {
640  required = 0;
641  }
642  QVariant::Type type = qDecodeODBCType(colType, p, unsignedFlag == SQL_FALSE);
643  QSqlField f(qColName, type);
644  f.setSqlType(colType);
645  f.setLength(colSize == 0 ? -1 : int(colSize));
646  f.setPrecision(colScale == 0 ? -1 : int(colScale));
647  if (nullable == SQL_NO_NULLS)
648  f.setRequired(true);
649  else if (nullable == SQL_NULLABLE)
650  f.setRequired(false);
651  // else we don't know
652  return f;
653 }
SQLHANDLE hStmt
Definition: qsql_odbc.cpp:173
int type
Definition: qmetatype.cpp:239
static QVariant::Type qDecodeODBCType(SQLSMALLINT sqltype, const T *p, bool isSigned=true)
Definition: qsql_odbc.cpp:293
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const int COLNAMESIZE
Definition: qsql_odbc.cpp:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static void qSqlWarning(const QString &message, const QODBCPrivate *odbc)
Definition: qsql_odbc.cpp:267
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
static QString fromSQLTCHAR(const QVarLengthArray< SQLTCHAR > &input, int size=-1)
Definition: qsql_odbc.cpp:70

◆ qODBCWarn() [1/2]

static QString qODBCWarn ( const QODBCPrivate odbc,
int *  nativeCode = 0 
)
static

Definition at line 254 of file qsql_odbc.cpp.

Referenced by QODBCResult::exec(), QODBCResult::nextResult(), qMakeError(), and qSqlWarning().

255 {
256  return QString(qWarnODBCHandle(SQL_HANDLE_ENV, odbc->dpEnv()) + QLatin1Char(' ')
257  + qWarnODBCHandle(SQL_HANDLE_DBC, odbc->dpDbc()) + QLatin1Char(' ')
258  + qWarnODBCHandle(SQL_HANDLE_STMT, odbc->hStmt, nativeCode)).simplified();
259 }
SQLHANDLE hStmt
Definition: qsql_odbc.cpp:173
The QString class provides a Unicode character string.
Definition: qstring.h:83
SQLHANDLE dpDbc() const
Definition: qsql_odbc.cpp:172
SQLHANDLE dpEnv() const
Definition: qsql_odbc.cpp:171
static QString qWarnODBCHandle(int handleType, SQLHANDLE handle, int *nativeCode=0)
Definition: qsql_odbc.cpp:202
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qODBCWarn() [2/2]

static QString qODBCWarn ( const QODBCDriverPrivate odbc,
int *  nativeCode = 0 
)
static

Definition at line 261 of file qsql_odbc.cpp.

262 {
263  return QString(qWarnODBCHandle(SQL_HANDLE_ENV, odbc->hEnv) + QLatin1Char(' ')
264  + qWarnODBCHandle(SQL_HANDLE_DBC, odbc->hDbc, nativeCode)).simplified();
265 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qWarnODBCHandle(int handleType, SQLHANDLE handle, int *nativeCode=0)
Definition: qsql_odbc.cpp:202
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qSqlWarning() [1/2]

static void qSqlWarning ( const QString message,
const QODBCPrivate odbc 
)
static

Definition at line 267 of file qsql_odbc.cpp.

Referenced by QODBCDriver::cleanup(), QODBCResult::exec(), QODBCResult::numRowsAffected(), QODBCDriver::open(), QODBCResult::prepare(), QODBCDriver::primaryIndex(), qGetODBCVersion(), qMakeFieldInfo(), QODBCDriver::record(), QODBCResult::reset(), QODBCDriver::tables(), and QODBCResult::~QODBCResult().

268 {
269  qWarning() << message << "\tError:" << qODBCWarn(odbc);
270 }
static QString qODBCWarn(const QODBCPrivate *odbc, int *nativeCode=0)
Definition: qsql_odbc.cpp:254
Q_CORE_EXPORT void qWarning(const char *,...)

◆ qSqlWarning() [2/2]

static void qSqlWarning ( const QString message,
const QODBCDriverPrivate odbc 
)
static

Definition at line 272 of file qsql_odbc.cpp.

273 {
274  qWarning() << message << "\tError:" << qODBCWarn(odbc);
275 }
static QString qODBCWarn(const QODBCPrivate *odbc, int *nativeCode=0)
Definition: qsql_odbc.cpp:254
Q_CORE_EXPORT void qWarning(const char *,...)

◆ qWarnODBCHandle()

static QString qWarnODBCHandle ( int  handleType,
SQLHANDLE  handle,
int *  nativeCode = 0 
)
static

Definition at line 202 of file qsql_odbc.cpp.

Referenced by qGetStringData(), qODBCWarn(), and QODBCDriver::tables().

203 {
204  SQLINTEGER nativeCode_ = 0;
205  SQLSMALLINT msgLen = 0;
206  SQLRETURN r = SQL_NO_DATA;
207  SQLTCHAR state_[SQL_SQLSTATE_SIZE+1];
208  QVarLengthArray<SQLTCHAR> description_(SQL_MAX_MESSAGE_LENGTH);
209  QString result;
210  int i = 1;
211 
212  description_[0] = 0;
213  do {
214  r = SQLGetDiagRec(handleType,
215  handle,
216  i,
217  state_,
218  &nativeCode_,
219  0,
220  0,
221  &msgLen);
222  if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && msgLen > 0)
223  description_.resize(msgLen+1);
224  r = SQLGetDiagRec(handleType,
225  handle,
226  i,
227  state_,
228  &nativeCode_,
229  description_.data(),
230  description_.size(),
231  &msgLen);
232  if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) {
233  if (nativeCode)
234  *nativeCode = nativeCode_;
235  QString tmpstore;
236 #ifdef UNICODE
237  tmpstore = fromSQLTCHAR(description_, msgLen);
238 #else
239  tmpstore = QString::fromUtf8((const char*)description_.constData(), msgLen);
240 #endif
241  if(result != tmpstore) {
242  if(!result.isEmpty())
243  result += QLatin1Char(' ');
244  result += tmpstore;
245  }
246  } else if (r == SQL_ERROR || r == SQL_INVALID_HANDLE) {
247  return result;
248  }
249  ++i;
250  } while (r != SQL_NO_DATA);
251  return result;
252 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static QString fromSQLTCHAR(const QVarLengthArray< SQLTCHAR > &input, int size=-1)
Definition: qsql_odbc.cpp:70
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ toSQLTCHAR()

static QVarLengthArray<SQLTCHAR> toSQLTCHAR ( const QString input)
inlinestatic

Definition at line 93 of file qsql_odbc.cpp.

Referenced by QODBCDriver::cleanup(), QODBCResult::exec(), QODBCDriver::open(), QODBCResult::prepare(), QODBCDriver::primaryIndex(), qGetODBCVersion(), QODBCDriver::record(), QODBCResult::reset(), and QODBCDriver::tables().

94 {
96  result.resize(input.size());
97  switch(sizeof(SQLTCHAR)) {
98  case 1:
99  memcpy(result.data(), input.toUtf8().data(), input.size());
100  break;
101  case 2:
102  memcpy(result.data(), input.unicode(), input.size() * 2);
103  break;
104  case 4:
105  memcpy(result.data(), input.toUcs4().data(), input.size() * 4);
106  break;
107  default:
108  qCritical() << "sizeof(SQLTCHAR) is " << sizeof(SQLTCHAR) << "Don't know how to handle this";
109  }
110  result.append(0); // make sure it's null terminated, doesn't matter if it already is, it does if it isn't.
111  return result;
112 }
void resize(int size)
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
void append(const T &t)
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
T * data()
Returns a pointer to the data stored in the vector.
Definition: qvector.h:152
QVector< uint > toUcs4() const Q_REQUIRED_RESULT
Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
Definition: qstring.cpp:4095
Q_CORE_EXPORT void qCritical(const char *,...)

Variable Documentation

◆ COLNAMESIZE

const int COLNAMESIZE = 256
static

Definition at line 66 of file qsql_odbc.cpp.

Referenced by qGetBinaryData(), and qMakeFieldInfo().

◆ qParamType

const SQLSMALLINT qParamType[4] = { SQL_PARAM_INPUT, SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT }
static

Definition at line 68 of file qsql_odbc.cpp.

Referenced by QODBCResult::exec().