2017-03-12 15:02:21 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-15 15:58:26 +03:00
|
|
|
import json, pymysql
|
|
|
|
|
|
|
|
conn = pymysql.connect(
|
2017-03-16 14:13:20 +03:00
|
|
|
db='test',
|
|
|
|
user='ksv',
|
|
|
|
passwd='1234567890',
|
2017-03-15 15:58:26 +03:00
|
|
|
host='kis',
|
|
|
|
charset='utf8')
|
|
|
|
c = conn.cursor()
|
|
|
|
|
2017-03-13 16:50:33 +03:00
|
|
|
def createTables(tbl_list):
|
2017-03-15 15:58:26 +03:00
|
|
|
global tbl_descr_list, tbl_struct_list
|
2017-03-13 16:50:33 +03:00
|
|
|
i = 0
|
|
|
|
tbl_names_list = []
|
2017-03-14 17:01:30 +03:00
|
|
|
tbl_descr_list = []
|
2017-03-15 15:58:26 +03:00
|
|
|
tbl_struct_list = []
|
2017-03-14 17:01:30 +03:00
|
|
|
|
2017-03-13 16:50:33 +03:00
|
|
|
while i < len(tbl_list):
|
2017-03-14 17:01:30 +03:00
|
|
|
one_Table_descr = []
|
2017-03-15 15:58:26 +03:00
|
|
|
one_Table_struct = []
|
2017-03-15 17:06:24 +03:00
|
|
|
one_Table_relation = []
|
2017-03-13 16:50:33 +03:00
|
|
|
tbl_descr = tbl_list[i]["tableDescription"]
|
|
|
|
tbl_name = tbl_list[i]["tableName"]
|
2017-03-16 14:13:20 +03:00
|
|
|
#field_list = tbl_list[i]["fieldList"][i].keys()
|
2017-03-14 17:01:30 +03:00
|
|
|
tbl_names_list.append([tbl_name, tbl_descr])
|
|
|
|
one_Table_descr.append(tbl_name)
|
2017-03-15 15:58:26 +03:00
|
|
|
one_Table_struct.append(tbl_name)
|
2017-03-13 16:50:33 +03:00
|
|
|
|
|
|
|
x = 0
|
|
|
|
# список всех полей таблицы
|
2017-03-16 14:13:20 +03:00
|
|
|
qwery_create = "CREATE TABLE IF NOT EXISTS " + tbl_name + " ("
|
2017-03-13 16:50:33 +03:00
|
|
|
index = ""
|
2017-03-14 17:01:30 +03:00
|
|
|
field_names_list = []
|
2017-03-15 15:58:26 +03:00
|
|
|
struct_fields_list = []
|
2017-03-13 16:50:33 +03:00
|
|
|
while x < len(tbl_list[i]["fieldList"]):
|
|
|
|
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"] + ")"
|
2017-03-14 17:01:30 +03:00
|
|
|
field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"])
|
2017-03-15 15:58:26 +03:00
|
|
|
struct_fields_list.append(tbl_list[i]["fieldList"][x]["fName"])
|
2017-03-15 17:06:24 +03:00
|
|
|
if tbl_list[i]["fieldList"][x]["relation"]:
|
|
|
|
relation_list=[tbl_list[i]["fieldList"][x]["fName"]]
|
|
|
|
relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
|
|
|
|
|
|
|
|
one_Table_relation.append(relation_list)
|
2017-03-13 16:50:33 +03:00
|
|
|
x = x + 1
|
2017-03-15 17:06:24 +03:00
|
|
|
|
2017-03-13 16:50:33 +03:00
|
|
|
qwery_create = qwery_create + index + ");"
|
2017-03-15 15:58:26 +03:00
|
|
|
one_Table_struct.append(struct_fields_list)
|
2017-03-15 17:06:24 +03:00
|
|
|
one_Table_struct.append(one_Table_relation)
|
2017-03-14 17:01:30 +03:00
|
|
|
one_Table_descr.append(field_names_list)
|
2017-03-13 16:50:33 +03:00
|
|
|
i = i + 1
|
2017-03-14 17:01:30 +03:00
|
|
|
tbl_descr_list.append(one_Table_descr)
|
2017-03-15 15:58:26 +03:00
|
|
|
tbl_struct_list.append(one_Table_struct)
|
2017-03-17 17:17:17 +03:00
|
|
|
print(qwery_create)
|
2017-03-16 14:13:20 +03:00
|
|
|
c.execute(qwery_create)
|
2017-03-13 16:50:33 +03:00
|
|
|
return tbl_names_list
|
|
|
|
|
|
|
|
def initDBstructure():
|
2017-03-14 17:01:30 +03:00
|
|
|
global tbl_descr_list
|
2017-03-13 16:50:33 +03:00
|
|
|
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)
|
2017-03-14 17:01:30 +03:00
|
|
|
|
2017-03-13 16:50:33 +03:00
|
|
|
return tbl_names_list
|
|
|
|
|
2017-03-15 15:58:26 +03:00
|
|
|
# выборка данных из заданной таблицы
|
|
|
|
def selectData(tbl):
|
|
|
|
global tbl_struct_list
|
2017-03-16 13:00:32 +03:00
|
|
|
# если юольше 1 поля добавить CONCAT
|
2017-03-15 15:58:26 +03:00
|
|
|
qwery = "SELECT "
|
2017-03-16 13:00:32 +03:00
|
|
|
subqwery = ""
|
2017-03-16 14:13:20 +03:00
|
|
|
for item in tbl_struct_list:
|
2017-03-15 15:58:26 +03:00
|
|
|
if item[0] == tbl:
|
|
|
|
for field in item[1]:
|
|
|
|
qwery = qwery + field + ","
|
2017-03-15 17:06:24 +03:00
|
|
|
for rel in item[2]:
|
|
|
|
field = rel[0]
|
|
|
|
field_rel = rel[1][0]
|
|
|
|
field_replace = rel[1][1]
|
2017-03-16 13:00:32 +03:00
|
|
|
field_replace = field_replace.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
|
|
|
|
qwery = qwery.replace(field, subqwery)
|
2017-03-15 17:06:24 +03:00
|
|
|
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
|
2017-03-17 17:17:17 +03:00
|
|
|
print(qwery)
|
2017-03-16 13:00:32 +03:00
|
|
|
c.execute(qwery)
|
2017-03-16 14:13:20 +03:00
|
|
|
|
2017-03-16 13:00:32 +03:00
|
|
|
return c.fetchall()
|
2017-03-13 16:50:33 +03:00
|
|
|
|
2017-03-14 17:01:30 +03:00
|
|
|
# получаем на вход имя таблицы и возвращаем список заголовков полей
|
|
|
|
def getTablesStructure(tbl):
|
|
|
|
global tbl_descr_list
|
|
|
|
for item in tbl_descr_list:
|
|
|
|
if item[0] == tbl:
|
|
|
|
return item[1]
|
|
|
|
|
|
|
|
#initDBstructure()
|