I_Dig_Sql
My way of managing SQL fragments using Ruby.
Warning:
You will hate using this. Instead, use:
History
I had trouble maintaining BIG sql queries.
I tried many things.
The best way (within my preferences) was to use sub-queries, CTEs, avoid joins as much as possible, and this gem to manage SQL fragments and CTEs.
Naturally, you would want to use prepared statements, compiled wat-cha-me-call-its, functions, views, thing-ma-jig-ers, and other tools available in your RDBMS.
So this gem is for lazy, stupid people like me.
Usage
Please note that none of this is ready yet.
require 'i_dig_sql'
sql = I_Dig_Sql.new
sql[:HEROES] = "SELECT id FROM hero WHERE id = :PERSON_ID"
sql[:VILLIANS] = "SELECT id FROM villian WHERE id = :PERSON_ID"
sql << %^
SELECT *
FROM people
WHERE
id IN ( << HEROES >> AND status = :ALIVE)
OR
id IN (SELECT ID FROM {{ HEROES }} AND status = :ALIVE)
OR
id IN ( << * HEROES >> )
OR
id IN ( << patron_id VILLIANS >> )
OR
id IN ( << VILLIANS >> )
^
sql.to_sql