Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New fix, still some need to be done
  • Loading branch information
Ailurupoda committed Feb 6, 2019
1 parent dff1853 commit f3dfd97
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 66 deletions.
7 changes: 1 addition & 6 deletions python/plugins/db_manager/db_plugins/connector.py
Expand Up @@ -232,9 +232,4 @@ def getSqlDictionary(self):
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 {}
8 changes: 8 additions & 0 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -620,6 +620,14 @@ def runVacuum(self):
""" 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 """

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/db_manager/db_plugins/gpkg/plugin.py
Expand Up @@ -177,8 +177,8 @@ def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=
vl.setSubsetString(sql)
return vl

def searchClass(self):
return "GPKGDatabase"
def supportsComment(self):
return False


class GPKGTable(Table):
Expand Down
20 changes: 14 additions & 6 deletions python/plugins/db_manager/db_plugins/oracle/connector.py
Expand Up @@ -475,16 +475,16 @@ def updateCache(self, tableList, schema=None):

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:
Expand Down Expand Up @@ -1306,6 +1306,14 @@ def renameSchema(self, schema, new_schema):
"""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."""
Expand Down
12 changes: 7 additions & 5 deletions python/plugins/db_manager/db_plugins/oracle/plugin.py
Expand Up @@ -91,7 +91,8 @@ def connect(self, parent=None):
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

Expand Down Expand Up @@ -202,7 +203,8 @@ def toSqlLayer(self, sql, geomCol, uniqueCol,
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()
Expand Down Expand Up @@ -261,8 +263,8 @@ def registerDatabaseActions(self, mainWindow):
mainWindow.registerAction(action, QApplication.translate(
"DBManagerPlugin", "&Table"), self.emptyTableActionSlot)

def searchClass(self):
return "ORDatabase"
def supportsComment(self):
return False


class ORSchema(Schema):
Expand Down Expand Up @@ -525,7 +527,7 @@ def __init__(self, row, table):
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)
Expand Down
1 change: 1 addition & 0 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -1096,6 +1096,7 @@ def definition(self):
return txt

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

def delete(self):
Expand Down
41 changes: 22 additions & 19 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -193,6 +193,7 @@ def getInfo(self):
self._close_cursor(c)
return res


def getSpatialInfo(self):
""" returns tuple about PostGIS support:
- lib version
Expand Down Expand Up @@ -511,25 +512,6 @@ def getTableFields(self, table):
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)
Expand Down Expand Up @@ -761,6 +743,27 @@ def renameTable(self, table, new_table):

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:
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/postgis/plugin.py
Expand Up @@ -181,8 +181,8 @@ def runRefreshMaterializedViewSlot(self, item, action, parent):
def hasLowercaseFieldNamesOption(self):
return True

def searchClass(self):
return "PGDatabase"
def supportsComment(self):
return True


class PGSchema(Schema):
Expand Down Expand Up @@ -399,7 +399,7 @@ def __init__(self, row, table):
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()
Expand All @@ -419,6 +419,7 @@ def getComment(self):
return ''



class PGTableConstraint(TableConstraint):

def __init__(self, row, table):
Expand Down
21 changes: 21 additions & 0 deletions python/plugins/db_manager/db_plugins/spatialite/connector.py
Expand Up @@ -465,6 +465,8 @@ def deleteTable(self, table):
self._execute(c, sql)
self._commit()

return True

def emptyTable(self, table):
""" delete all rows from table """
if self.isRasterTable(table):
Expand Down Expand Up @@ -494,6 +496,7 @@ def renameTable(self, table, new_table):
self._execute(c, sql)

self._commit()
return True

def moveTable(self, table, new_table, new_schema=None):
return self.renameTable(table, new_table)
Expand Down Expand Up @@ -568,10 +571,28 @@ def runVacuum(self):
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 """
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/db_manager/db_plugins/spatialite/plugin.py
Expand Up @@ -175,8 +175,8 @@ def explicitSpatialIndex(self):
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):
Expand Down
8 changes: 8 additions & 0 deletions python/plugins/db_manager/db_plugins/vlayers/connector.py
Expand Up @@ -342,6 +342,14 @@ def runVacuum(self):
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
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/db_manager/db_plugins/vlayers/plugin.py
Expand Up @@ -132,8 +132,8 @@ def explicitSpatialIndex(self):
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):
Expand Down
34 changes: 25 additions & 9 deletions python/plugins/db_manager/dlg_field_properties.py
Expand Up @@ -43,21 +43,37 @@ def __init__(self, parent=None, fld=None, table=None, db=None):
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()
Expand Down
11 changes: 5 additions & 6 deletions python/plugins/db_manager/dlg_import_vector.py
Expand Up @@ -25,8 +25,7 @@
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,
Expand All @@ -52,8 +51,8 @@ def __init__(self, inLayer, outDb, outUri, parent=None):
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)

Expand Down Expand Up @@ -374,9 +373,9 @@ def accept(self):
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()
Expand Down

0 comments on commit f3dfd97

Please sign in to comment.