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 import json, pymysql
conn = pymysql.connect( conn = pymysql.connect(
db='ats', db='test',
user='dba', user='ksv',
passwd='AlsprofilinE', passwd='1234567890',
host='kis', host='kis',
charset='utf8') charset='utf8')
c = conn.cursor() c = conn.cursor()
@ -23,19 +23,18 @@ def createTables(tbl_list):
one_Table_relation = [] one_Table_relation = []
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]) tbl_names_list.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)
x = 0 x = 0
# список всех полей таблицы # список всех полей таблицы
qwery_create = "CREATE TABLE " + tbl_name + " (" qwery_create = "CREATE TABLE IF NOT EXISTS " + tbl_name + " ("
index = "" index = ""
field_names_list = [] field_names_list = []
struct_fields_list = [] struct_fields_list = []
while x < len(tbl_list[i]["fieldList"]): while x < len(tbl_list[i]["fieldList"]):
#one_Table_relation = []
if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes": if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes":
auto_increment = " AUTO_INCREMENT, " auto_increment = " AUTO_INCREMENT, "
else: else:
@ -47,15 +46,11 @@ def createTables(tbl_list):
field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"]) field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"])
struct_fields_list.append(tbl_list[i]["fieldList"][x]["fName"]) struct_fields_list.append(tbl_list[i]["fieldList"][x]["fName"])
if tbl_list[i]["fieldList"][x]["relation"]: 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=[tbl_list[i]["fieldList"][x]["fName"]]
relation_list.append(tbl_list[i]["fieldList"][x]["relation"]) relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
one_Table_relation.append(relation_list) one_Table_relation.append(relation_list)
x = x + 1 x = x + 1
#print(one_Table_relation)
qwery_create = qwery_create + index + ");" qwery_create = qwery_create + index + ");"
one_Table_struct.append(struct_fields_list) one_Table_struct.append(struct_fields_list)
@ -64,6 +59,7 @@ def createTables(tbl_list):
i = i + 1 i = i + 1
tbl_descr_list.append(one_Table_descr) tbl_descr_list.append(one_Table_descr)
tbl_struct_list.append(one_Table_struct) tbl_struct_list.append(one_Table_struct)
c.execute(qwery_create)
return tbl_names_list return tbl_names_list
def initDBstructure(): def initDBstructure():
@ -78,13 +74,10 @@ def initDBstructure():
# выборка данных из заданной таблицы # выборка данных из заданной таблицы
def selectData(tbl): def selectData(tbl):
global tbl_struct_list 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 # если юольше 1 поля добавить CONCAT
qwery = "SELECT " qwery = "SELECT "
subqwery = "" subqwery = ""
tsl = tbl_struct_list for item in tbl_struct_list:
for item in tsl:
if item[0] == tbl: if item[0] == tbl:
for field in item[1]: for field in item[1]:
qwery = qwery + field + "," qwery = qwery + field + ","
@ -93,7 +86,6 @@ def selectData(tbl):
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(",", ",' ',")
#print(field + "->" + field_rel + "->" + field_replace)
# определяем название таблицы для вложенного запроса # определяем название таблицы для вложенного запроса
table1 = field_rel.split('.')[0] table1 = field_rel.split('.')[0]
field1 = field_rel.split('.')[1] field1 = field_rel.split('.')[1]
@ -104,14 +96,10 @@ def selectData(tbl):
subqwery = table1 subqwery = table1
# составляем подзапрос и подменяем им поле в запросе # составляем подзапрос и подменяем им поле в запросе
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
#print(subqwery)
qwery = qwery.replace(field, subqwery) qwery = qwery.replace(field, subqwery)
#print(item[1])
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000" qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
c.execute(qwery) c.execute(qwery)
print(qwery)
#tsl.clear()
return c.fetchall() return c.fetchall()
# получаем на вход имя таблицы и возвращаем список заголовков полей # получаем на вход имя таблицы и возвращаем список заголовков полей

View File

@ -147,8 +147,8 @@
] ]
}, },
{ {
"tableName": "cdr", "tableName": "test",
"tableDescription": "Записи о звонках", "tableDescription": "Шляпа",
"fieldList": [ "fieldList": [
{ {
"fName": "id", "fName": "id",
@ -159,60 +159,12 @@
"relation": [] "relation": []
}, },
{ {
"fName": "int_number", "fName": "user",
"fDescription": "Внутренний номер", "fDescription": "Юзер шляпы",
"fType": "varchar(11)", "fType": "int(11)",
"index": "no", "index": "no",
"autoIncrement": "no", "autoIncrement": "no",
"relation": [] "relation": ["users.id", "last_name, name, middle_name"]
},
{
"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": []
} }
] ]
} }