Skip to content

Commit

Permalink
[bugfix] Add some more logic to get the schema name if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 22, 2017
1 parent 23dfb1d commit 9f56878
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
17 changes: 16 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3696,7 +3696,8 @@ QgsVectorLayerExporter::ExportError QgsPostgresProvider::createEmptyLayer( const
{
// populate members from the uri structure
QgsDataSourceUri dsUri( uri );
QString schemaName = dsUri.schema().isEmpty() ? QStringLiteral( "public" ) : dsUri.schema();

QString schemaName = dsUri.schema();
QString tableName = dsUri.table();

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

// We want a valid schema name ...
if ( schemaName.isEmpty() )
{
QString sql = QString( "SELECT current_schema" );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
throw PGException( result );
schemaName = result.PQgetvalue( 0, 0 );
if ( schemaName.isEmpty() )
{
schemaName = QStringLiteral( "public" );
}
}

QString sql = QString( "SELECT 1"
" FROM pg_class AS cls JOIN pg_namespace AS nsp"
" ON nsp.oid=cls.relnamespace "
Expand Down
39 changes: 29 additions & 10 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -771,17 +771,36 @@ def testKey(lyr, key, kfnames):

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

uri = "%s sslmode=disable table=\"b17518\" (geom) sql" % self.dbconn
err = QgsVectorLayerExporter.exportLayer(lyr, uri, "postgres", lyr.crs())
olyr = QgsVectorLayer(uri, "y", "postgres")
self.assertTrue(olyr.isValid())
def _test(table, schema=None):
self.execSQLCommand('DROP TABLE IF EXISTS %s CASCADE' % table)
uri = 'point?field=f1:int'
uri += '&field=F2:double(6,4)'
uri += '&field=f3:string(20)'
lyr = QgsVectorLayer(uri, "x", "memory")
self.assertTrue(lyr.isValid())

table = ("%s" % table) if schema is None else ("\"%s\".\"%s\"" % (schema, table))
dest_uri = "%s sslmode=disable table=%s (geom) sql" % (self.dbconn, table)
err = QgsVectorLayerExporter.exportLayer(lyr, dest_uri, "postgres", lyr.crs())
olyr = QgsVectorLayer(dest_uri, "y", "postgres")
self.assertTrue(olyr.isValid(), "Failed URI: %s" % dest_uri)

# Test bug 17518
_test('b17518')

# Test fully qualified table (with schema)
_test("b17518", "qgis_test")

# Test empty schema
_test("b17518", "")

# Test public schema
_test("b17518", "public")

# Test fully qualified table (with wrong schema)
with self.assertRaises(AssertionError):
_test("b17518", "qgis_test_wrong")

def testStyle(self):
self.execSQLCommand('DROP TABLE IF EXISTS layer_styles CASCADE')
Expand Down

0 comments on commit 9f56878

Please sign in to comment.