Commit 1c58b6e6 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 08aaa17a
System:
Env: public
MySQL:
User: root
Database:
Type: mysql
Driver: pymysql
Username: root
Password: localhost123
Host: localhost
Port: 3306
......
......@@ -13,7 +13,7 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from application.utils import dsn
from application.utils import DatabaseURI
db = SQLAlchemy()
......@@ -25,5 +25,17 @@ def init_database(app: Flask) -> None:
:param app: flask.Flask application instance
:return: None
"""
app.config.setdefault('SQLALCHEMY_DATABASE_URI', dsn(app))
cfg = app.config.get('Database')
if cfg is None:
raise KeyError('Key Database error')
uri = DatabaseURI(
db_type=cfg.Type,
username=cfg.Username,
password=cfg.Password,
host=cfg.Host,
port=cfg.Port,
db=cfg.DB,
driver=cfg.Driver,
)
app.config.setdefault('SQLALCHEMY_DATABASE_URI', uri.create())
db.init_app(app)
......@@ -10,5 +10,5 @@
# @Description :
"""
from .dsn import dsn
from .dsn import dsn, DatabaseURI
from .elasticsearch import ElasticsearchUtils
......@@ -10,4 +10,4 @@
# @Description :
"""
from .dsn import dsn
from .dsn import DatabaseURI
......@@ -10,18 +10,57 @@
# @Description :
"""
from flask import Flask
class DatabaseURI:
"""
The DatabaseURI class is a utility for generating SQLAlchemy database
connection strings for various types of databases.
:param db_type: The type of the database. It could be "mysql", "postgresql", "oracle", "sqlite".
:param username: The username of the database. Not required for "sqlite".
:param password: The password of the database. Not required for "sqlite".
:param host: The host of the database. Not required for "sqlite".
:param port: The port of the database. Not required for "sqlite".
:param db: The database name. For "sqlite", it is the path to the database file.
:param driver: The python library to connect to the database. Not required but could be provided.
"""
def dsn(app: Flask) -> str:
def __init__(self,
db_type: str,
username: str = None,
password: str = None,
host: str = None,
port: str = None,
db: str = None,
driver: str = None
) -> None:
self.db_type = db_type
self.username = username
self.password = password
self.host = host
self.port = port
self.db = db
self.driver = driver
def create(self) -> str:
"""
Initialize the MySQL dsn extension
Creates a SQLAlchemy engine string for the specified database.
:param app: flask.Flask application instance
:return: dsn
:return: A SQLAlchemy engine string.
"""
mysql_cfg = app.config.get('MySQL')
if mysql_cfg is None:
raise KeyError('Key MySQL error')
return (f'mysql+pymysql://{app.config.MySQL.User}:'
f'{app.config.MySQL.Password}@{app.config.MySQL.Host}:{app.config.MySQL.Port}/{app.config.MySQL.DB}')
# SQLite database only require a path to the database file
if self.db_type == 'sqlite':
return f'{self.db_type}:///{self.db}'
else:
# Other databases require a full connection string with optional driver
if self.driver:
return (f'{self.db_type}+{self.driver}://'
f'{self.username}:{self.password}@{self.host}:{self.port}/{self.db}')
else:
return f'{self.db_type}://{self.username}:{self.password}@{self.host}:{self.port}/{self.db}'
def __repr__(self):
return f"<DatabaseURI({self.db_type})>"
def __str__(self):
return self.create()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment