Skip to content

Commit

Permalink
Add compose postgis and dev example of old/new sql req
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Oct 29, 2022
1 parent 31eca12 commit cef31ea
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/dev/dev_connections_old.py
Original file line number Diff line number Diff line change
@@ -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()
76 changes: 76 additions & 0 deletions tests/dev/dev_db_connections_new.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions tests/dev/docker-compose_postgis.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cef31ea

Please sign in to comment.