Live data from Hacker News

Blending SQL and Python with Sqlorm

hyperflask.dev

21–28 of 28 posts

Re: Blending SQL and Python with Sqlorm

#21
post #12

Kudos for making the leap. Your pattern of re-interpreting __doc__ is kinda weird though. Why not just add a `return` statement?

I guess it's more clear that it should be a to statically readable value? eg you shouldn't do things like use arguments to build the str

Re: Blending SQL and Python with Sqlorm

#23
post #15

Earlier quoted context omitted.

I honestly have no opinion in this discussion, but I will 100% upvote the first Fossil repository I've seen shared on here! How do you find developing on the Fossil platform?

It has a sane ui/syntax, all the additional infrastructure you might want is built in (webpage, wiki, forum, tickets) and is trivial to set up. It is nearly the perfect version control system. Especially for the small independent amateur developer.

Awesome. Does it have any sort of CI/CD capabilities? And do you have any recommendations for learning resources for someone to get started?

Re: Blending SQL and Python with Sqlorm

#24
post #15

Earlier quoted context omitted.

It has a sane ui/syntax, all the additional infrastructure you might want is built in (webpage, wiki, forum, tickets) and is trivial to set up. It is nearly the perfect version control system. Especially for the small independent amateur developer.

Awesome. Does it have any sort of CI/CD capabilities? And do you have any recommendations for learning resources for someone to get started?

For getting started there is the quick start guide https://fossil-scm.org/home/doc/trunk/www/quickstart.wiki and after that the built in help gets you pretty far. Really, most distributed version control systems have the same workflow the devil is in the exact syntax used.

As for CI there are pre-commit hooks I suspect could be used for that process. https://fossil-scm.org/home/help/hook and https://fossil-scm.org/home/doc/trunk/www/hooks.md

Re: Blending SQL and Python with Sqlorm

#25
post #12

Kudos for making the leap. Your pattern of re-interpreting __doc__ is kinda weird though. Why not just add a `return` statement?

I guess it's more clear that it should be a to statically readable value? eg you shouldn't do things like use arguments to build the str

I would def use this if there was a return “select …” option. There are heaps of scenarios where sql is modified based on parameters. If no doc string just use the return value maybe?

Our queries are typically large, not 3-5 liners.

(Filter view queries where you might add additional CTA’s to provide the necessary filter conditions, but aren’t desirable if particular filter parameter is nill, etc.)

Re: Blending SQL and Python with Sqlorm

#26
post #25

Earlier quoted context omitted.

I guess it's more clear that it should be a to statically readable value? eg you shouldn't do things like use arguments to build the str

I would def use this if there was a return “select …” option. There are heaps of scenarios where sql is modified based on parameters. If no doc string just use the return value maybe? Our queries are typically large, not 3-5 liners. (Filter view queries where you might add additional CTA’s to provide the necessary filter conditions, but aren’t desirable if particular filter parameter is nill, etc.)

Hello, author here. It is actually possible to use return instead with a different set of decorators: check out the first "tip" block on this page: https://hyperflask.github.io/sqlorm/sql-functions/

Re: Blending SQL and Python with Sqlorm

#27
post #5

Here is my rather naive take on the same subject. But I had a very different motivation than the author. See I actually quite like SQL and enjoy programming in it, but what I don't like is mixing sql and python. So one night in a flash of inspiration or perhaps a fever dream I wrote this thing that lets you have stand alone parameterized sql queries and you call them like a python function or generator. It is one of…

That's basically the idea behind Sqlc [1]. By letting SQL be SQL, you avoid the many awkward mechanisms ORMs need to integrate SQLisms into the native language, and you define the query only in terms of its inputs and outputs, which can be made type-safe since they're declaratively defined.

The downside is that parameterized queries are a bit of a chore; for example, if a query should support an optional filter on user_id, you need to craft it like this:

    WHERE ...
      AND CASE
        WHEN sqlc.narg('user_id') IS NOT NULL THEN sqlc.narg('user_id')
        ELSE true
      END
This is not too bad, though, and the conditionals get optimized away by the database planner.

[1] https://sqlc.dev/

Re: Blending SQL and Python with Sqlorm

#28
post #25

Earlier quoted context omitted.

I guess it's more clear that it should be a to statically readable value? eg you shouldn't do things like use arguments to build the str

I would def use this if there was a return “select …” option. There are heaps of scenarios where sql is modified based on parameters. If no doc string just use the return value maybe? Our queries are typically large, not 3-5 liners. (Filter view queries where you might add additional CTA’s to provide the necessary filter conditions, but aren’t desirable if particular filter parameter is nill, etc.)

Just keep in mind best practice is to use the built-in parameter interpolation that comes with your db library, since it handles escaping SQL injection for you.

Be very careful if you ever use bare string formatting to construct your queries.

Post reply on HN