wok-4.x view qt4/stuff/add-postgresql9.patch @ rev 9055
bluefish: fix genpkg_rules
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Mar 04 11:43:00 2011 +0100 (2011-03-04) |
parents | |
children |
line source
1 From 03f94089a16cf6b6a3b533ba1f90444eb18c29ab Mon Sep 17 00:00:00 2001
2 From: Charles Yin <charles.yin@nokia.com>
3 Date: Thu, 4 Nov 2010 15:38:57 +1000
4 Subject: [PATCH] Add Postgresql 8.x and 9 supports
6 Change-Id: Ic740686ead768cc3e106703049d878549dfd3c6a
7 Task-number:QTBUG-14206
8 Reviewed-by: Michael Goddard
9 ---
10 src/sql/drivers/psql/qsql_psql.cpp | 116 +++++++++++++++++++++---------
11 src/sql/drivers/psql/qsql_psql.h | 6 ++-
12 tests/auto/qsqldatabase/tst_databases.h | 2 +
13 3 files changed, 88 insertions(+), 36 deletions(-)
15 diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp
16 index 2a4e595..bf9685f 100644
17 --- a/src/sql/drivers/psql/qsql_psql.cpp
18 +++ b/src/sql/drivers/psql/qsql_psql.cpp
19 @@ -54,7 +54,6 @@
20 #include <qstringlist.h>
21 #include <qmutex.h>
23 -
24 #include <libpq-fe.h>
25 #include <pg_config.h>
27 @@ -619,6 +618,50 @@ static void setDatestyle(PGconn* connection)
28 PQclear(result);
29 }
31 +static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin)
32 +{
33 + switch (vMaj) {
34 + case 6:
35 + return QPSQLDriver::Version6;
36 + case 7:
37 + {
38 + switch (vMin) {
39 + case 1:
40 + return QPSQLDriver::Version71;
41 + case 3:
42 + return QPSQLDriver::Version73;
43 + case 4:
44 + return QPSQLDriver::Version74;
45 + default:
46 + return QPSQLDriver::Version7;
47 + }
48 + break;
49 + }
50 + case 8:
51 + {
52 + switch (vMin) {
53 + case 1:
54 + return QPSQLDriver::Version81;
55 + case 2:
56 + return QPSQLDriver::Version82;
57 + case 3:
58 + return QPSQLDriver::Version83;
59 + case 4:
60 + return QPSQLDriver::Version84;
61 + default:
62 + return QPSQLDriver::Version8;
63 + }
64 + break;
65 + }
66 + case 9:
67 + return QPSQLDriver::Version9;
68 + break;
69 + default:
70 + break;
71 + }
72 + return QPSQLDriver::VersionUnknown;
73 +}
74 +
75 static QPSQLDriver::Protocol getPSQLVersion(PGconn* connection)
76 {
77 QPSQLDriver::Protocol serverVersion = QPSQLDriver::Version6;
78 @@ -626,50 +669,44 @@ static QPSQLDriver::Protocol getPSQLVersion(PGconn* connection)
79 int status = PQresultStatus(result);
80 if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK) {
81 QString val = QString::fromAscii(PQgetvalue(result, 0, 0));
82 +
83 QRegExp rx(QLatin1String("(\\d+)\\.(\\d+)"));
84 rx.setMinimal(true); // enforce non-greedy RegExp
85 +
86 if (rx.indexIn(val) != -1) {
87 int vMaj = rx.cap(1).toInt();
88 int vMin = rx.cap(2).toInt();
89 -
90 - switch (vMaj) {
91 - case 7:
92 - switch (vMin) {
93 - case 0:
94 - serverVersion = QPSQLDriver::Version7;
95 - break;
96 - case 1:
97 - case 2:
98 - serverVersion = QPSQLDriver::Version71;
99 - break;
100 - default:
101 - serverVersion = QPSQLDriver::Version73;
102 - break;
103 - }
104 - break;
105 - case 8:
106 - switch (vMin) {
107 - case 0:
108 - serverVersion = QPSQLDriver::Version8;
109 - break;
110 - case 1:
111 - serverVersion = QPSQLDriver::Version81;
112 - break;
113 - case 2:
114 - default:
115 - serverVersion = QPSQLDriver::Version82;
116 - break;
117 - }
118 - break;
119 - default:
120 - break;
121 + serverVersion = qMakePSQLVersion(vMaj, vMin);
122 +#ifdef PG_MAJORVERSION
123 + if (rx.indexIn(QLatin1String(PG_MAJORVERSION)) != -1) {
124 + vMaj = rx.cap(1).toInt();
125 + vMin = rx.cap(2).toInt();
126 + }
127 + QPSQLDriver::Protocol clientVersion = qMakePSQLVersion(vMaj, vMin);
128 +
129 + if (serverVersion >= QPSQLDriver::Version9 && clientVersion < QPSQLDriver::Version9) {
130 + //Client version before QPSQLDriver::Version9 only supports escape mode for bytea type,
131 + //but bytea format is set to hex by default in PSQL 9 and above. So need to force the
132 + //server use the old escape mode when connects to the new server with old client library.
133 + result = PQexec(connection, "SET bytea_output=escape; ");
134 + status = PQresultStatus(result);
135 + } else if (serverVersion == QPSQLDriver::VersionUnknown) {
136 + serverVersion = clientVersion;
137 + if (serverVersion != QPSQLDriver::VersionUnknown)
138 + qWarning("The server version of this PostgreSQL is unknown, falling back to the client version.");
139 }
140 +#endif
141 }
142 }
143 PQclear(result);
145 - if (serverVersion < QPSQLDriver::Version71)
146 + //keep the old behavior unchanged
147 + if (serverVersion == QPSQLDriver::VersionUnknown)
148 + serverVersion = QPSQLDriver::Version6;
149 +
150 + if (serverVersion < QPSQLDriver::Version71) {
151 qWarning("This version of PostgreSQL is not supported and may not work.");
152 + }
154 return serverVersion;
155 }
156 @@ -852,7 +889,10 @@ bool QPSQLDriver::commitTransaction()
157 // This hack can dissapear once there is an API to query this sort of information.
158 if (d->pro == QPSQLDriver::Version8 ||
159 d->pro == QPSQLDriver::Version81 ||
160 - d->pro == QPSQLDriver::Version82) {
161 + d->pro == QPSQLDriver::Version82 ||
162 + d->pro == QPSQLDriver::Version83 ||
163 + d->pro == QPSQLDriver::Version84 ||
164 + d->pro == QPSQLDriver::Version9) {
165 transaction_failed = qstrcmp(PQcmdStatus(res), "ROLLBACK") == 0;
166 }
168 @@ -963,6 +1003,9 @@ QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const
169 case QPSQLDriver::Version8:
170 case QPSQLDriver::Version81:
171 case QPSQLDriver::Version82:
172 + case QPSQLDriver::Version83:
173 + case QPSQLDriver::Version84:
174 + case QPSQLDriver::Version9:
175 stmt = QLatin1String("SELECT pg_attribute.attname, pg_attribute.atttypid::int, "
176 "pg_class.relname "
177 "FROM pg_attribute, pg_class "
178 @@ -1046,6 +1089,9 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const
179 case QPSQLDriver::Version8:
180 case QPSQLDriver::Version81:
181 case QPSQLDriver::Version82:
182 + case QPSQLDriver::Version83:
183 + case QPSQLDriver::Version84:
184 + case QPSQLDriver::Version9:
185 stmt = QLatin1String("select pg_attribute.attname, pg_attribute.atttypid::int, "
186 "pg_attribute.attnotnull, pg_attribute.attlen, pg_attribute.atttypmod, "
187 "pg_attrdef.adsrc "
188 diff --git a/src/sql/drivers/psql/qsql_psql.h b/src/sql/drivers/psql/qsql_psql.h
189 index 22871ff..107da87 100644
190 --- a/src/sql/drivers/psql/qsql_psql.h
191 +++ b/src/sql/drivers/psql/qsql_psql.h
192 @@ -97,6 +97,7 @@ class Q_EXPORT_SQLDRIVER_PSQL QPSQLDriver : public QSqlDriver
193 Q_OBJECT
194 public:
195 enum Protocol {
196 + VersionUnknown = -1,
197 Version6 = 6,
198 Version7 = 7,
199 Version71 = 8,
200 @@ -104,7 +105,10 @@ public:
201 Version74 = 10,
202 Version8 = 11,
203 Version81 = 12,
204 - Version82 = 13
205 + Version82 = 13,
206 + Version83 = 14,
207 + Version84 = 15,
208 + Version9 = 16,
209 };
211 explicit QPSQLDriver(QObject *parent=0);
213 --
214 1.6.1