WYRDTEK Home Features Reference Downloads About



int es_mba_addformula( struct es_mba *agg,
                       const char *formulaname,
                       const char *formuladefn,
                       int fldtype,
                       char *errbuf,
                       unsigned errbufsize );


struct es_mba *agg Database aggregator.
const char *formulaname Formula field name.
const char *formuladefn Formula definition.
int fldtype Formula field numerical type. See class field types for list of integer and double-precision types.
char *errbuf Error message buffer. Populated by es_mba_addformula() on error.

Return Codes

ES_ERROR_NONE Success. No errors.
ES_ERROR_INVALIDFIELDTYPE ftype is not an integer or double-precision type.
ES_ERROR_FIELDEXISTS forumulaname has already been specified as a formula.
ES_ERROR_FAILTOPARSE Failed to parse formuladefn. errbuf is populated with an error message.


Create an algebraic formula to use in deriving key, result or other formula values.

formulaname can be regarded as the name of a variable that is assigned the result of evaluating the expression contained in the formuladefn parameter. The formula expression can refer to numerical constants, database fields and other formula names as variables. A set of mathematical operators and functions are provided. Usual mathematical operator precendence applies.

For example, a formula to compute aggregate profit from a sales database might look something like:

struct es_mba *agg;
char errbuf[1024];
es_mba_addformula( agg, "Profit", "DollarSales - UnitSales*(ProductCost+ShippingCost)",
                   ES_FLDTYPE_F64, errbuf, 1024 );
es_mba_addresult( agg, "Profit", ES_AGGTYPE_SUM );

An incomplete or otherwise invalid formuladefn causes es_mba_addformula() to fail with a ES_ERROR_FAILTOPARSE return code. errbuf is populated with an appropriate error message.

Formula Construction

The set of valid formula constants, variables, operators and functions is as follows:
42 Integer numeric constant.
42.0 Double-precision numeric constant.
Profit Formula already defined via previous invocation of es_mba_addformula.
UnitSales Database field defined in input database definition. Must be integer or double-precision.
Numerical Operators
() Parenthesis.
+ Add.
- Subtract.
* Multiply.
/ Divide.
Comparison Operators
! Not (unary operator).
== Equal.
!= Not Equal.
< Less than.
<= Less than or equal.
> Greater than.
>= Greater than or equal.
Logical Operators
&& And.
|| Or.
min(X,Y) Minimum.
max(X,Y) Maximum.
abs(X) Absolute.
getyear(X) Get year from argument assumed to be a date-id.
getmonth(X) Get month from argument assumed to be a date-id.
getday(X) Get day from argument assumed to be a date-id.


Type implication and coercion follows the C language convention: i.e. if both left and right arguments of a numerical or comparison operator is an integer, the operation will be fixed-point. If either argument is a floating-point number, both arguments are coerced to floating-point and the subsequent operation will be floating-point. Comparison and logical operators both yield integer results.

The final result is coerced to the type defined by the fldtype parameter.

[Back To Reference] [Back To Reference/es_mba]