File: Queries.hs

package info (click to toggle)
haskell-hsql 1.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 656 kB
  • ctags: 43
  • sloc: sh: 2,723; makefile: 192; haskell: 174; ansic: 37
file content (33 lines) | stat: -rwxr-xr-x 1,026 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Queries where

import Database.HSQL

createTables :: Connection -> IO ()
createTables c = do
	execute c "create table Test(id integer not null, name varchar(255) not null)"

dropTables :: Connection -> IO ()
dropTables c = do
	execute c "drop table Test"
	
insertRecords :: Connection -> IO ()
insertRecords c = do
	execute c "insert into Test(id,name) values (1,'Test1')"
	execute c "insert into Test(id,name) values (2,'Test2')"
	execute c "insert into Test(id,name) values (3,'Test3')"
	execute c "insert into Test(id,name) values (4,'Test4')"

retrieveRecords :: Connection -> IO [(Int,String)]
retrieveRecords c = do
	query c "select id, name from Test" >>= collectRows getRow
	where
		getRow :: Statement -> IO (Int,String)
		getRow stmt = do
			id   <- getFieldValue stmt "id"
			name <- getFieldValue stmt "name"
			return (id,name)

getMetaInfo :: Connection -> IO [(String,[FieldDef])]
getMetaInfo c = do
	ts <- tables c
	mapM (\t -> describe c t >>= \cs -> return (t,cs)) ts