4 ms·
How do people who prefer to write raw SQL usually deal with: 1. Query building, particularly when the query needs to be dynamic based on user input? Do you end
by nodamage 6y ago
How do people who prefer to write raw SQL usually deal with:
1. Query building, particularly when the query needs to be dynamic based on user input? Do you end up concatenating strings together or do you use a separate query builder?
2. Coalescing result sets produced by JOINs back into object form? Example: if you want to fetch users along with all their posts your query will return multiple rows per user, but when working with objects in your app you want each user to have a list of posts so you can simply say users.posts.
3. Property change tracking? Example: different parts of your app might update different properties for each user. If the user's email and last_login changes you need to write one query. If the user's password changes you need to write a different query. If the user's email, name, and location changes you need to write another query. An ORM with change tracking will figure out exactly which properties have been modified and issue the correct SQL to update only the changed properties. When working with raw SQL do you simply end up writing different queries for each possible permutation of changes?
- cooperadymas 6y agoUsually by writing their own lightweight ORM as part of their application. ¯\_(ツ)_/¯
- stilisstuk 6y ago1) concatenating. Not always possible in complex cases. 2) I don't think it's that hard to write 'select posts from user where user = ?' 3) I prefer functional to oop. In my small apps writing a function with a transparent SQL have worked fine.
- nodamage 6y agoRe: 2) For a single user, that's fine. What if you are trying to fetch posts for N users at a time? Issue N separate queries?