From 0fc2dee6520ce143a7e09f54273f11ff988bac49 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Tue, 29 Jul 2025 09:51:08 +0200 Subject: [PATCH] Use DeriveAnyClass, add readDayPaths --- src/Yore/DB.hs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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