<commit_before>{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}

--------------------------------------------------------------------------------
-- | See TodoManual.hs to avoid using TemplateHaskell, though you should be
-- | aware that this will make you write more boilerplate code
module OpaleyeDemo.Todo
      ( -- * Exports
        todoTable
      , todoQuery
      ) where

import Opaleye
import Data.Profunctor.Product

data Todo' i t d p = Todo' { _id :: i
                           , _title :: t
                           , _dueDate :: d
                           , _prio :: p
                           }
type TodoId = Int
type Prio   = Maybe Int
type Todo   = Todo' TodoId String Day Prio

type TodoColumn = Todo' ( Column PGInt4, Column PGText
                        , Column PGDate, Maybe (Column PGInt4) )


$(makeAdaptorAndInstance "pTodo" ''Todo')

todoTable
  :: Table (Column PGInt4, Column PGText, Column PGDate, Maybe (Column PGInt4))
           (Column PGInt4, Column PGText, Column PGDate, Column PGInt4)
todoTable = Table "todos" (p4 ( required "id"
                              , required "title"
                              , required "due_date"
                              , optional "prio" ))

todoQuery :: Query (Column PGInt4, Column PGText, Column PGDate, Column PGInt4)
todoQuery = queryTable todoTable
<commit_msg>add todo<commit_after>{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}

--------------------------------------------------------------------------------
-- | See TodoManual.hs to avoid using TemplateHaskell, though you should be
-- | aware that this will make you write more boilerplate code
module OpaleyeDemo.Todo where

import Control.Lens
       ( makeLenses )
import Data.Time.Calendar
       ( Day )
import Opaleye
       ( Table(..), Query, Column, Nullable, PGInt4, PGText, PGDate
       , queryTable, required, optional )
import Data.Profunctor.Product.TH
       ( makeAdaptorAndInstance )

--------------------------------------------------------------------------------
-- | Todo Id
data TodoId' a = TodoId { todoId :: a } deriving Show
makeAdaptorAndInstance "pTodoId" ''TodoId'

type TodoId = TodoId' Int
type TodoIdColumn = TodoId' (Column PGInt4)
type TodoIdColumnMaybe = TodoId' (Maybe (Column PGInt4))
type TodoIdColumnNullable = TodoId' (Column (Nullable PGInt4))

--------------------------------------------------------------------------------
-- | Priority indicator
data Prio' a = Prio { unPrio :: a } deriving Show
makeAdaptorAndInstance "pPrio" ''Prio'

type Prio = Prio' Int
type PrioColumn = Prio' (Column PGInt4)
type PrioColumnMaybe = Prio' (Maybe (Column PGInt4))
type PrioColumnNullable = Prio' (Column (Nullable PGInt4))

--------------------------------------------------------------------------------
-- | Todo declaration
data Todo' i t d p = Todo { _id :: i
                          , _title :: t
                          , _dueDate :: d
                          , _prio :: p
                          }

makeLenses ''Todo'
makeAdaptorAndInstance "pTodo" ''Todo'

--------------------------------------------------------------------------------
-- | Todo Columns
type TodoColumns
  = Todo' TodoIdColumn (Column PGText) (Column PGDate) PrioColumn
type TodoInsertColumns
  = Todo' TodoIdColumnMaybe (Column PGText) (Column PGDate) PrioColumnMaybe
type Todo
  = Todo' TodoId String Day Prio

todoTable :: Table TodoInsertColumns TodoColumns
todoTable = Table "todos" $ pTodo Todo
    { _id      = pTodoId . TodoId $ optional "id"
    , _title   = required "title"
    , _dueDate = required "due_date"
    , _prio    = pPrio . Prio $ optional "prio"
    }

todoQuery :: Query TodoColumns
todoQuery = queryTable todoTable

{'fraction_non_alphanumeric': 0.2056482670089859, 'fraction_numerical': 0.004107830551989731, 'mean_word_length': 1.7018030513176143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6628898', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1110, 'n_words': 329}