Skip to main content

No project description provided

Project description

relations-sqlite

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

This is the SQL library used by relations-sqlite3, 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_sqlite import *

select

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

query.generate()
self.assertEqual(query.sql, """SELECT FAST * FROM `people` WHERE `stuff`>?""")
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 json_extract(`things`,?)>?)"
)
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 json_extract(`things`,?)>?) "
    "GROUP BY `fee`,`fie` HAVING `foe`=? "
    "ORDER BY `yin`,`yang` DESC LIMIT ? OFFSET ?"
)
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 (?,?),(?,?)")
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`=? WHERE `things`=? ORDER BY `yin`,`yang` DESC LIMIT ?""")
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`=? ORDER BY `yin`,`yang` DESC LIMIT ?""")
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-sqlite-0.6.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

relations_sqlite-0.6.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for relations-sqlite-0.6.2.tar.gz
Algorithm Hash digest
SHA256 5f2dda59cc10d0ae57b8e4f5e5254ab1f4a1114d8d5cf672c823b68d869214f0
MD5 fa3e1a0e469706e9c954a54a93abca06
BLAKE2b-256 66c1160bf38b5af04c12fbb9109a9036d6a51cfbcfc8e8e830609b2a6b43908f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for relations_sqlite-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 910b65ff5e9d52249f67106a520aab5bb8f8624b3b757cfb63e90efe1f9fa16a
MD5 c8129fd392bfe4ed9b7ec6ab9398b5d2
BLAKE2b-256 77ccf7e98cbbf6c6056148012bf79f2c5b8aa7e12df118075978e0b4a6217a63

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