5 ms·
My initial thought is I'd rather just have a module that would find project-related .sql files and parse into named blocks based on the comments, treating them
by jdnier 6y ago
My initial thought is I'd rather just have a module that would find project-related .sql files and parse into named blocks based on the comments, treating them as named strings. No db entanglement required, just a simple way to organize queries in sql files. You would use it more like this:
import sqlite3
import project_queries as queries # a module that loads queries from local .sql files as strings in module namespace
conn = sqlite3.connect('myapp.db')
cursor = conn.cursor(queries.get_all_users)
# ^^^^^^^^^^^^^^^^^^^^^
users = cursor.fetchall()
# >>> [(1, "nackjicholson", "William", "Vaughn"), (2, "johndoe", "John", "Doe"), ...]
aiosql may add some features to help with passing values to the query. I see `^` used with `:users` in an example but don't quite get it.
-- name: get-user-by-username^
I often need to generate DDL details like table and column names in the query, which you don't want escaped, along with data like values and ids where you would want escaping (:users or %(users)s).
It works nicely to use string formatting (`CREATE TABLE {table_name} ...`) for DDL along with interpolation (`WHERE user_id IN %(user_ids)s`) in the same query.
- kristjansson 6y agoI’ll unhelpfully note that I could swear I’ve seen something that does what you describe, and thought that this project was what I recalled - alas.
- deleted 6y ago[deleted]
- wswope 6y agoAnoSQL is a project along these lines I’ve seen on here before.
- nackjicholson 6y agoI forked aiosql from anosql in order to support asyncio based sql drivers. Huge thanks to Honza Pokorny for starting this adventure.
- kristjansson 6y agoThat’s the one, thank you!
- cheez 6y agohttps://docs.python.org/3/library/importlib.html#module-importlib.resources https://docs.python.org/3/library/importlib.html#module-impo...
- jdnier 6y agoPerfect, thank you! I wasn't aware of importlib.resources.
- cheez 6y agoIt's new. A good habit I've developed recently is to read all the PEPs since the last thing, and all the library changes. Python 3 is in full swing and some great things are in there already
- hprotagonist 6y agoit’s only a decade old or so ;)
- cheez 6y agoOh it was a fun ten years
- hprotagonist 6y agoi know how lucky i was to only start on 3.5. Avoided a LOT of pain.
- cheez 6y agoYou should check out the typing stuff in 3.8. Combined with mypy, gets a lot of the way there to statically checked python without the hassles of statically typed code.
- jimbokun 6y agoThe "^" marks a query returning one or zero values: https://nackjicholson.github.io/aiosql/defining-sql-queries/#select-one https://nackjicholson.github.io/aiosql/defining-sql-queries/...
- deleted 6y ago[deleted]