Skip to main content

No project description provided

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

relations-postgresql-0.6.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

relations_postgresql-0.6.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file relations-postgresql-0.6.2.tar.gz.

File metadata

  • Download URL: relations-postgresql-0.6.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for relations-postgresql-0.6.2.tar.gz
Algorithm Hash digest
SHA256 5d5862dc4da69764904f0e71a7a7dd1cba67613ebf5c88e75f950b6707e3cb8c
MD5 79da86c711b921529b429c7f846f2d0f
BLAKE2b-256 f35398b3e7c87c2e7a100ff9737b6b2468e02714ee618d601262b79ab1091ab7

See more details on using hashes here.

File details

Details for the file relations_postgresql-0.6.2-py3-none-any.whl.

File metadata

File hashes

Hashes for relations_postgresql-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e1ed88a775c5b63347efe40ce7127f4a29584ce5b0194095e95abfe63da0fe45
MD5 f05ea937c882e780b952755b4f31414e
BLAKE2b-256 b4512e86aa1e85a5aac20b6caf2ed330dedd5ed94a1e7ae69dba5a147aaef893

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page