Skip to main content

relations-postgresql

Module for interacting with Relations and PostgreSQL. It generates PostgreSQL compatible SQL from SQL like classes in Python.

This is the SQL library used by relations-psycopg2, but folks may find it useful. So here's some of the main unittests so you get the general idea.

import

All the classes are capitalized to prevent collisions with reserved keywords. Plus it looks like actual SQL. So you can do a full import without worries.

from relations_postgresql import *

select

query = SELECT("*").OPTIONS("FAST").FROM("people").WHERE(stuff__gt="things")

query.generate()
self.assertEqual(query.sql, """SELECT FAST * FROM "people" WHERE "stuff">%s""")
self.assertEqual(query.args, ["things"])

query = SELECT(
    "*"
).OPTIONS(
    "FAST"
).FROM(
    people=SELECT(
        "a.b.c"
    ).FROM(
        "d.e"
    )
).WHERE(
    stuff__in=SELECT(
        "f"
    ).FROM(
        "g"
    ).WHERE(
        things__a__0___1____2_____3__gt=5
    )
)

query.generate()
self.assertEqual(query.sql,
    """SELECT FAST * FROM (SELECT "a"."b"."c" FROM "d"."e") """
    """AS "people" WHERE "stuff" IN """
    """(SELECT "f" FROM "g" WHERE ("things"#>>%s)::JSONB>(%s)::JSONB)"""
)
self.assertEqual(query.args, ['{a,0,-1,"2","-3"}', "5"])

query.GROUP_BY("fee", "fie").HAVING(foe="fum").ORDER_BY("yin", yang=DESC).LIMIT(1, 2)

query.generate()
self.assertEqual(query.sql,
    """SELECT FAST * FROM (SELECT "a"."b"."c" FROM "d"."e") """
    """AS "people" WHERE "stuff" IN """
    """(SELECT "f" FROM "g" WHERE ("things"#>>%s)::JSONB>(%s)::JSONB) """
    """GROUP BY "fee","fie" HAVING "foe"=%s """
    """ORDER BY "yin","yang" DESC LIMIT %s OFFSET %s"""
)
self.assertEqual(query.args, ['{a,0,-1,"2","-3"}', "5", 'fum', 1, 2])

insert

query = INSERT("people").VALUES(stuff=1, things=2).VALUES(3, 4)

query.generate()
self.assertEqual(query.sql,"""INSERT INTO "people" ("stuff","things") VALUES (%s,%s),(%s,%s)""")
self.assertEqual(query.args, [1, 2, 3, 4])

query = INSERT("people").OPTIONS("FAST")
query.SELECT("stuff").FROM("things")

query.generate()
self.assertEqual(query.sql,"""INSERT FAST INTO "people" SELECT "stuff" FROM "things\"""")
self.assertEqual(query.args, [])

query = INSERT("people").VALUES(stuff=1, things=2).VALUES(3, 4)
query.SELECT("stuff").FROM("things")

self.assertRaisesRegex(relations_sql.SQLError, "set VALUES or SELECT but not both", query.generate)

update

query = UPDATE("people").SET(stuff="things").WHERE(things="stuff")
query.OPTIONS("FAST").ORDER_BY("yin", yang=DESC).LIMIT(5)

query.generate()
self.assertEqual(query.sql, """UPDATE FAST "people" SET "stuff"=%s WHERE "things"=%s ORDER BY "yin","yang" DESC LIMIT %s""")
self.assertEqual(query.args, ["things", "stuff", 5])

query.LIMIT(10)

self.assertRaisesRegex(relations_sql.SQLError, "LIMIT can only be total", query.generate)

delete

query = DELETE("people").WHERE(things="stuff")
query.OPTIONS("FAST").ORDER_BY("yin", yang=DESC).LIMIT(5)

query.generate()
self.assertEqual(query.sql, """DELETE FAST FROM "people" WHERE "things"=%s ORDER BY "yin","yang" DESC LIMIT %s""")
self.assertEqual(query.args, ["stuff", 5])

query.LIMIT(10)

self.assertRaisesRegex(relations_sql.SQLError, "LIMIT can only be total", query.generate)

Metadata

Release files for relations-postgresql 0.6.4

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

Source distribution (sdist)

Source distribution for relations-postgresql 0.6.4
File Size Uploaded Provenance Actions
relations_postgresql-0.6.4.tar.gz 8.5 kB Without provenance Details for relations_postgresql-0.6.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for relations-postgresql 0.6.4
File Interpreter ABI Platform Actions
relations_postgresql-0.6.4-py3-none-any.whl Python 3 none any Details for relations_postgresql-0.6.4-py3-none-any.whl

Total release size: 18.5 kB

Release files / relations_postgresql-0.6.4.tar.gz

Download URL relations_postgresql-0.6.4.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1d34502901e78d60c03af430e5e76bc8a8424126284c7d2290cb2ec9c4faeae7
BLAKE2b-256 checksum
How to use checksums
eb40608eab83960190489a2d6c5ba933a6f6628d7ee06f924feab470564eec4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / relations_postgresql-0.6.4-py3-none-any.whl

Download URL relations_postgresql-0.6.4-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e82764e42e0a203c05adec08e57d940d8942fe66bf67ad64ca9866e7ed9a516
BLAKE2b-256 checksum
How to use checksums
aede4ff49a08fbb1e9323dd29c5ec0eb77436b4603d8ddecfab1a091c54fdf2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for relations-postgresql 0.6.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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