Qt 4.8
Classes | Macros | Functions
qsql_psql.cpp File Reference
#include "qsql_psql.h"
#include <qcoreapplication.h>
#include <qvariant.h>
#include <qdatetime.h>
#include <qregexp.h>
#include <qsqlerror.h>
#include <qsqlfield.h>
#include <qsqlindex.h>
#include <qsqlrecord.h>
#include <qsqlquery.h>
#include <qsocketnotifier.h>
#include <qstringlist.h>
#include <qmutex.h>
#include <libpq-fe.h>
#include <pg_config.h>
#include <stdlib.h>
#include <math.h>

Go to the source code of this file.

Classes

class  QPSQLDriverPrivate
 
class  QPSQLResultPrivate
 

Macros

#define isinf(x)
 
#define isnan(x)
 
#define QABSTIMEOID   702
 
#define QBOOLOID   16
 
#define QBYTEAOID   17
 
#define QCIDOID   29
 
#define QDATEOID   1082
 
#define QFLOAT4OID   700
 
#define QFLOAT8OID   701
 
#define QINT2OID   21
 
#define QINT4OID   23
 
#define QINT8OID   20
 
#define QNUMERICOID   1700
 
#define QOIDOID   2278
 
#define QREGPROCOID   24
 
#define QRELTIMEOID   703
 
#define QTIMEOID   1083
 
#define QTIMESTAMPOID   1114
 
#define QTIMESTAMPTZOID   1184
 
#define QTIMETZOID   1266
 
#define QXIDOID   28
 

Functions

static int isinf_d (double x)
 
static int isinf_f (float x)
 
static int isinf_ld (long double x)
 
static int isnan_d (double x)
 
static int isnan_f (float x)
 
static int isnan_ld (long double x)
 
template<typename T >
void PQfreemem (T *t, int=0)
 
static QString qCreateParamString (const QVector< QVariant > boundValues, const QSqlDriver *driver)
 
static void qDeallocatePreparedStmt (QPSQLResultPrivate *d)
 
static QVariant::Type qDecodePSQLType (int t)
 
static QSqlError qMakeError (const QString &err, QSqlError::ErrorType type, const QPSQLDriverPrivate *p, PGresult *result=0)
 
QString qMakePreparedStmtId ()
 
static QPSQLDriver::Protocol qMakePSQLVersion (int vMaj, int vMin)
 
void qPQfreemem (void *buffer)
 
static QString qQuote (QString s)
 
static QString qReplacePlaceholderMarkers (const QString &query)
 
static void qSplitTableName (QString &tablename, QString &schema)
 

Macro Definition Documentation

◆ isinf

#define isinf (   x)
Value:
(sizeof (x) == sizeof (long double) ? isinf_ld (x) \
: sizeof (x) == sizeof (double) ? isinf_d (x) \
: isinf_f (x))
static int isinf_d(double x)
Definition: qsql_psql.cpp:79
static int isinf_ld(long double x)
Definition: qsql_psql.cpp:80
static int isinf_f(float x)
Definition: qsql_psql.cpp:78

Definition at line 74 of file qsql_psql.cpp.

Referenced by QPSQLDriver::formatValue().

◆ isnan

#define isnan (   x)
Value:
(sizeof (x) == sizeof (long double) ? isnan_ld (x) \
: sizeof (x) == sizeof (double) ? isnan_d (x) \
: isnan_f (x))
static int isnan_d(double x)
Definition: qsql_psql.cpp:69
static int isnan_ld(long double x)
Definition: qsql_psql.cpp:70
static int isnan_f(float x)
Definition: qsql_psql.cpp:68

Definition at line 64 of file qsql_psql.cpp.

Referenced by QPSQLDriver::formatValue(), isinf_d(), isinf_f(), and isinf_ld().

◆ QABSTIMEOID

#define QABSTIMEOID   702

Definition at line 92 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QBOOLOID

#define QBOOLOID   16

Definition at line 85 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QBYTEAOID

#define QBYTEAOID   17

Definition at line 100 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QCIDOID

#define QCIDOID   29

Definition at line 103 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QDATEOID

#define QDATEOID   1082

Definition at line 94 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QFLOAT4OID

#define QFLOAT4OID   700

Definition at line 90 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QFLOAT8OID

#define QFLOAT8OID   701

Definition at line 91 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QINT2OID

#define QINT2OID   21

Definition at line 87 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QINT4OID

#define QINT4OID   23

Definition at line 88 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QINT8OID

#define QINT8OID   20

Definition at line 86 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QNUMERICOID

#define QNUMERICOID   1700

Definition at line 89 of file qsql_psql.cpp.

Referenced by QPSQLResult::data(), and qDecodePSQLType().

◆ QOIDOID

#define QOIDOID   2278

Definition at line 99 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QREGPROCOID

#define QREGPROCOID   24

Definition at line 101 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QRELTIMEOID

#define QRELTIMEOID   703

Definition at line 93 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QTIMEOID

#define QTIMEOID   1083

Definition at line 95 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QTIMESTAMPOID

#define QTIMESTAMPOID   1114

Definition at line 97 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QTIMESTAMPTZOID

#define QTIMESTAMPTZOID   1184

Definition at line 98 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QTIMETZOID

#define QTIMETZOID   1266

Definition at line 96 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

◆ QXIDOID

#define QXIDOID   28

Definition at line 102 of file qsql_psql.cpp.

Referenced by qDecodePSQLType().

Function Documentation

◆ isinf_d()

static int isinf_d ( double  x)
inlinestatic

Definition at line 79 of file qsql_psql.cpp.

79 { return isnan (x - x); }
#define isnan(x)
Definition: qsql_psql.cpp:64

◆ isinf_f()

static int isinf_f ( float  x)
inlinestatic

Definition at line 78 of file qsql_psql.cpp.

78 { return isnan (x - x); }
#define isnan(x)
Definition: qsql_psql.cpp:64

◆ isinf_ld()

static int isinf_ld ( long double  x)
inlinestatic

Definition at line 80 of file qsql_psql.cpp.

80 { return isnan (x - x); }
#define isnan(x)
Definition: qsql_psql.cpp:64

◆ isnan_d()

static int isnan_d ( double  x)
inlinestatic

Definition at line 69 of file qsql_psql.cpp.

69 { return x != x; }

◆ isnan_f()

static int isnan_f ( float  x)
inlinestatic

Definition at line 68 of file qsql_psql.cpp.

68 { return x != x; }

◆ isnan_ld()

static int isnan_ld ( long double  x)
inlinestatic

Definition at line 70 of file qsql_psql.cpp.

70 { return x != x; }

◆ PQfreemem()

template<typename T >
void PQfreemem ( T *  t,
int  = 0 
)
inline

Definition at line 108 of file qsql_psql.cpp.

Referenced by qPQfreemem().

108 { free(t); }

◆ qCreateParamString()

static QString qCreateParamString ( const QVector< QVariant boundValues,
const QSqlDriver driver 
)
static

Definition at line 552 of file qsql_psql.cpp.

Referenced by QPSQLResult::exec().

553 {
554  if (boundValues.isEmpty())
555  return QString();
556 
557  QString params;
558  QSqlField f;
559  for (int i = 0; i < boundValues.count(); ++i) {
560  const QVariant &val = boundValues.at(i);
561 
562  f.setType(val.type());
563  if (val.isNull())
564  f.clear();
565  else
566  f.setValue(val);
567  if(!params.isNull())
568  params.append(QLatin1String(", "));
569  params.append(driver->formatValue(f));
570  }
571  return params;
572 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setType(QVariant::Type type)
Set&#39;s the field&#39;s variant type to type.
Definition: qsqlfield.cpp:404
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QString class provides a Unicode character string.
Definition: qstring.h:83
void clear()
Clears the value of the field and sets it to NULL.
Definition: qsqlfield.cpp:334
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
QString & append(QChar c)
Definition: qstring.cpp:1777
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
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
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
void setValue(const QVariant &value)
Sets the value of the field to value.
Definition: qsqlfield.cpp:320

◆ qDeallocatePreparedStmt()

static void qDeallocatePreparedStmt ( QPSQLResultPrivate d)
static

Definition at line 285 of file qsql_psql.cpp.

Referenced by QPSQLResult::prepare(), and QPSQLResult::~QPSQLResult().

286 {
287  const QString stmt = QLatin1String("DEALLOCATE ") + d->preparedStmtId;
288  PGresult *result = d->driver->exec(stmt);
289 
290  if (PQresultStatus(result) != PGRES_COMMAND_OK)
291  qWarning("Unable to free statement: %s", PQerrorMessage(d->driver->connection));
292  PQclear(result);
293  d->preparedStmtId.clear();
294 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString preparedStmtId
Definition: qsql_psql.cpp:200
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qWarning(const char *,...)
struct pg_result PGresult
Definition: qsql_psql.h:57
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196

◆ qDecodePSQLType()

static QVariant::Type qDecodePSQLType ( int  t)
static

Definition at line 239 of file qsql_psql.cpp.

Referenced by QPSQLResult::data(), QPSQLDriver::primaryIndex(), QPSQLResult::record(), and QPSQLDriver::record().

240 {
242  switch (t) {
243  case QBOOLOID:
244  type = QVariant::Bool;
245  break;
246  case QINT8OID:
247  type = QVariant::LongLong;
248  break;
249  case QINT2OID:
250  case QINT4OID:
251  case QOIDOID:
252  case QREGPROCOID:
253  case QXIDOID:
254  case QCIDOID:
255  type = QVariant::Int;
256  break;
257  case QNUMERICOID:
258  case QFLOAT4OID:
259  case QFLOAT8OID:
260  type = QVariant::Double;
261  break;
262  case QABSTIMEOID:
263  case QRELTIMEOID:
264  case QDATEOID:
265  type = QVariant::Date;
266  break;
267  case QTIMEOID:
268  case QTIMETZOID:
269  type = QVariant::Time;
270  break;
271  case QTIMESTAMPOID:
272  case QTIMESTAMPTZOID:
273  type = QVariant::DateTime;
274  break;
275  case QBYTEAOID:
276  type = QVariant::ByteArray;
277  break;
278  default:
279  type = QVariant::String;
280  break;
281  }
282  return type;
283 }
int type
Definition: qmetatype.cpp:239
#define QTIMESTAMPTZOID
Definition: qsql_psql.cpp:98
#define QREGPROCOID
Definition: qsql_psql.cpp:101
#define QDATEOID
Definition: qsql_psql.cpp:94
#define QTIMEOID
Definition: qsql_psql.cpp:95
#define QTIMESTAMPOID
Definition: qsql_psql.cpp:97
#define QBOOLOID
Definition: qsql_psql.cpp:85
#define QINT8OID
Definition: qsql_psql.cpp:86
#define QABSTIMEOID
Definition: qsql_psql.cpp:92
#define QCIDOID
Definition: qsql_psql.cpp:103
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
#define QXIDOID
Definition: qsql_psql.cpp:102
#define QFLOAT4OID
Definition: qsql_psql.cpp:90
#define QRELTIMEOID
Definition: qsql_psql.cpp:93
#define QTIMETZOID
Definition: qsql_psql.cpp:96
#define QFLOAT8OID
Definition: qsql_psql.cpp:91
#define QBYTEAOID
Definition: qsql_psql.cpp:100
#define QINT2OID
Definition: qsql_psql.cpp:87
#define QINT4OID
Definition: qsql_psql.cpp:88
#define QOIDOID
Definition: qsql_psql.cpp:99
#define QNUMERICOID
Definition: qsql_psql.cpp:89

◆ qMakeError()

static QSqlError qMakeError ( const QString err,
QSqlError::ErrorType  type,
const QPSQLDriverPrivate p,
PGresult result = 0 
)
static

Definition at line 205 of file qsql_psql.cpp.

Referenced by QPSQLDriver::beginTransaction(), QPSQLDriver::commitTransaction(), QPSQLDriver::open(), QPSQLResult::prepare(), QPSQLResultPrivate::processResults(), QPSQLDriver::rollbackTransaction(), QPSQLDriver::subscribeToNotificationImplementation(), and QPSQLDriver::unsubscribeFromNotificationImplementation().

207 {
208  const char *s = PQerrorMessage(p->connection);
209  QString msg = p->isUtf8 ? QString::fromUtf8(s) : QString::fromLocal8Bit(s);
210  if (result) {
211  const char *sCode = PQresultErrorField(result, PG_DIAG_SQLSTATE);
212  msg += QString::fromLatin1("(%1)").arg(QString::fromLatin1(sCode));
213  }
214  return QSqlError(QLatin1String("QPSQL: ") + err, msg, type);
215 }
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
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
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
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
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

◆ qMakePreparedStmtId()

QString qMakePreparedStmtId ( )

Definition at line 575 of file qsql_psql.cpp.

Referenced by QPSQLResult::prepare().

576 {
577  qMutex()->lock();
578  static unsigned int qPreparedStmtCount = 0;
579  QString id = QLatin1String("qpsqlpstmt_") + QString::number(++qPreparedStmtCount, 16);
580  qMutex()->unlock();
581  return id;
582 }
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ qMakePSQLVersion()

static QPSQLDriver::Protocol qMakePSQLVersion ( int  vMaj,
int  vMin 
)
static

Definition at line 667 of file qsql_psql.cpp.

Referenced by QPSQLDriverPrivate::getPSQLVersion().

668 {
669  switch (vMaj) {
670  case 6:
671  return QPSQLDriver::Version6;
672  case 7:
673  {
674  switch (vMin) {
675  case 1:
676  return QPSQLDriver::Version71;
677  case 3:
678  return QPSQLDriver::Version73;
679  case 4:
680  return QPSQLDriver::Version74;
681  default:
682  return QPSQLDriver::Version7;
683  }
684  break;
685  }
686  case 8:
687  {
688  switch (vMin) {
689  case 1:
690  return QPSQLDriver::Version81;
691  case 2:
692  return QPSQLDriver::Version82;
693  case 3:
694  return QPSQLDriver::Version83;
695  case 4:
696  return QPSQLDriver::Version84;
697  default:
698  return QPSQLDriver::Version8;
699  }
700  break;
701  }
702  case 9:
703  return QPSQLDriver::Version9;
704  break;
705  default:
706  break;
707  }
709 }

◆ qPQfreemem()

void qPQfreemem ( void *  buffer)
inline

Definition at line 115 of file qsql_psql.cpp.

Referenced by QPSQLDriver::_q_handleNotification(), QPSQLResult::data(), and QPSQLDriver::formatValue().

116 {
117  PQfreemem(buffer);
118 }
void PQfreemem(T *t, int=0)
Definition: qsql_psql.cpp:108

◆ qQuote()

static QString qQuote ( QString  s)
static

Definition at line 828 of file qsql_psql.cpp.

Referenced by QPSQLDriver::open().

829 {
830  s.replace(QLatin1Char('\\'), QLatin1String("\\\\"));
831  s.replace(QLatin1Char('\''), QLatin1String("\\'"));
832  s.append(QLatin1Char('\'')).prepend(QLatin1Char('\''));
833  return s;
834 }
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
QString & prepend(QChar c)
Definition: qstring.h:261
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString & append(QChar c)
Definition: qstring.cpp:1777
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qReplacePlaceholderMarkers()

static QString qReplacePlaceholderMarkers ( const QString query)
static

Definition at line 529 of file qsql_psql.cpp.

Referenced by QPSQLResult::prepare().

530 {
531  const int originalLength = query.length();
532  bool inQuote = false;
533  int markerIdx = 0;
534  QString result;
535  result.reserve(originalLength + 23);
536  for (int i = 0; i < originalLength; ++i) {
537  const QChar ch = query.at(i);
538  if (ch == QLatin1Char('?') && !inQuote) {
539  result += QLatin1Char('$');
540  result += QString::number(++markerIdx);
541  } else {
542  if (ch == QLatin1Char('\''))
543  inQuote = !inQuote;
544  result += ch;
545  }
546  }
547 
548  result.squeeze();
549  return result;
550 }
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
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
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
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
void squeeze()
Releases any memory not required to store the character data.
Definition: qstring.h:114
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qSplitTableName()

static void qSplitTableName ( QString tablename,
QString schema 
)
static

Definition at line 996 of file qsql_psql.cpp.

Referenced by QPSQLDriver::primaryIndex(), and QPSQLDriver::record().

997 {
998  int dot = tablename.indexOf(QLatin1Char('.'));
999  if (dot == -1)
1000  return;
1001  schema = tablename.left(dot);
1002  tablename = tablename.mid(dot + 1);
1003 }
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static qreal dot(const QPointF &a, const QPointF &b)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55