Code refactoring (use format() command for generate qwery string, rename some variables)

This commit is contained in:
svk28
2017-04-11 12:37:36 +03:00
parent 339e353b2b
commit a4528dccef
2 changed files with 80 additions and 71 deletions

97
dm.py
View File

@@ -88,11 +88,11 @@ def dbConnect():
return c
def createTables(tbl_list):
global tbl_descr_list, tbl_struct_list, c, db_type
global dbTablesDescriptionList, dbTablesStructList, c, db_type
i = 0
tbl_names_list = []
tbl_descr_list = []
tbl_struct_list = []
dbTablesNamesList = []
dbTablesDescriptionList = []
dbTablesStructList = []
while i < len(tbl_list):
one_Table_descr = []
@@ -101,7 +101,7 @@ def createTables(tbl_list):
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])
dbTablesNamesList.append([tbl_name, tbl_descr])
one_Table_descr.append(tbl_name)
one_Table_struct.append(tbl_name)
@@ -165,14 +165,14 @@ def createTables(tbl_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)
dbTablesDescriptionList.append(one_Table_descr)
dbTablesStructList.append(one_Table_struct)
#print(qwery_create)
c.execute(qwery_create)
return tbl_names_list
return dbTablesNamesList
def initDBstructure():
global tbl_descr_list, template_file, tblNamesList
global dbTablesDescriptionList, template_file, tblNamesList
table_list = open(template_file, "r", encoding="utf-8")
data = json.load(table_list, encoding="utf-8")
tbl_list = data["tables"]
@@ -182,11 +182,11 @@ def initDBstructure():
# выборка данных из заданной таблицы
def selectData(tbl):
global tbl_struct_list, c, db_type
global dbTablesStructList, c, db_type
# если юольше 1 поля добавить CONCAT
qwery = "SELECT "
subqwery = ""
for item in tbl_struct_list:
for item in dbTablesStructList:
if item[0] == tbl:
for field in item[1]:
field = field[0]
@@ -195,8 +195,6 @@ def selectData(tbl):
field = rel[0]
field_rel = rel[1][0]
field_replace = rel[1][1]
#field_replace = field_replace.replace(",", ",' ',")
#field_replace = field_replace.replace(",", " || ")
# определяем название таблицы для вложенного запроса
table1 = field_rel.split('.')[0]
field1 = field_rel.split('.')[1]
@@ -208,38 +206,43 @@ def selectData(tbl):
# составляем подзапрос и подменяем им поле в запросе
if db_type == "mysql":
field_replace = field_replace.replace(",", ",' ',")
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
subqwery = "(SELECT CONCAT({}) FROM {} WHERE {}.{}={}.{}) AS {}"\
.format(field_replace,subqwery,table1,field1,tbl,field,field)
elif db_type == "sqlite":
field_replace = field_replace.replace(",", " || ' ' ||")
subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
#subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
subqwery = "(SELECT ({}) FROM {} WHERE {}.{}={}.{}) AS {}" \
.format(field_replace, subqwery, table1, field1, tbl, field, field)
qwery = qwery.replace(field, subqwery)
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
#qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
qwery = '{} FROM {} LIMIT 10000'.format(qwery.rstrip(','), tbl)
#print(qwery)
c.execute(qwery)
return c.fetchall()
# получаем на вход имя таблицы и возвращаем список заголовков полей
def getTableStructure(tbl):
global tbl_descr_list, tbl_struct_list
#print(tbl_descr_list)
#print(tbl_struct_list)
for item in tbl_descr_list:
global dbTablesDescriptionList, dbTablesStructList
#print(dbTablesDescriptionList)
#print(dbTablesStructList)
for item in dbTablesDescriptionList:
if item[0] == tbl:
return item[1]
# Получаем список названий полей и типов для заданной таблицы
def getFields(tbl):
global tbl_descr_list, tbl_struct_list
#print(tbl_descr_list)
#print(tbl_struct_list)
for item in tbl_struct_list:
global dbTablesDescriptionList, dbTablesStructList
#print(dbTablesDescriptionList)
#print(dbTablesStructList)
for item in dbTablesStructList:
if item[0] == tbl:
#print(item[1])
return item[1]
# Ищем описание поля по его названию и возвращаем.
def getFieldDescription(tbl, field):
global tbl_descr_list, tbl_struct_list
for item in tbl_descr_list:
global dbTablesDescriptionList, dbTablesStructList
for item in dbTablesDescriptionList:
if item[0] == tbl:
for i in item[1]:
if i[0] == field:
@@ -263,10 +266,10 @@ def fieldTypeConvert(ftype):
return fType
def getFieldType(tbl, field):
global tbl_descr_list, tbl_struct_list
#print(tbl_descr_list)
#print(tbl_struct_list)
for item in tbl_struct_list:
global dbTablesDescriptionList, dbTablesStructList
#print(dbTablesDescriptionList)
#print(dbTablesStructList)
for item in dbTablesStructList:
if item[0] == tbl:
for i in item[1]:
if i[0] == field:
@@ -276,12 +279,12 @@ def getFieldType(tbl, field):
#return item[1]
def getRelationsForField(tblSearch, fieldName):
global tbl_struct_list, c, db_type
global dbTablesStructList, c, db_type
#print("Ищем связи для:" +tblSearch +','+ fieldName)
#print(tbl_struct_list)
#print(dbTablesStructList)
#searchField = tblSearch + '.' + fieldName
dataList = []
for item in tbl_struct_list:
for item in dbTablesStructList:
#print(item)
tblName = item[0]
for field in item[1]:
@@ -348,11 +351,11 @@ def insertDataIntoBD(dataList):
# Выборка данных по значению
# принимает на вход название таблицы, назване поля, значение этого поля
def selectDataFromDB(tblName, fieldName, fieldValue):
global tbl_struct_list, c, db_type
global dbTablesStructList, c, db_type
qwery = "SELECT "
subqwery = ""
for item in tbl_struct_list:
for item in dbTablesStructList:
if item[0] == tblName:
for field in item[1]:
field = field[0]
@@ -372,23 +375,29 @@ def selectDataFromDB(tblName, fieldName, fieldValue):
# составляем подзапрос и подменяем им поле в запросе
if db_type == "mysql":
fieldReplace = fieldReplace.replace(",", ",' ',")
subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field
#subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field
subqwery = '(SELECT CONCAT({}) FROM {} WHERE {}.{}={}.{}) AS {}'\
.format(fieldReplace, subqwery, table1, field1, tblName, field, field)
elif db_type == "sqlite":
fieldReplace = fieldReplace.replace(",", " || ' ' ||")
subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
#subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
subqwery = '(SELECT ({}) FROM {} WHERE {}.{}={}.{}) AS {}'\
.format(fieldReplace, subqwery, table1, field1, tblName, field, field)
#print("---" + subqwery)
qwery = qwery.replace(field, subqwery)
#qwery = qwery.rstrip(',') + " FROM " + tblName + " LIMIT 10000"
qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue
#qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue
qwery = '{} FROM {} WHERE {}={}'.format(qwery.rstrip(','), tblName, fieldName, fieldValue)
#print(qwery)
c.execute(qwery)
return c.fetchall()
def selectRelationsDataFromDB(tblSearch, fieldName, fieldValue):
global tbl_struct_list, c, db_type
global dbTablesStructList, c, db_type
searchField = tblSearch + '.' + fieldName
dataList = []
for item in tbl_struct_list:
for item in dbTablesStructList:
tblName = item[0]
for field in item[1]:
field = field[0]
@@ -415,7 +424,6 @@ def getTablesDescriptionOfName(tblName):
# Удаление записи из БД
def deleteRecordsFromDB(tableName, valueList):
global c, db_type
qwery = 'DELETE FROM {} WHERE '.format(tableName)
print(qwery)
subQwery=''
@@ -423,10 +431,11 @@ def deleteRecordsFromDB(tableName, valueList):
for item in getFields(tableName):
print(item[0], item[1])
if item[1] == 'integer':
subQwery = subQwery + item[0] + '=' + valueList[i] + ' AND '
#subQwery = subQwery + item[0] + '=' + valueList[i] + ' AND '
subQwery = '{}{}={} AND'.format(subQwery, item[0], valueList[i])
else:
subQwery = subQwery + item[0] + '=\'' + valueList[i] + '\' AND '
#subQwery = '{}'.format(subQwery,item[0])
#subQwery = subQwery + item[0] + '=\'' + valueList[i] + '\' AND '
subQwery = "{}{}='{}' AND ".format(subQwery, item[0], valueList[i])
i += 1
qwery += subQwery
print(qwery)