Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick and dirty patch to DB-Manager after PR 8831 #9086

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/gpkg/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ def __init__(self, row, 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 ''


class GPKGTableIndex(TableIndex):

Expand Down
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/oracle/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ def update(self, new_name, new_type_str=None, new_not_null=None,
self.table().refreshIndexes()
return ret

def getComment(self):
"""Returns the comment for a field"""
return ''


class ORTableConstraint(TableConstraint):

Expand Down
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/spatialite/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ def __init__(self, row, 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 ''


class SLTableIndex(TableIndex):

Expand Down
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/vlayers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,7 @@ def __init__(self, row, table):
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 ''
29 changes: 17 additions & 12 deletions python/plugins/db_manager/dlg_field_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,23 @@ def setField(self, fld):
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
Expand Down
18 changes: 15 additions & 3 deletions python/plugins/db_manager/dlg_table_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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
Expand Down Expand Up @@ -333,25 +333,37 @@ def deleteIndex(self):
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elpaso is there any chance to avoid the catch all exception? I think even for quick and dirty patches this should be avoided.

# 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('')
Expand Down