API

aiosql.from_path

aiosql.from_path(sql_path, driver_adapter, record_classes=None, *, loader_cls=, queries_cls=)

Load queries from a .sql file, or directory of .sql files.

Parameters:

  • sql_path - Path to a .sql file or directory containing .sql files.
  • driver_adapter - Either a string to designate one of the aiosql built-in database driver adapters. One of "sqlite3", "psycopg2", "aiosqlite", or "asyncpg". If you have defined your own adapter class, you may pass its constructor.
  • record_classes - (optional) DEPRECATED Mapping of strings used in "record_class" declarations to the python classes which aiosql should use when marshaling SQL results.
  • loader_cls - (optional) Custom constructor for QueryLoader extensions.
  • queries_cls - (optional) Custom constructor for Queries extensions.

Returns: Queries

Usage:

>>> queries = aiosql.from_path("./sql", "pscycopg2")
>>> queries = aiosql.from_path("./sql", MyDBAdapter)

aiosql.from_str

aiosql.from_str(sql, driver_adapter, record_classes=None, *, loader_cls=, queries_cls=)

Load queries from a SQL string.

Parameters:

  • sql - A string containing SQL statements and aiosql name.
  • driver_adapter - Either a string to designate one of the aiosql built-in database driver adapters. One of "sqlite3", "psycopg2", "aiosqlite", or "asyncpg". If you have defined your own adapter class, you can pass it's constructor.
  • record_classes - (optional) DEPRECATED Mapping of strings used in "record_class" declarations to the python classes which aiosql should use when marshaling SQL results.
  • loader_cls - (optional) Custom constructor for QueryLoader extensions.
  • queries_cls - (optional) Custom constructor for Queries extensions.

Returns: Queries

Usage:

Loading queries from a SQL string.

import sqlite3
import aiosql

sql_text = """
-- name: get-all-greetings
-- Get all the greetings in the database
select * from greetings;

-- name: get-user-by-username^
-- Get all the users from the database,
-- and return it as a dict
select * from users where username = :username;
"""

queries = aiosql.from_str(sql_text, "sqlite3")
queries.get_all_greetings(conn)
queries.get_user_by_username(conn, username="willvaughn")