Difference between revisions of "Applications and libraries/Database interfaces/HaskellDB"

From HaskellWiki
Jump to navigation Jump to search
(Added a remark about compiling problems and a link to the HaskellDB Talk Trailer)
(note on building, links to papers, examples)
Line 5: Line 5:
   
 
All the queries and operations are completely expressed within Haskell, no embedded (SQL) commands are needed.
 
All the queries and operations are completely expressed within Haskell, no embedded (SQL) commands are needed.
  +
  +
== Status ==
  +
  +
HaskellDB, pulled from it's 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 ==
 
== Homes ==
Line 10: Line 18:
 
=== Current Version ===
 
=== Current Version ===
 
The current repository is stored at code.haskell.org.
 
The current repository is stored at code.haskell.org.
There was a [http://www.haskell.org/pipermail/haskell-cafe/2008-December/051557.html complaint on Haskell-cafe] that HaskellDB does not compile with GHC 6.10.1.
+
There was a [http://www.haskell.org/pipermail/haskell-cafe/2008-December/051557.html 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:
 
The following procedure worked as of October, 2008:
Line 38: Line 46:
 
runghc Setup.hs build
 
runghc Setup.hs build
 
sudo runghc Setup.hs install
 
sudo runghc Setup.hs install
  +
* Example Part 1: Compile a description of a database
 
 
=== [http://www.haskell.org/haskellDB/index.html Daan Leijen's original version] ===
 
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 record]]s).
  +
 
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.
  +
 
=== [http://haskelldb.sourceforge.net Chalmers version] ===
 
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 record]]s (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 [http://www.cs.chalmers.se/~bringert/publ/haskelldb/haskelldb-db-2005.pdf HaskellDB -- Type safe declarative database combinators].
  +
 
== Other materials ==
  +
 
=== How to use ===
  +
  +
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.
  +
  +
<haskell>
 
-- Compile with:
 
-- Compile with:
 
-- ghc --make <filename>
 
-- ghc --make <filename>
Line 69: Line 98:
 
-- Create the database tables
 
-- Create the database tables
 
withDB "testdsn" $ \db -> dbSpecToDatabase db testdb
 
withDB "testdsn" $ \db -> dbSpecToDatabase db testdb
  +
</haskell>
   
  +
Now we can express queries in a nice declarative and type-safe manner:
* Example Part 2: Query the database and print results.
 
  +
  +
<haskell>
 
-- Compile with:
 
-- Compile with:
 
-- ghc --make <filename>
 
-- ghc --make <filename>
Line 104: Line 136:
 
rs <- withDB "testdsn" $ \db -> getSomeData db
 
rs <- withDB "testdsn" $ \db -> getSomeData db
 
putStrLn $ foldl (\str (a,b) -> str ++ "(" ++ (show a) ++ "," ++ (show b) ++ ")\n") "" rs
 
putStrLn $ foldl (\str (a,b) -> str ++ "(" ++ (show a) ++ "," ++ (show b) ++ ")\n") "" rs
  +
</haskell>
   
  +
For more examples, see [http://haskelldb.sourceforge.net/getting-started.html Getting Started] and [http://haskelldb.sourceforge.net/guide/ Guide to Hacking].
=== [http://www.haskell.org/haskellDB/index.html Daan Leijen's original version] ===
 
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 record]]s).
 
   
  +
=== ICS Wiki (FIXME: links seem broken) ===
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.
 
 
=== [http://haskelldb.sourceforge.net Chalmers version] ===
 
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 record]]s (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 [http://www.cs.chalmers.se/~bringert/publ/haskelldb/haskelldb-db-2005.pdf HaskellDB -- Type safe declarative database combinators].
 
 
== Other materials ==
 
 
=== ICS Wiki ===
 
 
[http://abaris.zoo.cs.uu.nl:8080/wiki/ ICS Wiki] is a huge resource of Haskell materials (among others).
 
[http://abaris.zoo.cs.uu.nl:8080/wiki/ ICS Wiki] is a huge resource of Haskell materials (among others).
 
* [http://abaris.zoo.cs.uu.nl:8080/wiki/pub/Afp/DomainSpecificLanguages/haskelldb.pdf Database programming with HaskellDB] -- a PDF file (slides). A webpage containing a link to it (among others): the bottom (attachment part) of [http://abaris.zoo.cs.uu.nl:8080/wiki/Afp/DomainSpecificLanguages the ''Domain Specific Languages'' page of ICS Wiki].
 
* [http://abaris.zoo.cs.uu.nl:8080/wiki/pub/Afp/DomainSpecificLanguages/haskelldb.pdf Database programming with HaskellDB] -- a PDF file (slides). A webpage containing a link to it (among others): the bottom (attachment part) of [http://abaris.zoo.cs.uu.nl:8080/wiki/Afp/DomainSpecificLanguages the ''Domain Specific Languages'' page of ICS Wiki].
Line 126: Line 147:
 
=== Other ===
 
=== Other ===
 
* [http://www.vimeo.com/1983774 HaskellDB Talk Trailer]
 
* [http://www.vimeo.com/1983774 HaskellDB Talk Trailer]
  +
* [http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=8F1C49953E49974D927950052C15CA41?doi=10.1.1.26.6906&rep=rep1&type=pdf Original HaskellDB paper]
 
   
 
== Future ==
 
== Future ==

Revision as of 05:45, 13 February 2009

Introduction

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

HaskellDB, pulled from it's 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

Current Version

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

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

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

How to use

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
        putStrLn $ foldl (\str (a,b) -> str ++ "(" ++ (show a) ++ "," ++ (show b) ++ ")\n") "" rs

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

ICS Wiki (FIXME: links seem broken)

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

Other

Future

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. Also it uses HList.

Related concepts

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