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

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