Skip to content

[Python] Generate Python database connector from data source string #806

@tonyyang-svail

Description

@tonyyang-svail

To avoid all the code generator having the same database connection logic, we may have a Python function like

def connect_with_data_source(data_source):
    driver, source = data_source.split("://")
    if driver != "mysql":
        raise ValueError("connect_with_data_source doesn't support driver type {}".format(driver))

    if driver == "mysql":
        user, passwd, _, host, port = re.findall("^(\w*):(\w*)@(\w*)\((.*):(.*)\)/?.*$", source)[0]
        from MySQLdb import connect
        conn = connect(user=user,
                       passwd=passwd,
                       host=host,
                       port=int(port))
    conn.driver = driver
    return conn

@weiguoz @Yancey1989 What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions