Added not full sqlite3 support. Added config file.
This commit is contained in:
parent
e8eca97b6b
commit
f9da7147d2
BIN
database.db
BIN
database.db
Binary file not shown.
118
dm.py
118
dm.py
|
@ -1,16 +1,94 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import json, pymysql
|
||||
import json, os, configparser, shutil
|
||||
|
||||
conn = pymysql.connect(
|
||||
db='test',
|
||||
user='ksv',
|
||||
passwd='1234567890',
|
||||
host='kis',
|
||||
charset='utf8')
|
||||
c = conn.cursor()
|
||||
print(os.getenv("HOME"))
|
||||
|
||||
def firstInit():
|
||||
# Инициализация переменныхб создание конфигруационного файла, копирование шаблонов
|
||||
global db_type, db_hostname, db_user, db_password, db_name, template_file, db_type
|
||||
config = configparser.RawConfigParser()
|
||||
# определим каталог для конфигруции и создадим если его нет
|
||||
cfg_dir = os.path.join(os.getenv("HOME"), ".dm")
|
||||
if os.path.isdir(cfg_dir):
|
||||
print(cfg_dir + " already exists")
|
||||
else:
|
||||
os.mkdir(cfg_dir)
|
||||
cfg_file = os.path.join(cfg_dir, 'dm.cfg')
|
||||
print(cfg_file)
|
||||
# создадим файл конфигурации
|
||||
config.add_section('DataBase')
|
||||
config.set('DataBase', 'db_type', 'mysql')
|
||||
config.set('DataBase', 'db_hostname', 'localhost')
|
||||
config.set('DataBase', 'db_name', 'dm')
|
||||
config.set('DataBase', 'db_user', 'dm')
|
||||
config.set('DataBase', 'db_password', 'password')
|
||||
config.add_section('Directory')
|
||||
config.set('Directory', 'work_dir', cfg_dir)
|
||||
config.set('Directory', 'template_dir', os.path.join(cfg_dir, 'db_template'))
|
||||
|
||||
# Если конфиг уже есть читаем его если нет, создаем и потом читаем
|
||||
if os.path.isfile(cfg_file):
|
||||
config.read(cfg_file)
|
||||
else:
|
||||
# Запись конфигурации в файл 'example.cfg'
|
||||
with open(cfg_file, 'w') as configfile:
|
||||
config.write(configfile)
|
||||
configfile.close()
|
||||
config.read(cfg_file)
|
||||
|
||||
work_dir = config.get('Directory', 'work_dir')
|
||||
template_dir = config.get('Directory', 'template_dir')
|
||||
db_type = config.get('DataBase', 'db_type')
|
||||
db_hostname = config.get('DataBase', 'db_hostname')
|
||||
if db_type == 'sqlite':
|
||||
db_name = os.path.join(work_dir, config.get('DataBase', 'db_name'))
|
||||
else:
|
||||
db_name = config.get('DataBase', 'db_name')
|
||||
db_user = config.get('DataBase', 'db_user')
|
||||
db_password = config.get('DataBase', 'db_password')
|
||||
|
||||
# Создаём нужные каталоги
|
||||
if os.path.isdir(work_dir):
|
||||
print(work_dir + " already exists")
|
||||
else:
|
||||
os.mkdir(work_dir)
|
||||
if os.path.isdir(template_dir):
|
||||
print(template_dir + " already exists")
|
||||
else:
|
||||
os.mkdir(template_dir)
|
||||
# копируем и читаем файл шаблон БД
|
||||
template_file = os.path.join(template_dir, 'tables.json')
|
||||
if os.path.isfile(template_file):
|
||||
print("Template file already exists")
|
||||
else:
|
||||
shutil.copy('tables.json', template_file)
|
||||
|
||||
|
||||
def dbConnect():
|
||||
global c, db_type, db_hostname, db_user, db_password, db_name
|
||||
print(db_name)
|
||||
print(db_type)
|
||||
if db_type == "mysql":
|
||||
import pymysql
|
||||
conn = pymysql.connect(
|
||||
db=db_name,
|
||||
user=db_user,
|
||||
passwd=db_password,
|
||||
host=db_hostname,
|
||||
charset='utf8')
|
||||
elif db_type == "sqlite":
|
||||
import sqlite3
|
||||
print(db_name)
|
||||
conn = sqlite3.connect(db_name)
|
||||
else:
|
||||
print(db_type + " database type does`t support")
|
||||
exit()
|
||||
#print(conn)
|
||||
c = conn.cursor()
|
||||
return c
|
||||
|
||||
def createTables(tbl_list):
|
||||
global tbl_descr_list, tbl_struct_list
|
||||
global tbl_descr_list, tbl_struct_list, c
|
||||
i = 0
|
||||
tbl_names_list = []
|
||||
tbl_descr_list = []
|
||||
|
@ -59,12 +137,12 @@ def createTables(tbl_list):
|
|||
tbl_descr_list.append(one_Table_descr)
|
||||
tbl_struct_list.append(one_Table_struct)
|
||||
print(qwery_create)
|
||||
c.execute(qwery_create)
|
||||
#c.execute(qwery_create)
|
||||
return tbl_names_list
|
||||
|
||||
def initDBstructure():
|
||||
global tbl_descr_list
|
||||
table_list = open("tables.json", "r", encoding="utf-8")
|
||||
global tbl_descr_list, template_file
|
||||
table_list = open(template_file, "r", encoding="utf-8")
|
||||
data = json.load(table_list, encoding="utf-8")
|
||||
tbl_list = data["tables"]
|
||||
tbl_names_list = createTables(tbl_list)
|
||||
|
@ -73,7 +151,7 @@ def initDBstructure():
|
|||
|
||||
# выборка данных из заданной таблицы
|
||||
def selectData(tbl):
|
||||
global tbl_struct_list
|
||||
global tbl_struct_list, c, db_type
|
||||
# если юольше 1 поля добавить CONCAT
|
||||
qwery = "SELECT "
|
||||
subqwery = ""
|
||||
|
@ -85,7 +163,8 @@ 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(",", ",' ',")
|
||||
#field_replace = field_replace.replace(",", " || ")
|
||||
# определяем название таблицы для вложенного запроса
|
||||
table1 = field_rel.split('.')[0]
|
||||
field1 = field_rel.split('.')[1]
|
||||
|
@ -95,7 +174,12 @@ def selectData(tbl):
|
|||
else:
|
||||
subqwery = table1
|
||||
# составляем подзапрос и подменяем им поле в запросе
|
||||
subqwery = "(SELECT CONCAT(" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
|
||||
if db_type == "mysql":
|
||||
field_replace = field_replace.replace(",", ",' ',")
|
||||
subqwery = "(SELECT CONCAT(" + field_replace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tbl + "." + field + ") AS " + field
|
||||
elif db_type == "sqlite":
|
||||
field_replace = field_replace.replace(",", " || ")
|
||||
subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
|
||||
qwery = qwery.replace(field, subqwery)
|
||||
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
|
||||
print(qwery)
|
||||
|
@ -110,4 +194,6 @@ def getTablesStructure(tbl):
|
|||
if item[0] == tbl:
|
||||
return item[1]
|
||||
|
||||
#initDBstructure()
|
||||
#initDBstructure()
|
||||
firstInit()
|
||||
dbConnect()
|
Loading…
Reference in New Issue
Block a user