Code refactoring (use format() command for generate qwery string, rename some variables)

This commit is contained in:
svk28 2017-04-11 12:37:36 +03:00
parent 339e353b2b
commit a4528dccef
2 changed files with 80 additions and 71 deletions

97
dm.py
View File

@ -88,11 +88,11 @@ def dbConnect():
return c return c
def createTables(tbl_list): def createTables(tbl_list):
global tbl_descr_list, tbl_struct_list, c, db_type global dbTablesDescriptionList, dbTablesStructList, c, db_type
i = 0 i = 0
tbl_names_list = [] dbTablesNamesList = []
tbl_descr_list = [] dbTablesDescriptionList = []
tbl_struct_list = [] dbTablesStructList = []
while i < len(tbl_list): while i < len(tbl_list):
one_Table_descr = [] one_Table_descr = []
@ -101,7 +101,7 @@ def createTables(tbl_list):
tbl_descr = tbl_list[i]["tableDescription"] tbl_descr = tbl_list[i]["tableDescription"]
tbl_name = tbl_list[i]["tableName"] tbl_name = tbl_list[i]["tableName"]
#field_list = tbl_list[i]["fieldList"][i].keys() #field_list = tbl_list[i]["fieldList"][i].keys()
tbl_names_list.append([tbl_name, tbl_descr]) dbTablesNamesList.append([tbl_name, tbl_descr])
one_Table_descr.append(tbl_name) one_Table_descr.append(tbl_name)
one_Table_struct.append(tbl_name) one_Table_struct.append(tbl_name)
@ -165,14 +165,14 @@ def createTables(tbl_list):
one_Table_struct.append(one_Table_relation) one_Table_struct.append(one_Table_relation)
one_Table_descr.append(field_names_list) one_Table_descr.append(field_names_list)
i = i + 1 i = i + 1
tbl_descr_list.append(one_Table_descr) dbTablesDescriptionList.append(one_Table_descr)
tbl_struct_list.append(one_Table_struct) dbTablesStructList.append(one_Table_struct)
#print(qwery_create) #print(qwery_create)
c.execute(qwery_create) c.execute(qwery_create)
return tbl_names_list return dbTablesNamesList
def initDBstructure(): def initDBstructure():
global tbl_descr_list, template_file, tblNamesList global dbTablesDescriptionList, template_file, tblNamesList
table_list = open(template_file, "r", encoding="utf-8") table_list = open(template_file, "r", encoding="utf-8")
data = json.load(table_list, encoding="utf-8") data = json.load(table_list, encoding="utf-8")
tbl_list = data["tables"] tbl_list = data["tables"]
@ -182,11 +182,11 @@ def initDBstructure():
# выборка данных из заданной таблицы # выборка данных из заданной таблицы
def selectData(tbl): def selectData(tbl):
global tbl_struct_list, c, db_type global dbTablesStructList, c, db_type
# если юольше 1 поля добавить CONCAT # если юольше 1 поля добавить CONCAT
qwery = "SELECT " qwery = "SELECT "
subqwery = "" subqwery = ""
for item in tbl_struct_list: for item in dbTablesStructList:
if item[0] == tbl: if item[0] == tbl:
for field in item[1]: for field in item[1]:
field = field[0] field = field[0]
@ -195,8 +195,6 @@ def selectData(tbl):
field = rel[0] field = rel[0]
field_rel = rel[1][0] field_rel = rel[1][0]
field_replace = rel[1][1] field_replace = rel[1][1]
#field_replace = field_replace.replace(",", ",' ',")
#field_replace = field_replace.replace(",", " || ")
# определяем название таблицы для вложенного запроса # определяем название таблицы для вложенного запроса
table1 = field_rel.split('.')[0] table1 = field_rel.split('.')[0]
field1 = field_rel.split('.')[1] field1 = field_rel.split('.')[1]
@ -208,38 +206,43 @@ def selectData(tbl):
# составляем подзапрос и подменяем им поле в запросе # составляем подзапрос и подменяем им поле в запросе
if db_type == "mysql": if db_type == "mysql":
field_replace = field_replace.replace(",", ",' ',") field_replace = field_replace.replace(",", ",' ',")
subqwery = "(SELECT CONCAT(" + field_replace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tbl + "." + field + ") AS " + field #subqwery = "(SELECT CONCAT(" + field_replace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tbl + "." + field + ") AS " + field
subqwery = "(SELECT CONCAT({}) FROM {} WHERE {}.{}={}.{}) AS {}"\
.format(field_replace,subqwery,table1,field1,tbl,field,field)
elif db_type == "sqlite": elif db_type == "sqlite":
field_replace = field_replace.replace(",", " || ' ' ||") field_replace = field_replace.replace(",", " || ' ' ||")
subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field #subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
subqwery = "(SELECT ({}) FROM {} WHERE {}.{}={}.{}) AS {}" \
.format(field_replace, subqwery, table1, field1, tbl, field, field)
qwery = qwery.replace(field, subqwery) qwery = qwery.replace(field, subqwery)
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000" #qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
qwery = '{} FROM {} LIMIT 10000'.format(qwery.rstrip(','), tbl)
#print(qwery) #print(qwery)
c.execute(qwery) c.execute(qwery)
return c.fetchall() return c.fetchall()
# получаем на вход имя таблицы и возвращаем список заголовков полей # получаем на вход имя таблицы и возвращаем список заголовков полей
def getTableStructure(tbl): def getTableStructure(tbl):
global tbl_descr_list, tbl_struct_list global dbTablesDescriptionList, dbTablesStructList
#print(tbl_descr_list) #print(dbTablesDescriptionList)
#print(tbl_struct_list) #print(dbTablesStructList)
for item in tbl_descr_list: for item in dbTablesDescriptionList:
if item[0] == tbl: if item[0] == tbl:
return item[1] return item[1]
# Получаем список названий полей и типов для заданной таблицы # Получаем список названий полей и типов для заданной таблицы
def getFields(tbl): def getFields(tbl):
global tbl_descr_list, tbl_struct_list global dbTablesDescriptionList, dbTablesStructList
#print(tbl_descr_list) #print(dbTablesDescriptionList)
#print(tbl_struct_list) #print(dbTablesStructList)
for item in tbl_struct_list: for item in dbTablesStructList:
if item[0] == tbl: if item[0] == tbl:
#print(item[1]) #print(item[1])
return item[1] return item[1]
# Ищем описание поля по его названию и возвращаем. # Ищем описание поля по его названию и возвращаем.
def getFieldDescription(tbl, field): def getFieldDescription(tbl, field):
global tbl_descr_list, tbl_struct_list global dbTablesDescriptionList, dbTablesStructList
for item in tbl_descr_list: for item in dbTablesDescriptionList:
if item[0] == tbl: if item[0] == tbl:
for i in item[1]: for i in item[1]:
if i[0] == field: if i[0] == field:
@ -263,10 +266,10 @@ def fieldTypeConvert(ftype):
return fType return fType
def getFieldType(tbl, field): def getFieldType(tbl, field):
global tbl_descr_list, tbl_struct_list global dbTablesDescriptionList, dbTablesStructList
#print(tbl_descr_list) #print(dbTablesDescriptionList)
#print(tbl_struct_list) #print(dbTablesStructList)
for item in tbl_struct_list: for item in dbTablesStructList:
if item[0] == tbl: if item[0] == tbl:
for i in item[1]: for i in item[1]:
if i[0] == field: if i[0] == field:
@ -276,12 +279,12 @@ def getFieldType(tbl, field):
#return item[1] #return item[1]
def getRelationsForField(tblSearch, fieldName): def getRelationsForField(tblSearch, fieldName):
global tbl_struct_list, c, db_type global dbTablesStructList, c, db_type
#print("Ищем связи для:" +tblSearch +','+ fieldName) #print("Ищем связи для:" +tblSearch +','+ fieldName)
#print(tbl_struct_list) #print(dbTablesStructList)
#searchField = tblSearch + '.' + fieldName #searchField = tblSearch + '.' + fieldName
dataList = [] dataList = []
for item in tbl_struct_list: for item in dbTablesStructList:
#print(item) #print(item)
tblName = item[0] tblName = item[0]
for field in item[1]: for field in item[1]:
@ -348,11 +351,11 @@ def insertDataIntoBD(dataList):
# Выборка данных по значению # Выборка данных по значению
# принимает на вход название таблицы, назване поля, значение этого поля # принимает на вход название таблицы, назване поля, значение этого поля
def selectDataFromDB(tblName, fieldName, fieldValue): def selectDataFromDB(tblName, fieldName, fieldValue):
global tbl_struct_list, c, db_type global dbTablesStructList, c, db_type
qwery = "SELECT " qwery = "SELECT "
subqwery = "" subqwery = ""
for item in tbl_struct_list: for item in dbTablesStructList:
if item[0] == tblName: if item[0] == tblName:
for field in item[1]: for field in item[1]:
field = field[0] field = field[0]
@ -372,23 +375,29 @@ def selectDataFromDB(tblName, fieldName, fieldValue):
# составляем подзапрос и подменяем им поле в запросе # составляем подзапрос и подменяем им поле в запросе
if db_type == "mysql": if db_type == "mysql":
fieldReplace = fieldReplace.replace(",", ",' ',") fieldReplace = fieldReplace.replace(",", ",' ',")
subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field #subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field
subqwery = '(SELECT CONCAT({}) FROM {} WHERE {}.{}={}.{}) AS {}'\
.format(fieldReplace, subqwery, table1, field1, tblName, field, field)
elif db_type == "sqlite": elif db_type == "sqlite":
fieldReplace = fieldReplace.replace(",", " || ' ' ||") fieldReplace = fieldReplace.replace(",", " || ' ' ||")
subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field #subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
subqwery = '(SELECT ({}) FROM {} WHERE {}.{}={}.{}) AS {}'\
.format(fieldReplace, subqwery, table1, field1, tblName, field, field)
#print("---" + subqwery) #print("---" + subqwery)
qwery = qwery.replace(field, subqwery) qwery = qwery.replace(field, subqwery)
#qwery = qwery.rstrip(',') + " FROM " + tblName + " LIMIT 10000" #qwery = qwery.rstrip(',') + " FROM " + tblName + " LIMIT 10000"
qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue #qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue
qwery = '{} FROM {} WHERE {}={}'.format(qwery.rstrip(','), tblName, fieldName, fieldValue)
#print(qwery) #print(qwery)
c.execute(qwery) c.execute(qwery)
return c.fetchall() return c.fetchall()
def selectRelationsDataFromDB(tblSearch, fieldName, fieldValue): def selectRelationsDataFromDB(tblSearch, fieldName, fieldValue):
global tbl_struct_list, c, db_type global dbTablesStructList, c, db_type
searchField = tblSearch + '.' + fieldName searchField = tblSearch + '.' + fieldName
dataList = [] dataList = []
for item in tbl_struct_list: for item in dbTablesStructList:
tblName = item[0] tblName = item[0]
for field in item[1]: for field in item[1]:
field = field[0] field = field[0]
@ -415,7 +424,6 @@ def getTablesDescriptionOfName(tblName):
# Удаление записи из БД # Удаление записи из БД
def deleteRecordsFromDB(tableName, valueList): def deleteRecordsFromDB(tableName, valueList):
global c, db_type global c, db_type
qwery = 'DELETE FROM {} WHERE '.format(tableName) qwery = 'DELETE FROM {} WHERE '.format(tableName)
print(qwery) print(qwery)
subQwery='' subQwery=''
@ -423,10 +431,11 @@ def deleteRecordsFromDB(tableName, valueList):
for item in getFields(tableName): for item in getFields(tableName):
print(item[0], item[1]) print(item[0], item[1])
if item[1] == 'integer': if item[1] == 'integer':
subQwery = subQwery + item[0] + '=' + valueList[i] + ' AND ' #subQwery = subQwery + item[0] + '=' + valueList[i] + ' AND '
subQwery = '{}{}={} AND'.format(subQwery, item[0], valueList[i])
else: else:
subQwery = subQwery + item[0] + '=\'' + valueList[i] + '\' AND ' #subQwery = subQwery + item[0] + '=\'' + valueList[i] + '\' AND '
#subQwery = '{}'.format(subQwery,item[0]) subQwery = "{}{}='{}' AND ".format(subQwery, item[0], valueList[i])
i += 1 i += 1
qwery += subQwery qwery += subQwery
print(qwery) print(qwery)

54
gui.py
View File

@ -303,8 +303,8 @@ class EditForm(QMainWindow):
#relForm.setParent(self) #relForm.setParent(self)
# обработка нажатия на списке таблиц БД # обработка нажатия на списке таблиц БД
def clickTablesList(tbl_name, tblDataWidget, data='NULL'): def clickTablesList(dbTableName, tblDataWidget, data='NULL'):
global tblList, tabRelationsData global dbTablesList, tabRelationsData
# удаляем все открытые табы # удаляем все открытые табы
i=0 i=0
if tabRelationsData.count(): if tabRelationsData.count():
@ -316,18 +316,18 @@ def clickTablesList(tbl_name, tblDataWidget, data='NULL'):
tabRelationsData.removeTab(i) tabRelationsData.removeTab(i)
i += 1 i += 1
addDataIntoTable(dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex())), tblDataWidget) addDataIntoTable(dm.getTablesNameOfDescription(listDBTables.model().data(listDBTables.currentIndex())), tblDataWidget)
# Пишем название активной таблицы БД в строку статуса, эадакая замена глобальной переменной # Пишем название активной таблицы БД в строку статуса, эадакая замена глобальной переменной
# как оно и где вылезет будем посмотреть. # как оно и где вылезет будем посмотреть.
tblDataWidget.setStatusTip(dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex()))) tblDataWidget.setStatusTip(dm.getTablesNameOfDescription(listDBTables.model().data(listDBTables.currentIndex())))
tblDataWidget.dbTableName = dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex())) tblDataWidget.dbTableName = dm.getTablesNameOfDescription(listDBTables.model().data(listDBTables.currentIndex()))
# вставка данных в табличный виджет # вставка данных в табличный виджет
def addDataIntoTable(tbl_name, tblDataWidget, data='NULL'): def addDataIntoTable(dbTableName, tblDataWidget, data='NULL'):
global tblList, tabRelationsData global dbTablesList, tabRelationsData
if data == 'NULL': if data == 'NULL':
data = dm.selectData(tbl_name) data = dm.selectData(dbTableName)
fieldNames = dm.getTableStructure(tbl_name) fieldNames = dm.getTableStructure(dbTableName)
#print(fieldNames) #print(fieldNames)
# проверка на наличие записей в таблице # проверка на наличие записей в таблице
if data: if data:
@ -345,7 +345,7 @@ def addDataIntoTable(tbl_name, tblDataWidget, data='NULL'):
fName = item[0] fName = item[0]
fDescr = item[1] fDescr = item[1]
#### определяем наличие связей и добавляем дополнительные столбцы #### определяем наличие связей и добавляем дополнительные столбцы
####print(fName, fDescr, dm.getRelationsForField(tbl_name, fName)) ####print(fName, fDescr, dm.getRelationsForField(dbTableName, fName))
# установка заголовков столбцов таблицы # установка заголовков столбцов таблицы
tblDataWidget.setHorizontalHeaderItem(n, QTableWidgetItem(fDescr)) tblDataWidget.setHorizontalHeaderItem(n, QTableWidgetItem(fDescr))
n += 1 n += 1
@ -432,30 +432,30 @@ def showMenuToolbar(window):
# форма для добавления новой записи # форма для добавления новой записи
def addNewRecord(): def addNewRecord():
global listTables, tblRelationsData global listDBTables, tblRelationsData
tblDescr = listTables.model().data(listTables.currentIndex()) tblDescr = listDBTables.model().data(listDBTables.currentIndex())
for i in tblList: for i in dbTablesList:
if i[1] == tblDescr: if i[1] == tblDescr:
tblName = i[0] tblName = i[0]
#print(tblName) #print(tblName)
editForm = EditForm(tblName, tblDescr) editForm = EditForm(tblName, tblDescr)
editForm.show() editForm.show()
#editForm.setParent(listTables) #editForm.setParent(listDBTables)
#editForm.show() #editForm.show()
#editForm.setParent(tblRelationsData) #editForm.setParent(tblRelationsData)
#editForm = Example() #editForm = Example()
# отображение списка связанных данных # отображение списка связанных данных
def showRelationsRecords(fieldIndex, fieldValue): def showRelationsRecords(fieldIndex, fieldValue):
global tabRelationsData, listTables global tabRelationsData, listDBTables
tblDescr = listTables.model().data(listTables.currentIndex()) tblDescr = listDBTables.model().data(listDBTables.currentIndex())
i=0 i=0
if tabRelationsData.count(): if tabRelationsData.count():
while i <= tabRelationsData.count(): while i <= tabRelationsData.count():
tabRelationsData.removeTab(i) tabRelationsData.removeTab(i)
i = i+1 i = i+1
for i in tblList: for i in dbTablesList:
if i[1] == tblDescr: if i[1] == tblDescr:
tblName = i[0] tblName = i[0]
@ -481,15 +481,15 @@ def showRelationsRecords(fieldIndex, fieldValue):
tblRelationsData.setStatusTip(tbl) tblRelationsData.setStatusTip(tbl)
def main(): def main():
global tblList, listTables, tabRelationsData global dbTablesList, listDBTables, tabRelationsData
app = QApplication(sys.argv) app = QApplication(sys.argv)
mainWin = QMainWindow() mainWin = QMainWindow()
workArea = QWidget() workArea = QWidget()
mainWin.setCentralWidget(workArea) mainWin.setCentralWidget(workArea)
hbox = QHBoxLayout() hbox = QHBoxLayout()
listTables = QListWidget() listDBTables = QListWidget()
listTables.setFrameShape(QFrame.StyledPanel) listDBTables.setFrameShape(QFrame.StyledPanel)
listTables.setFixedWidth(200) listDBTables.setFixedWidth(200)
# выводим список таблиц в левом поле # выводим список таблиц в левом поле
tblData = MyTable() tblData = MyTable()
tblData.setFrameShape(QFrame.StyledPanel) tblData.setFrameShape(QFrame.StyledPanel)
@ -498,7 +498,7 @@ def main():
#tabRelationsData.setFrameShape(QFrame.StyledPanel) #tabRelationsData.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Horizontal) splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(listTables) splitter1.addWidget(listDBTables)
splitter2 = QSplitter(Qt.Vertical) splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(tblData) splitter2.addWidget(tblData)
splitter2.addWidget(tabRelationsData) splitter2.addWidget(tabRelationsData)
@ -506,13 +506,13 @@ def main():
hbox.addWidget(splitter1) hbox.addWidget(splitter1)
workArea.setLayout(hbox) workArea.setLayout(hbox)
showMenuToolbar(mainWin) showMenuToolbar(mainWin)
listTables.itemClicked.connect(lambda: clickTablesList(dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex())), tblData)) listDBTables.itemClicked.connect(lambda: clickTablesList(dm.getTablesNameOfDescription(listDBTables.model().data(listDBTables.currentIndex())), tblData))
tblList = dm.initDBstructure() dbTablesList = dm.initDBstructure()
for i in tblList: for i in dbTablesList:
listTables.addItem(i[1]) listDBTables.addItem(i[1])
#tblData = QTableWidget() #tblData = QTableWidget()
print(dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex()))) print(dm.getTablesNameOfDescription(listDBTables.model().data(listDBTables.currentIndex())))
mainWin.setGeometry(300, 300, 800, 600) mainWin.setGeometry(300, 300, 800, 600)