Skip to content

Commit 355fc91

Browse files
committed
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
1 parent bba1985 commit 355fc91

File tree

4 files changed

+130
-17
lines changed

4 files changed

+130
-17
lines changed

src/providers/postgres/qgspostgresconn.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
496496
layerProperty.types = QList<QGis::WkbType>() << ( QgsPostgresConn::wkbTypeFromPostgis( type ) );
497497
layerProperty.srids = QList<int>() << srid;
498498
layerProperty.sql = "";
499+
layerProperty.relKind = relkind;
500+
layerProperty.isView = isView;
499501
/*
500502
* force2d may get a false negative value
501503
* (dim == 2 but is not really constrained)
@@ -599,6 +601,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
599601
layerProperty.schemaName = schemaName;
600602
layerProperty.tableName = tableName;
601603
layerProperty.geometryColName = column;
604+
layerProperty.relKind = relkind;
605+
layerProperty.isView = isView;
602606
if ( coltype == "geometry" )
603607
{
604608
layerProperty.geometryColType = sctGeometry;
@@ -682,6 +686,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
682686
layerProperty.tableName = table;
683687
layerProperty.geometryColName = QString::null;
684688
layerProperty.geometryColType = sctNone;
689+
layerProperty.relKind = relkind;
690+
layerProperty.isView = isView;
685691

686692
//check if we've already added this layer in some form
687693
bool alreadyFound = false;

src/providers/postgres/qgspostgresconn.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ struct QgsPostgresLayerProperty
7777
unsigned int nSpCols;
7878
QString sql;
7979
bool force2d;
80+
QString relKind;
81+
bool isView;
8082

8183

8284
// TODO: rename this !
@@ -105,6 +107,8 @@ struct QgsPostgresLayerProperty
105107
property.nSpCols = nSpCols;
106108
property.sql = sql;
107109
property.force2d = force2d;
110+
property.relKind = relKind;
111+
property.isView = isView;
108112

109113
return property;
110114
}

src/providers/postgres/qgspostgresdataitems.cpp

Lines changed: 118 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsnewnamedialog.h"
2626

2727
#include <QMessageBox>
28+
#include <QInputDialog>
2829
#include <QProgressDialog>
2930
#include <climits>
3031

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

101-
QAction* actionEdit = new QAction( tr( "Edit..." ), this );
102+
QAction* actionRefresh = new QAction( tr( "Refresh" ), this );
103+
connect( actionRefresh, SIGNAL( triggered() ), this, SLOT( refreshConnection() ) );
104+
lst.append( actionRefresh );
105+
106+
QAction* separator = new QAction( this );
107+
separator->setSeparator( true );
108+
lst.append( separator );
109+
110+
QAction* actionEdit = new QAction( tr( "Edit Connection..." ), this );
102111
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
103112
lst.append( actionEdit );
104113

105-
QAction* actionDelete = new QAction( tr( "Delete" ), this );
114+
QAction* actionDelete = new QAction( tr( "Delete Connection" ), this );
106115
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
107116
lst.append( actionDelete );
108117

109-
QAction* actionRefresh = new QAction( tr( "Refresh" ), this );
110-
connect( actionRefresh, SIGNAL( triggered() ), this, SLOT( refreshConnection() ) );
111-
lst.append( actionRefresh );
118+
QAction* separator2 = new QAction( this );
119+
separator2->setSeparator( true );
120+
lst.append( separator2 );
121+
122+
QAction* actionCreateSchema = new QAction( tr( "Create Schema..." ), this );
123+
connect( actionCreateSchema, SIGNAL( triggered() ), this, SLOT( createSchema() ) );
124+
lst.append( actionCreateSchema );
112125

113126
return lst;
114127
}
@@ -143,6 +156,36 @@ void QgsPGConnectionItem::refreshConnection()
143156
refresh();
144157
}
145158

159+
void QgsPGConnectionItem::createSchema()
160+
{
161+
QString schemaName = QInputDialog::getText( 0, tr( "Create Schema" ), tr( "Shema name:" ) );
162+
if ( schemaName.isEmpty() )
163+
return;
164+
165+
QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
166+
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo(), false );
167+
if ( !conn )
168+
{
169+
QMessageBox::warning( 0, tr( "Create Schema" ), tr( "Unable to create schema." ) );
170+
return;
171+
}
172+
173+
//create the schema
174+
QString sql = QString( "CREATE SCHEMA %1" ).arg( QgsPostgresConn::quotedIdentifier( schemaName ) );
175+
176+
QgsPostgresResult result = conn->PQexec( sql );
177+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
178+
{
179+
QMessageBox::warning( 0, tr( "Create Schema" ), tr( "Unable to create schema %1\n%2" ).arg( schemaName )
180+
.arg( result.PQresultErrorMessage() ) );
181+
conn->unref();
182+
return;
183+
}
184+
185+
conn->unref();
186+
refresh();
187+
}
188+
146189
bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
147190
{
148191
if ( !QgsMimeDataUtils::isUriList( data ) )
@@ -235,20 +278,29 @@ QList<QAction*> QgsPGLayerItem::actions()
235278
{
236279
QList<QAction*> lst;
237280

238-
QAction* actionRenameLayer = new QAction( tr( "Rename Layer..." ), this );
281+
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );
282+
283+
QAction* actionRenameLayer = new QAction( tr( "Rename %1..." ).arg( typeName ), this );
239284
connect( actionRenameLayer, SIGNAL( triggered() ), this, SLOT( renameLayer() ) );
240285
lst.append( actionRenameLayer );
241286

242-
QAction* actionDeleteLayer = new QAction( tr( "Delete Layer" ), this );
287+
QAction* actionDeleteLayer = new QAction( tr( "Delete %1" ).arg( typeName ), this );
243288
connect( actionDeleteLayer, SIGNAL( triggered() ), this, SLOT( deleteLayer() ) );
244289
lst.append( actionDeleteLayer );
245290

291+
if ( !mLayerProperty.isView )
292+
{
293+
QAction* actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), this );
294+
connect( actionTruncateLayer, SIGNAL( triggered() ), this, SLOT( truncateTable() ) );
295+
lst.append( actionTruncateLayer );
296+
}
297+
246298
return lst;
247299
}
248300

249301
void QgsPGLayerItem::deleteLayer()
250302
{
251-
if ( QMessageBox::question( 0, QObject::tr( "Delete Object" ),
303+
if ( QMessageBox::question( 0, QObject::tr( "Delete Table" ),
252304
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ),
253305
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
254306
return;
@@ -257,24 +309,26 @@ void QgsPGLayerItem::deleteLayer()
257309
bool res = ::deleteLayer( mUri, errCause );
258310
if ( !res )
259311
{
260-
QMessageBox::warning( 0, tr( "Delete Layer" ), errCause );
312+
QMessageBox::warning( 0, tr( "Delete Table" ), errCause );
261313
}
262314
else
263315
{
264-
QMessageBox::information( 0, tr( "Delete Layer" ), tr( "Layer deleted successfully." ) );
316+
QMessageBox::information( 0, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
265317
if ( mParent )
266318
mParent->refresh();
267319
}
268320
}
269321

270322
void QgsPGLayerItem::renameLayer()
271323
{
272-
QgsNewNameDialog dlg( tr( "layer %1.%2" ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ), mLayerProperty.tableName );
273-
dlg.setWindowTitle( tr( "Rename Layer" ) );
324+
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );
325+
QString lowerTypeName = mLayerProperty.isView ? tr( "view" ) : tr( "table" );
326+
327+
QgsNewNameDialog dlg( tr( "%1 %2.%3" ).arg( lowerTypeName ).arg( mLayerProperty.schemaName ).arg( mLayerProperty.tableName ), mLayerProperty.tableName );
328+
dlg.setWindowTitle( tr( "Rename %1" ).arg( typeName ) );
274329
if ( dlg.exec() != QDialog::Accepted || dlg.name() == mLayerProperty.tableName )
275330
return;
276331

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

296350
//rename the layer
297-
QString sql = QString( "ALTER TABLE %1 RENAME TO %2" ).arg( oldName ).arg( newName );
351+
QString sql;
352+
if ( mLayerProperty.isView )
353+
{
354+
sql = QString( "ALTER %1 VIEW %2 RENAME TO %3" ).arg( mLayerProperty.relKind == "m" ? QString( "MATERIALIZED" ) : QString() )
355+
.arg( oldName ).arg( newName );
356+
}
357+
else
358+
{
359+
sql = QString( "ALTER TABLE %1 RENAME TO %2" ).arg( oldName ).arg( newName );
360+
}
298361

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

308371
conn->unref();
309-
QMessageBox::information( 0, tr( "Rename Layer" ), tr( "Layer renamed successfully." ) );
310372
if ( mParent )
311373
mParent->refresh();
312374
}
313375

376+
void QgsPGLayerItem::truncateTable()
377+
{
378+
if ( QMessageBox::question( 0, QObject::tr( "Truncate Table" ),
379+
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 ),
380+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
381+
return;
382+
383+
QgsDataSourceURI dsUri( mUri );
384+
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
385+
if ( !conn )
386+
{
387+
QMessageBox::warning( 0, tr( "Truncate Table" ), tr( "Unable to truncate table." ) );
388+
return;
389+
}
390+
391+
QString schemaName = mLayerProperty.schemaName;
392+
QString tableName = mLayerProperty.tableName;
393+
QString schemaTableName;
394+
if ( !schemaName.isEmpty() )
395+
{
396+
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + ".";
397+
}
398+
QString tableRef = schemaTableName + QgsPostgresConn::quotedIdentifier( tableName );
399+
400+
QString sql = QString( "TRUNCATE TABLE %1" ).arg( tableRef );
401+
402+
QgsPostgresResult result = conn->PQexec( sql );
403+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
404+
{
405+
QMessageBox::warning( 0, tr( "Truncate Table" ), tr( "Unable to truncate %1\n%2" ).arg( mName )
406+
.arg( result.PQresultErrorMessage() ) );
407+
conn->unref();
408+
return;
409+
}
410+
411+
conn->unref();
412+
QMessageBox::information( 0, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
413+
}
414+
314415
QString QgsPGLayerItem::createUri()
315416
{
316417
QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at( 0 ) : QString::null;

src/providers/postgres/qgspostgresdataitems.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class QgsPGConnectionItem : public QgsDataCollectionItem
7070
void editConnection();
7171
void deleteConnection();
7272
void refreshConnection();
73+
void createSchema();
7374

7475
};
7576

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

112114
private:
113115
QgsPostgresLayerProperty mLayerProperty;

0 commit comments

Comments
 (0)