 |
| How to write dynamic programs? |  |
 |
 |
What does it actually mean?
As is generally known tdbengine is a database engine, an interpreter and acompiler in one.
So we are happy that a source code written in EASY can be compiled.
Many potential errors in the code found without compiler only at
the runtime of Interpreter would cause a data chaos. Imagine while
doing a backup interpreter finds out that a parrameter of a function
will transfered as a wrong type and aborts the process. The question is
now do you have a backup or you don't.
But
sometime it would be of use if you could mix the fixed structure up.
That could help to keep source codes abstact to use them easier in
other projects. It would also allow us to give the user of our
applications powerful tools. In other database systems like MS ACCESS
you have a SQL editor, too, which you can do many things (in line with
SQL) with.
Fortunately tdbengine offers divers options how to affect dynamic program stream. That's what I want to specify to you. DO _"continue_reading()"
DO _
Using DO combined with _ (underline) you can analyse and following run any expression.
Example:
var x : String = "cgiwriteln('hello world')"
DO _x
Only
at runtime more precisely when the interpreter comes in the second line
to the underline the value of the variable x will be analysed. Here
it's nothing mind because it's value is alleged. DO makes tdbengine running the expression delivered by the underline _. In this situation we also could do it easier:
cgiwriteln('hello world')
It would suffice.
Another example:
PROCEDURE deletefromadressen(nID : Integer) //exemplarily delete routine ... DelRec(dbADDRESSES, FindAuto(dbADDRESSES, nID)) ...
ENDPROC
PROCEDURE deletefromniederlassungen(nID : Integer) ... DelRec(dbNETHERL, FindAuto(dbNETHERL, nID)) ...
ENDPROC
PROCEDURE delete(cTable : String; nID : Integer) var x : String = "deletefrom"+cTable+"("+Str(nID)+")" //analyse x at runtime and "run" it's result DO _x
ENDPROC
We assume that any table in our project will need a table specific
cleanup if datasets will be deleted. But in the whole application
should be used only one routine
delete(). This routine has to take care for running the deleteform...()
procedure that belongs to the table. Sure, we could do it with a
IF-THEN-ELSIF-ELSE-END monster ,too. But with any new table this IF tree would grow in and some projects it could pssibly become very
complex. In the example the variables affect the program action at the
runtime.
The function Val()
Val() evaluates any strings. At least Val() tries to do it. In other words Val() makes a string out of an integer.
For example:
Var c1 : String = "123"
Var c2 : String = "123 + 321 + 1 / 2 * 3"
Var c3 : String = "Val(c1) + Val(c2)"
Var c4 : String = "Val('999' + Str(Val(c3),0,2))"
Var n : REAL
SetPara("nv 0")
n:=val(c1) // n = 123
n:=val(c2) // n = 445,5
n:=val(c3) // n = 568,5
n:=val(c4) // n = 999568,5
So you see with Val() you can evaluate nearly any string. Thereby they may also access functions and variables of the program.
However thereto you have to set the runtime switchnv to 0.
The function ExecProg()
ExecProg(cFilename : String)
Compiles and runs any EASY programs.
Thereby cFilename may refer both file and ramtext:.
(Very easy) Example:
PROCEDURE Main var c : String var f : Integer = rewrite("ramtext:code")
writeln(f, "Procedure XYZ : String") writeln(f, " return 'xyz'") writeln(f, "Endproc") writeln(f, "") writeln(f, "c := xyz") close(f)
ExecProg("ramtext:code")
cgiwriteln(c)
ENDPROC
Here the ramtext:code will be written only at the runtime following compiled and runed with ExecProg().
Dynamic
selection programs demonstrate an interesting application which will
be individually assembled only at runtime of the blocks. An (A very
complex)
application area is here for example analysis and conversion of SQL
statements Anwendungsgebiet wäre hier zum Beispiel die Auswertung
und Umsetzung
von SQL-Statements to EASY code.
Continuative links:
/documentation/runtime switch
Author: Thomas Friebel <tf@tdb.de>
|