-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Add methods to handle QGIS service connections
- Loading branch information
1 parent
0fecfea
commit b1b7e10
Showing
1 changed file
with
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from qgis.core import ( | ||
QgsAbstractDatabaseProviderConnection, | ||
QgsDataSourceUri, | ||
QgsProviderRegistry, | ||
) | ||
from qgis.gui import QgsGui | ||
from qgis.PyQt.QtCore import QSettings | ||
|
||
|
||
def get_connections(service: str) -> dict[str, QgsAbstractDatabaseProviderConnection]: | ||
res = {} | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
conns = provider.connections() | ||
for key, pg_conn in conns.items(): | ||
if QgsDataSourceUri(pg_conn.uri()).service() == service: | ||
res[key] = pg_conn | ||
|
||
return res | ||
|
||
|
||
def create_connection(service: str, name: str) -> None: | ||
config = {} | ||
uri = f"service='{service}'" | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
conn = provider.createConnection(uri, config) | ||
provider.saveConnection(conn, name) | ||
# conn.store(name) | ||
|
||
|
||
def remove_connection(conn_name: str) -> None: | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
provider.deleteConnection(conn_name) | ||
|
||
|
||
def edit_connection(conn_name: str) -> None: | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
|
||
if conn_name in provider.dbConnections(): | ||
pg = QgsGui.sourceSelectProviderRegistry().providerByName("postgres") | ||
w = pg.createDataSourceWidget() | ||
|
||
settings = QSettings() | ||
settings.value("PostgreSQL/connections/selected") | ||
settings.setValue("PostgreSQL/connections/selected", conn_name) | ||
|
||
w.refresh() # To reflect the newly selected connection | ||
w.btnEdit_clicked() |