diff --git a/application/config/config.yaml b/application/config/config.yaml index 86fc09f7bc51705b1de895d9c624378fafc78385..7f867b412fd2da08dca1df6f5be4d63ee0cb5e64 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -1,8 +1,10 @@ System: Env: public -MySQL: - User: root +Database: + Type: mysql + Driver: pymysql + Username: root Password: localhost123 Host: localhost Port: 3306 diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index 1e47102837636cf6cf059975fc0b7fedd58c1bdc..adc777ecc15d7f1bd3e26f56f2a7f7c07bebe3f3 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -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) diff --git a/application/utils/__init__.py b/application/utils/__init__.py index eb4d1d2cacdc4170133ad42b74646e43f997a78c..796c9b16afcb9027da3807a11d94c0323e7a2b55 100644 --- a/application/utils/__init__.py +++ b/application/utils/__init__.py @@ -10,5 +10,5 @@ # @Description : """ -from .dsn import dsn +from .dsn import dsn, DatabaseURI from .elasticsearch import ElasticsearchUtils diff --git a/application/utils/dsn/__init__.py b/application/utils/dsn/__init__.py index 57116622d6545692a031cc7ddfa1d7da138b7387..de7c835fc7b83fde0597ad1ea00ae276f9d0fd6f 100644 --- a/application/utils/dsn/__init__.py +++ b/application/utils/dsn/__init__.py @@ -10,4 +10,4 @@ # @Description : """ -from .dsn import dsn +from .dsn import DatabaseURI diff --git a/application/utils/dsn/dsn.py b/application/utils/dsn/dsn.py index 87937cfbde0865080e894f5ef14652ae84ae984f..13cc031272f7a6e4be9c2b60c4cc3198c5b526b9 100644 --- a/application/utils/dsn/dsn.py +++ b/application/utils/dsn/dsn.py @@ -10,18 +10,57 @@ # @Description : """ -from flask import Flask - -def dsn(app: Flask) -> str: +class DatabaseURI: """ - Initialize the MySQL dsn extension + The DatabaseURI class is a utility for generating SQLAlchemy database + connection strings for various types of databases. - :param app: flask.Flask application instance - :return: dsn + :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. """ - 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}') + + 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: + """ + Creates a SQLAlchemy engine string for the specified database. + + :return: A SQLAlchemy engine string. + """ + # 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"" + + def __str__(self): + return self.create()