DBQC for Python
Schema discovery generates typed table objects. Use them as a Python expression language; DBQC keeps the relational dependency graph, orders CTEs, checks SQL types, and compiles to the selected database dialect.
from dbqc_schema import orders, restaurants
from dbqc_dsl import (
delete_from, desc, insert_into, params, recursive, row_number, sum_, update,
)
The ordinary case
ids = restaurants.where(city="New York").select(restaurants.id)
directory = (
restaurants
.where(city="New York", status="active")
.select(restaurants.id, restaurants.name)
)
Keyword filters mean DBQC equality (IS NOT DISTINCT FROM). They are ordinary Python keyword arguments whose names and values are checked against the discovered schema; they are not Python's assignment or equality operators.
Predicates and joins
Python's comparison operators are overloaded as SQL expressions:
high_value = (
restaurants
.join(orders, restaurants.id == orders.restaurant_id)
.where(orders.total > "100.00")
.select(restaurants.name, orders.total)
.order_by(desc(orders.total))
)
== means DBQC null-safe equality, which compiles to IS NOT DISTINCT FROM. !=, <, <=, >, and >= build the corresponding SQL predicates. Use .and_(), .or_(), and .not_() for boolean composition because Python's and, or, and not cannot be overloaded.
open_in_new_york = restaurants.where(
(restaurants.city == "New York").and_(restaurants.status != "closed")
)
Use .left_join(), .right_join(), or .lateral_join() when those SQL join semantics are intended.
Runtime parameters
Parameters are a relational source:
input_ = params({"restaurant_id": "uuid"})
by_id = (
restaurants
.join(input_, restaurants.id == input_.field("restaurant_id"))
.select(restaurants.id, restaurants.name)
)
compiled = by_id.compile("psql")
print(compiled.sql)
print(compiled.param_order) # ["restaurant_id"]
Bind runtime values in param_order; DBQC does not interpolate them into SQL.
Aggregates and HAVING
revenue = (
orders
.group_by(orders.restaurant_id)
.having(sum_(orders.total) > "0.00")
.select(
orders.restaurant_id,
sum_(orders.total).as_("revenue"),
)
)
Grouping fields retain their generated names. Only a computed result needs .as_().
Reusable queries and CTE ordering
revenue = (
orders
.group_by(orders.restaurant_id)
.select(
orders.restaurant_id,
sum_(orders.total).as_("revenue"),
)
.named("revenue")
)
ranked = (
restaurants
.join(revenue, restaurants.id == revenue.field("restaurant_id"))
.where(revenue.field("revenue") > "1000.00")
.select(restaurants.name, revenue.field("revenue"))
)
DBQC derives the dependency DAG and emits CTEs in valid order. Code composes relational values instead of maintaining a WITH list by hand.
Windows, JSON, and pagination
feed = (
orders
.select(
orders.id,
orders.restaurant_id,
row_number()
.over(
partition_by=[orders.restaurant_id],
order_by=[desc(orders.created_at)],
)
.as_("number"),
)
.page(25, desc(orders.created_at))
)
contacts = restaurants.select(
restaurants.id,
restaurants.metadata.json_path_text("$.contact.email").as_("email"),
)
Writes
Discovered types lift ordinary Python values:
created = (
insert_into(restaurants)
.values(
id=restaurant_id,
name="Saffron",
city="New York",
cuisine="Indian",
status="active",
)
.returning("id")
)
moved = (
update(restaurants)
.set(city="Brooklyn")
.where(restaurants.id == restaurant_id)
.returning("id")
)
removed = (
delete_from(restaurants)
.where(restaurants.status == "closed")
.returning("id")
)
.on_conflict(...).do_nothing(), .do_update(...), insert-from-select, update-from, locks, transactions, and procedures remain on this facade.
Recursive queries
The recursive output heading is declared once because it does not come directly from a generated table:
tree = recursive(
"tree",
{"id": "uuid", "parent_id": "uuid"},
roots.select(roots.id, roots.parent_id),
lambda tree: (
nodes
.join(tree, nodes.parent_id == tree.field("id"))
.select(nodes.id, nodes.parent_id)
),
)
Use recursive_distinct() when recursive UNION semantics are needed for deduplication or cycle termination.
Compile and execute
compiled = ranked.compile("psql")
cursor.execute(compiled.sql, [runtime[name] for name in compiled.param_order])
rows = cursor.fetchall()
DBQC compiles and describes results; your DB-API driver executes the SQL. Set DBQC_ENDPOINT and DBQC_KEY, or pass endpoint credentials to compile().
Escape hatch
raw() and raw_source() support vendor features DBQC does not model. Keep them as explicit boundaries; normal queries should use generated fields so DBQC retains type checks, dependency ordering, and portability.