Skip to main content

Simple SQL in Python.

Project description

aiosql

Simple SQL in Python.

SQL is code, you should be able to write it, version control it, comment on it, and use it in database tools like psql as you would any other SQL. But, you also want to be able to use it from your python applications, and that's where aiosql can help. With aiosql you can organize your SQL statements in .sql files and load them into a python object as methods to call.

This project supports sync and asyncio based drivers for SQLite (sqlite3, aiosqlite) and PostgreSQL (psycopg2, asyncpg) out of the box, and can be extended to support other database drivers by you! The asyncio support restricts this package to python versions >3.6. If you are using older versions of python please see the related anosql package which this project is based on.

Install

pip install aiosql

Or if you you use poetry:

poetry add aiosql

Getting Started

Basic Usage

Given you have a SQL file like the one below called users.sql

-- name: get-all-users
-- Get all user records
select * from users;


-- name: get-user-by-username
-- Get user with the given username field.
select userid,
       username,
       firstname,
       lastname
  from users
 where username = :username;

You can use aiosql to load the queries in this file for use in your Python application:

import aiosql
import sqlite3

conn = sqlite3.connect("myapp.db")
queries = aiosql.from_path("users.sql", "sqlite3")

users = queries.get_all_users(conn)
# >>> [(1, "nackjicholson", "William", "Vaughn"), (2, "johndoe", "John", "Doe"), ...]

users = queries.get_user_by_username(conn, username="nackjicholson")
# >>> [(1, "nackjicholson", "William", "Vaughn")

This is pretty nice, we're able to define our methods in SQL and use them as methods from python!

Query Operators to define different types of SQL actions

aiosql can help you do even more by allowing you to declare in the SQL how you would like a query to be executed and returned in python. For instance, the get-user-by-username query above should really only return a single result instead of a list containing one user. With the raw sqlite3 driver in python we would probably have used cur.fetchone() instead of cur.fetchall() to retrieve a single row. We can inform aiosql to select a single row by using the ^ (select one) operator on the end of our query name.

-- name: get-user-by-username^
-- Get user with the given username field.
select userid,
       username,
       firstname,
       lastname
  from users
 where username = :username;
nack = queries.get_user_by_username(conn, username="nackjicholson")
# >>> (1, "nackjicholson", "William", "Vaughn")

Using your own python types for SQL data.

By declaring a record_class directive in our SQL file we can inform aiosql to automatically marshal our data to a custom class we've defined in python. In python3.7 a good choice for this is the new dataclass package.

-- name: get-user-by-username^
-- record_class: User
-- Get user with the given username field.
select userid,
       username,
       firstname,
       lastname
  from users
 where username = :username;

All we have to do is provide our custom type to aiosql when we load our queries via the record_classes argument.

import aiosql
import sqlite3
from dataclasses import dataclass


@dataclass
class User:
    userid: int
    username: str
    firstname: str
    lastname: str


conn = sqlite3.connect("myapp.db")
queries = aiosql.from_path("users.sql", "sqlite3", record_classes={"User": User})

nack = queries.get_user_by_username(conn, username="nackjicholson")
# >>> User(userid=1, username="nackjicholson", firstname="William", lastname="Vaughn")

Hopefully this is enough to intrigue you and entice you to give aiosql a try. Check the documentation site for more information, and more features. Happy SQLing!

Documentation

Project and API docs https://nackjicholson.github.io/aiosql

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-3.0.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

aiosql-3.0.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiosql-3.0.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.13 CPython/3.7.1 Darwin/18.5.0

File hashes

Hashes for aiosql-3.0.0.tar.gz
Algorithm Hash digest
SHA256 9256d6825ed958cfe70c4af483a05c220fbbdbfbdd237122325dd853ccc1569b
MD5 da34dcaa8a35d6ba0ffb42c2aa6bb935
BLAKE2b-256 55861a699186cdeb97952aeadc94a70c680093f1b27e2bcc0e64bf747b407e23

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiosql-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.13 CPython/3.7.1 Darwin/18.5.0

File hashes

Hashes for aiosql-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7842de7e4ab2e3aa20c0a721c7a173e2cfdbea6197d0e24a0261061413b3ea1
MD5 0c3a51e9679cb66f01113e9c55b21ee3
BLAKE2b-256 bda48d539e82d977b495c1d3929967d33d459bf1529f1f9743b18aa4360070d8

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