New fix, still some need to be done

This commit is contained in:
Ailurupoda 2019-02-06 17:37:48 +01:00
parent dff185355d
commit f3dfd97d90
15 changed files with 127 additions and 66 deletions

View File

@ -232,9 +232,4 @@ class DBConnector(object):
return []
def getQueryBuilderDictionary(self):
return {}
def setField(self, fld, tablename=None, db=None):
if fld is None:
return
return fld.name, fld.dataType, str(fld.modifier), fld.notNull, fld.default, None
return {}

View File

@ -620,6 +620,14 @@ class GPKGDBConnector(DBConnector):
""" run vacuum on the db """
self._execute_and_commit("VACUUM")
def commentTable(self, schema, tablename, comment):
"""Comment the table"""
return ''
def getComment(self, tab, field, db):
"""Returns the comment for a field"""
return ''
def addTableColumn(self, table, field_def):
""" add a column to table """

View File

@ -177,8 +177,8 @@ class GPKGDatabase(Database):
vl.setSubsetString(sql)
return vl
def searchClass(self):
return "GPKGDatabase"
def supportsComment(self):
return False
class GPKGTable(Table):

View File

@ -475,16 +475,16 @@ class OracleDBConnector(DBConnector):
def singleGeomTypes(self, geomtypes, srids):
"""Intelligent wkbtype grouping (multi with non multi)"""
if (QgsWkbTypes.Polygon in geomtypes and
QgsWkbTypes.MultiPolygon in geomtypes):
if (QgsWkbTypes.Polygon in geomtypes
and QgsWkbTypes.MultiPolygon in geomtypes):
srids.pop(geomtypes.index(QgsWkbTypes.Polygon))
geomtypes.pop(geomtypes.index(QgsWkbTypes.Polygon))
if (QgsWkbTypes.Point in geomtypes and
QgsWkbTypes.MultiPoint in geomtypes):
if (QgsWkbTypes.Point in geomtypes
and QgsWkbTypes.MultiPoint in geomtypes):
srids.pop(geomtypes.index(QgsWkbTypes.Point))
geomtypes.pop(geomtypes.index(QgsWkbTypes.Point))
if (QgsWkbTypes.LineString in geomtypes and
QgsWkbTypes.MultiLineString in geomtypes):
if (QgsWkbTypes.LineString in geomtypes
and QgsWkbTypes.MultiLineString in geomtypes):
srids.pop(geomtypes.index(QgsWkbTypes.LineString))
geomtypes.pop(geomtypes.index(QgsWkbTypes.LineString))
if QgsWkbTypes.Unknown in geomtypes and len(geomtypes) > 1:
@ -1306,6 +1306,14 @@ class OracleDBConnector(DBConnector):
"""Rename a schema in the database."""
# Unsupported in Oracle
pass
def commentTable(self, schema, tablename, comment):
"""Comment the table"""
return ''
def getComment(self, tab, field, db):
"""Returns the comment for a field"""
return ''
def addTableColumn(self, table, field_def):
"""Add a column to a table."""

View File

@ -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()
@ -261,8 +263,8 @@ class ORDatabase(Database):
mainWindow.registerAction(action, QApplication.translate(
"DBManagerPlugin", "&Table"), self.emptyTableActionSlot)
def searchClass(self):
return "ORDatabase"
def supportsComment(self):
return False
class ORSchema(Schema):
@ -525,7 +527,7 @@ class ORTableField(TableField):
def type2String(self):
if (u"TIMESTAMP" in self.dataType or
self.dataType in [u"DATE", u"SDO_GEOMETRY",
u"BINARY_FLOAT", u"BINARY_DOUBLE"]):
u"BINARY_FLOAT", u"BINARY_DOUBLE"]):
return u"{}".format(self.dataType)
if self.charMaxLen in [None, -1]:
return u"{}".format(self.dataType)

View File

@ -1096,6 +1096,7 @@ class TableField(TableSubItemObject):
return txt
def getComment(self):
"""Returns the comment for a field"""
return ''
def delete(self):

View File

@ -193,6 +193,7 @@ class PostGisDBConnector(DBConnector):
self._close_cursor(c)
return res
def getSpatialInfo(self):
""" returns tuple about PostGIS support:
- lib version
@ -511,25 +512,6 @@ class PostGisDBConnector(DBConnector):
self._close_cursor(c)
return res
def setField(self, fld, tablename, db):
if fld is None:
return
print (tablename)
# 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" % (tablename, fld.name)
# 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" % (tablename, fld.name)
c = db.connector._execute(None, sql_cpt) # Execute check query
res = 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 = db.connector._execute(None, sql) # Execute query returning the comment value
res2 = db.connector._fetchone(c)[0] # Fetch the comment value
db.connector._close_cursor(c) # Close cursor
else :
res2 = None
return fld.name, fld.dataType, str(fld.modifier), fld.notNull, fld.default, res2
def getTableIndexes(self, table):
""" get info about table's indexes. ignore primary key constraint index, they get listed in constraints """
schema, tablename = self.getSchemaTableName(table)
@ -761,6 +743,27 @@ class PostGisDBConnector(DBConnector):
self._commit()
def commentTable(self, schema, tablename, comment):
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'{2}\''.format(schema, tablename, comment))
def getComment(self, tab, field, db):
"""Returns the comment for a field"""
# SQL Query checking 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" % (tab, field)
# SQL Query that return the comment of the field
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" % (tab, field)
c = db.connector._execute(None, sql_cpt) # Execute Check query
res = db.connector._fetchone(c)[0] # Store result
print(tab, field, sql_cpt, sql)
if res == 1:
# When a comment exists
c = db.connector._execute(None, sql) # Execute query
res = db.connector._fetchone(c)[0] # Store result
db.connector._close_cursor(c) # Close cursor
return res # Return comment
else:
return ''
def moveTableToSchema(self, table, new_schema):
schema, tablename = self.getSchemaTableName(table)
if new_schema == schema:

View File

@ -181,8 +181,8 @@ class PGDatabase(Database):
def hasLowercaseFieldNamesOption(self):
return True
def searchClass(self):
return "PGDatabase"
def supportsComment(self):
return True
class PGSchema(Schema):
@ -399,7 +399,7 @@ class PGTableField(TableField):
if con.type == TableConstraint.TypePrimaryKey and self.num in con.columns:
self.primaryKey = True
break
def getComment(self):
"""Returns the comment for a field"""
tab = self.table()
@ -419,6 +419,7 @@ class PGTableField(TableField):
return ''
class PGTableConstraint(TableConstraint):
def __init__(self, row, table):

View File

@ -465,6 +465,8 @@ class SpatiaLiteDBConnector(DBConnector):
self._execute(c, sql)
self._commit()
return True
def emptyTable(self, table):
""" delete all rows from table """
if self.isRasterTable(table):
@ -494,6 +496,7 @@ class SpatiaLiteDBConnector(DBConnector):
self._execute(c, sql)
self._commit()
return True
def moveTable(self, table, new_table, new_schema=None):
return self.renameTable(table, new_table)
@ -568,10 +571,28 @@ class SpatiaLiteDBConnector(DBConnector):
c.execute('VACUUM')
self.connection.isolation_level = '' # reset to default isolation
def commentTable(self, schema, tablename, comment):
"""Comment the table"""
return ''
def getComment(self, tab, field, db):
"""Returns the comment for a field"""
return ''
def addTableColumn(self, table, field_def):
""" add a column to table """
sql = u"ALTER TABLE %s ADD %s" % (self.quoteId(table), field_def)
self._execute_and_commit(sql)
self._execute(None, sql)
sql = u"SELECT InvalidateLayerStatistics(%s)" % (self.quoteId(table))
self._execute(None, sql)
sql = u"SELECT UpdateLayerStatistics(%s)" % (self.quoteId(table))
self._execute(None, sql)
self._commit()
return True
def deleteTableColumn(self, table, column):
""" delete column from a table """

View File

@ -175,8 +175,8 @@ class SLDatabase(Database):
def spatialIndexClause(self, src_table, src_column, dest_table, dest_column):
return u""" "%s".ROWID IN (\nSELECT ROWID FROM SpatialIndex WHERE f_table_name='%s' AND search_frame="%s"."%s") """ % (src_table, src_table, dest_table, dest_column)
def searchClass(self):
return "SLDatabase"
def supportsComment(self):
return False
class SLTable(Table):

View File

@ -342,6 +342,14 @@ class VLayerConnector(DBConnector):
print("**unimplemented** runVacuum")
return False
def commentTable(self, schema, tablename, comment):
"""Comment the table"""
return ''
def getComment(self, tab, field, db):
"""Returns the comment for a field"""
return ''
def addTableColumn(self, table, field_def):
print("**unimplemented** addTableColumn")
return False

View File

@ -132,8 +132,8 @@ class FakeDatabase(Database):
def spatialIndexClause(self, src_table, src_column, dest_table, dest_column):
return '"%s"._search_frame_ = "%s"."%s"' % (src_table, dest_table, dest_column)
def searchClass(self):
return "FakeDatabase"
def supportsComment(self):
return False
class LTable(Table):

View File

@ -43,21 +43,37 @@ class DlgFieldProperties(QDialog, Ui_Dialog):
self.cboType.addItem(item)
objClass = self.db.searchClass()
if objClass != "PGDatabase":
supportCom = self.db.supportsComment()
if supportCom != True:
self.label_6.setVisible(False)
self.editCom.setVisible(False)
name, dataType, modifier, chkNull, hasDefault, chkCom = self.db.connector.setField(self.fld, self.table.name, self.db)
self.editName.setText(name)
self.cboType.setEditText(dataType)
self.editLength.setText(modifier)
self.chkNull.setChecked(not chkNull)
self.editDefault.setText(hasDefault)
self.editCom.setText(chkCom)
self.setField(fld)
self.buttonBox.accepted.connect(self.onOK)
def setField(self, fld):
print('ok')
if fld is None:
return
self.editName.setText(fld.name)
self.cboType.setEditText(fld.dataType)
if fld.modifier:
self.editLength.setText(str(fld.modifier))
self.chkNull.setChecked(not fld.notNull)
if fld.hasDefault:
self.editDefault.setText(fld.default)
print(self.table)
tab = self.table.name
print(tab)
field = fld.name
print(field)
res = self.db.connector.getComment(tab, field, self.db)
print(res)
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
fld.name = self.editName.text()

View File

@ -25,8 +25,7 @@ from builtins import str
from builtins import range
from qgis.PyQt.QtCore import Qt, QFileInfo
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QMessageBox, QApplication
from qgis.PyQt.QtGui import QCursor
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QMessageBox
from qgis.core import (QgsDataSourceUri,
QgsVectorLayer,
@ -52,8 +51,8 @@ class DlgImportVector(QDialog, Ui_Dialog):
self.outUri = outUri
self.setupUi(self)
objClass = self.db.searchClass()
if objClass != "PGDatabase":
supportCom = self.db.supportsComment()
if supportCom != True:
self.chkCom.setVisible(False)
self.editCom.setVisible(False)
@ -374,9 +373,9 @@ class DlgImportVector(QDialog, Ui_Dialog):
self.db.connector.createSpatialIndex((schema, table), geom)
# add comment on table
if self.chkCom.isEnabled() and self.chkCom.isChecked() and objClass == "PGDatabase":
if self.chkCom.isEnabled() and self.chkCom.isChecked() and supportCom == True:
# using connector executing COMMENT ON TABLE query (with editCome.text() value)
self.db.connector._execute(None, 'COMMENT ON TABLE "{0}"."{1}" IS E\'{2}\''.format(schema, table, self.editCom.text()))
self.db.connector.commentTable(db, schema, table, self.editCom.text())
self.db.connection().reconnect()
self.db.refresh()

View File

@ -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
@ -50,8 +50,8 @@ class DlgTableProperties(QDialog, Ui_Dialog):
self.db = self.table.database()
objClass = self.db.searchClass()
if objClass != "PGDatabase":
supportCom = self.db.supportsComment()
if supportCom != True:
self.tabs.removeTab(3)
m = TableFieldsModel(self)
@ -105,7 +105,6 @@ class DlgTableProperties(QDialog, Ui_Dialog):
def populateFields(self):
""" load field information from database """
m = self.viewFields.model()
m.clear()
@ -337,7 +336,7 @@ 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()))
@ -349,7 +348,7 @@ class DlgTableProperties(QDialog, Ui_Dialog):
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))