News for hackers. Stuff that matters.
Re:Cache is the new RAM by cturner on Wednesday November 19, 2014 @04:24PM (#8632708)
It's impractical to write maintainable code in the forms of SQL I'm familiar with. But maybe someone here knows better.

You should be able to abstract logic out to blocks for the duration of the query. Along these lines:

    cset means select id, venue_id from customer

    select venue_name from venue where id = cset.venue_id
cset would be scoped to your connection.

Instead of pages of copy-and-paste queries where the levels are all mixed up with one another. You can get something similar by polluting the namespace (e.g. views, temporary tables) but that's hacky and not the same as good.

Re:Cache is the new RAM by Ntrails on Wednesday November 19, 2014 @05:22PM (#8633061)
I mean, you can also use CTEs for that but it is of course limited.

For all of the flaws of managing SQL, it's when I get handed giant swathes of dynamic SQL that I die a little inside.

Re:Cache is the new RAM by cturner on Wednesday November 19, 2014 @05:48PM (#8633206)
I've just been reading up on CTEs - thanks. That's the kind of thing I thought would be out there.
Re:Cache is the new RAM by adlpz on Wednesday November 19, 2014 @04:45PM (#8632810)
Well that's why (in my opinion) you should _generate_ the queries, not _write_ them. Use an ORM or whatever flavor of basically a SQL generator you prefer. Or even easier, use any programming language to concatenate around some strings and build your SQL.

The power of using SQL comes from the fact that, no matter what, it's always there. You can always write/buy/find another client for your DB in another language, and as long as it generates SQL, it's all good.