Simulating database table in Haskell
This is a simulation application which has done by using Haskell.
Haskell is a functional programming language which is a different programming paradigm.
This is a prototype for a banking system which can
- Register New Account
- Delete Account
- Withdraw money
- Check Balance
- Transfer Money
This is my table
module Database where
data Table = Table
String
[String]
[[String]]
deriving (Read, Show)
accounts = Table
"Bank Accounts - APIIT Secure Bank"
["ID","Name","Balance "]
[["1","Viduruvan Suvinith","100000.00"]
,["2","Tharindu Roshantha","100000.00"]
,["3","Upeka Gunawardhena","100000.00"]
,["4","Chandima Godawaththa","25000.00"]
,["5","Damee Veena","100000.00"]
,["6","Malaka Gunarathne","35000.00"]
,["7","Asanka Mahendra","15000.00"]
,["8","Chamath de Silva","156000.00"]
,["9","Aravinth","154000.00"]
,["10","Kamal Jayasooriya","3000.00"]
]
These are the basic functions of Table
module DBFunctions where
import Prelude -- call for perlude libarary
import List -- list livararay
import Database
--http://stackoverflow.com/questions/830021/haskell-writing-text-files-and-parsing-them-back-to-original-format
-- Print Table
{- setC- Set the Columns
Input [List of String]
Output String
Funcatnality Take List of String and covert them in to a single string
Steps
Step 1 : IF list is a empty list return ""
IF list is not empty this goes directly to next step
Step 2 ; In this step it runs primitive recursion
take first element of the list and recursive till list is empty.
-}
setC :: [String] -> String -- The input of this function is a list of string what this does is it takes that list and concatinate
setC [] = "" -- Checks wheather colomn is emplty , actually list of string is empty
setC (ns:ms) = ns ++ "\t\t" ++ "|" ++ setC ms -- Premitive Recursion
{- setR - Set the Rows
Input [List of List of String]
Output String
Funcatnality Take list of list of String and covert them in to a single string
Steps
Step 1 : IF list is a empty list return ""
IF list is not empty this goes directly to next step
Step 2 ; In this step it runs primitive recursion
take first element of the list(which is also a another list) and pass it to SetC function and recursive till the list is empty.
-}
setR :: [[String]] -> String
setR [] = ""
setR (ns:ms) = "|" ++ setC ns ++ "\n" ++ setR ms -- Premitive Recursion
{- writeTable - writeTable
Input Table which is user defined data type
Output String (because to print or show data in user interface it has to use string
Funcatnality Simply this convert the given table to a String which can use to print
In first line it take table name and print it
Then it takes Column which in this case header of the table
Then it takes all other data which is a list of list of string and put it to setRow function.Set Rrow function convert it to a single String
-}
writeTable :: Table -> String
writeTable (Table name column rows) = "\n\n" ++ "\t\t" ++ name -- This line for printing name of the table and this concatinate with next line
++ "\n\n"
++ "\n"
++ setC column ++ "\n" -- Take colomns of the database
++ "-----------------------------------------------" ++ "\n"
++ setR rows ++ "\n"
{- print table function is a simple IO function which prints the table -}
viewTable :: Table -> IO()
viewTable = putStr.writeTable
--Insert
insertrecord :: Table -> [String]->Table
insertrecord (Table name column rows) xs = Table name column (insert xs rows)
--Delete
deleterecord :: Table -> [String]->Table
deleterecord (Table name column rows) xs = Table name column (delete xs rows)
--Select
retriewElement :: String -> [String] -> Int
retriewElement getColumn (selColumn:column) = if (getColumn == selColumn)
then 0
else
1 + retriewElement getColumn column
selectrecord::String->(String->Bool)->Table->Table
selectrecord getColumn p (Table name column rows) = Table name column [ rows | rows <- rows, p( rows !! retriewElement getColumn column )]
--Update
-- update ”<column name>” (<condition>) (“<new value>”) <Table Name>
update :: String->(String->Bool)->String-> Table -> Table
update colname con nvalue (Table name column rows) = Table name column (getupRows con colname nvalue rows)
-- singlerowUp <Selected Row> ”<column name>” (“<new value>”) (<condition>)
singlerowUp::[String]->String->String->(String->Bool)->[String]
singlerowUp x selcol nvalue con=
do
if( (selcol=="ID" && (filter (con)[x!!0])==[x!!0])) then nvalue:x!!1:x!!2:[]
else if (selcol=="Name" && (filter (con)[x!!1])==[x!!1])then x!!0:nvalue:x!!2:[]
else if (selcol=="Balance" && (filter (con)[x!!2])==[x!!2] )then x!!0:x!!1:nvalue:[]
else do x!!0:x!!1:x!!2:[]
-- getupRows (<condition>) ”<column name>” (“<new value>”) <raws>
getupRows::(String->Bool)->String->String->[[String]]->[[String]]
getupRows con colname nvalue (xss)=[(singlerowUp x colname nvalue con)|x<-xss]