All Unkept
Posted in: Haskell, Web development  —  July 20, 2007 at 10:54 PM

Haskell Blog Rewrite - Session 4 - 2007-07-19

by Luke Plant

Aims

  • Get HaskellDB working
  • Create schema for blog posts (possibly also comments, categories and quips)
  • Experiment with using HaskellDB

Notes

  • (all relative paths are inside the 'HaskellDB' darcs checkout)
  • to make API documentation for HaskellDB, use 'runhaskell Setup.hs haddock', output is in dist/doc/ . You could also browse the online API documentation, but I don't know if it corresponds very well to what is in darcs
  • See HaskellDB - Getting Started to get started with creating tables and so on, but note that the build instructions are very out of date, and there seems to be lots of other problems with the documentation.
  • Since I haven't created my DB tables yet, I want to start with Haskell code and generate them from there. The above link, plus 'test/old/dbspec.hs', will tell you what you need to know. (You can use 'DBDirect' program to go the other way).
  • There are multiple versions of HaskellDB, which confuses things, and documentation is somewhat scattered. The best I've found so far is: http://haskelldb.sourceforge.net/guide/
  • The 'test/old/dbspec.hs' file, from the above 'Getting started' link, doesn't compile - 'genericConnect' not in scope. Finding out how to actually do a database connection is proving tricky -- browsing API docs is really hard, I need to get hoogle set up or something ... (30 minutes getting lambdabot, hoogle, and seeing if I can generate a hoogle database for HaskellDB. Lambdabot won't build (neither v4.0.1, or darcs HEAD), so give up here, and go back to grep)
  • Aha -- I also should have built API documentation in the 'driver-dynamic/' directory, to get docs for 'DynamicConnect'
  • In the end I used 'mysqlConnect', and successfully created tables
  • Session over, 1h 44m, only really managed the first of my aims.

Code

dbspec.hs

module Main where

import Database.HaskellDB
import Database.HaskellDB.FieldType
import Database.HaskellDB.DBSpec
import Database.HaskellDB.HSQL.MySQL

import qualified Settings

test = DBInfo {dbname = "ctest", opts = testopts, tbls = [testtbl1,testtbl2]}

testopts = DBOptions {useBString = False}

testtbl1 = TInfo {tname = "ctesttbl1", cols = [testcol11,testcol12]}
testtbl2 = TInfo {tname = "ctesttbl2", cols = [testcol21,testcol22]}

testcol11 = CInfo {cname = "ctestcol11", descr = (IntT,False)}
testcol12 = CInfo {cname = "ctestcol12", descr = (BStrT 8,True)}

testcol21 = CInfo {cname = "ctestcol21", descr = (BStrT 6,False)}
testcol22 = CInfo {cname = "ctestcol22", descr = (IntT,True)}

main = do
       let db = mysqlConnect MySQLOptions { server = Settings.dbhost,
                                            db = Settings.dbname,
                                            uid = Settings.dbusername,
                                            pwd = Settings.dbpassword }
       db (\a -> dbSpecToDatabase a test)

settings.hs

module Settings where

dbhost = "localhost"
dbname = "hdb_test_db"
dbusername = "tester"
dbpassword = "test"

Command line:

ghc --make -package haskelldb -o dbspec dbspec.hs settings.hs

Comments §

blog comments powered by Disqus