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

Update project

parent 08aaa17a
System: System:
Env: public Env: public
MySQL: Database:
User: root Type: mysql
Driver: pymysql
Username: root
Password: localhost123 Password: localhost123
Host: localhost Host: localhost
Port: 3306 Port: 3306
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
from flask import Flask from flask import Flask
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from application.utils import dsn from application.utils import DatabaseURI
db = SQLAlchemy() db = SQLAlchemy()
...@@ -25,5 +25,17 @@ def init_database(app: Flask) -> None: ...@@ -25,5 +25,17 @@ def init_database(app: Flask) -> None:
:param app: flask.Flask application instance :param app: flask.Flask application instance
:return: None :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) db.init_app(app)
...@@ -10,5 +10,5 @@ ...@@ -10,5 +10,5 @@
# @Description : # @Description :
""" """
from .dsn import dsn from .dsn import dsn, DatabaseURI
from .elasticsearch import ElasticsearchUtils from .elasticsearch import ElasticsearchUtils
...@@ -10,4 +10,4 @@ ...@@ -10,4 +10,4 @@
# @Description : # @Description :
""" """
from .dsn import dsn from .dsn import DatabaseURI
...@@ -10,18 +10,57 @@ ...@@ -10,18 +10,57 @@
# @Description : # @Description :
""" """
from flask import Flask
class DatabaseURI:
def dsn(app: Flask) -> str:
""" """
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 :param db_type: The type of the database. It could be "mysql", "postgresql", "oracle", "sqlite".
:return: dsn :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: def __init__(self,
raise KeyError('Key MySQL error') db_type: str,
return (f'mysql+pymysql://{app.config.MySQL.User}:' username: str = None,
f'{app.config.MySQL.Password}@{app.config.MySQL.Host}:{app.config.MySQL.Port}/{app.config.MySQL.DB}') 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"<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