Hi |
Re: Create Attributes
It was a very poor decision by QSS years ago to provide many 'attribute' functions that presume the current module. Since the 'current' module is sometimes ambiguous (it may or may not be the last module you opened), DXL that uses these functions should routinely set the current module before using the functions. That may create another problem elsewhere in your code if you presume the currerent module; thus you need to set current back after you use the funcion. In your case:
//******************
Module fSetCurrMod(Module modNew)
{ // Set the "Current" module, returning the previous current
// module. You cannot "clear" current by passing null.
Module modPrev = current()
if (!null modNew and modNew != modPrev)
(current ModuleRef__) = modNew
return(modPrev)
} // end fSetCurrMod()
//*******************
bool fCreateAttr(Module mod, string NameAttr, string NameType,
bool DoModule, DoObject, DoInherit, DoHistory, DoBars, DoDate,
ReplaceAttr, EraseAttr, string &ErrMess)
{ // Create the attribute with the specified properties in the specified module.
// The specified Attr Type 'NameType' must already exist.
// If it does already exist and the user opts to NOT replace it, report an error.
// Otherwise optionally erase its old values.
// In any case set its properties as defined.
// Note: Erasing is better than deleting then creating since the
// attribute may appear in some view.
AttrDef ad
AttrType at
ErrMess = ""
Module modPrevCurr // previous current module
if (null mod or null NameAttr or null NameType or (!DoModule and !DoObject))
{ ErrMess = "fCreateAttr: Internal Error, bad input."
return(false)
}
at = find(mod, NameType)
ad = find(mod, NameAttr)
if (null at)
{ ErrMess = "Attr Type *" NameType "* does not exist"
}
elseif (!null ad)
{ // *** Attribute already Exists ***
if (!ReplaceAttr)
{ ErrMess = "Attr Exists." // User didn't want the attr replaced if it already exists
}
elseif (ad.typeName != NameType)
{ ErrMess = "It is not of type *" NameType "*. You must delete it manually.\n"
}
if (!canWrite(mod, NameAttr))
{ ErrMess = ErrMess "No Write Access to *" NameAttr "*. You must resolve this."
}
elseif (EraseAttr and !fEraseAttr(mod, NameAttr))
{ ErrMess = "Error erasing existing Values of '" NameAttr "'."
}
else
{} // everything OK now.
if (!null ErrMess)
{ ErrMess = "Attribute *" NameAttr "* already exists.\n" ErrMess
}
else
{ // Exists OK. Make its characteristics match call parameters
modPrevCurr = fSetCurrMod(mod)
noError()
if (DoModule and DoObject)
{ ad = modify (ad, module object //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
}
elseif (DoModule and !DoObject)
{ ad = modify (ad, module //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
}
elseif (!DoModule and DoObject)
{ ad = modify (ad, object //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
}
else{} // Neither Module nor Object attr??
ErrMess = lastError() // Capture the modify error
if (null ad or !null ErrMess) ErrMess = "Error Modifying Attr <">\n" ErrMess
fSetCurrMod(modPrevCurr) // reset current module
} // End attempt to modify it.
} // end Attr already exists
else // create target attribute
{ modPrevCurr = fSetCurrMod(mod)
noError()
if (DoModule and DoObject)
ad = create(module object //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
elseif (DoModule and !DoObject)
ad = create(module //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
elseif (!DoModule and DoObject)
ad = create(object //-
type NameType history DoHistory changeBars DoBars //-
date DoDate inherit DoInherit attribute NameAttr
)
else{} // must be either DoModule or DoObject or Both.
ErrMess = lastError() // Capture the Create error, if any
if (null ad or !null ErrMess) ErrMess = "Error Creating Attr <">\n" ErrMess
fSetCurrMod(modPrevCurr)
}
if (null ErrMess)
return(true)
else return(false)
} // end fCreateAttr()
Example:
if (!fCreateAttr(mod, NameAttr, NameType, false, true, false, true, true, true, false, false, ErrMess))
|
Re: Create Attributes |