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.62.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.62.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sql_artifex-0.62.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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.62.0.tar.gz
Algorithm Hash digest
SHA256 a72bec45cdf9e95f4b64322d58d1dd7374f57a471aad6de65eaa0e1fccf6d0e8
MD5 1808365ceb6f6567a2d9b90bac05f2dd
BLAKE2b-256 64b9cba8645eb127fa7b07eaa71232d8b20ae1e516cbc460d7c5132fb23d7584

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sql_artifex-0.62.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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.62.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5284378ac432d3c66522e7f072e55e866a06b8e644cf27870de74099ef33f17c
MD5 aaecf18ad4fd46dd825bacb068dcaad1
BLAKE2b-256 a5a090d98b8d9ac67e7a7108453fc2b73615c4cb23ddab1673e82a874b5348c9

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