Skip to content

Commit

Permalink
QPSQL: Add support for PostgreSQL 12
Browse files Browse the repository at this point in the history
Add proper version check and replace long deprecated and now removed
access to pg_attrdef.adsrc.

[ChangeLog][QtSql][QPSQL] added support for PostgreSQL 12

Fixes: QTBUG-79033
Fixes: QTBUG-79064
Change-Id: Iec1b13945c34ea017139ad1c5539ab5b7f1e03aa
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
chehrlic committed Oct 17, 2019
1 parent e5c003b commit 14b61d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/plugins/sqldrivers/psql/qsql_psql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,10 @@ static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin)
return QPSQLDriver::Version10;
case 11:
return QPSQLDriver::Version11;
case 12:
return QPSQLDriver::Version12;
default:
if (vMaj > 11)
if (vMaj > 12)
return QPSQLDriver::UnknownLaterVersion;
break;
}
Expand Down Expand Up @@ -1439,26 +1441,29 @@ QSqlRecord QPSQLDriver::record(const QString &tablename) const
schema = stripDelimiters(schema, QSqlDriver::TableName);
tbl = stripDelimiters(tbl, QSqlDriver::TableName);

QString stmt = QStringLiteral("SELECT pg_attribute.attname, pg_attribute.atttypid::int, "
"pg_attribute.attnotnull, pg_attribute.attlen, pg_attribute.atttypmod, "
"pg_attrdef.adsrc "
"FROM pg_class, pg_attribute "
"LEFT JOIN pg_attrdef ON (pg_attrdef.adrelid = "
"pg_attribute.attrelid AND pg_attrdef.adnum = pg_attribute.attnum) "
"WHERE %1 "
"AND pg_class.relname = '%2' "
"AND pg_attribute.attnum > 0 "
"AND pg_attribute.attrelid = pg_class.oid "
"AND pg_attribute.attisdropped = false "
"ORDER BY pg_attribute.attnum");
if (schema.isEmpty())
stmt = stmt.arg(QStringLiteral("pg_table_is_visible(pg_class.oid)"));
else
stmt = stmt.arg(QStringLiteral("pg_class.relnamespace = (SELECT oid FROM "
"pg_namespace WHERE pg_namespace.nspname = '%1')").arg(schema));
const QString adsrc = protocol() < Version8
? QStringLiteral("pg_attrdef.adsrc")
: QStringLiteral("pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid)");
const QString nspname = schema.isEmpty()
? QStringLiteral("pg_table_is_visible(pg_class.oid)")
: QStringLiteral("pg_class.relnamespace = (SELECT oid FROM "
"pg_namespace WHERE pg_namespace.nspname = '%1')").arg(schema);
const QString stmt =
QStringLiteral("SELECT pg_attribute.attname, pg_attribute.atttypid::int, "
"pg_attribute.attnotnull, pg_attribute.attlen, pg_attribute.atttypmod, "
"%1 "
"FROM pg_class, pg_attribute "
"LEFT JOIN pg_attrdef ON (pg_attrdef.adrelid = "
"pg_attribute.attrelid AND pg_attrdef.adnum = pg_attribute.attnum) "
"WHERE %2 "
"AND pg_class.relname = '%3' "
"AND pg_attribute.attnum > 0 "
"AND pg_attribute.attrelid = pg_class.oid "
"AND pg_attribute.attisdropped = false "
"ORDER BY pg_attribute.attnum").arg(adsrc, nspname, tbl);

QSqlQuery query(createResult());
query.exec(stmt.arg(tbl));
query.exec(stmt);
while (query.next()) {
int len = query.value(3).toInt();
int precision = query.value(4).toInt();
Expand Down
1 change: 1 addition & 0 deletions src/plugins/sqldrivers/psql/qsql_psql_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Q_EXPORT_SQLDRIVER_PSQL QPSQLDriver : public QSqlDriver
Version9_6 = 22,
Version10 = 23,
Version11 = 24,
Version12 = 25,
UnknownLaterVersion = 100000
};

Expand Down

0 comments on commit 14b61d4

Please sign in to comment.