Skip to content
Permalink
Browse files

[ogr] Use table name as base layer name when adding ogr database layers

Fixes #20579 - Improve layer name rendering for MySQL connections
  • Loading branch information
elpaso committed Nov 21, 2018
1 parent fee3935 commit 84eed010fe7cb8833d82b83ae3b630c92edb94ef
Showing with 44 additions and 7 deletions.
  1. +13 −7 src/app/qgisapp.cpp
  2. +22 −0 src/providers/ogr/qgsogrprovider.cpp
  3. +9 −0 tests/src/providers/testqgsogrprovider.cpp
@@ -4587,15 +4587,15 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
Q_FOREACH ( QString src, layerQStringList )
{
src = src.trimmed();
QString base;
QString baseName;
if ( dataSourceType == QLatin1String( "file" ) )
{
QString srcWithoutLayername( src );
int posPipe = srcWithoutLayername.indexOf( '|' );
if ( posPipe >= 0 )
srcWithoutLayername.resize( posPipe );
QFileInfo fi( srcWithoutLayername );
base = fi.completeBaseName();
baseName = fi.completeBaseName();

// if needed prompt for zipitem layers
QString vsiPrefix = QgsZipItem::vsiPrefix( src );
@@ -4608,25 +4608,31 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
}
else if ( dataSourceType == QLatin1String( "database" ) )
{
base = src;
// Try to extract the database name and use it as base name
// sublayers names (if any) will be appended to the layer name
auto parts( QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "ogr" ), src ) );
if ( ! parts.value( QStringLiteral( "layerName" ) ).isNull() )
baseName = parts.value( QStringLiteral( "layerName" ) ).toString();
else
baseName = src;
}
else //directory //protocol
{
QFileInfo fi( src );
base = fi.completeBaseName();
baseName = fi.completeBaseName();
}
if ( settings.value( QStringLiteral( "qgis/formatLayerName" ), false ).toBool() )
{
base = QgsMapLayer::formatLayerName( base );
baseName = QgsMapLayer::formatLayerName( baseName );
}

QgsDebugMsg( "completeBaseName: " + base );
QgsDebugMsgLevel( "completeBaseName: " + baseName, 2 );

// create the layer

QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), options );
QgsVectorLayer *layer = new QgsVectorLayer( src, baseName, QStringLiteral( "ogr" ), options );
Q_CHECK_PTR( layer );

if ( ! layer )
@@ -3098,6 +3098,28 @@ QGISEXTERN QVariantMap decodeUri( const QString &uri )
path = path.left( pipeIndex );
}

// Handles DB connections extracting database name if possible
// Example: MySQL:database_name,host=localhost,port=3306 authcfg='f8wwfx8'
if ( uri.startsWith( QStringLiteral( "MySQL" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "PostgreSQL" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "MSSQL" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "ODBC" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "PGeo" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "SDE" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "OGDI" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "Ingres" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "IDB" ), Qt::CaseSensitivity::CaseInsensitive ) ||
uri.startsWith( QStringLiteral( "OCI" ), Qt::CaseSensitivity::CaseInsensitive ) )
{
auto parts( uri.split( ':' ) );
if ( parts.count( ) > 1 )
{
auto dataParts( parts.at( 1 ).split( ',' ) );
if ( dataParts.count() > 0 )
layerName = dataParts.at( 0 );
}
}

QString vsiPrefix = qgsVsiPrefix( path );
if ( !vsiPrefix.isEmpty() )
path = path.mid( vsiPrefix.count() );
@@ -45,6 +45,7 @@ class TestQgsOgrProvider : public QObject
void cleanup() {}// will be called after every testfunction.

void setupProxy();
void decodeUri();

private:
QString mTestDataDir;
@@ -119,6 +120,14 @@ void TestQgsOgrProvider::setupProxy()

}

void TestQgsOgrProvider::decodeUri()
{
auto parts( QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "ogr" ), QStringLiteral( "MySQL:database_name,host=localhost,port=3306 authcfg='f8wwfx8'" ) ) );
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QString( "database_name" ) );
parts = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "ogr" ), QStringLiteral( "MYSQL:westholland,user=root,password=psv9570,port=3306,tables=bedrijven" ) );
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QString( "westholland" ) );
}


QGSTEST_MAIN( TestQgsOgrProvider )
#include "testqgsogrprovider.moc"

0 comments on commit 84eed01

Please sign in to comment.
You can’t perform that action at this time.