[HN Gopher] Show HN: Inline SQL in any Python program
___________________________________________________________________
Show HN: Inline SQL in any Python program
Author : ekzhang
Score : 10 points
Date : 2022-10-08 14:57 UTC (8 hours ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| pizza wrote:
| This is awesome. Is there a way to get it to work with e.g.
| dataclasses-sql? I am thinking of something such as
| stuff: list[mydataclass] sql_val^ "select * from stuff
| where stuff.field = 2"
|
| over any kind of 'fmappable' container; perhaps eg.
| stuff: dict[int, mydataclass] stuff2: dict[int,
| mydataclass] sql_val^ "select (s2.val, s.key) from
| stuff as s, stuff2 as s2 where s.key > s2.key and s2.value !=
| s.value)"
|
| etc
| rubenfiszel wrote:
| Something was not clear to me from the README. When you do string
| interpolations as in your examples, those are actually prepared
| sql statements right ? Otherwise it would open the way for sql
| injections.
| ekzhang wrote:
| Yep, they're prepared SQL statements. Literal string
| interpolation would be unacceptable in any self-respecting SQL
| library: it's parameters. Good question though!
| lazzlazzlazz wrote:
| This is great. I have a personal-use version of this (also using
| DuckDB's fantastic Pandas compatibility) and will see if I can
| just switch over.
| orf wrote:
| Looks really interesting, but why overload the ^ operator? Is a
| function call _really_ that much more verbose?
| ekzhang wrote:
| Thanks for the feedback orf! I agree it looks strange at first,
| this is very experimental so I played a bit with syntax design
| choices. Sometimes for DSLs it's nice to have a bit of
| flexibility. You can compare it to the %timeit, %cython "magic"
| statements in Jupyter.
|
| We could use a standard function application, but that doesn't
| convey the point that there's some DSL embedding going on here,
| such as variable substitution from the outside scope. It's also
| less nice to end multi-line queries with the string: """).
|
| Other reasons: the bitwise XOR operator is almost never used in
| Python, so it's highly unlikely that people would confuse it as
| such (this is also why it's formatted without a space before
| the operator). Open to other proposals for syntax though.
| orf wrote:
| These are good points, especially around the magic
| substitution. I hadn't really considered that.
|
| Even so, pandas has the same magic substitution going on with
| just a standard function call, and it's somewhat intuitive.
|
| However, regardless of the syntax nitpicking, this is a cool
| project. Nice one!
| rubenfiszel wrote:
| Very cool, I have lots of ideas of how to leverage it for
| windmill.dev. We were using psycopg2 for all our examples but
| this is much cleaner. Thanks for sharing!
| ekzhang wrote:
| That's great! There's nothing I would be happier to hear than
| this experiment making someone think of query languages a
| little differently. :)
| biggerChris wrote:
| Python and Haskell programmers at this point are just bored. All
| python and Haskell programmers need to start being developers.
| pizza wrote:
| Boredom in the right hands leads to works of tremendous beauty
___________________________________________________________________
(page generated 2022-10-08 23:01 UTC)