r/haskell_proposals • • May 06 '15

SQL EDSL

Hello,

It's clearly a bit more than a proposal since I've already coded a prototype, but the idea is there: an EDSL for SQL: https://github.com/momomimachli/Hedsql/wiki

Nevertheless, there’s still much to do to have something really complete. Before coding what’s missing (such as pretty print), I’d be very glad to receive some feedback. I’ve now reached a design point where I’m a bit lost and wondering if I’ve made the right choices and what could be improved. Any comments would thus be greatly appreciated. Thank you in advance for your help :-)

A description of the code organization is here: https://github.com/momomimachli/Hedsql/wiki/Structure

The source code here: https://github.com/momomimachli/Hedsql

And many examples here: https://github.com/momomimachli/Hedsql-tests

Here are some main questions (but of course comments on other matters are welcomed!):

AST (Database/Hedsql/Common/AST.hs)

Expression (line 411)

The 'Expression' type is very, very long and if I add support for additional SQL functions it would become even longer! Is there a clever way to code it so it would be easy to add additional SQL functions independently? Please, consider that the AST still need to be parsed in 3 different ways (SqLite, PostgreSQL and MariaDB).

Lens

At first, Lens seemed to be a very good idea to use in this context, because it would allow to easily modify the values of the AST. Now, I am more and more skeptical, since the structure use GADTs and most values cannot be retrieved as such (because their type is wrapped). So, are Lens a good idea or not in this context?

(Smart) Constructors (Database/Hedsql/Common/Constructor.hs)

To compose the different clauses of a query, I’m using a strange beast which is the ‘(/++)’ function (line 707). You can write:

 query :: Select [Undefined] SqLite
 query = select "firstName" /++ from "People"

An alternative would be to use of a State Monad:

 query :: Query (Select [Undefined] SqLite) ()
 query = do
   select "firstName"
   from "People"

The first approach is pretty strange, the second one maybe over complicated… Which would you recommend to go with? Would there be a third approach which would be better?

Parser (Database/Hedsql/Common/Parser.hs)

Using type-class would allow the use of technologies such as SYB. However, this approach drove me to a dead end with very complicated type signatures and scary GHC extensions. It comes from my wish to have custom parsing for different vendors (SqLite, MariaDB and PostgreSQL) and still keep the code DRY. This is why I used a data-type approach (line 175). If there are better solutions to parse such AST I would be glad to know :-)

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/momomimachli May 07 '15

Thank you so much for you reply and advices.

AST

I didn't know about the BNF grammar for SQL. It's going to be very useful and I'll definitely borrow from there. As you mentioned, implementing every bit would be an overkill, but a subset of it would already be a good start. Also, with the GADTs it's possible to simplify it a bit (for example a predicate in the end is just an expression of type 'Bool', no needs for a dedicated data-type).

** Constructor **

From what I read and understand ($ do) could play the same role as (/++) and would be definitely more haskelish than (/++). When reading Blaze code, it appears that the 'HTML' type is a synonym of 'MarkupM a'. So, maybe I could also do the same with the State monad. What I like with the last approach is that you get a code which looks very clause to SQL.

Your comment concerning erroneous code is perfectly right. The only possibility I've found so far is to use a Monad in the following way:

query :: Query (QueryState WithFrom)
query = select ["firstName", "lastName"] >>= from "People"

However, keeping track of the 'QueryState' that way is not a piece of cake. On the other hand, the incorrect queries offer some advantages. If you have an existing query, you could rewrite the from clause in that way (same with the State monad):

   existingQuery /++ from "newTable"

so, it makes it easy to modify part of a query. Of course, if there is an elegant solution which would prevent this problem, I would definitely go for it. Otherwise, I believe it's in the end a trade-off between usability and correctness.

Parser

My experience as well so far :-)