relations-sql
Module for interacting with Relations and SQL
This is an abstract library used by relations-mysql, relations-postgresql, relations-sqlite, etc. to generate queries specific to those databases.
But folks may find it useful for their usages. So here's some of the main unittests so you get the general idea.
I'll probably switch this to ANSI SQL? I dunno. Not sure how useful that'll be.
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_sql 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(
"*"
).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 * FROM (SELECT `a`.`b`.`c` FROM `d`.`e`) "
"AS `people` WHERE `stuff` IN "
"(SELECT `f` FROM `g` WHERE `things`#>>%s>JSON(%s))"
)
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 * FROM (SELECT `a`.`b`.`c` FROM `d`.`e`) "
"AS `people` WHERE `stuff` IN "
"(SELECT `f` FROM `g` WHERE `things`#>>%s>JSON(%s)) "
"GROUP BY `fee`,`fie` HAVING `foe`=%s "
"ORDER BY `yin`,`yang` DESC LIMIT %s,%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)
inherit
The unittest demonstrate how to abstract through inheritance. Classes know which other classes they'll need through class attributes.
The unittests are where the backticks escaping comes from. It's not a default or anything.
In test_clause.py
class OPTIONS(relations_sql.OPTIONS):
pass
class FIELDS(relations_sql.FIELDS):
ARGS = test_expression.FIELD
KWARG = test_expression.FIELD
KWARGS = test_expression.AS
In test_query.py
class SELECT(relations_sql.SELECT):
CLAUSES = collections.OrderedDict([
("OPTIONS", test_clause.OPTIONS),
("FIELDS", test_clause.FIELDS),
("FROM", test_clause.FROM),
("WHERE", test_clause.WHERE),
("GROUP_BY", test_clause.GROUP_BY),
("HAVING", test_clause.HAVING),
("ORDER_BY", test_clause.ORDER_BY),
("LIMIT", test_clause.LIMIT)
])
class INSERT(relations_sql.INSERT):
CLAUSES = collections.OrderedDict([
("OPTIONS", test_clause.OPTIONS),
("TABLE", test_expression.TABLE),
("FIELDS", test_expression.NAMES),
("VALUES", test_clause.VALUES),
("SELECT", SELECT)
])
class UPDATE(relations_sql.UPDATE):
CLAUSES = collections.OrderedDict([
("OPTIONS", test_clause.OPTIONS),
("TABLE", test_expression.TABLE),
("SET", test_clause.SET),
("WHERE", test_clause.WHERE),
("ORDER_BY", test_clause.ORDER_BY),
("LIMIT", test_clause.LIMIT)
])
class DELETE(relations_sql.DELETE):
CLAUSES = collections.OrderedDict([
("OPTIONS", test_clause.OPTIONS),
("TABLE", test_expression.TABLE),
("WHERE", test_clause.WHERE),
("ORDER_BY", test_clause.ORDER_BY),
("LIMIT", test_clause.LIMIT)
])
In the case of query classes, the OrderedDict's there also specific the order in which clauses are generated and appended. So it's pretty easy to extend.
Metadata
Release files for relations-sql 0.6.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| relations_sql-0.6.10.tar.gz | 17.7 kB | Without provenance | Details for relations_sql-0.6.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| relations_sql-0.6.10-py3-none-any.whl | Python 3 | none | any | Details for relations_sql-0.6.10-py3-none-any.whl |
Total release size: 38.3 kB
Release files / relations_sql-0.6.10.tar.gz
| Download URL | relations_sql-0.6.10.tar.gz |
|---|---|
| Size | 17.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f4d54b5693447d90ee9fbbcc7831ef32069532211d86b87987733660d87732be
|
|
BLAKE2b-256 checksum How to use checksums |
e665a6e31db8651941cd9f7a460a1318d824c57a7c03e902e463a0a53bfbe7ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Release files / relations_sql-0.6.10-py3-none-any.whl
| Download URL | relations_sql-0.6.10-py3-none-any.whl |
|---|---|
| Size | 20.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
895dcc11b8e2defe81680409a1d80e297ff71a06a770d27eb1c5cb1739daabdb
|
|
BLAKE2b-256 checksum How to use checksums |
6dff69224b7657c080dbed41379a257ede12265db610cabd88a7fb05e1e733b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Security for relations-sql 0.6.10
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.