Complete rewrite (with testsuite and haskell types for database types)