db module for requests
This commit is contained in:
34
App/Requests/db/DBconnect.py
Normal file
34
App/Requests/db/DBconnect.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
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
|
||||||
|
|
||||||
0
App/Requests/db/__init__.py
Normal file
0
App/Requests/db/__init__.py
Normal file
12
App/Requests/db/select.py
Normal file
12
App/Requests/db/select.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
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
|
||||||
14
App/Requests/db/sql_provider.py
Normal file
14
App/Requests/db/sql_provider.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
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)
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from Database.select import select_list
|
from db.select import select_list
|
||||||
from Database.sql_provider import SQLProvider
|
from db.sql_provider import SQLProvider
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user