I would like to create a system that auto-generates correct SQL statements for storing and retrieving Haskell data structures from a SQL database. Doing this requires a package that manipulates the structure of Haskell datatypes. It appears that there are two packages to do this: Derive (or whatever its current name is) PolyP I have used niether (but the derive documentation seems easier to follow). Can someone give a recommendation about which package would be more appropriate for this task? Details follow. --- INSERT The idea is that if you have a data structure like > data MyType = Foo Int String | Goo String String You would have functions that work like this: > toSQLInsert f@(Foo x string) = > "INSERT into Foo VALUES "++(show (hash f,x,string)) > toSQLInsert g@(Goo s1 s2) = > "INSERT into Goo VALUES "++(show (hash g,s1,s2)) > hash x = md5Hash $ show x -- reason for hash is below More generally, the system would also handle > data MyType2= Bar String MyType generating > toSQLInsert b@(Bar s2 myInstance) = > "INSERT into Bar VALUES "++ > (show (hash b,s2,tableName myInstance,hash myInstance)++ > '\n':(toSQLInsert myInstance) > tableName myInstance = constructorName myInstance -- + type information? hash functions are used to generate foreign keys for joins by select statements with the table defined in tableName. Ideally I would be able to cover parametrized types, but I am not yet sure how that should work. writing functions based on the structure of data types. Can someone recommend one or the other as being particularly better? My instinct is that derive is better because it gives you straightforward access to constructur names, but I am new to this. --- CREATE Table The system should be able to generate "CREATE TABLE" statements corresponding to the above INSERT system. The difficulty here is correct handling of parametrized types. The system should generate functions that take types as argument and output table create like: >createTable_MyType = "CREATE TABLE Foo > (c_1 Integer not null, > (c_2 varchar not null) I am not sure how to deal with the reality of fixed length strings, but I would guess I can annotate in some way. ---- SELECT On the select side, Heribert Schultz showed me a prototype of a Monadic system for composing SQL queries. It would be nice if this monad system has access to the names of selector functions of record types so the programmer would not have to deal with remembering the structure of the SQL database. ---- BONUS Ideally there would be an inverse of this system that imports database schema into Haskell datastructures, but that is a lower priority (for me). -Alex- ___________________________________________________________________ S. Alexander Jacobson i2x Media 1-212-697-0184 voice 1-212-697-1427 fax
Autogenerating SQL code w/ Derive or PolyP?
S. Alexander Jacobson Tue, 13 Oct 1998 10:34:15 -0400 (Eastern Daylight Time)