Skip to content

rustmy

rustmy is a MySQL and MariaDB driver written in Rust, with PyMySQL's %s and %(name)s placeholders. SQL[T] is a str subclass, so rustmy runs it as is; sqlty.drivers.rustmy also carries T through to the rows: the type checker sees T, and at runtime the helpers build T.

Install the optional dependency:

pip install "sqlty[rustmy]"

sqlty checks the queries with dialect = "mysql", which reads -- sql as the marker: MySQL takes -- as a comment only when a space follows it.

[tool.sqlty]
dialect = "mysql"
schema = "schema.sql"

schema.sql:

CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(255));

execute

execute(conn, query, params=None) runs query: SQL[T] on a new cursor and returns its rows as T:

from dataclasses import dataclass

import rustmy
from sqlty import SQL
from sqlty.drivers.rustmy import execute


@dataclass
class User:
    id: int
    name: str


by_name = SQL[User]("-- sql\nSELECT id, name FROM users WHERE name = %s")

with rustmy.connect(host="localhost", database="app") as conn:
    users = execute(conn, by_name, ("ada",)).fetchall()  # list[User]
    first = execute(conn, by_name, ("ada",)).fetchone()  # User | None
# error:
Open in playground

sqlty check makes sure User matches the columns before the code runs (see Row types), and that the call passes the query's parameters. The rows are built from the query's runtime row type (row_form):

T Cursor Row
tuple[...] Cursor the tuple
TypedDict, dict[...] DictCursor the dict
Protocol Cursor a named tuple of the columns
any other class (dataclass, NamedTuple, Pydantic model) DictCursor T(**row)

A query without a runtime row type, such as a plain string, yields tuples. The returned Rows[T] has fetchone, fetchmany, fetchall and iteration; its cursor is rustmy's, for rowcount and lastrowid.

With typed parameters, SQL[T, P], pyright and ty check params against P; query.bind(params) checks them on every type checker:

from typing import TypedDict

import rustmy
from sqlty import SQL
from sqlty.drivers.rustmy import execute, scalar


class ById(TypedDict):
    id: int


name_by_id = SQL[tuple[str], ById]("-- sql\nSELECT name FROM users WHERE id = %(id)s")

with rustmy.connect(host="localhost", database="app") as conn:
    row = execute(conn, name_by_id, {"id": 1}).fetchone()  # tuple[str] | None
    name = scalar(conn, name_by_id.bind({"id": 1}))  # str
# error:
Open in playground

scalar

scalar(conn, query, [form,] params=None) returns the only column of the first row, typed from query: SQL[tuple[T]] or from form, a PEP 747 TypeForm. A query that returns no rows raises LookupError, so None always means SQL NULL. A single value after the query is its parameters unless it is a type, as for the cursor: scalar(conn, query, user_id).

import rustmy
from sqlty.drivers.rustmy import scalar

with rustmy.connect(host="localhost", database="app") as conn:
    count = scalar(conn, "-- sql\nSELECT COUNT(*) FROM users", int)
Open in playground

asyncio

execute_async and scalar_async take a rustmy.aio connection, such as one a pool lends:

from dataclasses import dataclass

from rustmy import aio
from sqlty import SQL
from sqlty.drivers.rustmy import execute_async


@dataclass
class User:
    id: int
    name: str


everyone = SQL[User]("-- sql\nSELECT id, name FROM users")


async def users(pool: aio.Pool) -> list[User]:
    async with pool.acquire() as conn:
        rows = await execute_async(conn, everyone)
        return await rows.fetchall()
# error:
Open in playground

Without the helpers

Any rustmy cursor accepts SQL[T], and sqlty check still checks its parameters (Driver calls). You lose only the typed rows.