sql.py 2.76 KB
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
1 2 3 4 5 6 7 8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/29 00:36
崔为之's avatar
崔为之 committed
9
# @File        : sql.py
崔为之's avatar
崔为之 committed
10
# @Description : Defines the DatabaseUri class for generating SQLAlchemy database connection strings.
Weizhi Cui's avatar
Weizhi Cui committed
11 12 13
"""


崔为之's avatar
崔为之 committed
14
class DatabaseUri:
Weizhi Cui's avatar
Weizhi Cui committed
15
    """
崔为之's avatar
崔为之 committed
16
    A utility class to generate SQLAlchemy database connection strings for various types of databases.
Weizhi Cui's avatar
Weizhi Cui committed
17

崔为之's avatar
崔为之 committed
18 19 20 21 22 23 24 25
    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.
Weizhi Cui's avatar
Weizhi Cui committed
26
    """
崔为之's avatar
崔为之 committed
27 28
    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:
崔为之's avatar
崔为之 committed
29 30 31 32 33 34 35 36 37 38 39 40
        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.

崔为之's avatar
崔为之 committed
41 42
        Returns:
            str: A SQLAlchemy engine string.
崔为之's avatar
崔为之 committed
43
        """
崔为之's avatar
崔为之 committed
44
        # SQLite database only requires a path to the database file
崔为之's avatar
崔为之 committed
45 46 47 48 49 50 51 52 53 54 55
        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):
崔为之's avatar
崔为之 committed
56 57 58 59 60 61
        """
        Returns a formal string representation of the DatabaseUri object.

        Returns:
            str: The formal string representation of the DatabaseUri object.
        """
崔为之's avatar
崔为之 committed
62
        return f'<DatabaseUri({self.db_type})>'
崔为之's avatar
崔为之 committed
63 64

    def __str__(self):
崔为之's avatar
崔为之 committed
65 66 67 68 69 70
        """
        Returns a string representation of the DatabaseUri object, which is the SQLAlchemy engine string.

        Returns:
            str: The string representation of the DatabaseUri object.
        """
崔为之's avatar
崔为之 committed
71
        return self.create()