Skip to content

Commit

Permalink
Move quering of schemas to library
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Nov 10, 2023
1 parent 9fa4a9d commit 3dc7e3c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion QgisModelBaker/gui/panel/db_config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DbConfigPanel(QWidget, metaclass=AbstractQWidgetMeta):
:type notify_field_modified: pyqtSignal(str)
"""

notify_fields_modified = pyqtSignal(str)
notify_fields_modified = pyqtSignal()

def __init__(self, parent: QWidget, db_action_type: DbActionType):
"""
Expand Down
3 changes: 0 additions & 3 deletions QgisModelBaker/gui/panel/gpkg_config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"""
import logging

from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtGui import QValidator

from QgisModelBaker.libs.modelbaker.utils.globals import DbActionType
Expand All @@ -41,8 +40,6 @@ class GpkgConfigPanel(DbConfigPanel, WIDGET_UI):
:type notify_field_modified: pyqtSignal(str)
"""

notify_fields_modified = pyqtSignal(str)

ValidExtensions = ["gpkg", "GPKG"]

def __init__(self, parent, db_action_type):
Expand Down
5 changes: 0 additions & 5 deletions QgisModelBaker/gui/panel/mssql_config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"""
import logging

from qgis.PyQt.QtCore import pyqtSignal

from QgisModelBaker.libs.modelbaker.utils.globals import DbActionType
from QgisModelBaker.libs.modelbaker.utils.qt_utils import (
NonEmptyStringValidator,
Expand All @@ -33,9 +31,6 @@


class MssqlConfigPanel(DbConfigPanel, WIDGET_UI):

notify_fields_modified = pyqtSignal(str)

def __init__(self, parent, db_action_type):
DbConfigPanel.__init__(self, parent, db_action_type)
self.setupUi(self)
Expand Down
67 changes: 37 additions & 30 deletions QgisModelBaker/gui/panel/pg_config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import logging
from enum import IntEnum

import psycopg2
import psycopg2.extras
from psycopg2 import OperationalError
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtCore import Qt, QTimer

import QgisModelBaker.libs.modelbaker.libs.pgserviceparser as pgserviceparser
import QgisModelBaker.libs.modelbaker.utils.db_utils as db_utils
from QgisModelBaker.libs.modelbaker.iliwrapper.globals import DbIliMode
from QgisModelBaker.libs.modelbaker.iliwrapper.ili2dbconfig import (
Ili2DbCommandConfiguration,
)
from QgisModelBaker.libs.modelbaker.utils.globals import DbActionType
from QgisModelBaker.libs.modelbaker.utils.qt_utils import (
NonEmptyStringValidator,
Expand Down Expand Up @@ -55,14 +57,18 @@ class _SERVICE_COMBOBOX_ROLE(IntEnum):
DBAUTHID = Qt.UserRole + 7
SSLMODE = Qt.UserRole + 8

notify_fields_modified = pyqtSignal(str)
REFRESH_SCHEMAS_TIMEOUT_MS = 500

def __init__(self, parent, db_action_type):
DbConfigPanel.__init__(self, parent, db_action_type)
self.setupUi(self)

self._current_service = None

self._fill_schema_combo_box_timer = QTimer()
self._fill_schema_combo_box_timer.setSingleShot(True)
self._fill_schema_combo_box_timer.timeout.connect(self._fill_schema_combo_box)

from QgisModelBaker.libs.modelbaker.iliwrapper.ili2dbconfig import (
BaseConfiguration,
)
Expand Down Expand Up @@ -98,9 +104,9 @@ def __init__(self, parent, db_action_type):
self.validators.validate_line_edits
)

self.pg_host_line_edit.textChanged.connect(self.notify_fields_modified)
self.pg_port_line_edit.textChanged.connect(self.notify_fields_modified)
self.pg_database_line_edit.textChanged.connect(self.notify_fields_modified)
self.pg_host_line_edit.textChanged.connect(self._fields_modified)
self.pg_port_line_edit.textChanged.connect(self._fields_modified)
self.pg_database_line_edit.textChanged.connect(self._fields_modified)
self.pg_schema_combo_box.currentTextChanged.connect(self.notify_fields_modified)

# Fill pg_services combo box
Expand Down Expand Up @@ -307,7 +313,7 @@ def _pg_service_combo_box_changed(self):
self.pg_port_line_edit.setText(service_config.get("port", ""))
self.pg_auth_settings.setUsername(service_config.get("user", ""))
self.pg_database_line_edit.setText(service_config.get("dbname", ""))
self.pg_schema_combo_box.setText("")
self.pg_schema_combo_box.setCurrentText("")
self.pg_auth_settings.setPassword(service_config.get("password", ""))
self.pg_auth_settings.setConfigId("")

Expand Down Expand Up @@ -402,6 +408,9 @@ def _pg_service_combo_box_changed(self):
== self.pg_ssl_mode_combo_box.findData(None)
)

logging.info("_pg_service_combo_box_changed")
self._fields_modified()

def _keep_custom_settings(self):

index = self.pg_service_combo_box.findData(
Expand Down Expand Up @@ -429,7 +438,7 @@ def _keep_custom_settings(self):
)
self.pg_service_combo_box.setItemData(
index,
self.pg_schema_combo_box.text().strip().lower(),
self.pg_schema_combo_box.currentText().strip().lower(),
PgConfigPanel._SERVICE_COMBOBOX_ROLE.DBSCHEMA,
)
self.pg_service_combo_box.setItemData(
Expand All @@ -448,28 +457,28 @@ def _keep_custom_settings(self):
PgConfigPanel._SERVICE_COMBOBOX_ROLE.SSLMODE,
)

def _fields_modified(self):

self._fill_schema_combo_box_timer.start(self.REFRESH_SCHEMAS_TIMEOUT_MS)

self.notify_fields_modified.emit()

def _fill_schema_combo_box(self):
connection = None
try:
connection = psycopg2.connect(
dbname=self.pg_database_line_edit.text(),
user=self.pg_auth_settings.username(),
password=self.pg_auth_settings.password(),
host=self.pg_host_line_edit.text(),
port=self.pg_port_line_edit.text(),
)

except OperationalError as exception:
logging.warning(f"Pg connection error: {exception}")
return
configuration = Ili2DbCommandConfiguration()

sql = """SELECT schema_name
FROM information_schema.schemata; """
mode = DbIliMode.pg
self.get_fields(configuration)

cursor = connection.cursor()
cursor.execute(sql)
configuration.tool = mode
configuration.db_ili_version = db_utils.db_ili_version(configuration)

schemas = cursor.fetchall()
db_connector = db_utils.get_db_connector(configuration)
if not db_connector:
logging.warning("Refresh schema list connection error")
return

schemas = db_connector.get_schemas()

AUTO_ADDED_SCHEMA = "auto_added_schema"

Expand All @@ -482,10 +491,8 @@ def _fill_schema_combo_box(self):
index_to_remove = self.pg_schema_combo_box.findData(AUTO_ADDED_SCHEMA)

for schema in schemas:
self.pg_schema_combo_box.addItem(schema[0], AUTO_ADDED_SCHEMA)
self.pg_schema_combo_box.addItem(schema, AUTO_ADDED_SCHEMA)

currentTextIndex = self.pg_schema_combo_box.findText(currentText)
if currentTextIndex > -1:
self.pg_schema_combo_box.setCurrentIndex(currentTextIndex)

connection.close()

0 comments on commit 3dc7e3c

Please sign in to comment.