[Ur] SQL query generation

Adam Chlipala adamc at impredicative.com
Thu Aug 11 19:50:50 EDT 2011


Vladimir Shabanov wrote:
> Ur allows us to generate any part of XML using
>      <xml>{...}</xml>.
>
> But with SQL only variable values can be specified:
>      SELECT ... WHERE id = {[id]}
>    

It's not true that only variables can go between {[...]} in SQL.  Any 
expressions are allowed (parsing-wise).

> Is is possible to extend Ur to support generation of SQL expressions?
> E.g. something like
>     SELECT ...
>     ORDER BY column {if descending then DESC else ASC}
>    

That particular example would need a change to the frontend, since no 
one has asked for it before.  Feel free to submit a Mantis bug if you'd 
like!

> or
>     SELECT ...
>     WHERE column1>  {[val1]}
>         {if filtering then {AND col2 = x} else {}}
>    

This is already supported with only slightly different code.  I'd suggest:

column1 > {[val1]} AND {if filtering then (SQL col2 = x) else (SQL TRUE)}

> I need to make some queries which are largely the same but differs in
> sorting order and/or have additional conditions. Currently I need to
> copy&paste all the query and change few lines of code. Maybe there is
> a better solution?
>    

Like Hao said, there is nothing fundamental about the parsing extensions 
that generate SQL queries.  Underneath it all are combinators you can 
call directly and whose types you can find in lib/ur/basis.urs.

Still, the parsing extensions lead to much prettier code, so please 
always feel free to suggest new extensions.



More information about the Ur mailing list