Monday, June 1, 2015

Dynamic tokens in ABAP

Usage of dynamic tokes is one way of how to code generic programs in ABAP. By definition generic programming or so called dynamic generation of source code enables ABAP programs to be as dynamic as possible. This involves specification of data objects on the fly or dynamic specification. Therefore we call fragments of the code which are specified dynamically as dynamic tokens. Dynamic token specifications are often used in combination with dynamic access to data objects. The tokens can be operands or whole ABAP language statements or clauses declared in form of character/string data objects which values are populated by runtime and passed to the ABAP statements. They must contain source code correctly defined during design mode which is executed during runtime.

Following are examples of dynamic tokens:

1. Dynamic access to attributes of classes (Dynamic Access)

ASSIGN | {class|(class_name)}=>{attr|(attr_name)} } 

2. Call procedures or methods dynamically (Dynamic Invoke)
CALL METHOD {(meth_name)
            | (class_name)=>(meth_name)

3. Run Time Type Services (RTTS) - enables dynamic type specifications for data on the fly - when it is not known what the type of data objects to be generated is. It is implemented by type class hierarchies (CL_ABAP_TYPEDESCR and its main subclasses: CL_ABAP_DATADESCR and CL_ABAP_OBJECTDESCR plus they having more child classes). These classes contain method for creation (RTTC) and identification (RTTI) of data objects.

CREATE DATA dref [ AREA HANDLE handle ] 
                 [ TYPE { {abap_type|(name) 
                           [LENGTH len] [DECIMALS dec]} 
                        | {[LINE OF] type|(name)} 
                        | {REF TO type|(name)} 
                        | {{{[STANDARD] TABLE} 
                           |{SORTED TABLE} 
                           |{HASHED TABLE}} 
                           OF [REF TO] {type|(name)} 
                           [WITH [UNIQUE|NON-UNIQUE] 
                              {KEY {comp1 comp2 ...}|(keytab)}|{DEFAULT KEY}] 
                           [INITIAL SIZE n]} 
                        | {HANDLE handle} } ].

4. Dynamic specifications of clauses when internal tables are accessed or in Open SQL. Dynamic tokens in Open SQL are dynamic notation of databases, dynamic WHERE clauses, dynamic SET expressions in UPDATE statements etc.)

DATA(sql_cond) = `BNAME = @input`. 
SELECT SINGLE * FROM usr02 WHERE (sql_cond) INTO @wa.

There is a certain change in ABAP Release 7.40 SP02 where a new S QL parser for Open SQL was introduced. The parser now checks both: statically specified Open SQL statements as well as content of dynamic tokens. These checks are more strict in this and subsequent ABAP releases. The strictness involves that dynamic tokens must be syntactically correct. This means that in former releases the checks were ignored. But in the Release 7.40 SP05 an exception occurs in such a fragment of the code. See Note 1810104 - Upgrade from Release 7.0 EhP3 or Release 7.3 EHP1 to Release 7.40.

No comments: