diff --git a/App/Report/db/DBconnect.py b/App/Database/DBconnect.py similarity index 91% rename from App/Report/db/DBconnect.py rename to App/Database/DBconnect.py index b335654..2280c13 100644 --- a/App/Report/db/DBconnect.py +++ b/App/Database/DBconnect.py @@ -13,7 +13,8 @@ class DBContextManager: port=self.db_config['port'], user=self.db_config['user'], password=self.db_config['password'], - db=self.db_config['db'] + db=self.db_config['db'], + charset=self.db_config['charset'] ) self.cursor = self.connection.cursor() return self.cursor diff --git a/App/Report/db/__init__.py b/App/Database/__init__.py similarity index 100% rename from App/Report/db/__init__.py rename to App/Database/__init__.py diff --git a/App/Report/db/sql_provider.py b/App/Database/sql_provider.py similarity index 100% rename from App/Report/db/sql_provider.py rename to App/Database/sql_provider.py diff --git a/App/Report/db/work.py b/App/Database/work.py similarity index 83% rename from App/Report/db/work.py rename to App/Database/work.py index 2fd1ff3..6437858 100644 --- a/App/Report/db/work.py +++ b/App/Database/work.py @@ -11,7 +11,7 @@ def select_list(db_config, sql) -> list: lst = [dict(zip(schema, row)) for row in result] return lst -def procedure(db_config, name, args: tuple): +def procedure(db_config, name, args: tuple) -> list: with DBContextManager(db_config) as cursor: if cursor is None: raise ValueError("Cursor not created") @@ -20,4 +20,8 @@ def procedure(db_config, name, args: tuple): result = cursor.fetchall()[0] schema = cursor.description[0] lst = dict(zip(schema, result)) - return lst \ No newline at end of file + return lst + +def transaction(cursor, sql): + cursor.execute(sql) + return True \ No newline at end of file diff --git a/App/Requests/db/DBconnect.py b/App/Requests/db/DBconnect.py deleted file mode 100644 index b335654..0000000 --- a/App/Requests/db/DBconnect.py +++ /dev/null @@ -1,34 +0,0 @@ -import pymysql -from pymysql.err import * -class DBContextManager: - def __init__(self, db_config : dict): - self.db_config = db_config - self.connection = None - self.cursor = None - - def __enter__(self): - try: - self.connection = pymysql.connect( - host=self.db_config['host'], - port=self.db_config['port'], - user=self.db_config['user'], - password=self.db_config['password'], - db=self.db_config['db'] - ) - self.cursor = self.connection.cursor() - return self.cursor - except (OperationalError, KeyError) as err: - print(err.args) - return None - - def __exit__(self, exc_type, exc_val, exc_tb): - if self.connection and self.cursor: - if exc_type: - print(exc_type, '\n', exc_val) - self.connection.rollback() - else: - self.connection.commit() - self.cursor.close() - self.connection.close() - return True - \ No newline at end of file diff --git a/App/Requests/db/__init__.py b/App/Requests/db/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/App/Requests/db/select.py b/App/Requests/db/select.py deleted file mode 100644 index 22cc3e7..0000000 --- a/App/Requests/db/select.py +++ /dev/null @@ -1,12 +0,0 @@ -from .DBconnect import DBContextManager - -def select_list(db_config, sql) -> list: - with DBContextManager(db_config) as cursor: - if cursor is None: - raise ValueError("Cursor not created") - else: - cursor.execute(sql) - result = cursor.fetchall() - schema = [item[0] for item in cursor.description] - lst = [dict(zip(schema, row)) for row in result] - return lst \ No newline at end of file diff --git a/App/Requests/db/sql_provider.py b/App/Requests/db/sql_provider.py deleted file mode 100644 index a3a1855..0000000 --- a/App/Requests/db/sql_provider.py +++ /dev/null @@ -1,14 +0,0 @@ -import os -from string import Template - -class SQLProvider: - def __init__(self, file_path): - self.scripts = {} - for file in os.listdir(file_path): - _sql = open(f'{file_path}/{file}').read() - self.scripts[file] = Template(_sql) - - def get(self, name, params) -> dict: - if name not in self.scripts: - raise ValueError(f'SQL template {name} not found') - return self.scripts[name].substitute(**params) \ No newline at end of file diff --git a/App/Waybill/db/DBconnect.py b/App/Waybill/db/DBconnect.py deleted file mode 100644 index b335654..0000000 --- a/App/Waybill/db/DBconnect.py +++ /dev/null @@ -1,34 +0,0 @@ -import pymysql -from pymysql.err import * -class DBContextManager: - def __init__(self, db_config : dict): - self.db_config = db_config - self.connection = None - self.cursor = None - - def __enter__(self): - try: - self.connection = pymysql.connect( - host=self.db_config['host'], - port=self.db_config['port'], - user=self.db_config['user'], - password=self.db_config['password'], - db=self.db_config['db'] - ) - self.cursor = self.connection.cursor() - return self.cursor - except (OperationalError, KeyError) as err: - print(err.args) - return None - - def __exit__(self, exc_type, exc_val, exc_tb): - if self.connection and self.cursor: - if exc_type: - print(exc_type, '\n', exc_val) - self.connection.rollback() - else: - self.connection.commit() - self.cursor.close() - self.connection.close() - return True - \ No newline at end of file diff --git a/App/Waybill/db/__init__.py b/App/Waybill/db/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/App/Waybill/db/sql_provider.py b/App/Waybill/db/sql_provider.py deleted file mode 100644 index a3a1855..0000000 --- a/App/Waybill/db/sql_provider.py +++ /dev/null @@ -1,14 +0,0 @@ -import os -from string import Template - -class SQLProvider: - def __init__(self, file_path): - self.scripts = {} - for file in os.listdir(file_path): - _sql = open(f'{file_path}/{file}').read() - self.scripts[file] = Template(_sql) - - def get(self, name, params) -> dict: - if name not in self.scripts: - raise ValueError(f'SQL template {name} not found') - return self.scripts[name].substitute(**params) \ No newline at end of file diff --git a/App/Waybill/db/work.py b/App/Waybill/db/work.py deleted file mode 100644 index 5a7f039..0000000 --- a/App/Waybill/db/work.py +++ /dev/null @@ -1,16 +0,0 @@ -from .DBconnect import DBContextManager - -def select_list(db_config, sql) -> list: - with DBContextManager(db_config) as cursor: - if cursor is None: - raise ValueError("Cursor not created") - else: - cursor.execute(sql) - result = cursor.fetchall() - schema = [item[0] for item in cursor.description] - lst = [dict(zip(schema, row)) for row in result] - return lst - -def transaction(cursor, sql): - cursor.execute(sql) - return True \ No newline at end of file diff --git a/App/Waybill/cache/__init__.py b/App/cache/__init__.py similarity index 100% rename from App/Waybill/cache/__init__.py rename to App/cache/__init__.py diff --git a/App/Waybill/cache/wrapper.py b/App/cache/wrapper.py similarity index 96% rename from App/Waybill/cache/wrapper.py rename to App/cache/wrapper.py index ff38803..64b2343 100644 --- a/App/Waybill/cache/wrapper.py +++ b/App/cache/wrapper.py @@ -1,5 +1,5 @@ from functools import wraps -from Waybill.cache import RedisCache +from . import RedisCache def fetch_from_cache(cache_name: str, cache_config: dict): cache_conn = RedisCache(cache_config['redis']) diff --git a/App/data/config.json b/App/data/config.json index 8b7b193..0f0565b 100644 --- a/App/data/config.json +++ b/App/data/config.json @@ -3,5 +3,6 @@ "port": 3306, "user": "manager", "password": "ilikepizza", - "db": "sklad" + "db": "sklad", + "charset": "utf8" } \ No newline at end of file