4 ms·
> you should never use positional semantics in your SQL If I'm understanding you, that along with your mention of a SQL builder means, for example, in Python,
by leetrout 6y ago
> you should never use positional semantics in your SQL
If I'm understanding you, that along with your mention of a SQL builder means, for example, in Python, you'd expect
CreateUser(name="blah")
As opposed to something like
db.Exec("INSERT INTO users (name) values (?)", "blah")
Is that correct? I think we're agreeing on reducing the amount of raw SQL being tossed around within the code.
- MereInterest 6y agoSQL columns can be referred to either by name or by index. Avoiding positional semantics means using "ORDER BY transaction_date ASC" rather than "ORDER BY 3 ASC". On the calling code, your python library may return either a tuple or a dictionary. The dictionary is the right way to do it, because then you access parameters by name, and reordering or adding parameters to the query output doesn't break your script.
- blacktriangle 6y agoLess about the exact syntax and more about the tool, for example: https://github.com/sqlkata/querybuilder https://github.com/sqlkata/querybuilder. I just chose that since it was on top of a search but the idea is the same. Your code generates raw SQL, so it's 100% interchangeable with writing SQL yourself however the builder library deals with the syntax, proper ordering, quoting, full attribute names, etc. Some such libraries even let you define your schema in code to make your SQL generation type safe.