Skip to main content

PostgreSQL Query Builder

pgqb

Typed Python PostgreSQL query builder ✨

Continuous Integration Package version


Source Code: https://github.com/yezz123/pgqb

Documentation: TBD


pgqb is a Python library for building SQL queries for PostgreSQL databases. It provides a simple and intuitive interface for constructing SQL statements using functions like delete, insert_into, select, and update. This README provides a brief overview of how to use pgqb to build queries and execute them safely with parameter binding.

Installation

You can install pgqb via pip:

pip install pgqb

Project using

from pgqb import Column, Table, select


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


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


sql, params = (
    select(User)
    .from_(User)
    .join(Task)
    .on(Task.user_id == User.id)
    .where(User.id == 1)
    .order_by(Task.value.desc())
).prepare()

expected = " ".join(
    [
        'SELECT "user".id, "user".first, "user".last',
        'FROM "user"',
        'JOIN "task" ON "task".user_id = "user".id',
        'WHERE "user".id = ?',
        'ORDER BY "task".value DESC',
    ]
)


print(sql==expected)
# > True

Create Table

from pgqb import Column, Table, 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())

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

Development

Setup environment

You should create a virtual environment and activate it:

Notes: You need to have python3.9 or higher installed.

I Use uv to manage virtual environments, you can install it with:

# Install uv
pip install uv

# Create a virtual environment
uv venv

# Activate the virtual environment
source .venv/bin/activate

And then install the development dependencies:

# Install dependencies
uv pip install -e .[test,lint]

Run tests 🌝

You can run all the tests with:

bash scripts/tests.sh

Format the code 🍂

Execute the following command to apply pre-commit formatting:

bash scripts/format.sh

Execute the following command to apply mypy type checking:

bash scripts/lint.sh

License 🍻

This project is licensed under the terms of the MIT license.

Metadata

Release files for pgqb 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgqb 0.1.0
File Size Uploaded
pgqb-0.1.0.tar.gz 92.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pgqb 0.1.0
File Interpreter ABI Platform
pgqb-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 104.6 kB

Release files / pgqb-0.1.0.tar.gz

Download URL pgqb-0.1.0.tar.gz
Size 92.5 kB
Tags Source
SHA-256 checksum
How to use checksums
afb61c05cd244ccf9f04bbc6510296a12af9f6b2caaf526e4183215bb109d6fa
BLAKE2b-256 checksum
How to use checksums
c9aa6866a4702ff0436f182d0406ca43dbf682867ad104ddd6a0254af98adaac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / pgqb-0.1.0-py3-none-any.whl

Download URL pgqb-0.1.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c77618005178c55c42ecae63d5df7efda1aedcae3f7d8841834576e37c9a5978
BLAKE2b-256 checksum
How to use checksums
57744f97329ebfe94437961ec62b016cddbf00a4fe238d507794161342e60740
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page