# -*- coding: utf-8 -*- import json, pymysql conn = pymysql.connect( db='ats', user='dba', passwd='AlsprofilinE', host='kis', charset='utf8') c = conn.cursor() def createTables(tbl_list): global tbl_descr_list, tbl_struct_list i = 0 tbl_names_list = [] tbl_descr_list = [] tbl_struct_list = [] while i < len(tbl_list): one_Table_descr = [] one_Table_struct = [] one_Table_relation = [] 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]) one_Table_descr.append(tbl_name) one_Table_struct.append(tbl_name) x = 0 # список всех полей таблицы qwery_create = "CREATE TABLE " + tbl_name + " (" index = "" field_names_list = [] struct_fields_list = [] while x < len(tbl_list[i]["fieldList"]): #one_Table_relation = [] if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes": auto_increment = " AUTO_INCREMENT, " else: auto_increment = ", " qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + " " + \ tbl_list[i]["fieldList"][x]["fType"] + auto_increment if tbl_list[i]["fieldList"][x]["index"] == "PRIMARY KEY": index = "PRIMARY KEY(" + tbl_list[i]["fieldList"][x]["fName"] + ")" field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"]) struct_fields_list.append(tbl_list[i]["fieldList"][x]["fName"]) if tbl_list[i]["fieldList"][x]["relation"]: #struct_fields_list.append(tbl_list[i]["fieldList"][x]["relation"]) #print(tbl_list[i]["fieldList"][x]["relation"]) #one_Table_relation.append(tbl_list[i]["fieldList"][x]["fName"]) relation_list=[tbl_list[i]["fieldList"][x]["fName"]] relation_list.append(tbl_list[i]["fieldList"][x]["relation"]) one_Table_relation.append(relation_list) x = x + 1 #print(one_Table_relation) qwery_create = qwery_create + index + ");" one_Table_struct.append(struct_fields_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) return tbl_names_list def initDBstructure(): global tbl_descr_list table_list = open("tables.json", "r", encoding="utf-8") data = json.load(table_list, encoding="utf-8") tbl_list = data["tables"] tbl_names_list = createTables(tbl_list) return tbl_names_list # выборка данных из заданной таблицы def selectData(tbl): global tbl_struct_list #print(tbl_struct_list) #select docs.doc_name, docs.description, (select CONCAT(last_name, ' ', name, ' ', middle_name) AS user from users where id = docs.users_id) as user from docs # если юольше 1 поля добавить CONCAT qwery = "SELECT " subqwery = "" tsl = tbl_struct_list for item in tsl: if item[0] == tbl: for field in item[1]: qwery = qwery + field + "," for rel in item[2]: field = rel[0] field_rel = rel[1][0] field_replace = rel[1][1] field_replace = field_replace.replace(",", ",' ',") #print(field + "->" + field_rel + "->" + field_replace) # определяем название таблицы для вложенного запроса table1 = field_rel.split('.')[0] field1 = field_rel.split('.')[1] if table1 == tbl: table1 = table1 + "_1" subqwery = tbl + " AS " +table1 else: subqwery = table1 # составляем подзапрос и подменяем им поле в запросе subqwery = "(SELECT CONCAT(" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field #print(subqwery) qwery = qwery.replace(field, subqwery) #print(item[1]) qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000" c.execute(qwery) print(qwery) #tsl.clear() return c.fetchall() # получаем на вход имя таблицы и возвращаем список заголовков полей def getTablesStructure(tbl): global tbl_descr_list for item in tbl_descr_list: if item[0] == tbl: return item[1] #initDBstructure()