Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql save raster style db #59938

Merged
merged 10 commits into from
Jan 17, 2025
3 changes: 3 additions & 0 deletions src/providers/postgres/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Files

set(PG_SRCS
qgspostgresutils.cpp
qgspostgresprovider.cpp
qgspostgresconn.cpp
qgspostgresconnpool.cpp
Expand All @@ -26,6 +27,7 @@ if (WITH_GUI)
qgspgnewconnection.cpp
qgspostgresprojectstoragedialog.cpp
raster/qgspostgresrastertemporalsettingswidget.cpp
qgspostgresutils.cpp
)

set(PG_UIS ${CMAKE_SOURCE_DIR}/src/ui/qgspostgresrastertemporalsettingswidgetbase.ui)
Expand Down Expand Up @@ -96,6 +98,7 @@ endif()
# Postgres Raster

set(PGRASTER_SRCS
qgspostgresutils.cpp
raster/qgspostgresrasterprovider.cpp
raster/qgspostgresrastershareddata.cpp
raster/qgspostgresrasterutils.cpp
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgsdataitemguiproviderutils.h"
#include "qgssettings.h"
#include "qgspostgresconn.h"
#include "qgspostgresutils.h"

#include <QFileDialog>
#include <QInputDialog>
Expand Down
144 changes: 1 addition & 143 deletions src/providers/postgres/qgspostgresdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,149 +33,7 @@
#include <QMessageBox>
#include <climits>
#include <qgsabstractdatabaseproviderconnection.h>

bool QgsPostgresUtils::deleteLayer( const QString &uri, QString &errCause )
{
QgsDebugMsgLevel( "deleting layer " + uri, 2 );

QgsDataSourceUri dsUri( uri );
QString schemaName = dsUri.schema();
QString tableName = dsUri.table();
QString geometryCol = dsUri.geometryColumn();

QString schemaTableName;
if ( !schemaName.isEmpty() )
{
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + '.';
}
schemaTableName += QgsPostgresConn::quotedIdentifier( tableName );

QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri, false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
}

// handle deletion of views
QString sqlViewCheck = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" )
.arg( QgsPostgresConn::quotedValue( schemaTableName ) );
QgsPostgresResult resViewCheck( conn->LoggedPQexec( "QgsPostgresUtils", sqlViewCheck ) );
const QString type = resViewCheck.PQgetvalue( 0, 0 );
const Qgis::PostgresRelKind relKind = QgsPostgresConn::relKindFromValue( type );

switch ( relKind )
{
case Qgis::PostgresRelKind::View:
case Qgis::PostgresRelKind::MaterializedView:
{
QString sql = QStringLiteral( "DROP %1VIEW %2" ).arg( type == QLatin1String( "m" ) ? QStringLiteral( "MATERIALIZED " ) : QString(), schemaTableName );
QgsPostgresResult result( conn->LoggedPQexec( "QgsPostgresUtils", sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to delete view %1: \n%2" )
.arg( schemaTableName, result.PQresultErrorMessage() );
conn->unref();
return false;
}
conn->unref();
return true;
}

case Qgis::PostgresRelKind::NotSet:
case Qgis::PostgresRelKind::Unknown:
case Qgis::PostgresRelKind::OrdinaryTable:
case Qgis::PostgresRelKind::Index:
case Qgis::PostgresRelKind::Sequence:
case Qgis::PostgresRelKind::CompositeType:
case Qgis::PostgresRelKind::ToastTable:
case Qgis::PostgresRelKind::ForeignTable:
case Qgis::PostgresRelKind::PartitionedTable:
{
// TODO -- this logic is being applied to a whole bunch
// of potentially non-table items, eg indexes and sequences.
// These should have special handling!

// check the geometry column count
QString sql = QString( "SELECT count(*) "
"FROM geometry_columns, pg_class, pg_namespace "
"WHERE f_table_name=relname AND f_table_schema=nspname "
"AND pg_class.relnamespace=pg_namespace.oid "
"AND f_table_schema=%1 AND f_table_name=%2" )
.arg( QgsPostgresConn::quotedValue( schemaName ), QgsPostgresConn::quotedValue( tableName ) );
QgsPostgresResult result( conn->LoggedPQexec( "QgsPostgresUtils", sql ) );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
.arg( schemaTableName, result.PQresultErrorMessage() );
conn->unref();
return false;
}

int count = result.PQgetvalue( 0, 0 ).toInt();

if ( !geometryCol.isEmpty() && count > 1 )
{
// the table has more geometry columns, drop just the geometry column
sql = QStringLiteral( "SELECT DropGeometryColumn(%1,%2,%3)" )
.arg( QgsPostgresConn::quotedValue( schemaName ), QgsPostgresConn::quotedValue( tableName ), QgsPostgresConn::quotedValue( geometryCol ) );
}
else
{
// drop the table
sql = QStringLiteral( "SELECT DropGeometryTable(%1,%2)" )
.arg( QgsPostgresConn::quotedValue( schemaName ), QgsPostgresConn::quotedValue( tableName ) );
}

result = conn->LoggedPQexec( "QgsPostgresUtils", sql );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
.arg( schemaTableName, result.PQresultErrorMessage() );
conn->unref();
return false;
}

conn->unref();
return true;
}
}
BUILTIN_UNREACHABLE
}

bool QgsPostgresUtils::deleteSchema( const QString &schema, const QgsDataSourceUri &uri, QString &errCause, bool cascade )
{
QgsDebugMsgLevel( "deleting schema " + schema, 2 );

if ( schema.isEmpty() )
return false;

QString schemaName = QgsPostgresConn::quotedIdentifier( schema );

QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri, false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
}

// drop the schema
QString sql = QStringLiteral( "DROP SCHEMA %1 %2" )
.arg( schemaName, cascade ? QStringLiteral( "CASCADE" ) : QString() );

QgsPostgresResult result( conn->LoggedPQexec( "QgsPostgresUtils", sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to delete schema %1: \n%2" )
.arg( schemaName, result.PQresultErrorMessage() );
conn->unref();
return false;
}

conn->unref();
return true;
}

#include "qgspostgresutils.h"

// ---------------------------------------------------------------------------
QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem *parent, const QString &name, const QString &path )
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsmessagelog.h"
#include "qgsexception.h"
#include "qgsgeometryengine.h"
#include "qgspostgresutils.h"

#include <QElapsedTimer>
#include <QObject>
Expand Down
Loading
Loading