How to use aiosql - 10 common examples

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_asyncpg.py View on Github external
def queries():
    dir_path = Path(__file__).parent / "blogdb/sql"
    return aiosql.from_path(dir_path, "asyncpg")
github nackjicholson / aiosql / tests / test_sqlite3.py View on Github external
def queries():
    p = Path(__file__).parent / "blogdb" / "sql"
    return aiosql.from_path(p, "sqlite3")
github nackjicholson / aiosql / tests / test_aiosqlite.py View on Github external
def queries():
    dir_path = Path(__file__).parent / "blogdb/sql"
    return aiosql.from_path(dir_path, "aiosqlite")
github nackjicholson / aiosql / tests / test_psycopg2.py View on Github external
def queries():
    dir_path = Path(__file__).parent / "blogdb" / "sql"
    return aiosql.from_path(dir_path, "psycopg2")
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 / aiosql.py View on Github external
def load_queries_from_sql(sql, driver_adapter):
    queries = []
    for query_text in query_name_definition_pattern.split(sql):
        if not empty_pattern.match(query_text):
            for method_pair in load_methods(query_text, driver_adapter):
                queries.append(method_pair)
    return queries
github nackjicholson / aiosql / aiosql / aiosql.py View on Github external
def load_queries_from_sql(sql, driver_adapter):
    queries = []
    for query_text in query_name_definition_pattern.split(sql):
        if not empty_pattern.match(query_text):
            for method_pair in load_methods(query_text, driver_adapter):
                queries.append(method_pair)
    return queries
github nackjicholson / aiosql / example / example.py View on Github external
class UserBlog(NamedTuple):
    blogid: int
    author: str
    title: str
    published: datetime

    def __post_init__(self):
        self.published = datetime.strptime(self.publised, "%Y-%m-%d %H:%M")


dir_path = Path(__file__).parent
sql_path = dir_path / "sql"
db_path = dir_path / "exampleblog.db"
queries = aiosql.from_path(
    dir_path / "sql", "sqlite3", record_classes={"User": User, "UserBlog": UserBlog}
)


users = [("bobsmith", "Bob", "Smith"), ("johndoe", "John", "Doe"), ("janedoe", "Jane", "Doe")]
blogs = [
    (
        1,
        "What I did Today",
        """\
I mowed the lawn, washed some clothes, and ate a burger.

Until next time,
Bob""",
        "2017-07-28",
    ),
github nackjicholson / aiosql / aiosql / aiosql.py View on Github external
def _recurse_load_queries(path):
        queries = Queries()
        for p in path.iterdir():
            if p.is_file() and p.suffix != ".sql":
                continue
            elif p.is_file() and p.suffix == ".sql":
                for query_name, fn in load_queries_from_file(p, driver_adapter):
                    queries.add_query(query_name, fn)
            elif p.is_dir():
                child_name = p.relative_to(dir_path).name
                child_queries = _recurse_load_queries(p)
                queries.add_child_queries(child_name, child_queries)
            else:
                # This should be practically unreachable.
                raise SQLLoadException(f"The path must be a directory or file, got {p}")
        return queries