Skip to main content

Typed Python SQLite/PostgreSQL query builder.

Project description

Artifex

Typed Python SQLite/PostgreSQL query builder.

This is in early development and may see massive refactor.

Usage

Use artifex functions delete, insert_into, select, update to build a query. Then call prepare() to get a tuple comprising a list of parameters used in the query and the query itself. Anywhere a parameter was used in the query will be replaced with ?, this way the database can handle param binding.

Black Formatted Example

from sql_artifex import Column, Table, and_, join, select


class User(Table):
    id = Column()
    first = Column()
    last = Column()


class Note(Table):
    id = Column()
    user_id = Column()
    value = Column()


sql, params = (
    select(
        User.id,
        User.first,
        User.last,
        Note.id.as_("note_id"),
        Note.value,
    )
    .from_(
        User,
        join(Note).on(Note.user_id == User.id),
    )
    .where(
        User.id == 1,
        and_(Note.value == "test_note"),
    )
    .order_by(
        Note.value.desc(),
    )
).prepare()

assert params == [1, "test_note"]
expected = " ".join(
    [
        "SELECT",
        '"user".id,',
        '"user".first,',
        '"user".last,',
        '"note".id AS note_id,',
        '"note".value',
        'FROM "user"',
        'JOIN "note" ON "note".user_id = "user".id',
        'WHERE "user".id = ? AND "note".value = ?',
        'ORDER BY "note".value DESC',
    ]
)
assert sql == expected

Create Table

from sql_artifex import Column, Table
from sql_artifex.types import TEXT, TIMESTAMP, UUID


class User(Table):
    id = Column(UUID(), primary=True)
    email = Column(TEXT(), unique=True, index=True)
    first = Column(TEXT())
    last = Column(TEXT())
    verified_at = Column(TIMESTAMP(with_time_zone=True))


User.table_set_unique_together(User.fist, User.last)
print(User.table_get_create())

Prints:

CREATE TABLE IF NOT EXISTS "user" (
  "id" UUID PRIMARY KEY,
  "email" TEXT NOT NULL UNIQUE,
  "first" TEXT NOT NULL,
  "last" TEXT NOT NULL,
  "verified_at" TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE INDEX email_idx ON "user" (email);

Support the Developer

Buy Me a Coffee

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

sql_artifex-0.61.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

sql_artifex-0.61.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file sql_artifex-0.61.0.tar.gz.

File metadata

  • Download URL: sql_artifex-0.61.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sql_artifex-0.61.0.tar.gz
Algorithm Hash digest
SHA256 f08487f7ee28d9755d0cca548c6a33f7fd9d0a0e47b4e5bb48ad0d9ec91879dd
MD5 8e216bcce078570610e530f49b6233f4
BLAKE2b-256 44c5119eb9000543a2d33e51ad04b420253a3c05ea175e1099e6d6368aa696b8

See more details on using hashes here.

File details

Details for the file sql_artifex-0.61.0-py3-none-any.whl.

File metadata

  • Download URL: sql_artifex-0.61.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sql_artifex-0.61.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ba6e5c4a7163343c51e22a818ad090d8f1b478c0b2fa07dbde91dfb413021e6
MD5 e257ca23ec21fc8618ec91ddf2ce399e
BLAKE2b-256 76aaa5a11560572f82f130185d1d491767d24d99d9338cf0ec841d472aa9ff39

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