Skip to main content

Typed Python PostgreSQL query builder.

Project description

Artifex

Typed Python 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))


print(User.create_table())

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.60.0.tar.gz (13.3 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.60.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sql_artifex-0.60.0.tar.gz
  • Upload date:
  • Size: 13.3 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.60.0.tar.gz
Algorithm Hash digest
SHA256 c134df97b457dde6d9510c3b1dab085c110d28bfd5185731e67fd18d30918a28
MD5 fded3b87eb0b6cacab4dd08444846a9a
BLAKE2b-256 374f2c539edb3e8275cf52cc04fffe23b3e28fe384ae40d04d96ac738bf580a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sql_artifex-0.60.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 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.60.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c90de28555f57af5df20f29d0b5eca23c1d609cd75f7309f558ee434408923a5
MD5 4037f01e09ac21f1f07626939f59377d
BLAKE2b-256 b683288c3cb6c8e508fcb9bff73f303300d39057cb63d1dee2b767a229ea1ba0

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