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)

Release files for relations-postgresql 0.6.3

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.3
File Size Uploaded
relations_postgresql-0.6.3.tar.gz 7.9 kB Details

Built distribution (wheel)

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

Total release size: 17.6 kB

Release files / relations_postgresql-0.6.3.tar.gz

Download URL relations_postgresql-0.6.3.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8b09ffc165f8b1a92b1dd1e618544ead55167a8625ec851dca56199f8c71b872
BLAKE2b-256 checksum
How to use checksums
c2e26c2235f00c4c3c99ae81782de0f0ce3d350fb16cca770e11f8af781df6ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.5

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

Download URL relations_postgresql-0.6.3-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90878307fd070ea2beba093c7642cf11acd2cfe5f3adc56f26d78dacc949a229
BLAKE2b-256 checksum
How to use checksums
54853693a5e51e817c0a0109c4e4d75dc5b762ae4c0e689383432b62d3ed2e8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.6.3 This release

2 release files

0.6.2

2 release files

0.6.1

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