@@ -1,6 +1,7 @@
11-- the Out-of-the-Tarpit example in Haskell and Project:M36
22{-# LANGUAGE DeriveAnyClass, DeriveGeneric, OverloadedStrings #-}
33import ProjectM36.Client
4+import ProjectM36.DataTypes.Primitive
45import qualified Data.Map as M
56import Data.Maybe
67import Control.Monad
@@ -131,7 +132,14 @@ createSchema sessionId conn = do
131132 toDatabaseContextExpr (undefined :: PriceBand),
132133 toDatabaseContextExpr (undefined :: AreaCode),
133134 toDatabaseContextExpr (undefined :: SpeedBand)]
135+ --create the stored atom functions
136+ priceBandScript = "(\\(DoubleAtom price:_) -> do\n let band = if price < 10000.0 then \"Low\" else if price < 20000.0 then \"Medium\" else if price < 30000.0 then \"High\" else \"Premium\"\n let aType = ConstructedAtomType \"PriceBand\" empty\n pure (ConstructedAtom band aType [])) :: [Atom] -> Either AtomFunctionError Atom"
137+ atomFuncs = [createScriptedAtomFunction "priceBandForPrice" [doubleTypeConstructor] (ADTypeConstructor "PriceBand" []) priceBandScript]
134138 --gather up and execute all database updates
135139 mErrs <- mapM (executeDatabaseContextExpr sessionId conn) (new_adts ++ rvDefs ++ incDepKeys ++ incDepForeignKeys)
136140 let errs = catMaybes mErrs
137− when (length errs > 0) (error (show errs))
141+ when (length errs > 0) (error (show errs))
142+
143+ mErrs' <- mapM (executeDatabaseContextIOExpr sessionId conn) atomFuncs
144+ let errs' = catMaybes mErrs'
145+ when (length errs' > 0) (error (show errs'))