From cef31ea41b1b62ec99bbd21367bb5fa8f5b8defa Mon Sep 17 00:00:00 2001 From: Julien M Date: Sat, 29 Oct 2022 09:53:42 +0200 Subject: [PATCH] Add compose postgis and dev example of old/new sql req --- tests/dev/dev_connections_old.py | 72 ++++++++++++++++++++++++++ tests/dev/dev_db_connections_new.py | 76 ++++++++++++++++++++++++++++ tests/dev/docker-compose_postgis.yml | 9 ++++ 3 files changed, 157 insertions(+) create mode 100644 tests/dev/dev_connections_old.py create mode 100644 tests/dev/dev_db_connections_new.py create mode 100644 tests/dev/docker-compose_postgis.yml diff --git a/tests/dev/dev_connections_old.py b/tests/dev/dev_connections_old.py new file mode 100644 index 00000000..48901130 --- /dev/null +++ b/tests/dev/dev_connections_old.py @@ -0,0 +1,72 @@ +import pprint + +from qgis.core import QgsDataSourceUri +from qgis.PyQt.QtCore import QSettings +from qgis.PyQt.QtGui import QIcon +from qgis.PyQt.QtWidgets import QDialog, QComboBox + +# variables +db_types = { + "GeoPackage": QIcon(":/images/themes/default/mGeoPackage.svg"), + "PostgreSQL": QIcon(":/images/themes/default/mIconPostgis.svg"), + "SpatiaLite": QIcon(":/images/themes/default/mIconSpatialite.svg"), +} +dico_connections_for_combobox = {} +settings = QSettings() + +for db_type in db_types: + print(db_type) + # retrouver les connections du type de base de données + settings.beginGroup(f"/{db_type}/connections/") + connections = settings.childGroups() + settings.endGroup() + + selected_conn = settings.value(f"/{db_type}/connections/selected", "", type=str) + if selected_conn not in connections: + connections.append(selected_conn) + + for connection_name in connections: + print(connection_name) + uri = QgsDataSourceUri() + uri.setConnection( + aHost=settings.value(f"{db_type}/connections/{connection_name}/host"), + aPort=settings.value(f"{db_type}/connections/{connection_name}/port"), + aDatabase=settings.value( + f"{db_type}/connections/{connection_name}/database" + ), + aUsername="", + aPassword="", + ) + # selon le type d'authentification configuré, on s'adapte + if ( + settings.value(f"{db_type}/connections/{connection_name}/saveUsername") + == "true" + ): + uri.setUsername( + settings.value(f"{db_type}/connections/{connection_name}/username"), + ) + if ( + settings.value(f"{db_type}/connections/{connection_name}/savePassword") + == "true" + ): + uri.setPassword( + settings.value(f"{db_type}/connections/{connection_name}/password"), + ) + dico_connections_for_combobox[connection_name] = db_type, uri + + +# la fenêtre de dialogue pour accueillir notre liste déroulante +dd = QDialog(iface.mainWindow()) +dd.setWindowTitle("Connexions {}".format(" / ".join(db_types))) + +# on remplit la liste déroulante +cbb_db_connections = QComboBox(dd) +for k, v in dico_connections_for_combobox.items(): + cbb_db_connections.addItem(db_types.get(v[0]), k, v[1]) + +# un peu de tunning des dimensions +dd.resize(300, 30) +cbb_db_connections.resize(300, 30) + +# on affiche +dd.show() diff --git a/tests/dev/dev_db_connections_new.py b/tests/dev/dev_db_connections_new.py new file mode 100644 index 00000000..8ee0652b --- /dev/null +++ b/tests/dev/dev_db_connections_new.py @@ -0,0 +1,76 @@ +import pprint +from functools import partial + +from qgis.core import QgsApplication, QgsDataSourceUri, QgsProviderRegistry +from qgis.PyQt.QtGui import QIcon +from qgis.PyQt.QtWidgets import QComboBox, QDialog + +# variables + +# point de vigilance, les noms du type de base de données ne sont pas exactement les mêmes... +db_types = ("ogr", "postgres", "spatialite") +dico_connections_for_combobox = {} + +# un peu d'info +print( + "Liste des formats disponibles : ", + sorted(QgsProviderRegistry.instance().providerList()), +) +print( + "Liste des drivers de base de données disponibles : ", + QgsProviderRegistry.instance().databaseDrivers(), +) + +for db_type in db_types: + print("listing des connexions de type : ", db_type) + # retrouver les connections du type de base de données + connections = QgsProviderRegistry.instance().providerMetadata(db_type).connections() + + for connection_name in connections: + dico_connections_for_combobox[connection_name] = db_type, connections.get( + connection_name + ) + + +def popo(): + print("hey") + print(cbb_db_connections.currentText()) + conn = dico_connections_for_combobox[cbb_db_connections.currentText()][1] + print(conn.providerKey(), conn.icon()) + + selected = cbb_db_connections.itemData(cbb_db_connections.currentIndex()) + print(selected) + print(selected.uri()) + print(isinstance(selected.uri(), str)) +# print(QgsDataSourceUri(selected.uri()).database()) +# print(QgsDataSourceUri(selected.uri()).host()) +# print(selected.configuration()) + + # print(conn.uri.connectionInfo(True)) + + # lister les schémas +# if conn.providerKey().startswith("postgr"): +# print("Schémas : ", conn.schemas()) + + # lister les tables + # print("Tables : ", [(t.defaultName(), str(t.flags())) for t in conn.tables()[:10]]) + + +# la fenêtre de dialogue pour accueillir notre liste déroulante +dd = QDialog(iface.mainWindow()) +dd.setWindowTitle("Connexions {}".format(" / ".join(db_types))) + +# on remplit la liste déroulante +cbb_db_connections = QComboBox(dd) +for k, v in dico_connections_for_combobox.items(): + cbb_db_connections.addItem(v[1].icon(), k, v[1]) + +cbb_db_connections.activated.connect(partial(popo)) + +# un peu de tunning des dimensions +dd.resize(300, 30) +cbb_db_connections.resize(300, 30) + + +# on affiche +dd.show() diff --git a/tests/dev/docker-compose_postgis.yml b/tests/dev/docker-compose_postgis.yml new file mode 100644 index 00000000..8ae4a2b5 --- /dev/null +++ b/tests/dev/docker-compose_postgis.yml @@ -0,0 +1,9 @@ +version: "3" +services: + postgis: + image: postgis/postgis:13-3.0-alpine + ports: + - "5432:5432" + environment: + - POSTGRES_USER=geotribu + - POSTGRES_PASSWORD=geotribu