<commit_before>{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
module Databrary.Model.Container.SQL
  ( selectContainerRow
  , selectVolumeContainer
  , selectContainer
  , insertContainer
  , updateContainer
  , deleteContainer
  -- for expanded query
  , setContainerId
  ) where

import qualified Language.Haskell.TH as TH

import Databrary.Model.SQL.Select
import Databrary.Model.Id.Types
import Databrary.Model.Audit.SQL
import Databrary.Model.Volume.SQL
import Databrary.Model.Release.SQL
import Databrary.Model.Container.Types

selectContainerRow :: Selector -- ^ @'ContainerRow'@
selectContainerRow = selectColumns 'ContainerRow "container" ["id", "top", "name", "date"]

selectVolumeContainer :: Selector -- ^ @'Volume' -> 'Container'@
selectVolumeContainer = selectJoin 'Container
  [ selectContainerRow
  , maybeJoinOn "container.id = slot_release.container AND slot_release.segment = '(,)'"
    releaseRow
  ]

selectContainer :: TH.Name -- ^ @'Identity'@
  -> Selector -- ^ @'Container'@
selectContainer ident = selectJoin '($)
  [ selectVolumeContainer
  , joinOn "container.volume = volume.id" $ selectVolume ident
  ]

containerKeys :: String -- ^ @'Container'@
  -> [(String, String)]
containerKeys o =
  [ ("id", "${containerId $ containerRow " ++ o ++ "}") ]

containerSets :: String -- ^ @'Container'@
  -> [(String, String)]
containerSets o =
  [ ("volume", "${volumeId $ volumeRow $ containerVolume " ++ o ++ "}")
  , ("top", "${containerTop $ containerRow " ++ o ++ "}")
  , ("name", "${containerName $ containerRow " ++ o ++ "}")
  , ("date", "${containerDate $ containerRow " ++ o ++ "}")
  ]

setContainerId :: Container -> Id Container -> Container
setContainerId o i = o{ containerRow = (containerRow o) { containerId = i } }

insertContainer :: TH.Name -- ^ @'AuditIdentity'@
  -> TH.Name -- ^ @'Container'@
  -> TH.ExpQ -- ^ @'Container'@
insertContainer ident o = auditInsert ident "container"
  (containerSets (nameRef o))
  (Just $ selectOutput $ selectMap ((TH.VarE 'setContainerId `TH.AppE` TH.VarE o) `TH.AppE`) $ selectColumn "container" "id")

updateContainer :: TH.Name -- ^ @'AuditIdentity'@
  -> TH.Name -- ^ @'Container'@
  -> TH.ExpQ -- ^ @()@
updateContainer ident o = auditUpdate ident "container"
  (containerSets (nameRef o))
  (whereEq $ containerKeys (nameRef o))
  Nothing

deleteContainer :: TH.Name -- ^ @'AuditIdentity'@
  -> TH.Name -- ^ @'Container'@
  -> TH.ExpQ -- ^ @()@
deleteContainer ident o = auditDelete ident "container"
  (whereEq $ containerKeys (nameRef o))
  Nothing

<commit_msg>Remove unused container sql helpers<commit_after>{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
module Databrary.Model.Container.SQL
  ( selectContainerRow
  , selectVolumeContainer
  , selectContainer
  -- for expanded query
  , setContainerId
  ) where

import qualified Language.Haskell.TH as TH

import Databrary.Model.SQL.Select
import Databrary.Model.Id.Types
import Databrary.Model.Volume.SQL
import Databrary.Model.Release.SQL
import Databrary.Model.Container.Types

selectContainerRow :: Selector -- ^ @'ContainerRow'@
selectContainerRow = selectColumns 'ContainerRow "container" ["id", "top", "name", "date"]

selectVolumeContainer :: Selector -- ^ @'Volume' -> 'Container'@
selectVolumeContainer = selectJoin 'Container
  [ selectContainerRow
  , maybeJoinOn "container.id = slot_release.container AND slot_release.segment = '(,)'"
    releaseRow
  ]

selectContainer :: TH.Name -- ^ @'Identity'@
  -> Selector -- ^ @'Container'@
selectContainer ident = selectJoin '($)
  [ selectVolumeContainer
  , joinOn "container.volume = volume.id" $ selectVolume ident
  ]

setContainerId :: Container -> Id Container -> Container
setContainerId o i = o{ containerRow = (containerRow o) { containerId = i } }

{'fraction_non_alphanumeric': 0.14706663127156888, 'fraction_numerical': 0.0, 'mean_word_length': 4.375178316690442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '5883392', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1143, 'n_words': 273}