Applications and libraries/Database interfaces/HaskellDB

From HaskellWiki
< Applications and libraries‎ | Database interfaces
Revision as of 23:47, 6 November 2008 by Bryan Turner (talk | contribs) (Build & install quick-start, with micro-example to get things rolling.)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.

Homes

Current Version

The current repository is stored at code.haskell.org. 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
  • Example Part 1: Compile a description of a database
-- 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
  • Example Part 2: Query the database and print results.
-- 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

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

ICS Wiki

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

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