Skip to content

Commit

Permalink
Updated printing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Antony authored and Franklin Antony committed Dec 4, 2020
1 parent a858b44 commit 46609b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
20 changes: 16 additions & 4 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Http
import Parse
import Database


main :: IO ()
main = do
let url = "https://date.nager.at/api/v2/publicholidays/2020/GB"
Expand All @@ -17,13 +18,24 @@ main = do
print recs
-- Print 5 records
-- Right recs -> print . (take 5) $ (HolidayRecord recs)
print "Saving on DB"
putStrLn "Saving on DB"
putStrLn "\n****************"
conn <- initialiseDB
insertDB conn recs
insertSB conn recs
insertLB conn recs
print "Done!"
putStrLn "Done!"
putStrLn "\n****************"
res <- queryDB conn "GB"
-- putStr $ show res
print $ "Number of rows is " ++ show (length res)
putStrLn "\n****************"
putStrLn "Before Formatting Query"
putStr $ show res
putStrLn "\n****************"
putStrLn "After Formatting Query"
let converRes = sqlRowToString res
mapM_ putStrLn converRes
putStrLn "\n****************"
putStrLn $ "Number of rows is " ++ show (length res)



59 changes: 31 additions & 28 deletions src/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ initialiseDB = do
-- storeHolidays _ [] = return ()
-- storeHolidays conn xs = do


-- This function will insert the holiday records into the database
insertDB :: Connection -> [HolidayRecord] -> IO ()
insertDB conn records = do
Expand All @@ -68,28 +67,29 @@ insertDB conn records = do
-- This function will insert the country records into the dsatabase
insertLB :: Connection -> [HolidayRecord] -> IO ()
insertLB conn records = do
let xs = records
stmt <- prepare conn "INSERT INTO countries (countryCode,global) VALUES (?,?)"
putStrLn "Adding"
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (global x)]) xs)
commit conn
let xs = records
stmt <- prepare conn "INSERT INTO countries (countryCode,global) VALUES (?,?)"
putStrLn "Adding"
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (global x)]) xs)
commit conn

-- This function will insert the country_holidays records into the dsatabase
insertSB :: Connection -> [HolidayRecord] -> IO ()
insertSB conn records = do
let xs = records
stmt <- prepare conn "INSERT INTO country_holidays (countryCode,localName) VALUES (?,?)"
putStrLn "Adding"
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (localName x)]) xs)
commit conn
let xs = records
stmt <- prepare conn "INSERT INTO country_holidays (countryCode,localName) VALUES (?,?)"
putStrLn "Adding"
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (localName x)]) xs)
commit conn

-- This function will select all the holidays of a given country
queryDB :: Connection -> String -> IO [[SqlValue]]
queryDB conn countryCode =
do quickQuery'
conn
"SELECT localName FROM country_holidays WHERE countryCode =(?)"
[toSql countryCode]
queryDB conn countryCode =
do
quickQuery'
conn
"SELECT localName FROM country_holidays WHERE countryCode =(?)"
[toSql countryCode]

--This function will select all the holidays in the date specified of a given country
querySQ :: Connection -> String -> IO Bool
Expand All @@ -106,7 +106,6 @@ getNAMEs conn = do
-- return $ map fromSql (map head res)
return $ map (fromSql . head) res


recordToSqlValues :: HolidayRecord -> [SqlValue]
recordToSqlValues holidays =
[ toSql $ date holidays,
Expand All @@ -117,28 +116,32 @@ recordToSqlValues holidays =
holidayToSqlValues :: HolidayRecord -> [SqlValue]
holidayToSqlValues countries =
[ toSql $ countryCode countries,
toSql $ global countries
toSql $ global countries
]


prepareInsertRecordStmt :: Connection -> IO Statement
prepareInsertRecordStmt conn = prepare conn "INSERT INTO holidays VALUES (?,?)"

prepareSelectRecordStma :: Connection -> IO Statement
prepareSelectRecordStma conn = prepare conn "SELECT FROM Country_holidays VALUES (?,?)"


saveHolidayRecord :: [HolidayRecord] -> Connection -> IO ()
saveHolidayRecord records conn = do
stmt <- prepareInsertRecordStmt conn
executeMany stmt (map recordToSqlValues records)
commit conn
stmt <- prepareInsertRecordStmt conn
executeMany stmt (map recordToSqlValues records)
commit conn

savecountriesRecord :: [HolidayRecord] -> Connection -> IO ()
savecountriesRecord record conn = do
stma <- prepareSelectRecordStma conn
executeMany stma (map recordToSqlValues record)
commit conn
stma <- prepareSelectRecordStma conn
executeMany stma (map recordToSqlValues record)
commit conn

-- sqlRowToLine :: [[SqlValue]] -> String
-- sqlRowToLine row =
-- map (fromSql :: SqlValue -> String)

sqlRowToString :: [[SqlValue]] -> [String]
sqlRowToString xs = map (fromSql :: SqlValue -> String) (concat xs)

dateNotInDB = undefined

nub = undefined

0 comments on commit 46609b4

Please sign in to comment.