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

54
gui.py
View File

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