Qt 4.8
Public Functions | Public Variables | List of all members
QPSQLDriverPrivate Class Reference

Public Functions

void appendTables (QStringList &tl, QSqlQuery &t, QChar type)
 
void detectBackslashEscape ()
 
PGresultexec (const char *stmt) const
 
PGresultexec (const QString &stmt) const
 
QPSQLDriver::Protocol getPSQLVersion ()
 
 QPSQLDriverPrivate (QPSQLDriver *qq)
 
void setDatestyle ()
 
bool setEncodingUtf8 ()
 

Public Variables

PGconnconnection
 
bool hasBackslashEscape
 
bool isUtf8
 
bool pendingNotifyCheck
 
QPSQLDriver::Protocol pro
 
QPSQLDriverq
 
QStringList seid
 
QSocketNotifiersn
 

Detailed Description

Definition at line 120 of file qsql_psql.cpp.

Constructors and Destructors

◆ QPSQLDriverPrivate()

QPSQLDriverPrivate::QPSQLDriverPrivate ( QPSQLDriver qq)
inline

Definition at line 123 of file qsql_psql.cpp.

124  : q(qq),
125  connection(0),
126  isUtf8(false),
128  sn(0),
129  pendingNotifyCheck(false),
130  hasBackslashEscape(false)
131  { }
QPSQLDriver * q
Definition: qsql_psql.cpp:133
QPSQLDriver::Protocol pro
Definition: qsql_psql.cpp:136
QSocketNotifier * sn
Definition: qsql_psql.cpp:137

Functions

◆ appendTables()

void QPSQLDriverPrivate::appendTables ( QStringList tl,
QSqlQuery t,
QChar  type 
)

Definition at line 151 of file qsql_psql.cpp.

Referenced by QPSQLDriver::tables().

152 {
153  QString query;
154  if (pro >= QPSQLDriver::Version73) {
155  query = QString::fromLatin1("select pg_class.relname, pg_namespace.nspname from pg_class "
156  "left join pg_namespace on (pg_class.relnamespace = pg_namespace.oid) "
157  "where (pg_class.relkind = '%1') and (pg_class.relname !~ '^Inv') "
158  "and (pg_class.relname !~ '^pg_') "
159  "and (pg_namespace.nspname != 'information_schema') ").arg(type);
160  } else {
161  query = QString::fromLatin1("select relname, null from pg_class where (relkind = '%1') "
162  "and (relname !~ '^Inv') "
163  "and (relname !~ '^pg_') ").arg(type);
164  }
165  t.exec(query);
166  while (t.next()) {
167  QString schema = t.value(1).toString();
168  if (schema.isEmpty() || schema == QLatin1String("public"))
169  tl.append(t.value(0).toString());
170  else
171  tl.append(t.value(0).toString().prepend(QLatin1Char('.')).prepend(schema));
172  }
173 }
QString & prepend(QChar c)
Definition: qstring.h:261
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QPSQLDriver::Protocol pro
Definition: qsql_psql.cpp:136
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
bool exec(const QString &query)
Executes the SQL in query.
Definition: qsqlquery.cpp:355
QVariant value(int i) const
Returns the value of field index in the current record.
Definition: qsqlquery.cpp:403
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: qsqlquery.cpp:594
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ detectBackslashEscape()

void QPSQLDriverPrivate::detectBackslashEscape ( )

Definition at line 650 of file qsql_psql.cpp.

Referenced by QPSQLDriver::open(), and QPSQLDriver::QPSQLDriver().

651 {
652  // standard_conforming_strings option introduced in 8.2
653  // http://www.postgresql.org/docs/8.2/static/runtime-config-compatible.html
654  if (pro < QPSQLDriver::Version82) {
655  hasBackslashEscape = true;
656  } else {
657  hasBackslashEscape = false;
658  PGresult* result = exec(QLatin1String("SELECT '\\\\' x"));
659  int status = PQresultStatus(result);
660  if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)
661  if (QString::fromLatin1(PQgetvalue(result, 0, 0)) == QLatin1String("\\"))
662  hasBackslashEscape = true;
663  PQclear(result);
664  }
665 }
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
struct pg_result PGresult
Definition: qsql_psql.h:57
QPSQLDriver::Protocol pro
Definition: qsql_psql.cpp:136
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
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175

◆ exec() [1/2]

PGresult * QPSQLDriverPrivate::exec ( const char *  stmt) const

Definition at line 175 of file qsql_psql.cpp.

Referenced by QPSQLDriver::beginTransaction(), QPSQLDriver::commitTransaction(), QPSQLResult::exec(), exec(), QPSQLResult::prepare(), qDeallocatePreparedStmt(), QPSQLResult::reset(), QPSQLDriver::rollbackTransaction(), QPSQLDriver::subscribeToNotificationImplementation(), and QPSQLDriver::unsubscribeFromNotificationImplementation().

176 {
177  PGresult *result = PQexec(connection, stmt);
178  if (seid.size() && !pendingNotifyCheck) {
179  pendingNotifyCheck = true;
180  QMetaObject::invokeMethod(q, "_q_handleNotification", Qt::QueuedConnection, Q_ARG(int,0));
181  }
182  return result;
183 }
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
QPSQLDriver * q
Definition: qsql_psql.cpp:133
struct pg_result PGresult
Definition: qsql_psql.h:57
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
QStringList seid
Definition: qsql_psql.cpp:138

◆ exec() [2/2]

PGresult * QPSQLDriverPrivate::exec ( const QString stmt) const

Definition at line 185 of file qsql_psql.cpp.

186 {
187  return exec(isUtf8 ? stmt.toUtf8().constData() : stmt.toLocal8Bit().constData());
188 }
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175

◆ getPSQLVersion()

QPSQLDriver::Protocol QPSQLDriverPrivate::getPSQLVersion ( )

Definition at line 711 of file qsql_psql.cpp.

Referenced by QPSQLDriver::open(), and QPSQLDriver::QPSQLDriver().

712 {
714  PGresult* result = exec("select version()");
715  int status = PQresultStatus(result);
716  if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK) {
717  QString val = QString::fromAscii(PQgetvalue(result, 0, 0));
718 
719  QRegExp rx(QLatin1String("(\\d+)\\.(\\d+)"));
720  rx.setMinimal(true); // enforce non-greedy RegExp
721 
722  if (rx.indexIn(val) != -1) {
723  int vMaj = rx.cap(1).toInt();
724  int vMin = rx.cap(2).toInt();
725  serverVersion = qMakePSQLVersion(vMaj, vMin);
726 #ifdef PG_MAJORVERSION
727  if (rx.indexIn(QLatin1String(PG_MAJORVERSION)) != -1) {
728  vMaj = rx.cap(1).toInt();
729  vMin = rx.cap(2).toInt();
730  }
731  QPSQLDriver::Protocol clientVersion = qMakePSQLVersion(vMaj, vMin);
732 
733  if (serverVersion >= QPSQLDriver::Version9 && clientVersion < QPSQLDriver::Version9) {
734  //Client version before QPSQLDriver::Version9 only supports escape mode for bytea type,
735  //but bytea format is set to hex by default in PSQL 9 and above. So need to force the
736  //server use the old escape mode when connects to the new server with old client library.
737  result = exec("SET bytea_output=escape; ");
738  status = PQresultStatus(result);
739  } else if (serverVersion == QPSQLDriver::VersionUnknown) {
740  serverVersion = clientVersion;
741  if (serverVersion != QPSQLDriver::VersionUnknown)
742  qWarning("The server version of this PostgreSQL is unknown, falling back to the client version.");
743  }
744 #endif
745  }
746  }
747  PQclear(result);
748 
749  //keep the old behavior unchanged
750  if (serverVersion == QPSQLDriver::VersionUnknown)
751  serverVersion = QPSQLDriver::Version6;
752 
753  if (serverVersion < QPSQLDriver::Version71) {
754  qWarning("This version of PostgreSQL is not supported and may not work.");
755  }
756 
757  return serverVersion;
758 }
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
struct pg_result PGresult
Definition: qsql_psql.h:57
static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin)
Definition: qsql_psql.cpp:667
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175

◆ setDatestyle()

void QPSQLDriverPrivate::setDatestyle ( )

Definition at line 641 of file qsql_psql.cpp.

Referenced by QPSQLDriver::open().

642 {
643  PGresult* result = exec("SET DATESTYLE TO 'ISO'");
644  int status = PQresultStatus(result);
645  if (status != PGRES_COMMAND_OK)
646  qWarning("%s", PQerrorMessage(connection));
647  PQclear(result);
648 }
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
Q_CORE_EXPORT void qWarning(const char *,...)
struct pg_result PGresult
Definition: qsql_psql.h:57
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175

◆ setEncodingUtf8()

bool QPSQLDriverPrivate::setEncodingUtf8 ( )

Definition at line 633 of file qsql_psql.cpp.

Referenced by QPSQLDriver::open().

634 {
635  PGresult* result = exec("SET CLIENT_ENCODING TO 'UNICODE'");
636  int status = PQresultStatus(result);
637  PQclear(result);
638  return status == PGRES_COMMAND_OK;
639 }
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
struct pg_result PGresult
Definition: qsql_psql.h:57
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175

Properties

◆ connection

PGconn* QPSQLDriverPrivate::connection

◆ hasBackslashEscape

bool QPSQLDriverPrivate::hasBackslashEscape

Definition at line 140 of file qsql_psql.cpp.

Referenced by QPSQLDriver::formatValue().

◆ isUtf8

bool QPSQLDriverPrivate::isUtf8

◆ pendingNotifyCheck

bool QPSQLDriverPrivate::pendingNotifyCheck
mutable

Definition at line 139 of file qsql_psql.cpp.

Referenced by QPSQLDriver::_q_handleNotification(), and exec().

◆ pro

QPSQLDriver::Protocol QPSQLDriverPrivate::pro

◆ q

QPSQLDriver* QPSQLDriverPrivate::q

Definition at line 133 of file qsql_psql.cpp.

Referenced by exec(), and QPSQLResultPrivate::processResults().

◆ seid

QStringList QPSQLDriverPrivate::seid

◆ sn

QSocketNotifier* QPSQLDriverPrivate::sn

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