diff --git a/src/Yore/DB.hs b/src/Yore/DB.hs index ac0ddf5..5a5d4fb 100644 --- a/src/Yore/DB.hs +++ b/src/Yore/DB.hs @@ -1,10 +1,13 @@ +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE DuplicateRecordFields #-} module Yore.DB ( DayIndex (..) , createDayFile , createDayIndex , readDayIndex + , readDayPaths , Table (..) , getTables ) where @@ -18,9 +21,7 @@ import qualified Database.PostgreSQL.Opium as Opium data Table = Table { schema :: String , name :: String - } deriving (Show, Generic) - -instance Opium.FromRow Table + } deriving (Show, Generic, Opium.FromRow) getTables :: Opium.Connection -> IO (Either Opium.Error [Table]) getTables = Opium.fetch_ "SELECT table_schema AS schema, table_name AS name FROM information_schema.tables" @@ -28,9 +29,13 @@ getTables = Opium.fetch_ "SELECT table_schema AS schema, table_name AS name FROM data DayIndex = DayIndex { id :: Int , day :: Day - } deriving (Show, Generic) + } deriving (Show, Generic, Opium.FromRow) -instance Opium.FromRow DayIndex +data DayFile = DayFile + { id :: Int + , day_id :: Int + , relative_path :: String + } deriving (Show, Generic, Opium.FromRow) createDayIndex :: Day -> Opium.Connection -> IO (Either Opium.Error DayIndex) createDayIndex date = ex runIdentity . @@ -49,5 +54,11 @@ readDayIndex date = "SELECT * FROM yore.day_index WHERE day = $1" (Identity date) +readDayPaths :: Day -> Opium.Connection -> IO (Either Opium.Error [(DayIndex, DayFile)]) +readDayPaths date = + Opium.fetch + "SELECT * FROM yore.day_index LEFT JOIN yore.day_file ON day_index.id = day_id WHERE day = $1" + (Identity date) + ex :: (a -> b) -> IO (Either e a) -> IO (Either e b) ex = fmap . fmap