Some changes in create table procedure and releations

This commit is contained in:
Sergey Kalinin 2017-03-16 14:13:20 +03:00
parent fa3029f48e
commit c337ef133d
2 changed files with 14 additions and 74 deletions

28
dm.py
View File

@ -2,9 +2,9 @@
import json, pymysql
conn = pymysql.connect(
db='ats',
user='dba',
passwd='AlsprofilinE',
db='test',
user='ksv',
passwd='1234567890',
host='kis',
charset='utf8')
c = conn.cursor()
@ -23,19 +23,18 @@ def createTables(tbl_list):
one_Table_relation = []
tbl_descr = tbl_list[i]["tableDescription"]
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])
one_Table_descr.append(tbl_name)
one_Table_struct.append(tbl_name)
x = 0
# список всех полей таблицы
qwery_create = "CREATE TABLE " + tbl_name + " ("
qwery_create = "CREATE TABLE IF NOT EXISTS " + 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:
@ -47,15 +46,11 @@ def createTables(tbl_list):
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)
@ -64,6 +59,7 @@ def createTables(tbl_list):
i = i + 1
tbl_descr_list.append(one_Table_descr)
tbl_struct_list.append(one_Table_struct)
c.execute(qwery_create)
return tbl_names_list
def initDBstructure():
@ -78,13 +74,10 @@ def initDBstructure():
# выборка данных из заданной таблицы
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:
for item in tbl_struct_list:
if item[0] == tbl:
for field in item[1]:
qwery = qwery + field + ","
@ -93,7 +86,6 @@ def selectData(tbl):
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]
@ -104,14 +96,10 @@ def selectData(tbl):
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()
# получаем на вход имя таблицы и возвращаем список заголовков полей

View File

@ -147,8 +147,8 @@
]
},
{
"tableName": "cdr",
"tableDescription": "Записи о звонках",
"tableName": "test",
"tableDescription": "Шляпа",
"fieldList": [
{
"fName": "id",
@ -159,60 +159,12 @@
"relation": []
},
{
"fName": "int_number",
"fDescription": "Внутренний номер",
"fType": "varchar(11)",
"fName": "user",
"fDescription": "Юзер шляпы",
"fType": "int(11)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "ext_co_line",
"fDescription": "Номер внешней линии",
"fType": "char(2)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "dial_number",
"fDescription": "Набранный номер",
"fType": "varchar(30)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "ring",
"fDescription": "ХЗ",
"fType": "varchar(5)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "acc_code",
"fDescription": "АСС код",
"fType": "varchar(20)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "call_code",
"fDescription": "Код звонка",
"fType": "char(2)",
"index": "no",
"autoIncrement": "no",
"relation": []
},
{
"fName": "call_direct",
"fDescription": "Направление",
"fType": "varchar(45)",
"index": "no",
"autoIncrement": "no",
"relation": []
"relation": ["users.id", "last_name, name, middle_name"]
}
]
}