Skip to content

Commit

Permalink
Support OGR GPKG in datasource uri connection info
Browse files Browse the repository at this point in the history
Makes QgsDataSourceUri::connectionInfo usable with
GPKG and OGR/Spatialite file-based DBs.

With tests.

Fixes qgis#36525
  • Loading branch information
elpaso committed May 22, 2020
1 parent 25e24e7 commit 33708ea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/qgsdatasourceuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ QgsDataSourceUri::QgsDataSourceUri()
}

QgsDataSourceUri::QgsDataSourceUri( const QString &u )
: mRawUri( u )
{
QString uri = u;
int i = 0;
Expand Down Expand Up @@ -532,6 +533,17 @@ QString QgsDataSourceUri::connectionInfo( bool expandAuthConfig ) const
}
}

// Is this an OGR file-based DB?
if ( connectionItems.isEmpty() )
{
// Handles OGR geopackages, gets the raw uri and remove what is after the '|'
const QStringList parts{ mRawUri.split( '|' ) };
if ( ! parts.isEmpty() )
{
connectionItems.push_back( parts.at( 0 ) );
}
}

return connectionItems.join( QStringLiteral( " " ) );
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsdatasourceuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CORE_EXPORT QgsDataSourceUri

/**
* Returns the connection part of the URI.
* This may be a file path in case of a filesystem-based DB (spatialite or GPKG).
*/
QString connectionInfo( bool expandAuthConfig = true ) const;

Expand Down Expand Up @@ -308,6 +309,9 @@ class CORE_EXPORT QgsDataSourceUri

/* data */

//! The original unmodified/unparsed uri string
QString mRawUri;

//! host name
QString mHost;
//! port the database server listens on
Expand Down
39 changes: 39 additions & 0 deletions tests/src/core/testqgsdatasourceuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class TestQgsDataSourceUri: public QObject
private slots:
void checkparser();
void checkparser_data();

/**
* connectionInfo is used in several places to extract the
* connection part of a layer URI, this means that for a networked
* DB we get the connection string, but for a file-based DB (spatialite and
* GPKG we need to get the file path. Let's test it.
*/
void checkConnectionInfo();
void checkConnectionInfo_data();
void checkAuthParams();
};

Expand Down Expand Up @@ -230,6 +239,36 @@ void TestQgsDataSourceUri::checkparser()
QCOMPARE( ds.param( "myparam" ), myparam );
}

void TestQgsDataSourceUri::checkConnectionInfo_data()
{
QTest::addColumn<QString>( "uri" );
QTest::addColumn<QString>( "connectionInfo" );
QTest::newRow( "PG" ) << "dbname='mydb' host='myhost' user='91divoc' password='quarantine' port='5432' mode='2' schema=myschema table=my_table (geom)"
<< "dbname='mydb' host=myhost port=5432 user='91divoc' password='quarantine'";
QTest::newRow( "PG Service" ) << "service=my_service schema=myschema table=my_table"
<< "service='my_service'";
QTest::newRow( "spatialite" ) << R"(dbname='/home/qgis/dev/QGIS/tests/testdata/provider/spatialite.db' table="somedata" (geom))"
<< R"(dbname='/home/qgis/dev/QGIS/tests/testdata/provider/spatialite.db')";
// This fail because the data source uri parsed adds another back slash, it is probably not an issue
//QTest::newRow( "spatialite on windows" ) << R"(dbname='C:\my fancy path\qgis\spatialite.db' table="somedata" (geom))"
// << R"(dbname='C:\my fancy path\qgis\spatialite.db')";
QTest::newRow( "geopackage" ) << "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg|layername=my_layer"
<< "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg";
QTest::newRow( "geopackage camel case" ) << "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg|layerName=my_layer"
<< "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg";
QTest::newRow( "geopackage no layername" ) << "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg"
<< "/home/qgis/dev/QGIS/tests/testdata/provider/geopackage.gpkg";
QTest::newRow( "geopackage no path" ) << "geopackage.gpkg|layername=my_layer"
<< "geopackage.gpkg";
}

void TestQgsDataSourceUri::checkConnectionInfo()
{
QFETCH( QString, uri );
QFETCH( QString, connectionInfo );
QCOMPARE( QgsDataSourceUri( uri ).connectionInfo( false ), connectionInfo );
}

void TestQgsDataSourceUri::checkAuthParams()
{
// some providers rely on the QgsDataSourceUri params for storing and retrieving username, password and authentication.
Expand Down

0 comments on commit 33708ea

Please sign in to comment.