|
definition: |
createTransaction :: ControllerGenerator
createTransaction erdname (Entity entityName attrList)
relationships allEntities = stCmtFunc
("Transaction to persist a new " ++ entityName ++ " entity to the database.")
(transFunctionName entityName "create")
1 Private
(baseType (newEntityTypeName entityName)
~> applyTC (dbconn "DBAction") [baseType (model erdname,entityName)])
[simpleRule
[tuplePattern
(map (\ (param, varId) -> CPVar (varId, param))
(zip (parameterList ++ map lowerFirst manyToOneEntities ++
map (\ (e,r) -> lowerFirst r ++ e ++ "s") manyToManyEntities)
[1..]))
] -- parameter list for controller
(doExpr $
CSPat (cpvar "newentity")
(applyF (entityConstructorFunction erdname
(Entity entityName attrList) relationships)
(map (\ ((Attribute name dom key null), varId) ->
if (isForeignKey (Attribute name dom key null))
then applyF (model erdname,
lowerFirst (getReferencedEntityName dom) ++ "Key")
[CVar (varId,
lowerFirst (getReferencedEntityName dom))]
else let cv = CVar (varId, lowerFirst name)
in if hasDefault dom && not (isStringDom dom)
&& not null
then applyF (pre "Just") [cv]
else cv)
(zip noPKeys [1..])
)) :
map (\ (en,rel) -> CSExpr $
applyF (controllerModuleName entityName, "add" ++ rel)
[cvar (lowerFirst rel ++ en ++ "s"),
cvar "newentity"])
manyToManyEntities ++
[CSExpr $ applyF (pre "return") [cvar "newentity"]])
]
where
noPKeys = (filter notPKey attrList)
-- foreignKeys = (filter isForeignKey attrList)
-- notGeneratedAttributes = filter (\attr -> (not (isForeignKey attr))
-- && (notPKey attr)) attrList
parameterList = map (\(Attribute name _ _ _) -> lowerFirst name)
(filter (not . isForeignKey) noPKeys)
manyToManyEntities = manyToMany allEntities (Entity entityName attrList)
manyToOneEntities = manyToOne (Entity entityName attrList) relationships
|
|
demand: |
argument 2 |
|
deterministic: |
deterministic operation |
|
documentation: |
Generates a transaction to store a new entity. |
|
failfree: |
<FAILING> |
|
indeterministic: |
referentially transparent operation |
|
infix: |
no fixity defined |
|
iotype: |
{(_,{Entity},_,_) |-> _}
|
|
name: |
createTransaction |
|
precedence: |
no precedence defined |
|
result-values: |
_ |
|
signature: |
String -> Database.ERD.Entity -> [Database.ERD.Relationship] -> [Database.ERD.Entity] -> AbstractCurry.Types.CFuncDecl |
|
solution-complete: |
operation might suspend on free variables |
|
terminating: |
possibly non-terminating |
|
totally-defined: |
possibly non-reducible on same data term |