Contribution:
haskellDB
Headline
Schema-aware database programming with Technology:HaskellDB
Motivation
The implementation demonstrates schema-aware database programming in Language:Haskell with Technology:HaskellDB. That is, the database schema of a database for companies is mapped to Haskell types. Database queries are expressed as Haskell functions on top of Haskell types that are derived by Technology:DBDirect, which is part of Technology:HaskellDB. The functions leverage HaskellDB's library for relational algebra and other CRUD expressiveness. For comparison, see Contribution:hdbc for a demonstration of schema-unaware database programming in Haskell where database queries are expressed in Language:SQL whose execution is embedded into Haskell. Technology:DBDirect derives one module per database table. These modules are the basis for accessing the database within the Haskell program in a schema-aware manner. For instance, operations for totaling and cutting salaries are implemented with the module for employees and while leveraging combinators for projection, selection, aggregation, and renaming. With the use of relational algebra and library functions for other CRUD expressiveness, all operations are effectively independent of the underlying DBMS <cite>bringert2004student</cite>. The connection to the underlining Technology:MySQL database is achieved by means of a "HaskellDB-Technology:HDBC-Technology:ODBC" back-end.
Illustration
Connecting to the database
We provide a function to connect to a database and execute an action:
execute :: (Database -> IO a) -> IO a
execute = connect driver conf
where
conf = [ ("Driver","MySQL ODBC 5.1 Driver")
, ("Port","3306")
, ("Server", "localhost")
, ("User", "root")
, ("Database", "101companies") ]
We use
connect
driver
Database -> IO a
execute
a
DBDirect
We use the DBDirect command
Totaling
We import the description modules for the companies and employees tables:
import qualified DBDesc.Employee as E
import qualified DBDesc.Company as C
We define a special field for storing the sum of all salaries:
data Ttl = Ttl
instance FieldTag Ttl where fieldName = "ttl"
ttl :: Attr Ttl Double
ttl = mkAttr Ttl
We declare
Ttl
FieldTag
mkAttr
ttl
Double
total :: String -> Query (Rel (RecCons Ttl (Expr Double) RecNil))
total cname = do
es <- table E.employee
cs <- table C.company
restrict $
( fromNull (constant 0) (cs!C.xid) .==. es!E.cid
.&&.
cs!C.name .==. constant cname )
project (ttl << sum (es!E.salary))
We are working in the
Query
table
restrict
cname
project
sum
ttl
Executing the query
We use
query
execute
let cname = "meganalysis"
[res] <- execute $ (flip $ query) $ total cname
This gives us a list (which we expect to be a singleton list) of records. We can now access the
ttl
res
(!)
print $ res!ttl
Architecture
We provide MySQL-scripts to create (see this!!Company.sql) company tables and populate (see this!!Meganalysis.sql) these tables. this!!Total.hs and this!!Cut.hs provide totaling and cutting functionality using HaskellDB's relational algebra library. this!!MyConnection.hs encapsulates the process of connecting to the MySQL database. this!!Main.hs collects test scenarios for totaling and cutting.
Usage
Setup
- Follow the steps of setting up the database as described in the usage section for the hdbc implementation.
Generating the database description
- Execute the following command in the implementation folder:
- Compile the DBDesc module using GHC: Invalid Language supplied
Testing
- this!!Main.hs has to be loaded into GHCi.
- The function has to be applied.
main
- The output should be equal to the content of the file this!!baseline.
Backlinks
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.