Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This registry provides a convenient place to add API relating to generic connection fetching and handling methods Currently contains a single method, which allows for retrieving matching connections using a "provider://name" format (e.g. "postgres://my connection")
- Loading branch information
1 parent
124f9b1
commit 40d5efc
Showing
10 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsconnectionregistry.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
|
||
class QgsConnectionRegistry : QObject | ||
{ | ||
%Docstring | ||
A registry for saved data provider connections, allowing retrieval of | ||
saved connections by name and provider type. | ||
|
||
QgsConnectionRegistry is not usually directly created, but rather accessed through | ||
:py:func:`QgsApplication.connectionRegistry()` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsconnectionregistry.h" | ||
%End | ||
public: | ||
|
||
QgsConnectionRegistry( QObject *parent /TransferThis/ = 0 ); | ||
%Docstring | ||
Constructor for QgsConnectionRegistry. | ||
%End | ||
|
||
|
||
QgsAbstractProviderConnection *createConnection( const QString &name ) throw( QgsProviderConnectionException ) /Factory/; | ||
%Docstring | ||
Creates a new connection by loading the connection with the given ``id`` from the settings. | ||
|
||
The ``id`` string must be of the format "provider://connection_name", e.g. "postgres://my_connection" for | ||
the PostgreSQL connection saved as "my_connection". | ||
|
||
Ownership is transferred to the caller. | ||
|
||
:raises :: py:class:`QgsProviderConnectionException` | ||
%End | ||
|
||
private: | ||
QgsConnectionRegistry( const QgsConnectionRegistry &other ); | ||
}; | ||
|
||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsconnectionregistry.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*************************************************************************** | ||
qgsconnectionregistry.cpp | ||
-------------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsconnectionregistry.h" | ||
#include "qgsproviderregistry.h" | ||
#include "qgsprovidermetadata.h" | ||
|
||
QgsConnectionRegistry::QgsConnectionRegistry( QObject *parent SIP_TRANSFERTHIS ) | ||
: QObject( parent ) | ||
{ | ||
} | ||
|
||
QgsAbstractProviderConnection *QgsConnectionRegistry::createConnection( const QString &id ) | ||
{ | ||
QRegularExpressionMatch m = QRegularExpression( QStringLiteral( "(.*?)\\://(.*)" ) ).match( id ); | ||
if ( !m.hasMatch() ) | ||
throw QgsProviderConnectionException( QObject::tr( "Invalid connection id" ) ); | ||
|
||
const QString providerKey = m.captured( 1 ); | ||
const QString name = m.captured( 2 ); | ||
|
||
QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( providerKey ); | ||
|
||
if ( !md ) | ||
throw QgsProviderConnectionException( QObject::tr( "Invalid provider key: %1" ).arg( providerKey ) ); | ||
|
||
return md->createConnection( name ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/*************************************************************************** | ||
qgsconnectionregistry.h | ||
------------------------ | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSCONNECTIONREGISTRY_H | ||
#define QGSCONNECTIONREGISTRY_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgis.h" | ||
#include <QObject> | ||
|
||
class QgsAbstractProviderConnection; | ||
|
||
|
||
/** | ||
* \class QgsConnectionRegistry | ||
* \ingroup core | ||
* A registry for saved data provider connections, allowing retrieval of | ||
* saved connections by name and provider type. | ||
* | ||
* QgsConnectionRegistry is not usually directly created, but rather accessed through | ||
* QgsApplication::connectionRegistry(). | ||
* \since QGIS 3.14 | ||
*/ | ||
class CORE_EXPORT QgsConnectionRegistry : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
/** | ||
* Constructor for QgsConnectionRegistry. | ||
*/ | ||
QgsConnectionRegistry( QObject *parent SIP_TRANSFERTHIS = nullptr ); | ||
|
||
//! Registry cannot be copied | ||
QgsConnectionRegistry( const QgsConnectionRegistry &other ) = delete; | ||
//! Registry cannot be copied | ||
QgsConnectionRegistry &operator=( const QgsConnectionRegistry &other ) = delete; | ||
|
||
/** | ||
* Creates a new connection by loading the connection with the given \a id from the settings. | ||
* | ||
* The \a id string must be of the format "provider://connection_name", e.g. "postgres://my_connection" for | ||
* the PostgreSQL connection saved as "my_connection". | ||
* | ||
* Ownership is transferred to the caller. | ||
* | ||
* \throws QgsProviderConnectionException | ||
*/ | ||
QgsAbstractProviderConnection *createConnection( const QString &name ) SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY; | ||
|
||
private: | ||
|
||
#ifdef SIP_RUN | ||
QgsConnectionRegistry( const QgsConnectionRegistry &other ); | ||
#endif | ||
}; | ||
|
||
#endif // QGSCONNECTIONREGISTRY_H | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsConnectionRegistry. | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Nyall Dawson' | ||
__date__ = '16/03/2020' | ||
__copyright__ = 'Copyright 2020, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
import shutil | ||
import os | ||
import tempfile | ||
from qgis.core import ( | ||
QgsApplication, | ||
QgsSettings, | ||
QgsProviderConnectionException, | ||
QgsVectorLayer, | ||
QgsProviderRegistry | ||
) | ||
from qgis.PyQt.QtCore import QCoreApplication | ||
from qgis.testing import start_app, unittest | ||
from utilities import unitTestDataPath | ||
|
||
# Convenience instances in case you may need them | ||
# to find the srs.db | ||
start_app() | ||
TEST_DATA_DIR = unitTestDataPath() | ||
|
||
|
||
class TestQgsConnectionRegistry(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Run before all tests""" | ||
QCoreApplication.setOrganizationName("QGIS_Test") | ||
QCoreApplication.setOrganizationDomain(cls.__name__) | ||
QCoreApplication.setApplicationName(cls.__name__) | ||
start_app() | ||
QgsSettings().clear() | ||
|
||
gpkg_original_path = '{}/qgis_server/test_project_wms_grouped_layers.gpkg'.format(TEST_DATA_DIR) | ||
cls.basetestpath = tempfile.mkdtemp() | ||
cls.gpkg_path = '{}/test_gpkg.gpkg'.format(cls.basetestpath) | ||
shutil.copy(gpkg_original_path, cls.gpkg_path) | ||
vl = QgsVectorLayer('{}|layername=cdb_lines'.format(cls.gpkg_path), 'test', 'ogr') | ||
assert vl.isValid() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
"""Run after all tests""" | ||
os.unlink(cls.gpkg_path) | ||
|
||
def testCreateConnectionBad(self): | ||
""" | ||
Test creating connection with bad parameters | ||
""" | ||
with self.assertRaises(QgsProviderConnectionException): | ||
QgsApplication.connectionRegistry().createConnection('invalid') | ||
|
||
with self.assertRaises(QgsProviderConnectionException): | ||
QgsApplication.connectionRegistry().createConnection('invalid://') | ||
|
||
with self.assertRaises(QgsProviderConnectionException): | ||
QgsApplication.connectionRegistry().createConnection('invalid://aa') | ||
|
||
def testCreateConnectionGood(self): | ||
# make a valid connection | ||
md = QgsProviderRegistry.instance().providerMetadata('ogr') | ||
conn = md.createConnection(self.gpkg_path, {}) | ||
md.saveConnection(conn, 'qgis_test1') | ||
|
||
conn = QgsApplication.connectionRegistry().createConnection('ogr://adasdas') | ||
self.assertFalse(conn.uri()) | ||
|
||
conn = QgsApplication.connectionRegistry().createConnection('ogr://qgis_test1') | ||
self.assertEqual(conn.uri(), self.gpkg_path) | ||
|
||
# case insensitive provider name | ||
conn = QgsApplication.connectionRegistry().createConnection('OGR://qgis_test1') | ||
self.assertEqual(conn.uri(), self.gpkg_path) | ||
|
||
# connection name with spaces | ||
md.saveConnection(conn, 'qgis Test 2') | ||
conn = QgsApplication.connectionRegistry().createConnection('OGR://qgis Test 2') | ||
self.assertEqual(conn.uri(), self.gpkg_path) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |