Skip to content

Commit

Permalink
Merge pull request #37387 from jgrocha/fix-regression-37338
Browse files Browse the repository at this point in the history
Fix missing type column in layer_styles in Postgresql
  • Loading branch information
elpaso committed Jun 25, 2020
2 parents 2553d45 + 5c3dff6 commit c45d9e2
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -5082,7 +5082,7 @@ bool QgsPostgresProviderMetadata::saveStyle( const QString &uri, const QString &
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4"
" AND type=%5"
" AND (type=%5 OR type IS NULL)"
" AND styleName=%6" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
Expand Down Expand Up @@ -5110,12 +5110,13 @@ bool QgsPostgresProviderMetadata::saveStyle( const QString &uri, const QString &
",styleSLD=XMLPARSE(DOCUMENT %13)"
",description=%4"
",owner=%5"
",type=%2"
" WHERE f_table_catalog=%6"
" AND f_table_schema=%7"
" AND f_table_name=%8"
" AND f_geometry_column=%9"
" AND styleName=%10"
" AND type=%2" )
" AND (type=%2 OR type IS NULL)" )
.arg( useAsDefault ? "true" : "false" )
.arg( wkbTypeString )
.arg( QgsPostgresConn::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) )
Expand All @@ -5138,7 +5139,7 @@ bool QgsPostgresProviderMetadata::saveStyle( const QString &uri, const QString &
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4"
" AND type=%5" )
" AND (type=%5 OR type IS NULL)" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
Expand All @@ -5163,6 +5164,7 @@ bool QgsPostgresProviderMetadata::saveStyle( const QString &uri, const QString &
QString QgsPostgresProviderMetadata::loadStyle( const QString &uri, QString &errCause )
{
QgsDataSourceUri dsUri( uri );
QString selectQmlQuery;

QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
Expand Down Expand Up @@ -5194,20 +5196,39 @@ QString QgsPostgresProviderMetadata::loadStyle( const QString &uri, QString &err

QString wkbTypeString = QgsPostgresConn::quotedValue( QgsWkbTypes::geometryDisplayString( QgsWkbTypes::geometryType( dsUri.wkbType() ) ) );

QString selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column %4"
" AND type=%5"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( geomColumnExpr )
.arg( wkbTypeString );
// support layer_styles without type column < 3.14
if ( !columnExists( *conn, QStringLiteral( "layer_styles" ), QStringLiteral( "type" ) ) )
{
selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column %4"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( geomColumnExpr );
}
else
{
selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column %4"
" AND (type=%5 OR type IS NULL)"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( geomColumnExpr )
.arg( wkbTypeString );
}

QgsPostgresResult result( conn->PQexec( selectQmlQuery ) );

Expand Down Expand Up @@ -5242,7 +5263,7 @@ int QgsPostgresProviderMetadata::listStyles( const QString &uri, QStringList &id
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4"
" AND type=%5"
" AND (type=%5 OR type IS NULL)"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
Expand Down

0 comments on commit c45d9e2

Please sign in to comment.