Add comment option on db_manager pluging postgis tables

This commit is contained in:
ailurupoda 2018-12-21 16:19:59 +01:00
parent 9200f538e9
commit 09a6b33a32
4 changed files with 128 additions and 10 deletions

View File

@ -39,6 +39,7 @@ from qgis.core import (QgsDataSourceUri,
from qgis.gui import QgsMessageViewer
from qgis.utils import OverrideCursor
import psycopg2
from .ui.ui_DlgImportVector import Ui_DbManagerDlgImportVector as Ui_Dialog
@ -51,6 +52,7 @@ class DlgImportVector(QDialog, Ui_Dialog):
self.db = outDb
self.outUri = outUri
self.setupUi(self)
print(self.db)
self.default_pk = "id"
self.default_geom = "geom"
@ -80,7 +82,9 @@ class DlgImportVector(QDialog, Ui_Dialog):
if mode == self.ASK_FOR_INPUT_MODE:
self.btnChooseInputFile.clicked.connect(self.chooseInputFile)
self.cboInputLayer.currentTextChanged.connect(self.updateInputLayer)
# self.cboInputLayer.lineEdit().editingFinished.connect(self.updateInputLayer)
self.cboInputLayer.editTextChanged.connect(self.inputPathChanged)
# self.cboInputLayer.currentIndexChanged.connect(self.updateInputLayer)
self.btnUpdateInputLayer.clicked.connect(self.updateInputLayer)
self.editPrimaryKey.setText(self.default_pk)
@ -156,9 +160,16 @@ class DlgImportVector(QDialog, Ui_Dialog):
settings.setValue("/db_manager/lastUsedDir", QFileInfo(filename).filePath())
settings.setValue("/UI/lastVectorFileFilter", lastVectorFormat)
self.cboInputLayer.setCurrentIndex(-1)
self.cboInputLayer.setEditText(filename)
def inputPathChanged(self, path):
if self.cboInputLayer.currentIndex() < 0:
return
self.cboInputLayer.blockSignals(True)
self.cboInputLayer.setCurrentIndex(-1)
self.cboInputLayer.setEditText(path)
self.cboInputLayer.blockSignals(False)
def reloadInputLayer(self):
""" create the input layer and update available options """
if self.mode != self.ASK_FOR_INPUT_MODE:
@ -369,6 +380,11 @@ class DlgImportVector(QDialog, Ui_Dialog):
if self.chkSpatialIndex.isEnabled() and self.chkSpatialIndex.isChecked():
self.db.connector.createSpatialIndex((schema, table), geom)
#Add comment on table
if self.chkCom.isEnabled() and self.chkCom.isChecked():
#Using connector executing COMMENT ON TABLE query (with editCome.text() value)
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'{2}E\' '.format(schema, table ,self.editCom.text()))
self.db.connection().reconnect()
self.db.refresh()
QMessageBox.information(self, self.tr("Import to Database"), self.tr("Import was successful."))

View File

@ -59,6 +59,10 @@ class DlgTableProperties(QDialog, Ui_Dialog):
m = TableIndexesModel(self)
self.viewIndexes.setModel(m)
#Display comment in line edit
m = self.table.comment
self.viewComment.setText(m)
self.btnAddColumn.clicked.connect(self.addColumn)
self.btnAddGeometryColumn.clicked.connect(self.addGeometryColumn)
self.btnEditColumn.clicked.connect(self.editColumn)
@ -71,6 +75,11 @@ class DlgTableProperties(QDialog, Ui_Dialog):
self.btnAddSpatialIndex.clicked.connect(self.createSpatialIndex)
self.btnDeleteIndex.clicked.connect(self.deleteIndex)
#Connect button add Comment to function
self.btnAddComment.clicked.connect(self.createComment)
#Connect button delete Comment to function
self.btnDeleteComment.clicked.connect(self.deleteComment)
self.refresh()
def refresh(self):
@ -322,3 +331,31 @@ class DlgTableProperties(QDialog, Ui_Dialog):
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
def createComment(self):
#Function that add 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
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
try:
#Using the db connector, executing de SQL query Comment on table. Comment is void ''
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'\' '.format(self.table.schema().name, self.table.name))
except DbError as e:
DlgDbError.showError(e, self)
return
self.refresh()
#Refresh line edit, put a void comment
self.viewComment.setText('')
#Display successful message
QMessageBox.information(self, self.tr("Delete comment"), self.tr("Comment deleted"))

16
python/plugins/db_manager/ui/DlgImportVector.ui Normal file → Executable file
View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>482</width>
<height>496</height>
<height>627</height>
</rect>
</property>
<property name="windowTitle">
@ -254,6 +254,20 @@
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QCheckBox" name="chkCom">
<property name="text">
<string>Comment</string>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QLineEdit" name="editCom">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>

65
python/plugins/db_manager/ui/DlgTableProperties.ui Normal file → Executable file
View File

@ -17,13 +17,13 @@
<item>
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="tabColumns">
<attribute name="title">
<string>Columns</string>
</attribute>
<layout class="QVBoxLayout">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="label">
<property name="text">
@ -68,8 +68,8 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
<width>13</width>
<height>29</height>
</size>
</property>
</spacer>
@ -89,7 +89,7 @@
<attribute name="title">
<string>Constraints</string>
</attribute>
<layout class="QVBoxLayout">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
@ -141,7 +141,7 @@
<attribute name="title">
<string>Indexes</string>
</attribute>
<layout class="QVBoxLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
@ -157,7 +157,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout">
<layout class="QHBoxLayout" name="_3">
<item>
<widget class="QPushButton" name="btnAddIndex">
<property name="text">
@ -196,6 +196,57 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tabComment">
<property name="enabled">
<bool>true</bool>
</property>
<attribute name="title">
<string>Comment</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Comment defined for this table:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="viewComment"/>
</item>
<item>
<layout class="QHBoxLayout" name="_4">
<item>
<widget class="QPushButton" name="btnAddComment">
<property name="text">
<string>Add index</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnDeleteComment">
<property name="text">
<string>Delete index</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
<item>