This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Ruby: Why sqldsl?
Feed Title: Jay Fields Thoughts
Feed URL: http://feeds.feedburner.com/jayfields/mjKQ
Feed Description: Blog about Ruby, Agile, Testing and other topics related to software development.
Much better support for conditional statement building.
"select column1 from table1, table2 where table1.column1 = table2.table1_id #{"and table1.column2 >= #{quantity}" if quantity > 0}"
# becomes
Select[:column1].from[:table1, :table2].where do table1.column1 = table2.table1_id table1.column2 >= quantity if quantity > 0 end
Fail without requiring a database trip for referencing tables in the where statement that do not appear in the table list.
Select[:column1].from[table1].where do table1.column1 = table2.column1 end #=> Raises an error stating that table2 does not exist in the from clause.
Open for extension where necessary. For example, ActiveRecord::Base.execute could be changed to take a block.
class ActiveRecord::Base alias original_execute execute def execute(arg, &block) original_execute block.call.to_sql(connection) end end
If this change were made, the sqldsl library could use the connection to create vendor specific sql. This allows for things like changing sql to be prepared statement compatible when the oracle adapter supports this in the future. The point is, as long as your sql is nothing more than a string it is not able to be easily modified. However, an object representation of your sql could be evaluated in various contexts to produce different results.