Skip to content

Commit e4df72e

Browse files
authored
Merge pull request #9086 from elpaso/bugfix-21151-db-manager-comments-armageddon
Quick and dirty patch to DB-Manager after PR 8831
2 parents a302925 + 33b8bbd commit e4df72e

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

python/plugins/db_manager/db_plugins/gpkg/plugin.py

+4
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ def __init__(self, row, table):
301301
self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row
302302
self.hasDefault = self.default
303303

304+
def getComment(self):
305+
"""Returns the comment for a field"""
306+
return ''
307+
304308

305309
class GPKGTableIndex(TableIndex):
306310

python/plugins/db_manager/db_plugins/oracle/plugin.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def connect(self, parent=None):
9191
uri = QgsDataSourceUri()
9292

9393
settingsList = ["host", "port", "database", "username", "password"]
94-
host, port, database, username, password = [settings.value(x, "", type=str) for x in settingsList]
94+
host, port, database, username, password = [
95+
settings.value(x, "", type=str) for x in settingsList]
9596

9697
# get all of the connexion options
9798

@@ -202,7 +203,8 @@ def toSqlLayer(self, sql, geomCol, uniqueCol,
202203
uri = self.uri()
203204
con = self.database().connector
204205

205-
uri.setDataSource(u"", u"({}\n)".format(sql), geomCol, filter, uniqueCol.strip(u'"'))
206+
uri.setDataSource(u"", u"({}\n)".format(
207+
sql), geomCol, filter, uniqueCol.strip(u'"'))
206208
if avoidSelectById:
207209
uri.disableSelectAtId(True)
208210
provider = self.dbplugin().providerName()
@@ -522,7 +524,7 @@ def __init__(self, row, table):
522524
def type2String(self):
523525
if (u"TIMESTAMP" in self.dataType or
524526
self.dataType in [u"DATE", u"SDO_GEOMETRY",
525-
u"BINARY_FLOAT", u"BINARY_DOUBLE"]):
527+
u"BINARY_FLOAT", u"BINARY_DOUBLE"]):
526528
return u"{}".format(self.dataType)
527529
if self.charMaxLen in [None, -1]:
528530
return u"{}".format(self.dataType)
@@ -557,6 +559,10 @@ def update(self, new_name, new_type_str=None, new_not_null=None,
557559
self.table().refreshIndexes()
558560
return ret
559561

562+
def getComment(self):
563+
"""Returns the comment for a field"""
564+
return ''
565+
560566

561567
class ORTableConstraint(TableConstraint):
562568

python/plugins/db_manager/db_plugins/spatialite/plugin.py

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ def __init__(self, row, table):
294294
self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row
295295
self.hasDefault = self.default
296296

297+
def getComment(self):
298+
"""Returns the comment for a field"""
299+
return ''
300+
297301

298302
class SLTableIndex(TableIndex):
299303

python/plugins/db_manager/db_plugins/vlayers/plugin.py

+4
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,7 @@ def __init__(self, row, table):
192192
TableField.__init__(self, table)
193193
self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row
194194
self.hasDefault = self.default
195+
196+
def getComment(self):
197+
"""Returns the comment for a field"""
198+
return ''

python/plugins/db_manager/dlg_field_properties.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,23 @@ def setField(self, fld):
5656
self.chkNull.setChecked(not fld.notNull)
5757
if fld.hasDefault:
5858
self.editDefault.setText(fld.default)
59-
# Check with SQL query if a comment exists for the field
60-
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())
61-
# Get the comment for the field with SQL Query
62-
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())
63-
c = self.db.connector._execute(None, sql_cpt) # Execute check query
64-
res = self.db.connector._fetchone(c)[0] # Fetch data
65-
# Check if result is 1 then it's ok, else we don't want to get a value
66-
if res == 1:
67-
c = self.db.connector._execute(None, sql) # Execute query returning the comment value
68-
res = self.db.connector._fetchone(c)[0] # Fetch the comment value
69-
self.db.connector._close_cursor(c) # Close cursor
70-
self.editCom.setText(res) # Set comment value
59+
# This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added
60+
# support for postgres only and broke all the others :(
61+
try:
62+
# Check with SQL query if a comment exists for the field
63+
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())
64+
# Get the comment for the field with SQL Query
65+
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())
66+
c = self.db.connector._execute(None, sql_cpt) # Execute check query
67+
res = self.db.connector._fetchone(c)[0] # Fetch data
68+
# Check if result is 1 then it's ok, else we don't want to get a value
69+
if res == 1:
70+
c = self.db.connector._execute(None, sql) # Execute query returning the comment value
71+
res = self.db.connector._fetchone(c)[0] # Fetch the comment value
72+
self.db.connector._close_cursor(c) # Close cursor
73+
self.editCom.setText(res) # Set comment value
74+
except:
75+
self.editCom.setEnabled(False)
7176

7277
def getField(self, newCopy=False):
7378
fld = TableField(self.table) if not self.fld or newCopy else self.fld

python/plugins/db_manager/dlg_table_properties.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from qgis.utils import OverrideCursor
3030

3131
from .db_plugins.data_model import TableFieldsModel, TableConstraintsModel, TableIndexesModel
32-
from .db_plugins.plugin import BaseError
32+
from .db_plugins.plugin import BaseError, DbError
3333
from .dlg_db_error import DlgDbError
3434

3535
from .dlg_field_properties import DlgFieldProperties
@@ -333,25 +333,37 @@ def deleteIndex(self):
333333
DlgDbError.showError(e, self)
334334

335335
def createComment(self):
336-
#Function that add a comment to the selected table
336+
"""Adds a comment to the selected table"""
337+
337338
try:
338339
#Using the db connector, executing de SQL query Comment on table
339340
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'{2}\';'.format(self.table.schema().name, self.table.name, self.viewComment.text()))
340341
except DbError as e:
341342
DlgDbError.showError(e, self)
342343
return
344+
except Exception as e:
345+
# This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added
346+
# support for postgres only and broke all the others :(
347+
QMessageBox.information(self, self.tr("Add comment"), self.tr("Comments are not supported for this database."))
348+
return
343349
self.refresh()
344350
#Display successful message
345351
QMessageBox.information(self, self.tr("Add comment"), self.tr("Table successfully commented"))
346352

347353
def deleteComment(self):
348-
#Function that drop the comment to the selected table
354+
"""Drops the comment on the selected table"""
355+
349356
try:
350357
#Using the db connector, executing de SQL query Comment on table using the NULL definition
351358
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS NULL;'.format(self.table.schema().name, self.table.name))
352359
except DbError as e:
353360
DlgDbError.showError(e, self)
354361
return
362+
except Exception as e:
363+
# This is an ugly patch, but the comments PR https://github.com/qgis/QGIS/pull/8831 added
364+
# support for postgres only and broke all the others :(
365+
QMessageBox.information(self, self.tr("Add comment"), self.tr("Comments are not supported for this database."))
366+
return
355367
self.refresh()
356368
#Refresh line edit, put a void comment
357369
self.viewComment.setText('')

0 commit comments

Comments
 (0)