hesql - Haskell's embedded SQL
hesql is a preprocessor. It reads a file (module) with
SQL-like functions and translates it to Haskell source code.
It uses HDBC.
The result of a select statement is automatically converted
to list of tuples. Also fromSql/toSql conversion are done.
The current implementation is just a proof of concept with narrow limits.
It sticks to a subset of PostgreSQL's variant of SQL. Don't expect too
much, but please send me patches to improve hesql.
An example is found under example/Account.hesql. It works with this
CREATE TABLE Account
(AccountID SERIAL PRIMARY KEY,
Login TEXT NOT NULL,
Password TEXT NOT NULL,
Email TEXT NOT NULL,
LoggedIn BOOL DEFAULT FALSE NOT NULL,
Locked BOOL DEFAULT FALSE NOT NULL,
LastRequest TimeStamp DEFAULT 'now' NOT NULL,
Validated BOOL DEFAULT FALSE NOT NULL
CREATE UNIQUE INDEX AccountIdx ON Account(Lower(Login));
A select query may return a list or just a maybe type. If you
just want the maybe type, use
maybe select a from t where e;
It is planned, that a prefix "lazy" should work in the same way. Then with
"lazy", the lazy HBDC-fetchAllRows are used.
verifyLogin login password =
maybe select Accountid, Login, RealName, Email from Account
where (Password is null or Password = md5(password)) and Lower(Login) = login
and not Locked;
will generate code for a Haskell-function with the type:
verifyLogin :: Stmts -> String -> String -> IO (Maybe (Int, String, String, String))
(To be honest, the type is simplified by me. hesql won't write any type annotation.)
Each module will have a function
init :: Connection -> IO Stmts
it prepares each statement for the later use.
- add a separate TODO file
- parse group by, having, column/table aliases, subselects
- verify table and column names
- resolve column names in SELECT * FROM ...
- fix command line argument parsing
- improve documentation
- make backup of old .hs-File
$ sudo make install
$ createdb account
$ psql account
# create the account table from above
$ cd example/
$ hesql Account.hesql dbname=account
$ less Account.hs
hesql depends on
- HDBC with PostgreSQL backend
- haskell-src (to print the haskell code)
- parsec (for the SQL parser)
- do we have to call finish on Maybe-Statements?