Skip to content

Extensions

miniset.extensions

SqlExtension

Bases: Extension

SQL extension for Jinja2

extract_param_name(tokens)

Extract param names

Parameters:

Name Type Description Default
tokens list[Token]

Tokens

required

Returns:

Name Type Description
str str

Param name

Source code in miniset/extensions.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def extract_param_name(self, tokens: list[Token]) -> str:
    """Extract param names

    Args:
        tokens (list[Token]): Tokens

    Returns:
        str: Param name
    """
    name: str = ""

    for token in tokens:
        if token.test("variable_begin"):
            continue

        if token.test("name") or token.test("dot"):
            name += token.value

        break

    if not name:
        name = "bind#0"

    return name

filter_stream(stream)

Convert {{ some.variable | filter1 | filter 2 }} to {{ ( some.variable | filter1 | filter 2 ) | bind }} for all variable declarations in the template

Note the extra ( and ). We want the | bind to apply to the entire value, not just the last value. The parentheses are mostly redundant, except in expressions like {{ '%' ~ myval ~ '%' }}

This function is called by Jinja2 immediately after the lexing stage, but before the parser is called.

Parameters:

Name Type Description Default
stream TokenStream

Token stream

required

Yields:

Type Description
Token

Generator[Token, None, None]: Converted token stream

Source code in miniset/extensions.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def filter_stream(self, stream: TokenStream) -> Generator[Token, None, None]:
    """Convert `{{ some.variable | filter1 | filter 2 }}` to `{{ ( some.variable | filter1 | filter 2 ) | bind }}` for all variable declarations in the template

    Note the extra ( and ). We want the | bind to apply to the entire value, not just the last value.
    The parentheses are mostly redundant, except in expressions like `{{ '%' ~ myval ~ '%' }}`

    This function is called by Jinja2 immediately after the lexing stage, but before the parser is called.

    Args:
        stream (TokenStream): Token stream

    Yields:
        Generator[Token, None, None]: Converted token stream
    """

    while not stream.eos:
        token = next(stream)
        if token.test("variable_begin"):
            var_expr: list[Token] = []

            while not token.test("variable_end"):
                var_expr.append(token)
                token = next(stream)

            variable_end = token

            last_token = var_expr[-1]
            lineno = last_token.lineno
            # don't bind twice
            if not last_token.test("name") or last_token.value not in (
                "bind",
                "where_in",
                "sql_safe",
            ):
                param_name = self.extract_param_name(var_expr)

                var_expr.insert(1, Token(lineno, "lparen", "("))
                var_expr.append(Token(lineno, "rparen", ")"))
                var_expr.append(Token(lineno, "pipe", "|"))
                var_expr.append(Token(lineno, "name", "bind"))
                var_expr.append(Token(lineno, "lparen", "("))
                var_expr.append(Token(lineno, "string", param_name))
                var_expr.append(Token(lineno, "rparen", ")"))

            var_expr.append(variable_end)

            for token in var_expr:
                yield token
        else:
            yield token