Skip to content

Commit

Permalink
Add comment on field | DbManager
Browse files Browse the repository at this point in the history
  • Loading branch information
corentin.falcone committed Jan 10, 2019
1 parent 2e97615 commit a764c08
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
6 changes: 3 additions & 3 deletions python/plugins/db_manager/db_plugins/data_model.py
Expand Up @@ -259,7 +259,7 @@ def getObjectIter(self):
class TableFieldsModel(SimpleTableModel):

def __init__(self, parent, editable=False):
SimpleTableModel.__init__(self, ['Name', 'Type', 'Null', 'Default'], editable, parent)
SimpleTableModel.__init__(self, ['Name', 'Type', 'Null', 'Default', 'Comment'], editable, parent)

def headerData(self, section, orientation, role):
if orientation == Qt.Vertical and role == Qt.DisplayRole:
Expand All @@ -273,7 +273,7 @@ def flags(self, index):
return flags

def append(self, fld):
data = [fld.name, fld.type2String(), not fld.notNull, fld.default2String()]
data = [fld.name, fld.type2String(), not fld.notNull, fld.default2String(), fld.getComment()]
self.appendRow(self.rowFromData(data))
row = self.rowCount() - 1
self.setData(self.index(row, 0), fld, Qt.UserRole)
Expand All @@ -290,7 +290,6 @@ def getObject(self, row):
val = self.data(self.index(row, 0), Qt.UserRole)
fld = val if val is not None else self._getNewObject()
fld.name = self.data(self.index(row, 0)) or ""

typestr = self.data(self.index(row, 1)) or ""
regex = QRegExp("([^\\(]+)\\(([^\\)]+)\\)")
startpos = regex.indexIn(typestr)
Expand All @@ -303,6 +302,7 @@ def getObject(self, row):

fld.notNull = self.data(self.index(row, 2), Qt.CheckStateRole) == Qt.Unchecked
fld.primaryKey = self.data(self.index(row, 1), Qt.UserRole)
fld.comment = self.data(self.index(row, 4), Qt.UserRole)
return fld

def getFields(self):
Expand Down
27 changes: 24 additions & 3 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -1095,13 +1095,31 @@ def definition(self):
txt += u" DEFAULT %s" % self.default2String()
return txt

def getComment(self):
# Function returning the comment of a field if exists
tab = self.table()
#SQL Query checking if a comment exists for the field
sql_cpt = u"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" % (tab.name, self.name)
#SQL Query that return the comment of the field
sql = u"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" % (tab.name, self.name)
c = tab.database().connector._execute(None, sql_cpt)# Execute Check query
res = tab.database().connector._fetchone(c)[0] #Store result
if res == 1:
# When a comment exists
c = tab.database().connector._execute(None, sql) #Execute query
res = tab.database().connector._fetchone(c)[0] #Store result
tab.database().connector._close_cursor(c) #Close cursor
return res #Return comment
else:
return '' #Return void string

def delete(self):
return self.table().deleteField(self)

def rename(self, new_name):
return self.update(new_name)

def update(self, new_name, new_type_str=None, new_not_null=None, new_default_str=None):
def update(self, new_name, new_type_str=None, new_not_null=None, new_default_str=None, new_comment=None):
self.table().aboutToChange.emit()
if self.name == new_name:
new_name = None
Expand All @@ -1111,15 +1129,18 @@ def update(self, new_name, new_type_str=None, new_not_null=None, new_default_str
new_not_null = None
if self.default2String() == new_default_str:
new_default_str = None

if self.comment == new_comment:
# Update also a new_comment
new_comment = None
ret = self.table().database().connector.updateTableColumn((self.table().schemaName(), self.table().name),
self.name, new_name, new_type_str, new_not_null,
new_default_str)
new_default_str, new_comment)
if ret is not False:
self.table().refreshFields()
return ret



class TableConstraint(TableSubItemObject):

""" class that represents a constraint of a table (relation) """
Expand Down
11 changes: 9 additions & 2 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -857,8 +857,8 @@ def deleteTableColumn(self, table, column):
sql = u"ALTER TABLE %s DROP %s" % (self.quoteId(table), self.quoteId(column))
self._execute_and_commit(sql)

def updateTableColumn(self, table, column, new_name=None, data_type=None, not_null=None, default=None):
if new_name is None and data_type is None and not_null is None and default is None:
def updateTableColumn(self, table, column, new_name=None, data_type=None, not_null=None, default=None, comment=None):
if new_name is None and data_type is None and not_null is None and default is None and comment is None:
return

c = self._get_cursor()
Expand Down Expand Up @@ -895,6 +895,13 @@ def updateTableColumn(self, table, column, new_name=None, data_type=None, not_nu
self.quoteString(new_name), schema_where, self.quoteString(tablename), self.quoteString(column))
self._execute(c, sql)

# comment the column
if comment is not None:
# Add the comment on the field
schema, tablename = self.getSchemaTableName(table)
sql = u"COMMENT ON COLUMN %s.%s.%s is '%s'" % (schema, tablename, column, comment)
self._execute(c, sql)

self._commit()

def renameTableColumn(self, table, column, new_name):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/db_manager/db_plugins/postgis/info_model.py
Expand Up @@ -162,7 +162,7 @@ def fieldsDetails(self):
header = (
"#", QApplication.translate("DBManagerPlugin", "Name"), QApplication.translate("DBManagerPlugin", "Type"),
QApplication.translate("DBManagerPlugin", "Length"), QApplication.translate("DBManagerPlugin", "Null"),
QApplication.translate("DBManagerPlugin", "Default"))
QApplication.translate("DBManagerPlugin", "Default"),QApplication.translate("DBManagerPlugin", "Comment"))
tbl.append(HtmlTableHeader(header))

# add table contents
Expand All @@ -174,7 +174,7 @@ def fieldsDetails(self):
attrs = {"class": "underline"} if fld.primaryKey else None
name = HtmlTableCol(fld.name, attrs)

tbl.append((fld.num, name, fld.type2String(), char_max_len, is_null_txt, fld.default2String()))
tbl.append((fld.num, name, fld.type2String(), char_max_len, is_null_txt, fld.default2String(), fld.getComment()))

return HtmlTable(tbl, {"class": "header"})

Expand Down
14 changes: 14 additions & 0 deletions python/plugins/db_manager/dlg_field_properties.py
Expand Up @@ -56,6 +56,18 @@ 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 = u"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 = u"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

def getField(self, newCopy=False):
fld = TableField(self.table) if not self.fld or newCopy else self.fld
Expand All @@ -64,6 +76,8 @@ def getField(self, newCopy=False):
fld.notNull = not self.chkNull.isChecked()
fld.default = self.editDefault.text()
fld.hasDefault = fld.default != ""
#Get the comment from the LineEdit
fld.comment = self.editCom.text()
try:
modifier = int(self.editLength.text())
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/db_manager/dlg_table_properties.py
Expand Up @@ -162,7 +162,7 @@ def editColumn(self):
with OverrideCursor(Qt.WaitCursor):
self.aboutToChangeTable.emit()
try:
fld.update(new_fld.name, new_fld.type2String(), new_fld.notNull, new_fld.default2String())
fld.update(new_fld.name, new_fld.type2String(), new_fld.notNull, new_fld.default2String(), new_fld.comment)
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
Expand Down

0 comments on commit a764c08

Please sign in to comment.