r/programming • u/genericlemon24 • Aug 25 '21
Write an SQL query builder in 150 lines of Python!
https://death.andgravity.com/query-builder-how0
Aug 25 '21
[deleted]
21
u/life-is-a-loop Aug 25 '21
Reinventing the wheel is an excellent exercise to understand how things work under the hood. And it's fun!
10
u/Worth_Trust_3825 Aug 25 '21
More often than not, these "builders" are much more limiting than just letting me write my own query and run it. It's all good that you reinvent something to see how it could work but then the very same solutions end up in production just because they're "inhouse"
1
u/genericlemon24 Aug 25 '21
these "builders" are much more limiting than just letting me write my own query and run it
Good thing being able to write plain SQL was part of the requirements.
then the very same solutions end up in production just because they're "inhouse"
Some "productions" (10 full-time devs) are different than others (1 dev with 10h/month), with very different trade-offs. I was careful to make it very clear building your own doesn't work everywhere (or in most contexts, for that matter).
2
u/genericlemon24 Aug 25 '21
And sometimes, you find out you didn't need a car, just a blinker, and that for your use case, an entire car is overkill.
0
Aug 25 '21
[deleted]
0
u/genericlemon24 Aug 25 '21
writing a toy version of a real battle tested tool and then publishing about it in this way gives people the sense that they understand what the real battle tested tool is doing, but in fact they don't
On its own and out of the blue, I agree, this can make things look deceptively simple.
Good thing there's another article explaining the context in which the tool was made, and how the lack of features was an informed decision, taken after looking at many of the existing solutions.
1
u/myusernameisokay Aug 25 '21
One of the easiest ways to learn is by doing. Anyone could read all the code for an established project like this, much like you could read all the code for GCC. But reading a ton of code is generally pretty boring, and there’s no guarantee you’ll actually understand what you’re reading.
Doing it yourself proves you actually can make a prototype. Sometimes you’ll come across difficult problems and you’ll need to figure out how to solve them. By solving them you’re improving your problem solving skills and programming skills.
2
u/genericlemon24 Aug 25 '21
I largely agree with you, in general. However, in my specific case, it was the right choice; I write more about it here (there's another disclaimer here).
What about subqueries, functions, aliasing, CTEs, aggregations...?
All already supported in the 150 lines. There are others that aren't and can be easily added, and some I'm OK with not having; I did the due diligence of finding out what I would need, and for the past 2 years I didn't need anything new.
Seriously -- this is a security issue (SQL injection).
No more than plain SQL, and this can be easily avoided with named parameters.
Writing your own is on the same level as rolling your own crypto.
Again, in general, I mostly agree, but I wouldn't go so far. Crypto is used by definition in critical applications, and way more difficult to understand and get right (haven't seen any ORM doctorates around).
I see no issue with doing your own thing if you have clear requirements, know what effort is required to solve the problem, and are willing to expend it. In general, you use a general solution; when you know what you need, it's OK to do something different.
5
u/lelanthran Aug 25 '21
SQL query builders are an excellent example of wrapping a clean, neat and secure API with an ugly, unintuitive, feature-incomplete, bug-ridden and exploitable wrapper.
Seriously, just use the SQL syntax so that the reader knows what is being queried. Using the wrapper means that the reader now needs to know your shitty interface as well as SQL itself.