Skip to content
Permalink
Browse files

PostGIS browser improvements

- Add ability to create schemas
- Add ability to truncate tables
- Fix renaming of views
- Better wording of table related actions, so it's clear whether
they operated on a table or query
  • Loading branch information
nyalldawson committed Jun 29, 2015
1 parent bba1985 commit 355fc91a6c7fe6e66233cf2839ec0c0e819bb8a7
@@ -496,6 +496,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.types = QList<QGis::WkbType>() << ( QgsPostgresConn::wkbTypeFromPostgis( type ) );
layerProperty.srids = QList<int>() << srid;
layerProperty.sql = "";
layerProperty.relKind = relkind;
layerProperty.isView = isView;
/*
* force2d may get a false negative value
* (dim == 2 but is not really constrained)
@@ -599,6 +601,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
layerProperty.relKind = relkind;
layerProperty.isView = isView;
if ( coltype == "geometry" )
{
layerProperty.geometryColType = sctGeometry;
@@ -682,6 +686,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.tableName = table;
layerProperty.geometryColName = QString::null;
layerProperty.geometryColType = sctNone;
layerProperty.relKind = relkind;
layerProperty.isView = isView;

//check if we've already added this layer in some form
bool alreadyFound = false;
@@ -77,6 +77,8 @@ struct QgsPostgresLayerProperty
unsigned int nSpCols;
QString sql;
bool force2d;
QString relKind;
bool isView;


// TODO: rename this !
@@ -105,6 +107,8 @@ struct QgsPostgresLayerProperty
property.nSpCols = nSpCols;
property.sql = sql;
property.force2d = force2d;
property.relKind = relKind;
property.isView = isView;

return property;
}
@@ -25,6 +25,7 @@
#include "qgsnewnamedialog.h"

#include <QMessageBox>
#include <QInputDialog>
#include <QProgressDialog>
#include <climits>

@@ -98,17 +99,29 @@ QList<QAction*> QgsPGConnectionItem::actions()
{
QList<QAction*> lst;

QAction* actionEdit = new QAction( tr( "Edit..." ), this );
QAction* actionRefresh = new QAction( tr( "Refresh" ), this );
connect( actionRefresh, SIGNAL( triggered() ), this, SLOT( refreshConnection() ) );
lst.append( actionRefresh );

QAction* separator = new QAction( this );
separator->setSeparator( true );
lst.append( separator );

QAction* actionEdit = new QAction( tr( "Edit Connection..." ), this );
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
lst.append( actionEdit );

QAction* actionDelete = new QAction( tr( "Delete" ), this );
QAction* actionDelete = new QAction( tr( "Delete Connection" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );

QAction* actionRefresh = new QAction( tr( "Refresh" ), this );
connect( actionRefresh, SIGNAL( triggered() ), this, SLOT( refreshConnection() ) );
lst.append( actionRefresh );
QAction* separator2 = new QAction( this );
separator2->setSeparator( true );
lst.append( separator2 );

QAction* actionCreateSchema = new QAction( tr( "Create Schema..." ), this );
connect( actionCreateSchema, SIGNAL( triggered() ), this, SLOT( createSchema() ) );
lst.append( actionCreateSchema );

return lst;
}
@@ -143,6 +156,36 @@ void QgsPGConnectionItem::refreshConnection()
refresh();
}

void QgsPGConnectionItem::createSchema()
{
QString schemaName = QInputDialog::getText( 0, tr( "Create Schema" ), tr( "Shema name:" ) );
if ( schemaName.isEmpty() )
return;

QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo(), false );
if ( !conn )
{
QMessageBox::warning( 0, tr( "Create Schema" ), tr( "Unable to create schema." ) );
return;
}

//create the schema
QString sql = QString( "CREATE SCHEMA %1" ).arg( QgsPostgresConn::quotedIdentifier( schemaName ) );

QgsPostgresResult result = conn->PQexec( sql );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( 0, tr( "Create Schema" ), tr( "Unable to create schema %1\n%2" ).arg( schemaName )
.arg( result.PQresultErrorMessage() ) );
conn->unref();
return;
}

conn->unref();
refresh();
}

bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
if ( !QgsMimeDataUtils::isUriList( data ) )
@@ -235,20 +278,29 @@ QList<QAction*> QgsPGLayerItem::actions()
{
QList<QAction*> lst;

QAction* actionRenameLayer = new QAction( tr( "Rename Layer..." ), this );
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );

QAction* actionRenameLayer = new QAction( tr( "Rename %1..." ).arg( typeName ), this );
connect( actionRenameLayer, SIGNAL( triggered() ), this, SLOT( renameLayer() ) );
lst.append( actionRenameLayer );

QAction* actionDeleteLayer = new QAction( tr( "Delete Layer" ), this );
QAction* actionDeleteLayer = new QAction( tr( "Delete %1" ).arg( typeName ), this );
connect( actionDeleteLayer, SIGNAL( triggered() ), this, SLOT( deleteLayer() ) );
lst.append( actionDeleteLayer );

if ( !mLayerProperty.isView )
{
QAction* actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), this );
connect( actionTruncateLayer, SIGNAL( triggered() ), this, SLOT( truncateTable() ) );
lst.append( actionTruncateLayer );
}

return lst;
}

void QgsPGLayerItem::deleteLayer()
{
if ( QMessageBox::question( 0, QObject::tr( "Delete Object" ),
if ( QMessageBox::question( 0, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
@@ -257,24 +309,26 @@ void QgsPGLayerItem::deleteLayer()
bool res = ::deleteLayer( mUri, errCause );
if ( !res )
{
QMessageBox::warning( 0, tr( "Delete Layer" ), errCause );
QMessageBox::warning( 0, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( 0, tr( "Delete Layer" ), tr( "Layer deleted successfully." ) );
QMessageBox::information( 0, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( mParent )
mParent->refresh();
}
}

void QgsPGLayerItem::renameLayer()
{
QgsNewNameDialog dlg( tr( "layer %1.%2" ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ), mLayerProperty.tableName );
dlg.setWindowTitle( tr( "Rename Layer" ) );
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );
QString lowerTypeName = mLayerProperty.isView ? tr( "view" ) : tr( "table" );

QgsNewNameDialog dlg( tr( "%1 %2.%3" ).arg( lowerTypeName ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ), mLayerProperty.tableName );
dlg.setWindowTitle( tr( "Rename %1" ).arg( typeName ) );
if ( dlg.exec() != QDialog::Accepted || dlg.name() == mLayerProperty.tableName )
return;


QString schemaName = mLayerProperty.schemaName;
QString tableName = mLayerProperty.tableName;
QString schemaTableName;
@@ -289,28 +343,75 @@ void QgsPGLayerItem::renameLayer()
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
QMessageBox::warning( 0, tr( "Rename Layer" ), tr( "Unable to rename layer." ) );
QMessageBox::warning( 0, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1." ).arg( lowerTypeName ) );
return;
}

//rename the layer
QString sql = QString( "ALTER TABLE %1 RENAME TO %2" ).arg( oldName ).arg( newName );
QString sql;
if ( mLayerProperty.isView )
{
sql = QString( "ALTER %1 VIEW %2 RENAME TO %3" ).arg( mLayerProperty.relKind == "m" ? QString( "MATERIALIZED" ) : QString() )
.arg( oldName ).arg( newName );
}
else
{
sql = QString( "ALTER TABLE %1 RENAME TO %2" ).arg( oldName ).arg( newName );
}

QgsPostgresResult result = conn->PQexec( sql );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( 0, tr( "Rename Layer" ), tr( "Unable to rename layer %1\n%2" ).arg( mName )
QMessageBox::warning( 0, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1 %2\n%3" ).arg( lowerTypeName ).arg( mName )
.arg( result.PQresultErrorMessage() ) );
conn->unref();
return;
}

conn->unref();
QMessageBox::information( 0, tr( "Rename Layer" ), tr( "Layer renamed successfully." ) );
if ( mParent )
mParent->refresh();
}

void QgsPGLayerItem::truncateTable()
{
if ( QMessageBox::question( 0, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate %1.%2?\n\nThis will delete all data within the table." ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QgsDataSourceURI dsUri( mUri );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
QMessageBox::warning( 0, tr( "Truncate Table" ), tr( "Unable to truncate table." ) );
return;
}

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

QString sql = QString( "TRUNCATE TABLE %1" ).arg( tableRef );

QgsPostgresResult result = conn->PQexec( sql );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( 0, tr( "Truncate Table" ), tr( "Unable to truncate %1\n%2" ).arg( mName )
.arg( result.PQresultErrorMessage() ) );
conn->unref();
return;
}

conn->unref();
QMessageBox::information( 0, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}

QString QgsPGLayerItem::createUri()
{
QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at( 0 ) : QString::null;
@@ -70,6 +70,7 @@ class QgsPGConnectionItem : public QgsDataCollectionItem
void editConnection();
void deleteConnection();
void refreshConnection();
void createSchema();

};

@@ -108,6 +109,7 @@ class QgsPGLayerItem : public QgsLayerItem
public slots:
void deleteLayer();
void renameLayer();
void truncateTable();

private:
QgsPostgresLayerProperty mLayerProperty;

0 comments on commit 355fc91

Please sign in to comment.
You can’t perform that action at this time.