How to use the aiosql.patterns.var_pattern function in aiosql

To help you get started, we’ve selected a few aiosql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nackjicholson / aiosql / tests / test_patterns.py View on Github external
def test_var_pattern_does_not_require_semicolon_trail():
    """Make sure keywords ending queries are recognized even without
    semi-colons.
    """
    sql = """
        select a,
               b,
               c
          FROM foo
         WHERE a = :a"""

    groupdicts = [m.groupdict() for m in var_pattern.finditer(sql)]
    assert len(groupdicts) == 1

    expected = {"dblquote": None, "lead": " ", "quote": None, "trail": "", "var_name": "a"}
    assert groupdicts[0] == expected
github nackjicholson / aiosql / tests / test_patterns.py View on Github external
def test_var_pattern_is_quote_aware():
    sql = """
          select foo_id,
                 bar_id,
                 to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SSOF')
            from foos
            join bars using(bar_id)
            join bazs using(baz_id)
           where created_at < :created_at_mark
             and foo_mark > :foo_mark
        order by created_at desc, source_name asc;
    """
    groupdicts = [m.groupdict() for m in var_pattern.finditer(sql)]
    assert len(groupdicts) == 3

    expected = [
        {
            "dblquote": None,
            "lead": None,
            "quote": "'YYYY-MM-DD\"T\"HH24:MI:SSOF'",
            "trail": None,
            "var_name": None,
        },
        {
            "dblquote": None,
            "lead": " ",
            "quote": None,
            "trail": "\n",
            "var_name": "created_at_mark",
github nackjicholson / aiosql / aiosql / adapters / asyncpg.py View on Github external
def process_sql(self, query_name, _op_type, sql):
        count = 0
        adj = 0

        for match in var_pattern.finditer(sql):
            gd = match.groupdict()
            # Do nothing if the match is found within quotes.
            if gd["dblquote"] is not None or gd["quote"] is not None:
                continue

            var_name = gd["var_name"]
            if var_name in self.var_replacements[query_name]:
                replacement = f"${self.var_replacements[query_name][var_name]}"
            else:
                count += 1
                replacement = f"${count}"
                self.var_replacements[query_name][var_name] = count

            start = match.start() + len(gd["lead"]) + adj
            end = match.end() - len(gd["trail"]) + adj