Ejemplo n.º 1
0
def read_from_db(datasource, sql):
    if sql is None:
        raise_runtime_error('sql is required parameter')

    with DbEngine(**datasource) as engine:
        df = pd.read_sql_query(sql, engine)
        util.validate_column_name(df)
        return {'table': df}
Ejemplo n.º 2
0
def read_from_db(datasource, sql):
    if sql is None:
        raise_runtime_error('sql is required parameter')
    import re
    sqlToken = re.sub(' +', ' ',
                      sql.lower().replace("(",
                                          " ( ").replace(")", " ) ")).replace(
                                              ". ", ".").split(" ")
    for i in range(len(sqlToken)):
        if sqlToken[i] == 'from':
            tmp_token = sqlToken[i + 1].split('.')
            if len(tmp_token) == 2 and tmp_token[1] in sys_table_lists:
                raise Exception(
                    'Cannot access system tables from Brightics: {}'.format(
                        sqlToken[i + 1]))
    with DbEngine(**datasource) as engine:
        df = pd.read_sql_query(sql, engine)
        util.validate_column_name(df)
        return {'table': df}
Ejemplo n.º 3
0
def write_to_db(table, tableName, datasource, ifExists='fail'):
    if not isinstance(table, pd.DataFrame):
        raise Exception('table is not pandas.DataFrame')

    with DbEngine(**datasource) as engine:
        table.to_sql(tableName, engine, if_exists=ifExists, index=False)