Skip to content

Commit

Permalink
Merge pull request #41153 from dericke/db_manager_list_comprehension
Browse files Browse the repository at this point in the history
Refactoring DB Manager Plugin to use list comprehensions and generators
  • Loading branch information
m-kuhn committed Jan 25, 2021
2 parents 7ba68b3 + d79100a commit f180ef9
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 93 deletions.
22 changes: 10 additions & 12 deletions python/plugins/db_manager/db_plugins/connector.py
Expand Up @@ -188,12 +188,11 @@ def _get_cursor_columns(self, c):
@classmethod
def quoteId(self, identifier):
if hasattr(identifier, '__iter__') and not isinstance(identifier, str):
ids = list()
for i in identifier:
if i is None or i == "":
continue
ids.append(self.quoteId(i))
return u'.'.join(ids)
return u'.'.join(
self.quoteId(i)
for i in identifier
if i is not None and i != ""
)

identifier = str(
identifier) if identifier is not None else str() # make sure it's python unicode string
Expand All @@ -203,12 +202,11 @@ def quoteId(self, identifier):
def quoteString(self, txt):
""" make the string safe - replace ' with '' """
if hasattr(txt, '__iter__') and not isinstance(txt, str):
txts = list()
for i in txt:
if i is None:
continue
txts.append(self.quoteString(i))
return u'.'.join(txts)
return u'.'.join(
self.quoteString(i)
for i in txt
if i is not None
)

txt = str(txt) if txt is not None else str() # make sure it's python unicode string
return u"'%s'" % txt.replace("'", "''")
Expand Down
32 changes: 16 additions & 16 deletions python/plugins/db_manager/db_plugins/data_model.py
Expand Up @@ -50,10 +50,10 @@ def headerToString(self, sep=u"\t"):
return sep.join(header)

def rowToString(self, row, sep=u"\t"):
text = u""
for col in range(self.columnCount()):
text += u"%s" % self.getData(row, col) + sep
return text[:-1]
return sep.join(
str(self.getData(row, col))
for col in range(self.columnCount())
)

def getData(self, row, col):
return self.resdata[row][col]
Expand Down Expand Up @@ -305,10 +305,10 @@ def getObject(self, row):
return fld

def getFields(self):
flds = []
for fld in self.getObjectIter():
flds.append(fld)
return flds
return [
fld
for fld in self.getObjectIter()
]


class TableConstraintsModel(SimpleTableModel):
Expand Down Expand Up @@ -342,10 +342,10 @@ def getObject(self, row):
return constr

def getConstraints(self):
constrs = []
for constr in self.getObjectIter():
constrs.append(constr)
return constrs
return [
constr
for constr in self.getObjectIter()
]


class TableIndexesModel(SimpleTableModel):
Expand Down Expand Up @@ -376,7 +376,7 @@ def getObject(self, row):
return idx

def getIndexes(self):
idxs = []
for idx in self.getObjectIter():
idxs.append(idx)
return idxs
return [
idx
for idx in self.getObjectIter()
]
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -401,9 +401,10 @@ def getTableIndexes(self, table):
sql = u"PRAGMA index_info(%s)" % (self.quoteId(name))

idx = [num, name, unique]
cols = []
for seq, cid, cname in self._fetchAll(sql):
cols.append(cid)
cols = [
cid
for seq, cid, cname in self._fetchAll(sql)
]
idx.append(cols)
indexes[i] = idx

Expand Down
9 changes: 4 additions & 5 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -571,11 +571,10 @@ def rasterTablesFactory(self, row, db, schema=None):
def tables(self, schema=None, sys_tables=False):
tables = self.connector.getTables(schema.name if schema else None, sys_tables)
if tables is not None:
ret = []
for t in tables:
table = self.tablesFactory(t, self, schema)
ret.append(table)

ret = [
self.tablesFactory(t, self, schema)
for t in tables
]
return ret

def createTable(self, table, fields, schema=None):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -1240,13 +1240,14 @@ def getSqlDictionary(self):
sql_dict = getSqlDictionary()

# get schemas, tables and field names
items = []
sql = u"""SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_' AND nspname != 'information_schema'
UNION SELECT relname FROM pg_class WHERE relkind IN ('v', 'r', 'm', 'p')
UNION SELECT attname FROM pg_attribute WHERE attnum > 0"""
c = self._execute(None, sql)
for row in self._fetchall(c):
items.append(row[0])
items = [
row[0]
for row in self._fetchall(c)
]
self._close_cursor(c)

sql_dict["identifier"] = items
Expand Down
Expand Up @@ -191,7 +191,7 @@ def sql_currentView(self):
def sql_functions(self):
cols = ",".join(self.columns)
all_cols = self.colPkey + "," + ",".join(self.columns)
old_cols = ",".join([u"OLD." + x for x in self.columns])
old_cols = ",".join(u"OLD." + x for x in self.columns)

sql = u"""
CREATE OR REPLACE FUNCTION %(func_at_time)s(timestamp)
Expand Down Expand Up @@ -253,8 +253,8 @@ def sql_triggers(self):
def sql_updatesView(self):
cols = ",".join(self.columns)
return_cols = self.colPkey + "," + ",".join(self.columns)
new_cols = ",".join([u"NEW." + x for x in self.columns])
assign_cols = ",".join([u"%s = NEW.%s" % (x, x) for x in self.columns])
new_cols = ",".join(u"NEW." + x for x in self.columns)
assign_cols = ",".join(u"%s = NEW.%s" % (x, x) for x in self.columns)

return u"""
CREATE OR REPLACE RULE "_DELETE" AS ON DELETE TO %(view)s DO INSTEAD
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/spatialite/connector.py
Expand Up @@ -343,9 +343,10 @@ def getTableIndexes(self, table):
self._execute(c, sql)

idx = [num, name, unique]
cols = []
for seq, cid, cname in c.fetchall():
cols.append(cid)
cols = [
cid
for seq, cid, cname in c.fetchall()
]
idx.append(cols)
indexes[i] = idx

Expand Down
20 changes: 11 additions & 9 deletions python/plugins/db_manager/dlg_query_builder.py
Expand Up @@ -349,15 +349,17 @@ def validate(self):
return
self.query = query

saveParameter = {}
saveParameter["coltables"] = self.coltables
saveParameter["col_col"] = self.col_col
saveParameter["col_where"] = self.col_where
saveParameter["col"] = query_col
saveParameter["tab"] = query_table
saveParameter["where"] = query_where
saveParameter["group"] = query_group
saveParameter["order"] = query_order
saveParameter = {
"coltables": self.coltables,
"col_col": self.col_col,
"col_where": self.col_where,
"col": query_col,
"tab": query_table,
"where": query_where,
"group": query_group,
"order": query_order,
}

QueryBuilderDlg.saveParameter = saveParameter

def restoreLastQuery(self):
Expand Down
35 changes: 20 additions & 15 deletions python/plugins/db_manager/dlg_sql_layer_window.py
Expand Up @@ -300,17 +300,20 @@ def executeSql(self):

def _getSqlLayer(self, _filter):
hasUniqueField = self.uniqueColumnCheck.checkState() == Qt.Checked
if hasUniqueField:
if self.allowMultiColumnPk:
checkedCols = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedCols.append(item.data())
uniqueFieldName = ",".join(checkedCols)
elif self.uniqueCombo.currentIndex() >= 0:
uniqueFieldName = self.uniqueModel.item(self.uniqueCombo.currentIndex()).data()
else:
uniqueFieldName = None
if hasUniqueField and self.allowMultiColumnPk:
checkedCols = [
item.data()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
]

uniqueFieldName = ",".join(checkedCols)
elif (
hasUniqueField
and not self.allowMultiColumnPk
and self.uniqueCombo.currentIndex() >= 0
):
uniqueFieldName = self.uniqueModel.item(self.uniqueCombo.currentIndex()).data()
else:
uniqueFieldName = None
hasGeomCol = self.hasGeometryCol.checkState() == Qt.Checked
Expand Down Expand Up @@ -538,10 +541,12 @@ def uniqueChanged(self):

def uniqueTextChanged(self, text):
# Whenever there is new text displayed in the combobox, check if it is the correct one and if not, display the correct one.
checkedItems = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedItems.append(item.text())
checkedItems = [
item.text()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
]

label = ", ".join(checkedItems)
if text != label:
self.uniqueCombo.setEditText(label)
Expand Down
50 changes: 26 additions & 24 deletions python/plugins/db_manager/dlg_sql_window.py
Expand Up @@ -80,13 +80,14 @@ def check_comments_in_sql(raw_sql_input):
for line in raw_sql_input.splitlines():
if not line.strip().startswith('--'):
if '--' in line:
comment_positions = []
comments = re.finditer(r'--', line)
for match in comments:
comment_positions.append(match.start())
quote_positions = []
comment_positions = [
match.start()
for match in comments
]
identifiers = re.finditer(r'"(?:[^"]|"")*"', line)
quotes = re.finditer(r"'(?:[^']|'')*'", line)
quote_positions = []
for match in identifiers:
quote_positions.append((match.start(), match.end()))
for match in quotes:
Expand Down Expand Up @@ -390,14 +391,14 @@ def executeSqlCompleted(self):
with OverrideCursor(Qt.WaitCursor):
if self.modelAsync.task.status() == QgsTask.Complete:
model = self.modelAsync.model
quotedCols = []

self.showError(None)
self.viewResult.setModel(model)
self.lblResult.setText(self.tr("{0} rows, {1:.3f} seconds").format(model.affectedRows(), model.secs()))
cols = self.viewResult.model().columnNames()
for col in cols:
quotedCols.append(self.db.connector.quoteId(col))
quotedCols = [
self.db.connector.quoteId(col)
for col in cols
]

self.setColumnCombos(cols, quotedCols)

Expand Down Expand Up @@ -446,17 +447,18 @@ def showError(self, error):

def _getSqlLayer(self, _filter):
hasUniqueField = self.uniqueColumnCheck.checkState() == Qt.Checked
if hasUniqueField:
if self.allowMultiColumnPk:
checkedCols = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedCols.append(item.data())
uniqueFieldName = ",".join(checkedCols)
elif self.uniqueCombo.currentIndex() >= 0:
uniqueFieldName = self.uniqueModel.item(self.uniqueCombo.currentIndex()).data()
else:
uniqueFieldName = None
if hasUniqueField and self.allowMultiColumnPk:
uniqueFieldName = ",".join(
item.data()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
)
elif (
hasUniqueField
and not self.allowMultiColumnPk
and self.uniqueCombo.currentIndex() >= 0
):
uniqueFieldName = self.uniqueModel.item(self.uniqueCombo.currentIndex()).data()
else:
uniqueFieldName = None
hasGeomCol = self.hasGeometryCol.checkState() == Qt.Checked
Expand Down Expand Up @@ -681,11 +683,11 @@ def uniqueChanged(self):

def uniqueTextChanged(self, text):
# Whenever there is new text displayed in the combobox, check if it is the correct one and if not, display the correct one.
checkedItems = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedItems.append(item.text())
label = ", ".join(checkedItems)
label = ", ".join(
item.text()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
)
if text != label:
self.uniqueCombo.setEditText(label)

Expand Down

0 comments on commit f180ef9

Please sign in to comment.