Rewriting "Add records" dialog. Added relations records view.
This commit is contained in:
parent
2c34632dd1
commit
a0cf5e5d10
81
dm.py
81
dm.py
|
@ -142,13 +142,13 @@ def createTables(tbl_list):
|
|||
return tbl_names_list
|
||||
|
||||
def initDBstructure():
|
||||
global tbl_descr_list, template_file
|
||||
global tbl_descr_list, template_file, tblNamesList
|
||||
table_list = open(template_file, "r", encoding="utf-8")
|
||||
data = json.load(table_list, encoding="utf-8")
|
||||
tbl_list = data["tables"]
|
||||
tbl_names_list = createTables(tbl_list)
|
||||
tblNamesList = createTables(tbl_list)
|
||||
|
||||
return tbl_names_list
|
||||
return tblNamesList
|
||||
|
||||
# выборка данных из заданной таблицы
|
||||
def selectData(tbl):
|
||||
|
@ -278,11 +278,84 @@ def insertDataIntoBD(dataList):
|
|||
|
||||
qwery = qwery + qweryField.rstrip(',') + ')' + ' VALUES (' + qweryData.rstrip(',') + ');'
|
||||
|
||||
print(qwery)
|
||||
#print(qwery)
|
||||
c.execute(qwery)
|
||||
#c.close()
|
||||
return
|
||||
|
||||
# Выборка данных по значению
|
||||
# принимает на вход название таблицы, назване поля, значение этого поля
|
||||
def selectDataFromDB(tblName, fieldName, fieldValue):
|
||||
global tbl_struct_list, c, db_type
|
||||
qwery = "SELECT "
|
||||
subqwery = ""
|
||||
for item in tbl_struct_list:
|
||||
if item[0] == tblName:
|
||||
for field in item[1]:
|
||||
field = field[0]
|
||||
qwery = qwery + field + ","
|
||||
for rel in item[2]:
|
||||
field = rel[0]
|
||||
fieldRel = rel[1][0]
|
||||
fieldReplace = rel[1][1]
|
||||
#field_replace = field_replace.replace(",", ",' ',")
|
||||
#field_replace = field_replace.replace(",", " || ")
|
||||
# определяем название таблицы для вложенного запроса
|
||||
table1 = fieldRel.split('.')[0]
|
||||
field1 = fieldRel.split('.')[1]
|
||||
if table1 == tblName:
|
||||
table1 = table1 + "_1"
|
||||
subqwery = tblName + " AS " +table1
|
||||
else:
|
||||
subqwery = table1
|
||||
# составляем подзапрос и подменяем им поле в запросе
|
||||
if db_type == "mysql":
|
||||
fieldReplace = fieldReplace.replace(",", ",' ',")
|
||||
subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field
|
||||
elif db_type == "sqlite":
|
||||
field_replace = fieldReplace.replace(",", " || ' ' ||")
|
||||
subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
|
||||
qwery = qwery.replace(field, subqwery)
|
||||
#qwery = qwery.rstrip(',') + " FROM " + tblName + " LIMIT 10000"
|
||||
qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue
|
||||
#print(qwery)
|
||||
c.execute(qwery)
|
||||
return c.fetchall()
|
||||
|
||||
def selectRelationsDataFromDB(tblSearch, fieldName, fieldValue):
|
||||
global tbl_struct_list, c, db_type
|
||||
#print("Ищем связи для:" +tblSearch +','+ fieldName +','+fieldValue)
|
||||
#print(tbl_struct_list)
|
||||
searchField = tblSearch + '.' + fieldName
|
||||
dataList = []
|
||||
for item in tbl_struct_list:
|
||||
#print(item)
|
||||
tblName = item[0]
|
||||
for field in item[1]:
|
||||
field = field[0]
|
||||
for rel in item[2]:
|
||||
if rel[1][0] == searchField:
|
||||
#print("таблица:" + tblName + "поле:"+ rel[0] + 'Значение:' + fieldValue)
|
||||
data = selectDataFromDB(tblName, rel[0], fieldValue)
|
||||
#print(data)
|
||||
dataList.append([tblName, data])
|
||||
return dataList
|
||||
|
||||
# Ищем название таблицы по её описанию
|
||||
def getTablesNameOfDescription(tblDescr):
|
||||
global tblNamesList
|
||||
for i in tblNamesList:
|
||||
if i[1] == tblDescr:
|
||||
return(i[0])
|
||||
|
||||
# Ищем описание таблицы по её названию
|
||||
def getTablesDescriptionOfName(tblName):
|
||||
global tblNamesList
|
||||
for i in tblNamesList:
|
||||
if i[0] == tblName:
|
||||
return(i[1])
|
||||
|
||||
|
||||
|
||||
#initDBstructure()
|
||||
firstInit()
|
||||
|
|
71
gui.py
71
gui.py
|
@ -33,7 +33,7 @@ class MyTable(QTableWidget):
|
|||
Rmenu = QMenu(self)
|
||||
addRecord = Rmenu.addAction("Добавить запись")
|
||||
delRecord = Rmenu.addAction("Удалить запись")
|
||||
relations = Rmenu.addAction("Показать связанные документы")
|
||||
showRelations = Rmenu.addAction("Показать связанные документы")
|
||||
action = Rmenu.exec_(self.mapToGlobal(event.pos()))
|
||||
if action == delRecord:
|
||||
row = self.rowAt(event.pos().y())
|
||||
|
@ -44,6 +44,9 @@ class MyTable(QTableWidget):
|
|||
elif action == addRecord:
|
||||
print("Добавить запись ")
|
||||
addNewRecord()
|
||||
elif action == showRelations:
|
||||
item = self.item(self.currentIndex().row(), self.currentIndex().column())
|
||||
showRelationsRecords(self.currentIndex().column(), item.text())
|
||||
|
||||
def on_click(self):
|
||||
print(self.cursor().pos().x())
|
||||
|
@ -119,7 +122,7 @@ class EditForm(QMainWindow):
|
|||
hboxEdit.addWidget(lbl)
|
||||
hboxEdit.addWidget(edit)
|
||||
l.addLayout(hboxEdit)
|
||||
print(self.widgetsList)
|
||||
#print(self.widgetsList)
|
||||
hboxBtn.addWidget(btnOk)
|
||||
hboxBtn.addWidget(btnCancel)
|
||||
l.addLayout(hboxBtn)
|
||||
|
@ -199,17 +202,21 @@ class EditForm_(QMainWindow):
|
|||
def save(self):
|
||||
print(self.children())
|
||||
|
||||
def addDataIntoTable(tbl_descr, tblDataWidget):
|
||||
def addDataIntoTable(tbl_name, tblDataWidget, data='NULL'):
|
||||
global tblList
|
||||
# выбираем название таблицы по описанию
|
||||
for i in tblList:
|
||||
if i[1] == tbl_descr:
|
||||
tbl_name = i[0]
|
||||
#print(tbl_descr)
|
||||
data = dm.selectData(tbl_name)
|
||||
# for i in tblList:
|
||||
# if i[1] == tbl_descr:
|
||||
# tbl_name = i[0]
|
||||
# else:
|
||||
# tbl_name = tbl_descr
|
||||
|
||||
#print(tbl_name)
|
||||
if data == 'NULL':
|
||||
data = dm.selectData(tbl_name)
|
||||
#print(data)
|
||||
fieldNames = dm.getTableStructure(tbl_name)
|
||||
print(fieldNames)
|
||||
#print(fieldNames)
|
||||
# проверка на наличие записей в таблице
|
||||
if data:
|
||||
# количество строк
|
||||
|
@ -318,7 +325,7 @@ def addNewRecord():
|
|||
if i[1] == tblDescr:
|
||||
tblName = i[0]
|
||||
|
||||
print(tblName)
|
||||
#print(tblName)
|
||||
editForm = EditForm(tblName, tblDescr)
|
||||
editForm.show()
|
||||
#editForm.setParent(listTables)
|
||||
|
@ -327,7 +334,7 @@ def addNewRecord():
|
|||
#editForm = Example()
|
||||
|
||||
def main():
|
||||
global tblList, listTables, tblRelationsData
|
||||
global tblList, listTables, tabRelationsData
|
||||
app = QApplication(sys.argv)
|
||||
mainWin = QMainWindow()
|
||||
workArea = QWidget()
|
||||
|
@ -339,18 +346,19 @@ def main():
|
|||
#tblData = QTableWidget()
|
||||
tblData = MyTable()
|
||||
tblData.setFrameShape(QFrame.StyledPanel)
|
||||
tblRelationsData = QTableWidget()
|
||||
tblRelationsData.setFrameShape(QFrame.StyledPanel)
|
||||
tabRelationsData = QTabWidget()
|
||||
|
||||
#tabRelationsData.setFrameShape(QFrame.StyledPanel)
|
||||
splitter1 = QSplitter(Qt.Horizontal)
|
||||
splitter1.addWidget(listTables)
|
||||
splitter2 = QSplitter(Qt.Vertical)
|
||||
splitter2.addWidget(tblData)
|
||||
splitter2.addWidget(tblRelationsData)
|
||||
splitter2.addWidget(tabRelationsData)
|
||||
splitter1.addWidget(splitter2)
|
||||
hbox.addWidget(splitter1)
|
||||
workArea.setLayout(hbox)
|
||||
showMenuToolbar(mainWin)
|
||||
listTables.itemClicked.connect(lambda: addDataIntoTable(listTables.model().data(listTables.currentIndex()), tblData))
|
||||
listTables.itemClicked.connect(lambda: addDataIntoTable(dm.getTablesNameOfDescription(listTables.model().data(listTables.currentIndex())), tblData))
|
||||
# выводим список таблиц в левом поле
|
||||
tblList = dm.initDBstructure()
|
||||
for i in tblList:
|
||||
|
@ -361,6 +369,39 @@ def main():
|
|||
mainWin.show()
|
||||
|
||||
return app.exec_()
|
||||
|
||||
|
||||
def showRelationsRecords(fieldIndex, fieldValue):
|
||||
global tabRelationsData, listTables
|
||||
tblDescr = listTables.model().data(listTables.currentIndex())
|
||||
i=0
|
||||
if tabRelationsData.count():
|
||||
while i <= tabRelationsData.count():
|
||||
tabRelationsData.removeTab(i)
|
||||
i = i+1
|
||||
for i in tblList:
|
||||
if i[1] == tblDescr:
|
||||
tblName = i[0]
|
||||
fieldNamesList = dm.getTableStructure(tblName)
|
||||
fieldName = fieldNamesList[fieldIndex][0]
|
||||
# print(fieldNamesList)
|
||||
# print(fieldName)
|
||||
# print("showRelations")
|
||||
# print(tblName)
|
||||
# print(fieldValue)
|
||||
data = dm.selectRelationsDataFromDB(tblName, fieldName, fieldValue)
|
||||
#print(data)
|
||||
for item in data:
|
||||
tbl = item[0]
|
||||
qweryResult = item[1]
|
||||
if qweryResult:
|
||||
print('report' + str(qweryResult) + 'report')
|
||||
tblRelationsData = MyTable()
|
||||
tblRelationsData.setParent(tabRelationsData)
|
||||
tabRelationsData.addTab(tblRelationsData, dm.getTablesDescriptionOfName(tbl))
|
||||
addDataIntoTable(tblName, tblRelationsData, qweryResult)
|
||||
|
||||
|
||||
#
|
||||
#
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user