Skip to main content

Simple SQL in Python.

Project description

aiosql

Simple SQL in Python

Based on the wonderful anosql library, which is in turn based on the excellent clojure library Yesql by Kris Jenkins.

Exciting features of aiosql:

  1. Out of the box support for asyncio based database drivers like aiosqlite and asyncpg.
  2. Load nested directories .sql files.
  3. Easy extension to accommodate custom database drivers.

Install

TODO

Usage

Basics

Given a greetings.sql file:

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

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

With the stdlib sqlite3 driver built into python you can use this sql file:

import sqlite3
import aiosql

queries = aiosql.from_path("greetings.sql", db_driver="sqlite3")
conn = sqlite3.connect("greetings.db")

# greetings = [(1, "Hi"), (2, "Aloha"), (3, "Hola")]
# users = [{"user_id": 1, "username": "willvaughn", "name": "Will"}]
greetings = queries.get_greetings(conn)
users = queries.get_users_by_username(conn, username="willvaughn")


name = users[0]["name"]
for _, greeting in greetings:
    print(f"{greeting}, {name}!")

# Hi, Will!
# Aloha, Will!
# Hola, Will!

To do this in an asyncio environment you can use the aiosqlite driver.

import asyncio

import aiosql
import aiosqlite


async def main():
    queries = aiosql.from_path("greetings.sql", db_driver="aiosqlite")

    # Parallel queries!!!
    with async aiosqlite.connect("greetings.db") as conn:
        greetings, users = await asyncio.gather(
            queries.get_all_greetings(conn),
            queries.get_users_by_username(conn, username="willvaughn")
        )
        
    # greetings = [(1, "Hi"), (2, "Aloha"), (3, "Hola")]
    # users = [{"user_id": 1, "username": "willvaughn", "name": "Will"}]
    name = users[0]["name"]
    for _, greeting in greetings:
        print(f"{greeting}, {name}!")
        
    # Hi, Will!
    # Aloha, Will!
    # Hola, Will!
    

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiosql-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiosql-0.1.1-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file aiosql-0.1.1.tar.gz.

File metadata

  • Download URL: aiosql-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.9 CPython/3.7.1 Darwin/18.2.0

File hashes

Hashes for aiosql-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa709b65ebf3ed74208773c479015709ca32df014193c985a81b6642e5319578
MD5 271de3ca90c93970c91786251087559e
BLAKE2b-256 6e7796211f82d676477dae5214159565452d51a27ca0d9a6e54cb8a14a6d9bdb

See more details on using hashes here.

File details

Details for the file aiosql-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: aiosql-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.9 CPython/3.7.1 Darwin/18.2.0

File hashes

Hashes for aiosql-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d111aec8ceb52f4575eba43e568d0c00b3e238a24c97e2a7540d184c5071453
MD5 0f77e8b4b66be0213cc06f86a935e7d8
BLAKE2b-256 e0b19c76701d48ccdb43df79de24194602828ba13c58ec171129e2899e5fb4de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page