Subject Re: [firebird-support] Calcualtion of a formula within a database
Author Pavel Menshchikov
Hello Paul,

p> I have an application written in Delphi using an FB1.5 database. One
p> table in the database contains a formula which is used in Delphi to
p> perform a calculation by subsituting numbers for the symbols in the
p> formula and then calcualting the answer as a float. It is used to
p> calculate a weight from dimesions and densities for materials of
p> different shapes, each entry in the table having a formula
p> appropriate to its shape. For example the formula might be A * B / C.
p> The Delphi code would substitue numbers for A, B and C then calculate
p> the resulting numberic expression. Is ther a way of performing this
p> type of operation inside a stored procedure passed values for the
p> symbols which would return a numeric result?
I have the same problem. To solve it, I developed a UDF to substitute
parameters in the string with their values, then I use it in a SP like
-----
/*
** char* substparams(char* Expr, char* Params)
** Substitutes in 'Expr' all occurances of param names
** with their values from 'Params' (list of reals, which are
** separated by space characters and placed as the following:
** ...)
*/
DECLARE EXTERNAL FUNCTION substparams
CSTRING(500), CSTRING(1000)
RETURNS CSTRING(1000) FREE_IT
ENTRY_POINT 'SubstParams' MODULE_NAME 'myudf';

CREATE PROCEDURE DOEVAL(
Expr VARCHAR(500) CHARACTER SET ASCII, /* expression to evaluate */
Params VARCHAR(1000) CHARACTER SET ASCII) /* list of parameter
values in a fixed order, for example
'1.0 2.0 3.05' - that is element width is 1.0,
element height is 2.0, and element depth is 3.05 */
RETURNS (Val DOUBLE PRECISION)
AS
begin
if (Expr is null) then Expr='';
if (Params is null) then Params=''; /* situations of empty
parameters are processed in the UDF */
execute statement 'select cast(' || substparams(Expr, Params) ||
' as double precision) from rdb$database ' into :Val;
when any do Val=0; /* exception processing - you may allow your
client app to process the exception */
end^
-----
Probably the solution is not very flexible, but it suites my needs.


HTH
--
Best regards,
Pavel Menshchikov
http://www.ls-software.com