Skip to content

Commit 9f56878

Browse files
committed
[bugfix] Add some more logic to get the schema name if empty
1 parent 23dfb1d commit 9f56878

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3696,7 +3696,8 @@ QgsVectorLayerExporter::ExportError QgsPostgresProvider::createEmptyLayer( const
36963696
{
36973697
// populate members from the uri structure
36983698
QgsDataSourceUri dsUri( uri );
3699-
QString schemaName = dsUri.schema().isEmpty() ? QStringLiteral( "public" ) : dsUri.schema();
3699+
3700+
QString schemaName = dsUri.schema();
37003701
QString tableName = dsUri.table();
37013702

37023703
QString geometryColumn = dsUri.geometryColumn();
@@ -3791,6 +3792,20 @@ QgsVectorLayerExporter::ExportError QgsPostgresProvider::createEmptyLayer( const
37913792
{
37923793
conn->PQexecNR( QStringLiteral( "BEGIN" ) );
37933794

3795+
// We want a valid schema name ...
3796+
if ( schemaName.isEmpty() )
3797+
{
3798+
QString sql = QString( "SELECT current_schema" );
3799+
QgsPostgresResult result( conn->PQexec( sql ) );
3800+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
3801+
throw PGException( result );
3802+
schemaName = result.PQgetvalue( 0, 0 );
3803+
if ( schemaName.isEmpty() )
3804+
{
3805+
schemaName = QStringLiteral( "public" );
3806+
}
3807+
}
3808+
37943809
QString sql = QString( "SELECT 1"
37953810
" FROM pg_class AS cls JOIN pg_namespace AS nsp"
37963811
" ON nsp.oid=cls.relnamespace "

tests/src/python/test_provider_postgres.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -771,17 +771,36 @@ def testKey(lyr, key, kfnames):
771771

772772
# See https://issues.qgis.org/issues/17518
773773
def testImportWithoutSchema(self):
774-
self.execSQLCommand('DROP TABLE IF EXISTS b17518 CASCADE')
775-
uri = 'point?field=f1:int'
776-
uri += '&field=F2:double(6,4)'
777-
uri += '&field=f3:string(20)'
778-
lyr = QgsVectorLayer(uri, "x", "memory")
779-
self.assertTrue(lyr.isValid())
780774

781-
uri = "%s sslmode=disable table=\"b17518\" (geom) sql" % self.dbconn
782-
err = QgsVectorLayerExporter.exportLayer(lyr, uri, "postgres", lyr.crs())
783-
olyr = QgsVectorLayer(uri, "y", "postgres")
784-
self.assertTrue(olyr.isValid())
775+
def _test(table, schema=None):
776+
self.execSQLCommand('DROP TABLE IF EXISTS %s CASCADE' % table)
777+
uri = 'point?field=f1:int'
778+
uri += '&field=F2:double(6,4)'
779+
uri += '&field=f3:string(20)'
780+
lyr = QgsVectorLayer(uri, "x", "memory")
781+
self.assertTrue(lyr.isValid())
782+
783+
table = ("%s" % table) if schema is None else ("\"%s\".\"%s\"" % (schema, table))
784+
dest_uri = "%s sslmode=disable table=%s (geom) sql" % (self.dbconn, table)
785+
err = QgsVectorLayerExporter.exportLayer(lyr, dest_uri, "postgres", lyr.crs())
786+
olyr = QgsVectorLayer(dest_uri, "y", "postgres")
787+
self.assertTrue(olyr.isValid(), "Failed URI: %s" % dest_uri)
788+
789+
# Test bug 17518
790+
_test('b17518')
791+
792+
# Test fully qualified table (with schema)
793+
_test("b17518", "qgis_test")
794+
795+
# Test empty schema
796+
_test("b17518", "")
797+
798+
# Test public schema
799+
_test("b17518", "public")
800+
801+
# Test fully qualified table (with wrong schema)
802+
with self.assertRaises(AssertionError):
803+
_test("b17518", "qgis_test_wrong")
785804

786805
def testStyle(self):
787806
self.execSQLCommand('DROP TABLE IF EXISTS layer_styles CASCADE')

0 commit comments

Comments
 (0)