Skip to content

SQL render

get_sql_from_file(filepath, jinja_args=None, **kwargs)

Read in an SQL file and inject arguments with Jinja (if given params). Returns the SQL as a str.

Parameters:

Name Type Description Default
filepath str

A filepath to your SQL file.

required
jinja_args dict

If not None, will pass the read in SQL file through a jinja template to render the template. Otherwise will just return the SQL file as is. Defaults to None.

None
kwargs

passed to the open() call.

{}
Source code in pydbtools/_sql_render.py
def get_sql_from_file(filepath: str, jinja_args: dict = None, **kwargs) -> str:
    """
    Read in an SQL file and inject arguments with Jinja (if given params).
    Returns the SQL as a str.

    Args:
        filepath (str): A filepath to your SQL file.
        jinja_args (dict, optional): If not None, will pass the read
            in SQL file through a jinja template to render the template.
            Otherwise will just return the SQL file as is. Defaults to None.
        kwargs: passed to the open() call.
    """
    with open(filepath, **kwargs) as f:
        sql = "".join(f.readlines())
    if jinja_args:
        sql = render_sql_template(sql, jinja_args)
    return sql

render_sql_template(sql, jinja_args)

Takes a SQL file templated with Jinja and then injects arguments. Returns the injected SQL.

Parameters:

Name Type Description Default
sql_file str

Path to SQL file

required
args dict

Arguments that is referenced in the SQL file

required

Returns:

Name Type Description
str str

SQL string that has args rendered into it

Source code in pydbtools/_sql_render.py
def render_sql_template(sql: str, jinja_args: dict) -> str:
    """
    Takes a SQL file templated with Jinja and then injects arguments.
    Returns the injected SQL.

    Args:
        sql_file (str): Path to SQL file
        args (dict): Arguments that is referenced in the SQL file

    Returns:
        str: SQL string that has args rendered into it
    """
    return Template(sql).render(**jinja_args)