packages feed

pg-schema-0.9.0.0: src/PgSchema/DML/KeyedWrite.hs

{-# LANGUAGE OverloadedRecordDot #-}
module PgSchema.DML.KeyedWrite
  ( mandatoryDbNames, identityCandidatesFromTab, pickKeyNames
  , keyWhereClause, upsertOnConflict
  , keyedUpdateSetAndKeys ) where

import Data.List as L
import Data.Map as M
import Data.Maybe
import Data.Text as T
import PgSchema.Schema
import Prelude as P


-- | Runtime conflict/update targets: the primary key, then unique keys over
-- @NOT NULL@ columns, then other unique keys (used by "PgSchema.DML.InsertJSON").
identityCandidatesFromTab :: TabInfo -> [[Text]]
identityCandidatesFromTab ti =
  P.filter (not . P.null) [ti.tiDef.tdKey] <> notNullUks <> nullUks
  where
    isNullable = (== Just True) . fmap (.fdNullable) . (`M.lookup` ti.tiFlds)
    (nullUks, notNullUks) = L.partition (P.any isNullable) ti.tiDef.tdUniq

mandatoryDbNames :: TabInfo -> [Text]
mandatoryDbNames ti =
  M.keys $ M.filter (\fd -> not $ fd.fdNullable || fd.fdHasDefault) ti.tiFlds

pickKeyNames :: TabInfo -> [Text] -> Maybe [Text]
pickKeyNames ti srcFlds =
  L.find (P.null . (L.\\ srcFlds)) $ identityCandidatesFromTab ti

keyWhereClause :: [(Text, Text)] -> Text
keyWhereClause pairs =
  " where " <> T.intercalate " and "
    (L.zipWith keyValDistinct (fst <$> pairs) (snd <$> pairs))
  where
    keyValDistinct name val = name <> " IS NOT DISTINCT FROM " <> val

-- | Partition plain columns for @UPDATE@ (SET non-keys, then WHERE keys).
keyedUpdateSetAndKeys
  :: TabInfo -> [(Text, Text)] -> ([(Text, Text)], [(Text, Text)])
keyedUpdateSetAndKeys ti plains =
  let
    srcFlds = fst <$> plains
    keyNames = fromMaybe [] $ pickKeyNames ti srcFlds
  in L.partition ((`L.elem` keyNames) . fst) plains

-- | @ON CONFLICT@ clause of a flat upsert with the conflict key @conflictCols@:
-- on conflict the other columns (@plainsOthers@) are updated.
--
-- If there are no other columns, an existing row is left as is. Without
-- @RETURNING@ it is @DO NOTHING@. With @RETURNING@ it is a no-op @DO UPDATE@
-- of a key column, because @DO NOTHING@ returns nothing for an existing row.
upsertOnConflict :: Bool -> [(Text, Text)] -> [Text] -> Text
upsertOnConflict withReturning plainsOthers conflictCols =
  case (conflictCols, plainsOthers) of
    ([], _) -> ""
    (k : _, [])
      | withReturning -> target <> " do update set " <> k <> " = EXCLUDED." <> k
      | otherwise -> target <> " do nothing"
    (_, xs) -> target <> " do update set "
      <> T.intercalate ", " (L.map (\(name, _) -> name <> " = EXCLUDED." <> name) xs)
  where
    target = " on conflict (" <> T.intercalate ", " conflictCols <> ")"