Commit 663fbfb2 authored by 崔为之's avatar 崔为之 💪🏽

Update project

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