PostgreSQL Query Builder
<p align="center">
<a href="https://github.com/yezz123/pgqb" target="_blank">
<img src="https://raw.githubusercontent.com/yezz123/pgqb/main/.github/logo.png" alt="pgqb">
</a>
<p align="center">
<em>Typed Python PostgreSQL query builder ✨</em>
</p>
</p>
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:
bash
pip install pgqb
Project using
py
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
py
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);