tdf#128111: "adsrc" doesn't exist from Postgresql 12
authorJulien Nabet <serval2412@yahoo.fr>
Sat, 12 Oct 2019 22:26:10 +0000 (00:26 +0200)
committerRene Engelhard <rene@debian.org>
Mon, 8 Mar 2021 12:13:24 +0000 (12:13 +0000)
Before Postgresql 8.0, there was only "adsrc"
then it's been deprecated
"The adsrc field is historical, and is best not used, because it does not track outside changes
 that might affect the representation of the default value.
 Reverse-compiling the adbin field (with pg_get_expr for example) is a better way to display the default value
"
and finally it's been removed with version 12

See evolution with:
- https://www.postgresql.org/docs/8/catalog-pg-attrdef.html
- https://www.postgresql.org/docs/11/catalog-pg-attrdef.html
- https://www.postgresql.org/docs/12/catalog-pg-attrdef.html

Merge with https://cgit.freedesktop.org/libreoffice/core/commit/?id=1ec93ef100bb5f6ccef91f12e28ed09feb3eb38b

Change-Id: I57e9da423a23b5a96bbb64b0e026b160e9643ab9
Reviewed-on: https://gerrit.libreoffice.org/80722
(cherry picked from commit 0c46c81e04530e8f6ce4f34195d8f0443ed8bfc3)
Reviewed-on: https://gerrit.libreoffice.org/80736
Tested-by: Jenkins
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
Gbp-Pq: Name Postgresql-12-no-adsrc.diff

connectivity/source/drivers/postgresql/pq_databasemetadata.cxx
connectivity/source/drivers/postgresql/pq_statement.cxx
connectivity/source/drivers/postgresql/pq_tools.cxx
connectivity/source/drivers/postgresql/pq_tools.hxx

index b69a1139c61026b72ae0ac0bfcdd5fb31473676e..04d8cfdfae5d1c79a9c2688f3263e3d6e8004fd8 100644 (file)
@@ -1516,7 +1516,7 @@ css::uno::Reference< XResultSet > DatabaseMetaData::getColumns(
     //                            allow NULL values. An empty string means
     //                            nobody knows.
     //               => pg_attribute.attnotnull
-
+    OUString strDefaultValue = getColExprForDefaultSettingVal(m_pSettings);
     Reference< XPreparedStatement > statement = m_origin->prepareStatement(
             "SELECT pg_namespace.nspname, "  // 1
             "pg_class.relname, "             // 2
@@ -1526,8 +1526,8 @@ css::uno::Reference< XResultSet > DatabaseMetaData::getColumns(
             "pg_attribute.attnotnull, "      // 6
             "pg_type.typdefault, "           // 7
             "pg_type.typtype, "              // 8
-            "pg_attrdef.adsrc, "             // 9
-            "pg_description.description, "   // 10
+            + strDefaultValue +              // 9
+            ",pg_description.description, "   // 10
             "pg_type.typbasetype, "          // 11
             "pg_attribute.attnum "           // 12
             "FROM pg_class, "
index 184b305aa1fdfb2c62e747060d1ea4db4fb71572..db3596a7b3ae2b2cd82b4bd160fe96aa76f118b7 100644 (file)
@@ -641,10 +641,12 @@ static void getAutoValues(
     String2StringMap & result,
     const Reference< XConnection > & connection,
     const OUString &schemaName,
-    const OUString & tableName )
+    const OUString & tableName,
+    ConnectionSettings *pConnectionSettings )
 {
+    OUString strDefaultValue = getColExprForDefaultSettingVal(pConnectionSettings);
     Reference< XPreparedStatement > stmt = connection->prepareStatement(
-                  "SELECT   pg_attribute.attname, pg_attrdef.adsrc "
+                  "SELECT   pg_attribute.attname, " + strDefaultValue +
                   "FROM pg_class, pg_namespace, pg_attribute "
                   "LEFT JOIN pg_attrdef ON pg_attribute.attrelid = pg_attrdef.adrelid AND "
                                         "pg_attribute.attnum = pg_attrdef.adnum "
@@ -654,7 +656,7 @@ static void getAutoValues(
                   // LEM TODO: this is weird; why "LIKE" and not "="?
                   // Most probably gives problems if tableName contains '%'
                         "pg_class.relname LIKE ? AND "
-                        "pg_attrdef.adsrc != ''"
+                        + strDefaultValue + " != ''"
             );
     DisposeGuard guard( stmt );
     Reference< XParameters > paras( stmt, UNO_QUERY );
@@ -748,7 +750,7 @@ Reference< XResultSet > getGeneratedValuesFromLastInsert(
                 {
                     if( autoValues.begin() == autoValues.end() )
                     {
-                        getAutoValues( autoValues, connection, schemaName, tableName );
+                        getAutoValues( autoValues, connection, schemaName, tableName, pConnectionSettings );
                     }
                     // this could mean, that the column is a default or auto value, check this ...
                     bool bColumnMatchAutoValue = false;
index 46e66310cadb9a13cc41985c635a2d1dd9ee3d10..4ae9bb59a853501a6c4e52ce2341d91116d4fd56 100644 (file)
@@ -835,6 +835,13 @@ OString extractSingleTableFromSelect( const std::vector< OString > &vec )
 
 }
 
+OUString getColExprForDefaultSettingVal(ConnectionSettings const *settings)
+{
+    return (PQserverVersion( settings->pConnection ) < 80000)?
+               OUString("pg_attrdef.adsrc"):
+               OUString("pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid, true)");
+}
+
 css::uno::Sequence< sal_Int32 > string2intarray( const OUString & str )
 {
     css::uno::Sequence< sal_Int32 > ret;
index 9d4e2349fcfbb3fd09e4a3f0b6281a96b679bb75..af751f8e633bae18423618e5c2353c4df55384a8 100644 (file)
@@ -100,6 +100,8 @@ void disposeObject( const css::uno::Reference< css::uno::XInterface > & r );
 OUString extractTableFromInsert( const OUString & sql );
 OString extractSingleTableFromSelect( const std::vector< OString > &vec );
 
+OUString getColExprForDefaultSettingVal(ConnectionSettings const *settings);
+
 void tokenizeSQL( const OString & sql, std::vector< OString > &vec  );
 void splitSQL( const OString & sql, std::vector< OString > &vec  );
 std::vector< sal_Int32 > parseIntArray( const OUString & str );