Blame SOURCES/qt-everywhere-opensource-src-4.8.5-QTBUG-30076.patch

e09033
From 8053ae4030b4342f73a08b9870ccd19189ae401d Mon Sep 17 00:00:00 2001
e09033
From: Mark Brand <mabrand@mabrand.nl>
e09033
Date: Thu, 7 Mar 2013 11:52:52 +0100
e09033
Subject: [PATCH 4/4] postgresql driver: escape backslashes only when server
e09033
 requires it
e09033
e09033
Task-number: QTBUG-30076
e09033
e09033
Change-Id: I408cda941884f01484d0edfa82c91fc19cb8718c
e09033
Reviewed-by: Israel Lins Albuquerque <israelins85@yahoo.com.br>
e09033
Reviewed-by: Sergey Blagodarin
e09033
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
e09033
Reviewed-by: Andras Mantia <andras@kdab.com>
e09033
(cherry picked from qtbase/e3c5351d06ce8a12f035cd0627356bc64d8c334a)
e09033
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
e09033
---
e09033
 src/sql/drivers/psql/qsql_psql.cpp | 38 +++++++++++++++++++++++++++++++++-----
e09033
 1 file changed, 33 insertions(+), 5 deletions(-)
e09033
e09033
diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp
e09033
index 51ddce0..c2a127c 100644
e09033
--- a/src/sql/drivers/psql/qsql_psql.cpp
e09033
+++ b/src/sql/drivers/psql/qsql_psql.cpp
e09033
@@ -120,7 +120,16 @@ inline void qPQfreemem(void *buffer)
e09033
 class QPSQLDriverPrivate
e09033
 {
e09033
 public:
e09033
-    QPSQLDriverPrivate(QPSQLDriver *qq) : q(qq), connection(0), isUtf8(false), pro(QPSQLDriver::Version6), sn(0), pendingNotifyCheck(false) {}
e09033
+    QPSQLDriverPrivate(QPSQLDriver *qq)
e09033
+      : q(qq),
e09033
+        connection(0),
e09033
+        isUtf8(false),
e09033
+        pro(QPSQLDriver::Version6),
e09033
+        sn(0),
e09033
+        pendingNotifyCheck(false),
e09033
+        hasBackslashEscape(false)
e09033
+    { }
e09033
+
e09033
     QPSQLDriver *q;
e09033
     PGconn *connection;
e09033
     bool isUtf8;
e09033
@@ -128,6 +137,7 @@ public:
e09033
     QSocketNotifier *sn;
e09033
     QStringList seid;
e09033
     mutable bool pendingNotifyCheck;
e09033
+    bool hasBackslashEscape;
e09033
 
e09033
     void appendTables(QStringList &tl, QSqlQuery &t, QChar type);
e09033
     PGresult * exec(const char * stmt) const;
e09033
@@ -135,6 +145,7 @@ public:
e09033
     QPSQLDriver::Protocol getPSQLVersion();
e09033
     bool setEncodingUtf8();
e09033
     void setDatestyle();
e09033
+    void detectBackslashEscape();
e09033
 };
e09033
 
e09033
 void QPSQLDriverPrivate::appendTables(QStringList &tl, QSqlQuery &t, QChar type)
e09033
@@ -636,6 +647,23 @@ void QPSQLDriverPrivate::setDatestyle()
e09033
     PQclear(result);
e09033
 }
e09033
 
e09033
+void QPSQLDriverPrivate::detectBackslashEscape()
e09033
+{
e09033
+    // standard_conforming_strings option introduced in 8.2
e09033
+    // http://www.postgresql.org/docs/8.2/static/runtime-config-compatible.html
e09033
+    if (pro < QPSQLDriver::Version82) {
e09033
+        hasBackslashEscape = true;
e09033
+    } else {
e09033
+        hasBackslashEscape = false;
e09033
+        PGresult* result = exec(QLatin1String("SELECT '\\\\' x"));
e09033
+        int status = PQresultStatus(result);
e09033
+        if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)
e09033
+            if (QString::fromLatin1(PQgetvalue(result, 0, 0)) == QLatin1String("\\"))
e09033
+                hasBackslashEscape = true;
e09033
+        PQclear(result);
e09033
+    }
e09033
+}
e09033
+
e09033
 static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin)
e09033
 {
e09033
     switch (vMaj) {
e09033
@@ -742,6 +770,7 @@ QPSQLDriver::QPSQLDriver(PGconn *conn, QObject *parent)
e09033
     d->connection = conn;
e09033
     if (conn) {
e09033
         d->pro = d->getPSQLVersion();
e09033
+        d->detectBackslashEscape();
e09033
         setOpen(true);
e09033
         setOpenError(false);
e09033
     }
e09033
@@ -842,6 +871,7 @@ bool QPSQLDriver::open(const QString & db,
e09033
     }
e09033
 
e09033
     d->pro = d->getPSQLVersion();
e09033
+    d->detectBackslashEscape();
e09033
     d->isUtf8 = d->setEncodingUtf8();
e09033
     d->setDatestyle();
e09033
 
e09033
@@ -1228,12 +1258,10 @@ QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const
e09033
             }
e09033
             break;
e09033
         case QVariant::String:
e09033
-        {
e09033
-            // Escape '\' characters
e09033
             r = QSqlDriver::formatValue(field, trimStrings);
e09033
-            r.replace(QLatin1String("\\"), QLatin1String("\\\\"));
e09033
+            if (d->hasBackslashEscape)
e09033
+                r.replace(QLatin1String("\\"), QLatin1String("\\\\"));
e09033
             break;
e09033
-        }
e09033
         case QVariant::Bool:
e09033
             if (field.value().toBool())
e09033
                 r = QLatin1String("TRUE");
e09033
-- 
e09033
1.8.3.1
e09033