From 46609b49f26d9551743843d68b22c48e1d111ed2 Mon Sep 17 00:00:00 2001 From: Franklin Antony Date: Fri, 4 Dec 2020 13:21:19 +0000 Subject: [PATCH] Updated printing logic --- app/Main.hs | 20 +++++++++++++---- src/Database.hs | 59 ++++++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index c7353cf..f957859 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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" @@ -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) + + diff --git a/src/Database.hs b/src/Database.hs index 7ead7f4..0a9a765 100644 --- a/src/Database.hs +++ b/src/Database.hs @@ -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 @@ -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 @@ -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, @@ -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 \ No newline at end of file