diff --git a/application/utils/dsn/sql.py b/application/utils/dsn/sql.py index ef2d914439feca0dacae9e5fb9d63331e0d6b6ab..33027bcbd870d9ba17f0b21eee59dc29453e0963 100644 --- a/application/utils/dsn/sql.py +++ b/application/utils/dsn/sql.py @@ -7,33 +7,25 @@ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/29 00:36 # @File : sql.py -# @Description : +# @Description : Defines the DatabaseUri class for generating SQLAlchemy database connection strings. """ class DatabaseUri: """ - The DatabaseURI class is a utility for generating SQLAlchemy database - connection strings for various types of databases. + A utility class to generate 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. + Args: + db_type (str): The type of the database. It could be "mysql", "postgresql", "oracle", "sqlite". + username (str, optional): The username of the database. Not required for "sqlite". + password (str, optional): The password of the database. Not required for "sqlite". + host (str, optional): The host of the database. Not required for "sqlite". + port (str, optional): The port of the database. Not required for "sqlite". + db (str, optional): The database name. For "sqlite", it is the path to the database file. + driver (str, optional): The python library to connect to the database. Not required but could be provided. """ - - 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: + 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 @@ -46,9 +38,10 @@ class DatabaseUri: """ Creates a SQLAlchemy engine string for the specified database. - :return: A SQLAlchemy engine string. + Returns: + str: A SQLAlchemy engine string. """ - # SQLite database only require a path to the database file + # SQLite database only requires a path to the database file if self.db_type == 'sqlite': return f'{self.db_type}:///{self.db}' else: @@ -60,7 +53,19 @@ class DatabaseUri: return f'{self.db_type}://{self.username}:{self.password}@{self.host}:{self.port}/{self.db}' def __repr__(self): + """ + Returns a formal string representation of the DatabaseUri object. + + Returns: + str: The formal string representation of the DatabaseUri object. + """ return f'' def __str__(self): + """ + Returns a string representation of the DatabaseUri object, which is the SQLAlchemy engine string. + + Returns: + str: The string representation of the DatabaseUri object. + """ return self.create()