/
qgspostgresdataitems.cpp
763 lines (642 loc) · 24.6 KB
/
qgspostgresdataitems.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/***************************************************************************
qgspostgresdataitems.cpp
---------------------
begin : October 2011
copyright : (C) 2011 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspostgresdataitems.h"
#include "qgspostgresconn.h"
#include "qgspostgresconnpool.h"
#include "qgspgnewconnection.h"
#include "qgscolumntypethread.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgsapplication.h"
#include "qgsmessageoutput.h"
#include "qgsnewnamedialog.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QProgressDialog>
#include <climits>
QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause );
QGISEXTERN bool deleteSchema( const QString& schema, const QgsDataSourceURI& uri, QString& errCause, bool cascade = false );
// ---------------------------------------------------------------------------
QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIconName = "mIconConnect.png";
}
QgsPGConnectionItem::~QgsPGConnectionItem()
{
}
QVector<QgsDataItem*> QgsPGConnectionItem::createChildren()
{
QVector<QgsDataItem*>items;
QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
// TODO: wee need to cancel somehow acquireConnection() if deleteLater() was called on this item to avoid later credential dialog if connection failed
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( uri.connectionInfo( false ) );
if ( !conn )
{
items.append( new QgsErrorItem( this, tr( "Connection failed" ), mPath + "/error" ) );
QgsDebugMsg( "Connection failed - " + uri.connectionInfo( false ) );
return items;
}
QList<QgsPostgresSchemaProperty> schemas;
bool ok = conn->getSchemas( schemas );
QgsPostgresConnPool::instance()->releaseConnection( conn );
if ( !ok )
{
items.append( new QgsErrorItem( this, tr( "Failed to get schemas" ), mPath + "/error" ) );
return items;
}
Q_FOREACH ( const QgsPostgresSchemaProperty& schema, schemas )
{
QgsPGSchemaItem * schemaItem = new QgsPGSchemaItem( this, mName, schema.name, mPath + '/' + schema.name );
if ( !schema.description.isEmpty() )
{
schemaItem->setToolTip( schema.description );
}
items.append( schemaItem );
}
return items;
}
bool QgsPGConnectionItem::equal( const QgsDataItem *other )
{
if ( type() != other->type() )
{
return false;
}
const QgsPGConnectionItem *o = qobject_cast<const QgsPGConnectionItem *>( other );
return ( mPath == o->mPath && mName == o->mName );
}
QList<QAction*> QgsPGConnectionItem::actions()
{
QList<QAction*> lst;
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 Connection" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );
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;
}
void QgsPGConnectionItem::editConnection()
{
QgsPgNewConnection nc( nullptr, mName );
if ( nc.exec() )
{
// the parent should be updated
if ( mParent )
mParent->refresh();
}
}
void QgsPGConnectionItem::deleteConnection()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Connection" ),
QObject::tr( "Are you sure you want to delete the connection to %1?" ).arg( mName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
QgsPostgresConn::deleteConnection( mName );
// the parent should be updated
if ( mParent )
mParent->refresh();
}
void QgsPGConnectionItem::refreshConnection()
{
// the parent should be updated
refresh();
}
void QgsPGConnectionItem::createSchema()
{
QString schemaName = QInputDialog::getText( nullptr, tr( "Create Schema" ), tr( "Schema name:" ) );
if ( schemaName.isEmpty() )
return;
QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, 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( nullptr, tr( "Create Schema" ), tr( "Unable to create schema %1\n%2" ).arg( schemaName,
result.PQresultErrorMessage() ) );
conn->unref();
return;
}
conn->unref();
refresh();
}
bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
return handleDrop( data, QString::null );
}
bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )
{
if ( !QgsMimeDataUtils::isUriList( data ) )
return false;
// TODO: probably should show a GUI with settings etc
QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
qApp->setOverrideCursor( Qt::WaitCursor );
QProgressDialog *progress = new QProgressDialog( tr( "Copying features..." ), tr( "Abort" ), 0, 0, nullptr );
progress->setWindowTitle( tr( "Import layer" ) );
progress->setWindowModality( Qt::WindowModal );
progress->show();
QStringList importResults;
bool hasError = false;
bool cancelled = false;
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst )
{
if ( u.layerType != "vector" )
{
importResults.append( tr( "%1: Not a vector layer!" ).arg( u.name ) );
hasError = true; // only vectors can be imported
continue;
}
// open the source layer
QgsVectorLayer* srcLayer = new QgsVectorLayer( u.uri, u.name, u.providerKey );
if ( srcLayer->isValid() )
{
uri.setDataSource( QString(), u.name, srcLayer->geometryType() != QGis::NoGeometry ? "geom" : QString() );
QgsDebugMsg( "URI " + uri.uri( false ) );
if ( !toSchema.isNull() )
{
uri.setSchema( toSchema );
}
QgsVectorLayerImport::ImportError err;
QString importError;
err = QgsVectorLayerImport::importLayer( srcLayer, uri.uri( false ), "postgres", &srcLayer->crs(), false, &importError, false, nullptr, progress );
if ( err == QgsVectorLayerImport::NoError )
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
else if ( err == QgsVectorLayerImport::ErrUserCancelled )
cancelled = true;
else
{
importResults.append( QString( "%1: %2" ).arg( u.name, importError ) );
hasError = true;
}
}
else
{
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
hasError = true;
}
delete srcLayer;
}
delete progress;
qApp->restoreOverrideCursor();
if ( cancelled )
{
QMessageBox::information( nullptr, tr( "Import to PostGIS database" ), tr( "Import cancelled." ) );
refresh();
}
else if ( hasError )
{
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
output->setTitle( tr( "Import to PostGIS database" ) );
output->setMessage( tr( "Failed to import some layers!\n\n" ) + importResults.join( "\n" ), QgsMessageOutput::MessageText );
output->showMessage();
}
else
{
QMessageBox::information( nullptr, tr( "Import to PostGIS database" ), tr( "Import was successful." ) );
refresh();
}
return true;
}
// ---------------------------------------------------------------------------
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" )
, mLayerProperty( layerProperty )
{
mUri = createUri();
setState( Populated );
Q_ASSERT( mLayerProperty.size() == 1 );
}
QString QgsPGLayerItem::comments() const
{
return mLayerProperty.tableComment;
}
QgsPGLayerItem::~QgsPGLayerItem()
{
}
QList<QAction*> QgsPGLayerItem::actions()
{
QList<QAction*> lst;
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 %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( nullptr, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
QString errCause;
bool res = ::deleteLayer( mUri, errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( mParent )
mParent->refresh();
}
}
void QgsPGLayerItem::renameLayer()
{
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );
QString lowerTypeName = mLayerProperty.isView ? tr( "view" ) : tr( "table" );
QgsNewNameDialog dlg( tr( "%1 %2.%3" ).arg( lowerTypeName, mLayerProperty.schemaName, 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;
if ( !schemaName.isEmpty() )
{
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + '.';
}
QString oldName = schemaTableName + QgsPostgresConn::quotedIdentifier( tableName );
QString newName = QgsPostgresConn::quotedIdentifier( dlg.name() );
QgsDataSourceURI dsUri( mUri );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1." ).arg( lowerTypeName ) );
return;
}
//rename the layer
QString sql;
if ( mLayerProperty.isView )
{
sql = QString( "ALTER %1 VIEW %2 RENAME TO %3" ).arg( mLayerProperty.relKind == "m" ? QString( "MATERIALIZED" ) : QString(),
oldName, newName );
}
else
{
sql = QString( "ALTER TABLE %1 RENAME TO %2" ).arg( oldName, newName );
}
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1 %2\n%3" ).arg( lowerTypeName, mName,
result.PQresultErrorMessage() ) );
conn->unref();
return;
}
conn->unref();
if ( mParent )
mParent->refresh();
}
void QgsPGLayerItem::truncateTable()
{
if ( QMessageBox::question( nullptr, 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, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
QgsDataSourceURI dsUri( mUri );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, 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( nullptr, tr( "Truncate Table" ), tr( "Unable to truncate %1\n%2" ).arg( mName,
result.PQresultErrorMessage() ) );
conn->unref();
return;
}
conn->unref();
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}
QString QgsPGLayerItem::createUri()
{
QString pkColName = !mLayerProperty.pkCols.isEmpty() ? mLayerProperty.pkCols.at( 0 ) : QString::null;
QgsPGConnectionItem *connItem = qobject_cast<QgsPGConnectionItem *>( parent() ? parent()->parent() : nullptr );
if ( !connItem )
{
QgsDebugMsg( "connection item not found." );
return QString::null;
}
QgsDataSourceURI uri( QgsPostgresConn::connUri( connItem->name() ).connectionInfo( false ) );
uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName );
uri.setWkbType( QGis::fromOldWkbType( mLayerProperty.types.at( 0 ) ) );
if ( uri.newWkbType() != QgsWKBTypes::NoGeometry )
uri.setSrid( QString::number( mLayerProperty.srids.at( 0 ) ) );
QgsDebugMsg( QString( "layer uri: %1" ).arg( uri.uri( false ) ) );
return uri.uri( false );
}
// ---------------------------------------------------------------------------
QgsPGSchemaItem::QgsPGSchemaItem( QgsDataItem* parent, QString connectionName, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
, mConnectionName( connectionName )
{
mIconName = "mIconDbSchema.png";
}
QgsPGSchemaItem::~QgsPGSchemaItem()
{
}
QVector<QgsDataItem*> QgsPGSchemaItem::createChildren()
{
QVector<QgsDataItem*>items;
QgsDataSourceURI uri = QgsPostgresConn::connUri( mConnectionName );
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( uri.connectionInfo( false ) );
if ( !conn )
{
items.append( new QgsErrorItem( this, tr( "Connection failed" ), mPath + "/error" ) );
QgsDebugMsg( "Connection failed - " + uri.connectionInfo( false ) );
return items;
}
QVector<QgsPostgresLayerProperty> layerProperties;
bool ok = conn->supportedLayers( layerProperties,
QgsPostgresConn::geometryColumnsOnly( mConnectionName ),
QgsPostgresConn::publicSchemaOnly( mConnectionName ),
QgsPostgresConn::allowGeometrylessTables( mConnectionName ), mName );
if ( !ok )
{
items.append( new QgsErrorItem( this, tr( "Failed to get layers" ), mPath + "/error" ) );
QgsPostgresConnPool::instance()->releaseConnection( conn );
return items;
}
bool dontResolveType = QgsPostgresConn::dontResolveType( mConnectionName );
Q_FOREACH ( QgsPostgresLayerProperty layerProperty, layerProperties )
{
if ( layerProperty.schemaName != mName )
continue;
if ( !layerProperty.geometryColName.isNull() &&
( layerProperty.types.value( 0, QGis::WKBUnknown ) == QGis::WKBUnknown ||
layerProperty.srids.value( 0, INT_MIN ) == INT_MIN ) )
{
if ( dontResolveType )
{
//QgsDebugMsg( QString( "skipping column %1.%2 without type constraint" ).arg( layerProperty.schemaName ).arg( layerProperty.tableName ) );
continue;
}
conn->retrieveLayerTypes( layerProperty, true /* useEstimatedMetadata */ );
}
for ( int i = 0; i < layerProperty.size(); i++ )
{
QgsPGLayerItem *layerItem = createLayer( layerProperty.at( i ) );
if ( layerItem )
items.append( layerItem );
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return items;
}
QList<QAction *> QgsPGSchemaItem::actions()
{
QList<QAction*> lst;
QAction* actionRefresh = new QAction( tr( "Refresh" ), this );
connect( actionRefresh, SIGNAL( triggered() ), this, SLOT( refresh() ) );
lst.append( actionRefresh );
QAction* separator = new QAction( this );
separator->setSeparator( true );
lst.append( separator );
QAction* actionRename = new QAction( tr( "Rename Schema..." ), this );
connect( actionRename, SIGNAL( triggered() ), this, SLOT( renameSchema() ) );
lst.append( actionRename );
QAction* actionDelete = new QAction( tr( "Delete Schema" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteSchema() ) );
lst.append( actionDelete );
return lst;
}
void QgsPGSchemaItem::deleteSchema()
{
// check if schema contains tables/views
QgsDataSourceURI uri = QgsPostgresConn::connUri( mConnectionName );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) );
return;
}
QString sql = QString( "SELECT table_name FROM information_schema.tables WHERE table_schema='%1'" ).arg( mName );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) );
conn->unref();
return;
}
QStringList childObjects;
int maxListed = 10;
for ( int idx = 0; idx < result.PQntuples(); idx++ )
{
childObjects << result.PQgetvalue( idx, 0 );
QgsPostgresSchemaProperty schema;
if ( idx == maxListed - 1 )
break;
}
int count = result.PQntuples();
if ( count > 0 )
{
QString objects = childObjects.join( "\n" );
if ( count > maxListed )
{
objects += QString( "\n[%1 additional objects not listed]" ).arg( count - maxListed );
}
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Schema" ),
QObject::tr( "Schema '%1' contains objects:\n\n%2\n\nAre you sure you want to delete the schema and all these objects?" ).arg( mName, objects ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
conn->unref();
return;
}
}
else
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Schema" ),
QObject::tr( "Are you sure you want to delete the schema '%1'?" ).arg( mName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
}
QString errCause;
bool res = ::deleteSchema( mName, uri, errCause, count > 0 );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Schema" ), tr( "Schema deleted successfully." ) );
if ( mParent )
mParent->refresh();
}
}
void QgsPGSchemaItem::renameSchema()
{
QgsNewNameDialog dlg( tr( "schema '%1'" ).arg( mName ), mName );
dlg.setWindowTitle( tr( "Rename Schema" ) );
if ( dlg.exec() != QDialog::Accepted || dlg.name() == mName )
return;
QString schemaName = QgsPostgresConn::quotedIdentifier( mName );
QgsDataSourceURI uri = QgsPostgresConn::connUri( mConnectionName );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema." ) );
return;
}
//rename the schema
QString sql = QString( "ALTER SCHEMA %1 RENAME TO %2" )
.arg( schemaName, QgsPostgresConn::quotedIdentifier( dlg.name() ) );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema %1\n%2" ).arg( schemaName,
result.PQresultErrorMessage() ) );
conn->unref();
return;
}
conn->unref();
QMessageBox::information( nullptr, tr( "Rename Schema" ), tr( "Schema renamed successfully." ) );
if ( mParent )
mParent->refresh();
}
QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProperty )
{
//QgsDebugMsg( "schemaName = " + layerProperty.schemaName + " tableName = " + layerProperty.tableName + " geometryColName = " + layerProperty.geometryColName );
QGis::WkbType wkbType = layerProperty.types.at( 0 );
QString tip = tr( "%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsPostgresConn::displayStringForWkbType( wkbType ) ).arg( layerProperty.srids.at( 0 ) );
if ( !layerProperty.tableComment.isEmpty() )
{
tip = layerProperty.tableComment + '\n' + tip;
}
QgsLayerItem::LayerType layerType;
QgsWKBTypes::GeometryType geomType = QgsWKBTypes::geometryType(( QgsWKBTypes::Type )wkbType );
switch ( geomType )
{
case QgsWKBTypes::PointGeometry:
layerType = QgsLayerItem::Point;
break;
case QgsWKBTypes::LineGeometry:
layerType = QgsLayerItem::Line;
break;
case QgsWKBTypes::PolygonGeometry:
layerType = QgsLayerItem::Polygon;
break;
default:
if ( !layerProperty.geometryColName.isEmpty() )
return nullptr;
layerType = QgsLayerItem::TableLayer;
tip = tr( "as geometryless table" );
}
QgsPGLayerItem *layerItem = new QgsPGLayerItem( this, layerProperty.defaultName(), mPath + '/' + layerProperty.tableName, layerType, layerProperty );
layerItem->setToolTip( tip );
return layerItem;
}
bool QgsPGSchemaItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
QgsPGConnectionItem *conn = qobject_cast<QgsPGConnectionItem *>( parent() );
if ( !conn )
return false;
bool result = conn->handleDrop( data, mName );
if ( result )
refresh();
return result;
}
// ---------------------------------------------------------------------------
QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mCapabilities |= Fast;
mIconName = "mIconPostgis.svg";
populate();
}
QgsPGRootItem::~QgsPGRootItem()
{
}
QVector<QgsDataItem*> QgsPGRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
Q_FOREACH ( const QString& connName, QgsPostgresConn::connectionList() )
{
connections << new QgsPGConnectionItem( this, connName, mPath + '/' + connName );
}
return connections;
}
QList<QAction*> QgsPGRootItem::actions()
{
QList<QAction*> lst;
QAction* actionNew = new QAction( tr( "New Connection..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
lst.append( actionNew );
return lst;
}
QWidget *QgsPGRootItem::paramWidget()
{
QgsPgSourceSelect *select = new QgsPgSourceSelect( nullptr, nullptr, true, true );
connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
return select;
}
void QgsPGRootItem::connectionsChanged()
{
refresh();
}
void QgsPGRootItem::newConnection()
{
QgsPgNewConnection nc( nullptr );
if ( nc.exec() )
{
refresh();
}
}
QMainWindow *QgsPGRootItem::sMainWindow = nullptr;
QGISEXTERN void registerGui( QMainWindow *mainWindow )
{
QgsPGRootItem::sMainWindow = mainWindow;
}