Use DeriveAnyClass, add readDayPaths

This commit is contained in:
Paul Brinkmeier 2025-07-29 09:51:08 +02:00
parent c8372b1fa5
commit 0fc2dee652

View File

@ -1,10 +1,13 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Yore.DB module Yore.DB
( DayIndex (..) ( DayIndex (..)
, createDayFile , createDayFile
, createDayIndex , createDayIndex
, readDayIndex , readDayIndex
, readDayPaths
, Table (..) , Table (..)
, getTables , getTables
) where ) where
@ -18,9 +21,7 @@ import qualified Database.PostgreSQL.Opium as Opium
data Table = Table data Table = Table
{ schema :: String { schema :: String
, name :: String , name :: String
} deriving (Show, Generic) } deriving (Show, Generic, Opium.FromRow)
instance Opium.FromRow Table
getTables :: Opium.Connection -> IO (Either Opium.Error [Table]) getTables :: Opium.Connection -> IO (Either Opium.Error [Table])
getTables = Opium.fetch_ "SELECT table_schema AS schema, table_name AS name FROM information_schema.tables" 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 data DayIndex = DayIndex
{ id :: Int { id :: Int
, day :: Day , 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 :: Day -> Opium.Connection -> IO (Either Opium.Error DayIndex)
createDayIndex date = ex runIdentity . createDayIndex date = ex runIdentity .
@ -49,5 +54,11 @@ readDayIndex date =
"SELECT * FROM yore.day_index WHERE day = $1" "SELECT * FROM yore.day_index WHERE day = $1"
(Identity date) (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 :: (a -> b) -> IO (Either e a) -> IO (Either e b)
ex = fmap . fmap ex = fmap . fmap