From cd6ce76480f19db651a5dd33134b7963b1b74daa Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Mon, 4 Feb 2019 18:26:24 +0100 Subject: [PATCH 1/3] Quick and dirty patch to DB-Manager after PR 8831 The "comments" PR 8831 added support for postgres only (and broke all the others backends). I'd be in favor of a revert of the whole original PR but this patch restores functionality and could be an acceptable temporary fix until the comments PR is reworked in a more maintainable and elegant way. Fixes #21151 btw --- .../db_manager/db_plugins/gpkg/plugin.py | 4 +++ .../db_manager/db_plugins/oracle/plugin.py | 4 +++ .../db_plugins/spatialite/plugin.py | 4 +++ .../db_manager/db_plugins/vlayers/plugin.py | 4 +++ .../db_manager/dlg_field_properties.py | 29 +++++++++++-------- .../db_manager/dlg_table_properties.py | 18 ++++++++++-- 6 files changed, 48 insertions(+), 15 deletions(-) diff --git a/python/plugins/db_manager/db_plugins/gpkg/plugin.py b/python/plugins/db_manager/db_plugins/gpkg/plugin.py index 1dbfdad01ce..eb23c1bef68 100644 --- a/python/plugins/db_manager/db_plugins/gpkg/plugin.py +++ b/python/plugins/db_manager/db_plugins/gpkg/plugin.py @@ -301,6 +301,10 @@ class GPKGTableField(TableField): self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row self.hasDefault = self.default + def getComment(self): + """Returns the comment for a field""" + return '' + class GPKGTableIndex(TableIndex): diff --git a/python/plugins/db_manager/db_plugins/oracle/plugin.py b/python/plugins/db_manager/db_plugins/oracle/plugin.py index 0ba34f33e23..ab22f807e7c 100644 --- a/python/plugins/db_manager/db_plugins/oracle/plugin.py +++ b/python/plugins/db_manager/db_plugins/oracle/plugin.py @@ -557,6 +557,10 @@ class ORTableField(TableField): self.table().refreshIndexes() return ret + def getComment(self): + """Returns the comment for a field""" + return '' + class ORTableConstraint(TableConstraint): diff --git a/python/plugins/db_manager/db_plugins/spatialite/plugin.py b/python/plugins/db_manager/db_plugins/spatialite/plugin.py index a6eca142bc6..48ef67fd8f2 100644 --- a/python/plugins/db_manager/db_plugins/spatialite/plugin.py +++ b/python/plugins/db_manager/db_plugins/spatialite/plugin.py @@ -294,6 +294,10 @@ class SLTableField(TableField): self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row self.hasDefault = self.default + def getComment(self): + """Returns the comment for a field""" + return '' + class SLTableIndex(TableIndex): diff --git a/python/plugins/db_manager/db_plugins/vlayers/plugin.py b/python/plugins/db_manager/db_plugins/vlayers/plugin.py index 8d94e4b7041..8b8b05413f4 100644 --- a/python/plugins/db_manager/db_plugins/vlayers/plugin.py +++ b/python/plugins/db_manager/db_plugins/vlayers/plugin.py @@ -192,3 +192,7 @@ class LTableField(TableField): TableField.__init__(self, table) self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row self.hasDefault = self.default + + def getComment(self): + """Returns the comment for a field""" + return '' diff --git a/python/plugins/db_manager/dlg_field_properties.py b/python/plugins/db_manager/dlg_field_properties.py index 5e4c2ed21e2..9979e9dc767 100644 --- a/python/plugins/db_manager/dlg_field_properties.py +++ b/python/plugins/db_manager/dlg_field_properties.py @@ -56,18 +56,23 @@ class DlgFieldProperties(QDialog, Ui_Dialog): self.chkNull.setChecked(not fld.notNull) if fld.hasDefault: self.editDefault.setText(fld.default) - # Check with SQL query if a comment exists for the field - sql_cpt = "Select count(*) from pg_description pd, pg_class pc, pg_attribute pa where relname = '%s' and attname = '%s' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum" % (self.table.name, self.editName.text()) - # Get the comment for the field with SQL Query - sql = "Select pd.description from pg_description pd, pg_class pc, pg_attribute pa where relname = '%s' and attname = '%s' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum" % (self.table.name, self.editName.text()) - c = self.db.connector._execute(None, sql_cpt) # Execute check query - res = self.db.connector._fetchone(c)[0] # Fetch data - # Check if result is 1 then it's ok, else we don't want to get a value - if res == 1: - c = self.db.connector._execute(None, sql) # Execute query returning the comment value - res = self.db.connector._fetchone(c)[0] # Fetch the comment value - self.db.connector._close_cursor(c) # Close cursor - self.editCom.setText(res) # Set comment value + # This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added + # support for postgres only and broke all the others :( + try: + # Check with SQL query if a comment exists for the field + sql_cpt = "Select count(*) from pg_description pd, pg_class pc, pg_attribute pa where relname = '%s' and attname = '%s' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum" % (self.table.name, self.editName.text()) + # Get the comment for the field with SQL Query + sql = "Select pd.description from pg_description pd, pg_class pc, pg_attribute pa where relname = '%s' and attname = '%s' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum" % (self.table.name, self.editName.text()) + c = self.db.connector._execute(None, sql_cpt) # Execute check query + res = self.db.connector._fetchone(c)[0] # Fetch data + # Check if result is 1 then it's ok, else we don't want to get a value + if res == 1: + c = self.db.connector._execute(None, sql) # Execute query returning the comment value + res = self.db.connector._fetchone(c)[0] # Fetch the comment value + self.db.connector._close_cursor(c) # Close cursor + self.editCom.setText(res) # Set comment value + except: + self.editCom.setEnabled(False) def getField(self, newCopy=False): fld = TableField(self.table) if not self.fld or newCopy else self.fld diff --git a/python/plugins/db_manager/dlg_table_properties.py b/python/plugins/db_manager/dlg_table_properties.py index 061852cb1d1..c975f41c07d 100644 --- a/python/plugins/db_manager/dlg_table_properties.py +++ b/python/plugins/db_manager/dlg_table_properties.py @@ -29,7 +29,7 @@ from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QApplication from qgis.utils import OverrideCursor from .db_plugins.data_model import TableFieldsModel, TableConstraintsModel, TableIndexesModel -from .db_plugins.plugin import BaseError +from .db_plugins.plugin import BaseError, DbError from .dlg_db_error import DlgDbError from .dlg_field_properties import DlgFieldProperties @@ -333,25 +333,37 @@ class DlgTableProperties(QDialog, Ui_Dialog): DlgDbError.showError(e, self) def createComment(self): - #Function that add a comment to the selected table + """Adds a comment to the selected table""" + try: #Using the db connector, executing de SQL query Comment on table self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'{2}\';'.format(self.table.schema().name, self.table.name, self.viewComment.text())) except DbError as e: DlgDbError.showError(e, self) return + except Exception as e: + # This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added + # support for postgres only and broke all the others :( + QMessageBox.information(self, self.tr("Add comment"), self.tr("Comments are not supported for this database.")) + return self.refresh() #Display successful message QMessageBox.information(self, self.tr("Add comment"), self.tr("Table successfully commented")) def deleteComment(self): - #Function that drop the comment to the selected table + """Drops the comment on the selected table""" + try: #Using the db connector, executing de SQL query Comment on table using the NULL definition self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS NULL;'.format(self.table.schema().name, self.table.name)) except DbError as e: DlgDbError.showError(e, self) return + except Exception as e: + # This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added + # support for postgres only and broke all the others :( + QMessageBox.information(self, self.tr("Add comment"), self.tr("Comments are not supported for this database.")) + return self.refresh() #Refresh line edit, put a void comment self.viewComment.setText('') From de5171ac89ada7e952b6bcef0908a452e9e0f1a8 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Tue, 5 Feb 2019 08:38:40 +0100 Subject: [PATCH 2/3] Code style (I had autopep8 installed btw) --- .../db_manager/db_plugins/oracle/plugin.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python/plugins/db_manager/db_plugins/oracle/plugin.py b/python/plugins/db_manager/db_plugins/oracle/plugin.py index ab22f807e7c..399a24cac73 100644 --- a/python/plugins/db_manager/db_plugins/oracle/plugin.py +++ b/python/plugins/db_manager/db_plugins/oracle/plugin.py @@ -403,18 +403,18 @@ class ORTable(Table): for idx in indexes: if idx.isUnique and len(idx.columns) == 1: fld = idx.fields()[idx.columns[0]] - if (fld.dataType == u"NUMBER" and - not fld.modifier and - fld.notNull and - fld not in ret): + if (fld.dataType == u"NUMBER" + and not fld.modifier + and fld.notNull + and fld not in ret): ret.append(fld) # and finally append the other suitable fields for fld in self.fields(): - if (fld.dataType == u"NUMBER" and - not fld.modifier and - fld.notNull and - fld not in ret): + if (fld.dataType == u"NUMBER" + and not fld.modifier + and fld.notNull + and fld not in ret): ret.append(fld) if onlyOne: @@ -514,15 +514,15 @@ class ORTableField(TableField): # find out whether fields are part of primary key for con in self.table().constraints(): - if (con.type == ORTableConstraint.TypePrimaryKey and - self.name == con.column): + if (con.type == ORTableConstraint.TypePrimaryKey + and self.name == con.column): self.primaryKey = True break def type2String(self): - if (u"TIMESTAMP" in self.dataType or - self.dataType in [u"DATE", u"SDO_GEOMETRY", - u"BINARY_FLOAT", u"BINARY_DOUBLE"]): + if (u"TIMESTAMP" in self.dataType + or self.dataType in [u"DATE", u"SDO_GEOMETRY", + u"BINARY_FLOAT", u"BINARY_DOUBLE"]): return u"{}".format(self.dataType) if self.charMaxLen in [None, -1]: return u"{}".format(self.dataType) From 33b8bbdff9febdfa08e1a4a73e244f02c3eb542f Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Tue, 5 Feb 2019 11:59:14 +0100 Subject: [PATCH 3/3] Astyle again --- .../db_manager/db_plugins/oracle/plugin.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/python/plugins/db_manager/db_plugins/oracle/plugin.py b/python/plugins/db_manager/db_plugins/oracle/plugin.py index 399a24cac73..84a8e7cc02a 100644 --- a/python/plugins/db_manager/db_plugins/oracle/plugin.py +++ b/python/plugins/db_manager/db_plugins/oracle/plugin.py @@ -91,7 +91,8 @@ class OracleDBPlugin(DBPlugin): uri = QgsDataSourceUri() settingsList = ["host", "port", "database", "username", "password"] - host, port, database, username, password = [settings.value(x, "", type=str) for x in settingsList] + host, port, database, username, password = [ + settings.value(x, "", type=str) for x in settingsList] # get all of the connexion options @@ -202,7 +203,8 @@ class ORDatabase(Database): uri = self.uri() con = self.database().connector - uri.setDataSource(u"", u"({}\n)".format(sql), geomCol, filter, uniqueCol.strip(u'"')) + uri.setDataSource(u"", u"({}\n)".format( + sql), geomCol, filter, uniqueCol.strip(u'"')) if avoidSelectById: uri.disableSelectAtId(True) provider = self.dbplugin().providerName() @@ -403,18 +405,18 @@ class ORTable(Table): for idx in indexes: if idx.isUnique and len(idx.columns) == 1: fld = idx.fields()[idx.columns[0]] - if (fld.dataType == u"NUMBER" - and not fld.modifier - and fld.notNull - and fld not in ret): + if (fld.dataType == u"NUMBER" and + not fld.modifier and + fld.notNull and + fld not in ret): ret.append(fld) # and finally append the other suitable fields for fld in self.fields(): - if (fld.dataType == u"NUMBER" - and not fld.modifier - and fld.notNull - and fld not in ret): + if (fld.dataType == u"NUMBER" and + not fld.modifier and + fld.notNull and + fld not in ret): ret.append(fld) if onlyOne: @@ -514,14 +516,14 @@ class ORTableField(TableField): # find out whether fields are part of primary key for con in self.table().constraints(): - if (con.type == ORTableConstraint.TypePrimaryKey - and self.name == con.column): + if (con.type == ORTableConstraint.TypePrimaryKey and + self.name == con.column): self.primaryKey = True break def type2String(self): - if (u"TIMESTAMP" in self.dataType - or self.dataType in [u"DATE", u"SDO_GEOMETRY", + if (u"TIMESTAMP" in self.dataType or + self.dataType in [u"DATE", u"SDO_GEOMETRY", u"BINARY_FLOAT", u"BINARY_DOUBLE"]): return u"{}".format(self.dataType) if self.charMaxLen in [None, -1]: