-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgspostgresproviderconnection.cpp
736 lines (679 loc) · 27.1 KB
/
qgspostgresproviderconnection.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
/***************************************************************************
qgspostgresproviderconnection.cpp - QgsPostgresProviderConnection
---------------------
begin : 2.8.2019
copyright : (C) 2019 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* 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 "qgspostgresproviderconnection.h"
#include "qgspostgresconn.h"
#include "qgspostgresconnpool.h"
#include "qgssettings.h"
#include "qgspostgresprovider.h"
#include "qgsexception.h"
#include "qgsapplication.h"
extern "C"
{
#include <libpq-fe.h>
}
QgsPostgresProviderConnection::QgsPostgresProviderConnection( const QString &name )
: QgsAbstractDatabaseProviderConnection( name )
{
mProviderKey = QStringLiteral( "postgres" );
// Remove the sql and table empty parts
const QRegularExpression removePartsRe { R"raw(\s*sql=\s*|\s*table=""\s*)raw" };
setUri( QgsPostgresConn::connUri( name ).uri().replace( removePartsRe, QString() ) );
setDefaultCapabilities();
}
QgsPostgresProviderConnection::QgsPostgresProviderConnection( const QString &uri, const QVariantMap &configuration ):
QgsAbstractDatabaseProviderConnection( QgsDataSourceUri( uri ).connectionInfo( false ), configuration )
{
mProviderKey = QStringLiteral( "postgres" );
setDefaultCapabilities();
}
void QgsPostgresProviderConnection::setDefaultCapabilities()
{
// TODO: we might check at this point if the user actually has the privileges and return
// properly filtered capabilities instead of all of them
mCapabilities =
{
Capability::DropVectorTable,
Capability::DropRasterTable,
Capability::CreateVectorTable,
Capability::RenameSchema,
Capability::DropSchema,
Capability::CreateSchema,
Capability::RenameVectorTable,
Capability::RenameRasterTable,
Capability::Vacuum,
Capability::ExecuteSql,
Capability::SqlLayers,
//Capability::Transaction,
Capability::Tables,
Capability::Schemas,
Capability::Spatial,
Capability::TableExists,
Capability::CreateSpatialIndex,
Capability::SpatialIndexExists,
Capability::DeleteSpatialIndex,
Capability::DeleteField,
Capability::DeleteFieldCascade,
Capability::AddField
};
mGeometryColumnCapabilities =
{
GeometryColumnCapability::Z,
GeometryColumnCapability::M,
GeometryColumnCapability::SinglePart,
GeometryColumnCapability::Curves
};
}
void QgsPostgresProviderConnection::dropTablePrivate( const QString &schema, const QString &name ) const
{
executeSqlPrivate( QStringLiteral( "DROP TABLE %1.%2" )
.arg( QgsPostgresConn::quotedIdentifier( schema ) )
.arg( QgsPostgresConn::quotedIdentifier( name ) ) );
}
void QgsPostgresProviderConnection::createVectorTable( const QString &schema,
const QString &name,
const QgsFields &fields,
QgsWkbTypes::Type wkbType,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
const QMap<QString,
QVariant> *options ) const
{
checkCapability( Capability::CreateVectorTable );
QgsDataSourceUri newUri { uri() };
newUri.setSchema( schema );
newUri.setTable( name );
// Set geometry column if it's not aspatial
if ( wkbType != QgsWkbTypes::Type::Unknown && wkbType != QgsWkbTypes::Type::NoGeometry )
{
newUri.setGeometryColumn( options->value( QStringLiteral( "geometryColumn" ), QStringLiteral( "geom" ) ).toString() );
}
QMap<int, int> map;
QString errCause;
QgsVectorLayerExporter::ExportError errCode = QgsPostgresProvider::createEmptyLayer(
newUri.uri(),
fields,
wkbType,
srs,
overwrite,
&map,
&errCause,
options
);
if ( errCode != QgsVectorLayerExporter::ExportError::NoError )
{
throw QgsProviderConnectionException( QObject::tr( "An error occurred while creating the vector layer: %1" ).arg( errCause ) );
}
}
QString QgsPostgresProviderConnection::tableUri( const QString &schema, const QString &name ) const
{
const auto tableInfo { table( schema, name ) };
QgsDataSourceUri dsUri( uri() );
dsUri.setTable( name );
dsUri.setSchema( schema );
return dsUri.uri( false );
}
void QgsPostgresProviderConnection::dropVectorTable( const QString &schema, const QString &name ) const
{
checkCapability( Capability::DropVectorTable );
dropTablePrivate( schema, name );
}
void QgsPostgresProviderConnection::dropRasterTable( const QString &schema, const QString &name ) const
{
checkCapability( Capability::DropRasterTable );
dropTablePrivate( schema, name );
}
void QgsPostgresProviderConnection::renameTablePrivate( const QString &schema, const QString &name, const QString &newName ) const
{
executeSqlPrivate( QStringLiteral( "ALTER TABLE %1.%2 RENAME TO %3" )
.arg( QgsPostgresConn::quotedIdentifier( schema ) )
.arg( QgsPostgresConn::quotedIdentifier( name ) )
.arg( QgsPostgresConn::quotedIdentifier( newName ) ) );
}
void QgsPostgresProviderConnection::renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameVectorTable );
renameTablePrivate( schema, name, newName );
}
void QgsPostgresProviderConnection::renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameRasterTable );
renameTablePrivate( schema, name, newName );
}
void QgsPostgresProviderConnection::createSchema( const QString &name ) const
{
checkCapability( Capability::CreateSchema );
executeSqlPrivate( QStringLiteral( "CREATE SCHEMA %1" )
.arg( QgsPostgresConn::quotedIdentifier( name ) ) );
}
void QgsPostgresProviderConnection::dropSchema( const QString &name, bool force ) const
{
checkCapability( Capability::DropSchema );
executeSqlPrivate( QStringLiteral( "DROP SCHEMA %1 %2" )
.arg( QgsPostgresConn::quotedIdentifier( name ) )
.arg( force ? QStringLiteral( "CASCADE" ) : QString() ) );
}
void QgsPostgresProviderConnection::renameSchema( const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameSchema );
executeSqlPrivate( QStringLiteral( "ALTER SCHEMA %1 RENAME TO %2" )
.arg( QgsPostgresConn::quotedIdentifier( name ) )
.arg( QgsPostgresConn::quotedIdentifier( newName ) ) );
}
QList<QVariantList> QgsPostgresProviderConnection::executeSql( const QString &sql, QgsFeedback *feedback ) const
{
checkCapability( Capability::ExecuteSql );
return executeSqlPrivate( sql, true, feedback );
}
QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback ) const
{
QList<QVariantList> results;
// Check feedback first!
if ( feedback && feedback->isCanceled() )
{
return results;
}
const QgsDataSourceUri dsUri { uri() };
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( dsUri.connectionInfo( false ) );
if ( !conn )
{
throw QgsProviderConnectionException( QObject::tr( "Connection failed: %1" ).arg( uri() ) );
}
else
{
if ( feedback && feedback->isCanceled() )
{
return results;
}
// This is gross but I tried with both conn and a context QObject without success: the lambda is never called.
QMetaObject::Connection qtConnection;
if ( feedback )
{
qtConnection = QObject::connect( feedback, &QgsFeedback::canceled, [ &conn ]
{
conn->PQCancel();
} );
}
QgsPostgresResult res( conn->PQexec( sql ) );
if ( feedback )
{
QObject::disconnect( qtConnection );
}
QString errCause;
if ( conn->PQstatus() != CONNECTION_OK || ! res.result() )
{
errCause = QObject::tr( "Connection error: %1 returned %2 [%3]" )
.arg( sql ).arg( conn->PQstatus() )
.arg( conn->PQerrorMessage() );
}
else
{
const QString err { conn->PQerrorMessage() };
if ( ! err.isEmpty() )
{
errCause = QObject::tr( "SQL error: %1 returned %2 [%3]" )
.arg( sql )
.arg( conn->PQstatus() )
.arg( err );
}
}
if ( res.PQntuples() > 0 )
{
// Try to convert value types at least for basic simple types that can be directly mapped to Python
QMap<int, QVariant::Type> typeMap;
if ( resolveTypes )
{
for ( int rowIdx = 0; rowIdx < res.PQnfields(); rowIdx++ )
{
if ( feedback && feedback->isCanceled() )
{
break;
}
const Oid oid { res.PQftype( rowIdx ) };
QList<QVariantList> typeRes { executeSqlPrivate( QStringLiteral( "SELECT typname FROM pg_type WHERE oid = %1" ).arg( oid ), false ) };
// Set the default to string
QVariant::Type vType { QVariant::Type::String };
if ( typeRes.size() > 0 && typeRes.first().size() > 0 )
{
static const QStringList intTypes = { QStringLiteral( "oid" ),
QStringLiteral( "int2" ),
QStringLiteral( "int4" ),
QStringLiteral( "int8" )
};
static const QStringList floatTypes = { QStringLiteral( "float4" ),
QStringLiteral( "float8" ),
QStringLiteral( "numeric" )
};
const QString typName { typeRes.first().first().toString() };
if ( floatTypes.contains( typName ) )
{
vType = QVariant::Double;
}
else if ( intTypes.contains( typName ) )
{
vType = QVariant::LongLong;
}
else if ( typName == QStringLiteral( "date" ) )
{
vType = QVariant::Date;
}
else if ( typName.startsWith( QStringLiteral( "timestamp" ) ) )
{
vType = QVariant::DateTime;
}
else if ( typName == QStringLiteral( "time" ) )
{
vType = QVariant::Time;
}
else if ( typName == QStringLiteral( "bool" ) )
{
vType = QVariant::Bool;
}
else if ( typName == QStringLiteral( "char" ) )
{
vType = QVariant::Char;
}
else
{
// Just a warning, usually ok
QgsDebugMsgLevel( QStringLiteral( "Unhandled PostgreSQL type %1, assuming string" ).arg( typName ), 2 );
}
}
typeMap[ rowIdx ] = vType;
}
}
for ( int rowIdx = 0; rowIdx < res.PQntuples(); rowIdx++ )
{
if ( feedback && feedback->isCanceled() )
{
break;
}
QVariantList row;
for ( int colIdx = 0; colIdx < res.PQnfields(); colIdx++ )
{
if ( resolveTypes )
{
const QVariant::Type vType { typeMap.value( colIdx, QVariant::Type::String ) };
QVariant val { res.PQgetvalue( rowIdx, colIdx ) };
// Special case for bools: 'f' and 't'
if ( vType == QVariant::Bool )
{
if ( ! val.toString().isEmpty() )
{
val = val.toString() == 't';
}
}
else if ( val.canConvert( static_cast<int>( vType ) ) )
{
val.convert( static_cast<int>( vType ) );
}
row.push_back( val );
}
else
{
row.push_back( res.PQgetvalue( rowIdx, colIdx ) );
}
}
results.push_back( row );
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
if ( ! errCause.isEmpty() )
{
throw QgsProviderConnectionException( errCause );
}
}
return results;
}
void QgsPostgresProviderConnection::vacuum( const QString &schema, const QString &name ) const
{
checkCapability( Capability::Vacuum );
executeSql( QStringLiteral( "VACUUM FULL ANALYZE %1.%2" )
.arg( QgsPostgresConn::quotedIdentifier( schema ) )
.arg( QgsPostgresConn::quotedIdentifier( name ) ) );
}
void QgsPostgresProviderConnection::createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options ) const
{
checkCapability( Capability::CreateSpatialIndex );
QString geometryColumnName { options.geometryColumnName };
if ( geometryColumnName.isEmpty() )
{
// Can we guess it?
try
{
const auto tp { table( schema, name ) };
geometryColumnName = tp.geometryColumn();
}
catch ( QgsProviderConnectionException & )
{
// pass
}
}
if ( geometryColumnName.isEmpty() )
{
throw QgsProviderConnectionException( QObject::tr( "Geometry column name not specified while creating spatial index" ) );
}
const QString indexName = QStringLiteral( "sidx_%1_%2" ).arg( name, geometryColumnName );
executeSql( QStringLiteral( "CREATE INDEX %1 ON %2.%3 USING GIST (%4);" )
.arg( indexName,
QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ),
QgsPostgresConn::quotedIdentifier( geometryColumnName ) ) );
}
bool QgsPostgresProviderConnection::spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const
{
checkCapability( Capability::SpatialIndexExists );
const QList<QVariantList> res = executeSql( QStringLiteral( R"""(SELECT COUNT(*)
FROM pg_class t, pg_class i, pg_namespace ns, pg_index ix, pg_attribute a
WHERE
t.oid=ix.indrelid
AND t.relnamespace=ns.oid
AND i.oid=ix.indexrelid
AND a.attrelid=t.oid
AND a.attnum=ANY(ix.indkey)
AND t.relkind='r'
AND ns.nspname=%1
AND t.relname=%2
AND a.attname=%3;
)""" ).arg(
QgsPostgresConn::quotedValue( schema ),
QgsPostgresConn::quotedValue( name ),
QgsPostgresConn::quotedValue( geometryColumn ) ) );
return !res.isEmpty() && !res.at( 0 ).isEmpty() && res.at( 0 ).at( 0 ).toBool();
}
void QgsPostgresProviderConnection::deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const
{
checkCapability( Capability::DeleteSpatialIndex );
const QList<QVariantList> res = executeSql( QStringLiteral( R"""(SELECT i.relname
FROM pg_class t, pg_class i, pg_namespace ns, pg_index ix, pg_attribute a
WHERE
t.oid=ix.indrelid
AND t.relnamespace=ns.oid
AND i.oid=ix.indexrelid
AND a.attrelid=t.oid
AND a.attnum=ANY(ix.indkey)
AND t.relkind='r'
AND ns.nspname=%1
AND t.relname=%2
AND a.attname=%3;
)""" ).arg(
QgsPostgresConn::quotedValue( schema ),
QgsPostgresConn::quotedValue( name ),
QgsPostgresConn::quotedValue( geometryColumn ) ) );
if ( res.isEmpty() )
throw QgsProviderConnectionException( QObject::tr( "No spatial index exists for %1.%2" ).arg( schema, name ) );
const QString indexName = res.at( 0 ).at( 0 ).toString();
executeSql( QStringLiteral( "DROP INDEX %1.%2" ).arg( QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( indexName ) ) );
}
QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnection::tables( const QString &schema, const TableFlags &flags ) const
{
checkCapability( Capability::Tables );
QList<QgsPostgresProviderConnection::TableProperty> tables;
QString errCause;
// TODO: set flags from the connection if flags argument is 0
const QgsDataSourceUri dsUri { uri() };
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( dsUri.connectionInfo( false ) );
if ( !conn )
{
errCause = QObject::tr( "Connection failed: %1" ).arg( uri() );
}
else
{
QVector<QgsPostgresLayerProperty> properties;
const bool aspatial { ! flags || flags.testFlag( TableFlag::Aspatial ) };
bool ok = conn->supportedLayers( properties, false, schema == QStringLiteral( "public" ), aspatial, schema );
if ( ! ok )
{
errCause = QObject::tr( "Could not retrieve tables: %1" ).arg( uri() );
}
else
{
bool dontResolveType = configuration().value( QStringLiteral( "dontResolveType" ), false ).toBool();
bool useEstimatedMetadata = configuration().value( QStringLiteral( "estimatedMetadata" ), false ).toBool();
// Cannot be const:
for ( auto &pr : properties )
{
// Classify
TableFlags prFlags;
if ( pr.isView )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::View );
}
if ( pr.isMaterializedView )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::MaterializedView );
}
if ( pr.isForeignTable )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::Foreign );
}
if ( pr.isRaster )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::Raster );
}
else if ( pr.nSpCols != 0 )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::Vector );
}
else
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::Aspatial );
}
// Filter
if ( ! flags || ( prFlags & flags ) )
{
// retrieve layer types if needed
if ( ! dontResolveType && ( !pr.geometryColName.isNull() &&
( pr.types.value( 0, QgsWkbTypes::Unknown ) == QgsWkbTypes::Unknown ||
pr.srids.value( 0, std::numeric_limits<int>::min() ) == std::numeric_limits<int>::min() ) ) )
{
conn->retrieveLayerTypes( pr, useEstimatedMetadata );
}
QgsPostgresProviderConnection::TableProperty property;
property.setFlags( prFlags );
for ( int i = 0; i < std::min( pr.types.size(), pr.srids.size() ) ; i++ )
{
property.addGeometryColumnType( pr.types.at( i ), QgsCoordinateReferenceSystem::fromEpsgId( pr.srids.at( i ) ) );
}
property.setTableName( pr.tableName );
property.setSchema( pr.schemaName );
property.setGeometryColumn( pr.geometryColName );
// These are candidates, not actual PKs
// property.setPrimaryKeyColumns( pr.pkCols );
property.setGeometryColumnCount( static_cast<int>( pr.nSpCols ) );
property.setComment( pr.tableComment );
// Get PKs
if ( pr.isView || pr.isMaterializedView || pr.isForeignTable )
{
// Set the candidates
property.setPrimaryKeyColumns( pr.pkCols );
}
else // Fetch and set the real pks
{
try
{
const auto pks = executeSql( QStringLiteral( R"(
WITH pkrelid AS (
SELECT indexrelid AS idxri FROM pg_index WHERE indrelid='%1.%2'::regclass AND (indisprimary OR indisunique)
ORDER BY CASE WHEN indisprimary THEN 1 ELSE 2 END LIMIT 1)
SELECT attname FROM pg_index,pg_attribute, pkrelid
WHERE indexrelid=pkrelid.idxri AND indrelid=attrelid AND pg_attribute.attnum=any(pg_index.indkey);
)" ).arg( QgsPostgresConn::quotedIdentifier( pr.schemaName ) )
.arg( QgsPostgresConn::quotedIdentifier( pr.tableName ) ) );
QStringList pkNames;
for ( const auto &pk : qgis::as_const( pks ) )
{
pkNames.push_back( pk.first().toString() );
}
property.setPrimaryKeyColumns( pkNames );
}
catch ( const QgsProviderConnectionException &ex )
{
QgsDebugMsg( QStringLiteral( "Error retrieving primary keys: %1" ).arg( ex.what() ) );
}
}
tables.push_back( property );
}
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
}
if ( ! errCause.isEmpty() )
{
throw QgsProviderConnectionException( errCause );
}
return tables;
}
QStringList QgsPostgresProviderConnection::schemas( ) const
{
checkCapability( Capability::Schemas );
QStringList schemas;
QString errCause;
const QgsDataSourceUri dsUri { uri() };
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( dsUri.connectionInfo( false ) );
if ( !conn )
{
errCause = QObject::tr( "Connection failed: %1" ).arg( uri() );
}
else
{
QList<QgsPostgresSchemaProperty> schemaProperties;
bool ok = conn->getSchemas( schemaProperties );
QgsPostgresConnPool::instance()->releaseConnection( conn );
if ( ! ok )
{
errCause = QObject::tr( "Could not retrieve schemas: %1" ).arg( uri() );
}
else
{
for ( const auto &s : qgis::as_const( schemaProperties ) )
{
schemas.push_back( s.name );
}
}
}
if ( ! errCause.isEmpty() )
{
throw QgsProviderConnectionException( errCause );
}
return schemas;
}
void QgsPostgresProviderConnection::store( const QString &name ) const
{
// TODO: move this to class configuration?
QString baseKey = QStringLiteral( "/PostgreSQL/connections/" );
// delete the original entry first
remove( name );
QgsSettings settings;
settings.beginGroup( baseKey );
settings.beginGroup( name );
// From URI
const QgsDataSourceUri dsUri { uri() };
settings.setValue( "service", dsUri.service() );
settings.setValue( "host", dsUri.host() );
settings.setValue( "port", dsUri.port() );
settings.setValue( "database", dsUri.database() );
settings.setValue( "username", dsUri.username() );
settings.setValue( "password", dsUri.password() );
settings.setValue( "authcfg", dsUri.authConfigId() );
settings.setEnumValue( "sslmode", dsUri.sslMode() );
// From configuration
static const QStringList configurationParameters
{
QStringLiteral( "publicOnly" ),
QStringLiteral( "geometryColumnsOnly" ),
QStringLiteral( "dontResolveType" ),
QStringLiteral( "allowGeometrylessTables" ),
QStringLiteral( "saveUsername" ),
QStringLiteral( "savePassword" ),
QStringLiteral( "estimatedMetadata" ),
QStringLiteral( "projectsInDatabase" )
};
for ( const auto &p : configurationParameters )
{
if ( configuration().contains( p ) )
{
settings.setValue( p, configuration().value( p ) );
}
}
settings.endGroup();
settings.endGroup();
}
void QgsPostgresProviderConnection::remove( const QString &name ) const
{
QgsPostgresConn::deleteConnection( name );
}
QIcon QgsPostgresProviderConnection::icon() const
{
return QgsApplication::getThemeIcon( QStringLiteral( "mIconPostgis.svg" ) );
}
QList<QgsVectorDataProvider::NativeType> QgsPostgresProviderConnection::nativeTypes() const
{
QList<QgsVectorDataProvider::NativeType> types;
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( QgsDataSourceUri{ uri() }.connectionInfo( false ) );
if ( conn )
{
types = conn->nativeTypes();
QgsPostgresConnPool::instance()->releaseConnection( conn );
}
if ( types.isEmpty() )
{
throw QgsProviderConnectionException( QObject::tr( "Error retrieving native types for connection %1" ).arg( uri() ) );
}
return types;
}
QgsFields QgsPostgresProviderConnection::fields( const QString &schema, const QString &tableName ) const
{
// Try the base implementation first and fall back to a more complex approch for the
// few PG-specific corner cases that do not work with the base implementation.
try
{
return QgsAbstractDatabaseProviderConnection::fields( schema, tableName );
}
catch ( QgsProviderConnectionException &ex )
{
// This table might expose multiple geometry columns (different geom type or SRID)
// but we are only interested in fields here, so let's pick the first one.
TableProperty tableInfo { table( schema, tableName ) };
try
{
QgsDataSourceUri tUri { tableUri( schema, tableName ) };
if ( tableInfo.geometryColumnTypes().count( ) > 1 )
{
TableProperty::GeometryColumnType geomCol { tableInfo.geometryColumnTypes().first() };
tUri.setGeometryColumn( tableInfo.geometryColumn() );
tUri.setWkbType( geomCol.wkbType );
tUri.setSrid( QString::number( geomCol.crs.postgisSrid() ) );
}
if ( tableInfo.primaryKeyColumns().count() > 0 )
{
tUri.setKeyColumn( tableInfo.primaryKeyColumns().first() );
}
tUri.setParam( QStringLiteral( "checkPrimaryKeyUnicity" ), QLatin1String( "0" ) );
QgsVectorLayer::LayerOptions options { false, true };
options.skipCrsValidation = true;
QgsVectorLayer vl { tUri.uri(), QStringLiteral( "temp_layer" ), mProviderKey, options };
if ( vl.isValid() )
{
return vl.fields();
}
}
catch ( QgsProviderConnectionException & )
{
// fall-through
}
throw ex;
}
}