Applications and libraries/Database interfaces/HaskellDB/Archive

From HaskellWiki

THIS PAGE CONTAINS OUTDATED AND INACCURATE INFORMATION.[edit]

This page exists for archival purposes only. Current information can be found on Applications and libraries/Database interfaces/HaskellDB.

Introduction[edit]

A combinator library for declarative, type safe database management. A domain specific embedded language, containing the concept of extensible record and a special Query monad (among other powerful ideas, see #Related concepts).

All the queries and operations are completely expressed within Haskell, no embedded (SQL) commands are needed.

Status[edit]

HaskellDB, pulled from its repository, builds on GHC 6.10.1.

Some of HaskellDB HDBC backends (ODBC and SQLite3 to be exact) have been updated to work with recent HDBC (2.1 as of this writing).

This library is in need of some tender love and care. Drop a mail at HaskellDB mailing list if you're interested in improving code or documentation.

Homes[edit]

Current Version[edit]

The current repository is stored at code.haskell.org. There was a complaint on Haskell-cafe that HaskellDB does not compile with GHC 6.10.1. This is only partially true: one can darcs get HaskellDB repository and build it themselves.

The following procedure worked as of October, 2008:

  • GHC 6.8.3
  • Cabal 1.6(?)
  • HDBC 1.1.5
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HDBC
runghc Setup.lhs configure
runghc Setup.lhs build
sudo runghc Setup.lhs install
  • HDBC.ODBC 1.1.4.4
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HDBC-odbc
runghc Setup.hs configure
runghc Setup.hs build
sudo runghc Setup.hs install
  • HaskellDB - patch 743
darcs get http://code.haskell.org/haskelldb
runghc Setup.hs configure
runghc Setup.hs build
sudo runghc Setup.hs install
cd driver-hdbc
runghc Setup.hs configure
runghc Setup.hs build
sudo runghc Setup.hs install
cd ../driver-hdbc-odbc
runghc Setup.hs configure
runghc Setup.hs build
sudo runghc Setup.hs install

Daan Leijen's original version[edit]

It makes possible to use Haskell's typecheck system for a type-safe, declarative database management -- a combinator library. It can prevent the user even from using multiple labels in the same record, but the price for this: it needs a special extension of Haskell called Trex (providing extensible records).

HaskellDB was originally developed by Daan Leijen, and is described in the paper Domain Specific Embedded Compilers, Daan Leijen and Erik Meijer. 2nd USENIX Conference on Domain-Specific Languages (DSL), Austin, USA, October 1999.

Chalmers version[edit]

A student project by Björn Bringert, Anders Höckersten, Conny Andersson, Martin Andersson, Mary Bergman, Victor Blomqvist, Torbjörn Martin.

It works well with the most common Haskell implementations, because extensible records (without check for multiple labels) are implemented in a way which does not need the Trex extension of Haskell.

See also Björn Bringert's slides HaskellDB -- Type safe declarative database combinators.

Other materials[edit]

How to use[edit]

HaskellDB needs database schema expressed in Haskell to operate (not only a schema, but also some boilerplate, too: fortunately, it can be derived automagically). Furthermore, HaskellDB description and real database schema should be kept in sync.

Here's an example of creating both database tables and appropriate Haskell boilerplate out of database specification.

 -- Compile with:
 --   ghc --make <filename>
 ---------------------------------
 -- Compile a Haskell description of a database (from a DBInfo)
 --
 import Database.HaskellDB
 import Database.HaskellDB.DBSpec.DBSpecToDBDirect
 import Database.HaskellDB.DBSpec
 import Database.HaskellDB.HDBC.ODBC
 import Database.HaskellDB.DBSpec.PPHelpers

 ---------------------------------
 -- Connect to a DSN through ODBC
 withDB dsn = (connect driver) [ ("dsn",dsn) ]

 ---------------------------------
 -- An example Database
 testdb    = DBInfo {dbname = "test", opts = testopts, tbls = [testtbl1]}
 testopts  = DBOptions {useBString = False, makeIdent = mkIdentPreserving}
 testtbl1  = TInfo  {tname = "test_tbl", cols = [testcol11,testcol12]}
 testcol11 = CInfo  {cname = "c11", descr = (IntT,False)}
 testcol12 = CInfo  {cname = "c12", descr = (IntT,True)}

 ---------------------------------
 main = do
        -- Generate the database type import files from the DBInfo structure
        dbInfoToModuleFiles "" "Test" testdb

        -- Create the database tables
        withDB "testdsn" $ \db -> dbSpecToDatabase db testdb

Now we can express queries in a nice declarative and type-safe manner:

 -- Compile with:
 --   ghc --make <filename>
 ----------------------------------------------
 -- Query the database and print results
 --
 import Database.HaskellDB
 import Database.HaskellDB.DBSpec.DBSpecToDBDirect
 import Database.HaskellDB.DBSpec
 import Database.HaskellDB.HDBC.ODBC

 -- This file generated from "dbInfoToModuleFiles" function in Part 1.
 import Test.Test_tbl

 ---------------------------------
 -- Connect to a DSN through ODBC
 withDB dsn = (connect driver) [ ("dsn",dsn) ]

 ---------------------------------
 -- Get all the rows from the test table
 getSomeData :: Database -> IO [(Int,Maybe Int)]
 getSomeData db = do
        let q = do
            t <- table test_tbl
            order [desc t c11]
            return t
        rs <- query db q
        return $ map (\row -> (row!c11, row!c12)) rs

 ---------------------------------
 main = do
        -- Query the database
        rs <- withDB "testdsn" $ \db -> getSomeData db
        mapM_ (putStrLn.(\(a,b) -> "(" ++ (show a) ++ "," ++ (show b) ++ ")")) rs

For more examples, see Getting Started and Guide to Hacking.

ICS Wiki (FIXME: links seem broken)[edit]

ICS Wiki is a huge resource of Haskell materials (among others).

Other[edit]

Future[edit]

HList --- a Haskell library for strongly typed heterogeneous collections includes also extensible records. Its relatedness to database programming is described in the articles, see also its possible relatedness to HaskellDB project.

To explore other declarative, type safe database managament approaches, see [[../CoddFish|CoddFish]]. Also it uses HList.

Related concepts[edit]

Concepts which are concerned by the papers of the two official HaskellDB homes