Пример #1
0
def init_db(db_conn_str, debug=False):
    """This function is used for init db connection object by conn_str
    including mongodb/mysql/hive.
    """
    db_dict = {}
    if db_conn_str.startswith('mysql://'):
        db_dict['type'] = 'mysql'
        db_orm = ArmoryOrm()
        db_orm.init_conf({'DEBUG': debug, 'db': db_conn_str})
        db_dict['obj'] = db_orm
    elif db_conn_str.startswith('mongodb://'):
        db_dict['type'] = 'mongo'
        # here we construct route name by `host`-`port`-`db`
        # and inspect mongo conn string
        db_route = create_mongo_route(db_conn_str)
        ArmoryMongo.init_conf({db_route: db_conn_str})
        db_dict['obj'] = db_route
    elif db_conn_str.startswith('hive://'):
        db_dict['type'] = 'hive'
        hive_orm = ArmoryHive(db_conn_str)
        hive_orm.hiveOpen()
        db_dict['obj'] = hive_orm
    else:
        # here we can add other db obj later
        db_dict['type'] = 'unknown'
        db_dict['obj'] = None

    return db_dict