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

From HaskellWiki
Jump to navigation Jump to search
(Build & install quick-start, with micro-example to get things rolling.)
(Added a remark about compiling problems and a link to the HaskellDB Talk Trailer)
Line 9: Line 9:
   
 
=== Current Version ===
 
=== Current Version ===
The current repository is stored at code.haskell.org. The following procedure worked as of October, 2008:
+
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.
  +
  +
The following procedure worked as of October, 2008:
 
* GHC 6.8.3
 
* GHC 6.8.3
 
* Cabal 1.6(?)
 
* Cabal 1.6(?)
Line 120: Line 123:
 
* [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].
 
* There are other links to HaskellDB materials in the [http://abaris.zoo.cs.uu.nl:8080/wiki/Afp/DomainSpecificLanguages#4_Haskell_DB HaskellDB section of the same page].
 
* There are other links to HaskellDB materials in the [http://abaris.zoo.cs.uu.nl:8080/wiki/Afp/DomainSpecificLanguages#4_Haskell_DB HaskellDB section of the same page].
  +
  +
=== Other ===
  +
* [http://www.vimeo.com/1983774 HaskellDB Talk Trailer]
  +
   
 
== Future ==
 
== Future ==

Revision as of 11:05, 7 December 2008

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. There was a complaint on Haskell-cafe that HaskellDB does not compile with GHC 6.10.1.

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).

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