Skip to content

Commit

Permalink
add layer_styles geometry type column to former tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha committed Oct 7, 2019
1 parent 0656f72 commit b815f70
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -70,6 +70,12 @@ static bool tableExists( QgsPostgresConn &conn, const QString &name )
return res.PQgetvalue( 0, 0 ).toInt() > 0;
}

static bool columnExists( QgsPostgresConn &conn, const QString &table, const QString &column )
{
QgsPostgresResult res( conn.PQexec( "SELECT COUNT(*) FROM information_schema.columns WHERE table_name=" + QgsPostgresConn::quotedValue( table ) + " and column_name=" + QgsPostgresConn::quotedValue( column ) ) );
return res.PQgetvalue( 0, 0 ).toInt() > 0;
}

QgsPostgresPrimaryKeyType
QgsPostgresProvider::pkType( const QgsField &f ) const
{
Expand Down Expand Up @@ -4740,6 +4746,19 @@ bool QgsPostgresProviderMetadata::saveStyle( const QString &uri, const QString &
return false;
}
}
else
{
if ( !columnExists( *conn, QStringLiteral( "layer_styles" ), QStringLiteral( "type" ) ) )
{
QgsPostgresResult res( conn->PQexec( "ALTER TABLE layer_styles ADD COLUMN type varchar(30) NULL" ) );
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to add column type to layer_styles table. Maybe this is due to table permissions (user=%1). Please contact your database admin" ).arg( dsUri.username() );
conn->unref();
return false;
}
}
}

if ( dsUri.database().isEmpty() ) // typically when a service file is used
{
Expand Down

0 comments on commit b815f70

Please sign in to comment.