Skip to content

Commit fe21c98

Browse files
committedApr 26, 2019
More tests for checkPrimaryKeyUnicity
1 parent 4009b2d commit fe21c98

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed
 

‎src/core/qgsvectorlayer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,23 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv
15611561
{
15621562
mProviderKey = provider;
15631563
delete mDataProvider;
1564+
1565+
// For Postgres provider primary key unicity is tested at construction time,
1566+
// so it has to be set before initializing the provider,
1567+
// this manipulation is necessary to preserve default behavior when
1568+
// "trust layer metadata" project level option is set and checkPrimaryKeyUnicity
1569+
// was not explicitely passed in the uri
1570+
if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
1571+
{
1572+
const QString checkUnicityKey { QStringLiteral( "checkPrimaryKeyUnicity" ) };
1573+
QgsDataSourceUri uri( mDataSource );
1574+
if ( ! uri.hasParam( checkUnicityKey ) )
1575+
{
1576+
uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
1577+
mDataSource = uri.uri( false );
1578+
}
1579+
}
1580+
15641581
mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options ) );
15651582
if ( !mDataProvider )
15661583
{

‎src/providers/postgres/qgspgtablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
130130
checkPkUnicityItem->setFlags( checkPkUnicityItem->flags() | Qt::ItemIsUserCheckable );
131131

132132
// Legacy: default value is determined by project option to trust layer's metadata
133-
// TODO: remove this default from QGIS 4 and leave default value to false
133+
// TODO: remove this default from QGIS 4 and leave default value to false?
134134
// checkPkUnicity has only effect on views and materialized views, so we can safely disable it
135135
if ( layerProperty.isView || layerProperty.isMaterializedView )
136136
{

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
108108

109109
if ( mUri.hasParam( QStringLiteral( "checkPrimaryKeyUnicity" ) ) )
110110
{
111+
111112
if ( mUri.param( QStringLiteral( "checkPrimaryKeyUnicity" ) ).compare( QLatin1String( "0" ) ) == 0 )
112113
{
113114
mCheckPrimaryKeyUnicity = false;

‎tests/src/python/test_provider_postgres.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,14 +1104,30 @@ def testCheckPkUnicityOnView(self):
11041104
vl0 = QgsVectorLayer(self.dbconn + ' checkPrimaryKeyUnicity=\'0\' sslmode=disable key=\'pk\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres')
11051105
self.assertTrue(vl0.isValid())
11061106

1107+
geom = vl0.getFeature(1).geometry().asWkt()
1108+
11071109
# This is NOT valid
11081110
vl0 = QgsVectorLayer(self.dbconn + ' checkPrimaryKeyUnicity=\'1\' sslmode=disable key=\'an_int\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres')
11091111
self.assertFalse(vl0.isValid())
11101112

1111-
# This is valid because the default is to not check unicity
1113+
# This is NOT valid because the default is to check unicity
11121114
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'an_int\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres')
11131115
self.assertFalse(vl0.isValid())
11141116

1117+
# This is valid because the readExtentFromXml option is set
1118+
options = QgsVectorLayer.LayerOptions(True, True) # loadDefaultStyle, readExtentFromXml
1119+
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'an_int\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres', options)
1120+
self.assertTrue(vl0.isValid())
1121+
1122+
# Valid because a_unique_int is unique
1123+
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'a_unique_int\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres')
1124+
self.assertEqual(vl0.getFeature(1).geometry().asWkt(), geom)
1125+
1126+
# Valid because a_unique_int is unique
1127+
vl0 = QgsVectorLayer(self.dbconn + ' checkPrimaryKeyUnicity=\'1\' sslmode=disable key=\'a_unique_int\' srid=0 type=POINT table="qgis_test"."b21839_pk_unicity_view" (geom) sql=', 'test', 'postgres')
1128+
self.assertTrue(vl0.isValid())
1129+
self.assertEqual(vl0.getFeature(1).geometry().asWkt(), geom)
1130+
11151131
def testNotify(self):
11161132
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
11171133
vl0.dataProvider().setListening(True)

‎tests/testdata/provider/testdata_pg.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ CREATE TABLE qgis_test.b21839_pk_unicity
548548
(
549549
pk serial NOT NULL,
550550
an_int integer NOT NULL,
551+
a_unique_int integer NOT NULL,
551552
geom geometry(Point),
552553
CONSTRAINT b21839_pk_unicity_pkey PRIMARY KEY (pk)
553554
)
@@ -557,19 +558,20 @@ WITH (
557558

558559

559560
INSERT INTO qgis_test.b21839_pk_unicity(
560-
pk, an_int, geom)
561-
VALUES (1, 1, ST_GeomFromText('point( 1 1)'));
561+
pk, an_int, a_unique_int , geom)
562+
VALUES (1, 1, 1, ST_GeomFromText('point( 1 1)'));
562563

563564

564565
INSERT INTO qgis_test.b21839_pk_unicity(
565-
pk, an_int, geom)
566-
VALUES (2, 1, ST_GeomFromText('point( 1 3)'));
566+
pk, an_int, a_unique_int, geom)
567+
VALUES (2, 1, 2, ST_GeomFromText('point( 1 3)'));
567568

568569

569570

570571
CREATE OR REPLACE VIEW qgis_test.b21839_pk_unicity_view AS
571572
SELECT b21839_pk_unicity.pk,
572573
b21839_pk_unicity.an_int,
574+
b21839_pk_unicity.a_unique_int,
573575
b21839_pk_unicity.geom
574576
FROM qgis_test.b21839_pk_unicity;
575577

0 commit comments

Comments
 (0)
Please sign in to comment.