With Ruby's ActiveRecord or Sequel, you can progressively build up SQL queries, adding where
or join
or order
clauses to a query depending on conditions at run time.
Here is a simple example, taken from ASCIIcasts:
def index
@articles = Article.order('name')
if params[:hidden]
@articles = @articles.where(:hidden =>(params[:hidden] == "1"))
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
The example shows how a WHERE clause can be appended to the underlying SQL query on the articles
table if an HTTP request query parameter named hidden
equals 1
.
I've been looking at HDBC and postgresql-simple in Haskell. It seems that postgresql-simple purposely makes it difficult to build up SQL queries from dynamically concatenated parts, in order to prevent SQL injection. HDBC does seem to be flexible enough to build up differently structured SQL queries based on conditions at runtime, but it doesn't seem to provide the level of abstraction that either ActiveRecord or Sequel provide.
Can anyone suggest a nice way to emulate ActiveRecord's dynamic query-building facilities using one of the Haskell database libraries?
If HDBC is the way to go, I'm fine with that. But an illustration would be appreciated.
I guess what I am lookinng for is the ability to dynamically compose queries, against a PostgreSQL backend.
You may want Persistent, you may want HaskellDB, you may want something like Esqueleto.
Here's a good discussion of the tradeoffs of each: http://blog.felipe.lessa.nom.br/?p=68