Logical Databases
Logical databases are special ABAP programs that retrieve data and make it available to application programs. The most common use of logical databases is still to read data from database tables by linking them to executable ABAP programs.
However, from Release 4.5A, it has also been possible to call logical databases using the function module LDB_PROCESS. This allows you to call several logical databases from any ABAP program, nested in any way. It is also possible to call a logical database more than once in a program, if it has been programmed to allow this. This is particularly useful for programs with type 1.
Logical databases contain Open SQL statements that read data from the database. You do not therefore need to use SQL in your own programs. The logical database reads the program, stores them in the program if necessary, and then passes them line by line to the application program or the function module LDB_PROCESS using an interface work area.
Logical Databases - Views of Data
A logical database provides a particular view of database tables in the R/3 System. It is always worth using logical databases if the structure of the data that you want to read corresponds to a view available through a logical database.
The data structure in a logical database is hierarchical. Many tables in the R/3 System are linked to each other using foreign key relationships. Some of these dependencies form tree-like hierarchical structures. Logical databases read data from database tables that are part of these structures.
The diagram illustrates how the R/3 System might represent the structure of a company. A logical database can read the lines of these tables one after the other into an executable program in a sequence which is normally defined by the hierarchical structure. The term logical database is sometimes used to mean not only the program itself, but also the data that it can procure.
Tasks of Logical Databases
As well as allowing you to read data from the database, logical databases also allow you to program other tasks centrally, making your application programs less complicated. They can be used for the following tasks:
- Reading the same data for several programs.
The individual programs do not then need to know the exact structure of the relevant database tables (and especially not their foreign key relationships). Instead, they can rely on the logical database to read the database entries in the right order during the GET event.
- Defining the same user interface for several programs.
Logical databases have a built-in selection screen. Therefore, all of the programs that use the logical database have the same user interface.
- Central authorization checks
Authorization checks for central and sensitive data can be programmed centrally in the database to prevent them from being bypassed by simple application programs.
- Improving performance
If you want to improve response times, logical databases permit you to take a number of measures to achieve this (for example, using joins instead of nested SELECT statements). These become immediately effective in all of the application programs concerned and save you from having to modify their source code.
Structure of Logical Databases
http://help.sap.com/saphelp_wp/helpdata/en/9f/db9b5e35c111d1829f0000e829fbfe/content.htm
logical database
从数据库中取数据有三种方式:OPEN SQL NATIVE SQL和LOGICAL DATABASE,LOGICAL DATABASE相对于前两种方式,有下列优势:
l 它是按照primary key sequence来取数据的
l 可以在logic database program加入权限检查
l 可以自动产生selection screen使查询更加灵活
l 程序员不需要知道logical database中表之间的具体关系
l 对于logical database的性能优化可以影响到使用它的任何程序
l 维护和增强可以实现中央处理
4.6C中SAP提供了大约190个logical database。如果logical database有三个字符,前两个是logical database的名字第三个是logical database的application area。看每个字符代表的那个application area可以通过程序的attribute的application field的F4来查看。只有executable或report可以使用logical database。关键字NODE <node>,决定了logical database中的那个node要被使用,NODES为这个node准备响应的存储空间(work area或table area由node type来决定)。在4.6中tables仍然可以被使用。当logical database从数据库中读取数据时就会执行PUT <node>从而触发GET事件,GET <node>并不取数据。事件的执行顺序是由logical database本身的结构决定的。在get event中使用fields很很重要就像OPEN SQL中尽量避免SELECT *一样。Logical database是由很多subobjects组成的,structure决定了hierarchy以及数据的读取顺序。Node name最多有14个字符组成,有四种Node type:
l Table(type T):node name是一个transparent table name,node name和table name的名称必须一致,不允许deep types。
l DDIC type(type S):任何Node name都可以,node type由dictionary中的structure或table type来决定,允许deep types。
l Type groups(type C):node type是在type group中定义,type group name必须在Type group field中维护。应当优先使用DDIC TYPE以便像SAP QUERY这样的应用也可以使用。
l Dynamic nodes(type A):没有固定的type,它在程序运行时才能够确定。
报表的START-OF-SELECTION在所有的get events之前发生,END-OF-SELECTION在所有的get events之后发生。GET <node> events一旦从logical database中取得数据时都回触发,所以这个事件会被多次触发。Get events的执行顺序由logical database的结构决定。当节点的所有子节点都被处理后再节点的下一个数据被处理之前就会触发GET <node> LATE事件,在事件开始系统会自动创建一line feed,并把color,font和intensity设为系统默认,如果在其他事件中设置了color,也会被重设为系统默认。CHECK结束当前事件,STOP结束程序处理执行END-OF-SELECTION,在END-OF-SELECTION中的STOP,则之际显示list buffer。EXIT结束程序处理,也不执行END-OF-SELECTION,直接显示list buffer中的内容。
还可以使用REJECT,当前的处理被终止,还可以在下一次数据读取时触发。Logical database程序有一个include:db<name>sel,这里定义了logical database的selection screen。For node把selection分配给单个的logical node。Selection screen的屏幕形式由程序中的nodes表达式决定。Field selection可以由Logical database程序中的node决定,这个通过SELECTION-SCREEN中的FIELD SELECTION FOR NODE决定。Logical database程序中的这段代码可以使application program中通过GET <node> FIELD <field list>来限制取出数据的数量,logical database程序中可以为某个Node设置dynamic selection,在语句中可以通过addition:DYNAMIC SELECTIONS FOR NODE来实现,这样Dynamic selection button就会在你的报表的selection screen中出现。另外logical database还可以定义不同的selection screen version。如果你在报表程序中指定了一个logical database,你就可以使用logical database的selection screen了。如果在report程序中你只指定了logical database的子节点,其上层的节点的筛选条件也可以被使用。对于T(table)类型的node,可以通过TABLES来声明它的work area。值得注意的是logical database总是按照自己的结构来读取数据的,所以当你需要读取logical database的比较深层次的node的数据的时候,你需要考虑换一个其所在层次比较高的logical database或者自己写取数逻辑。Selection view决定了什么样的字段会显示在selection screen。可以创建类型为CUS的自己的view,或者覆盖类型为SAP的view。
Logical database程序的命名规则是:SAPDB<ldbname>其中ldbname是logical database的名称。这些程序是由subroutine组成的,每个subroutine对应于一个事件。例如subroutine <init>是在logical database程序启动前执行,用来初始化变量和屏幕选择。还有其他的一些subroutine也是在屏幕的PBO或PAI事件中发生的。例如AUTHORITY CHECK是在PAI中发生的。数据库访问是在put_<node> subroutine中实现的。这个subroutine可能会被多次执行,这取决于屏幕的选择条件。这些subroutine的执行顺序由logical database的structure来决定。使用logical database的report在执行时控制权首先属于logical database program。每个事件在logical database program中都有一个对应的subroutine。在LDB和相应的report之间交互的时候LDB中的subroutine都会先执行。Logical database program读取数据的顺序是按照其structure执行的,总是从最顶部向下循环。LDB program是通过PUT_<NODE> subroutine来访问数据的,在PUT event中,control由LDB program传向report program的GET EVENT。取出来的数据或放到report program相应的work area中。GET event一旦执行完毕control就会还给LDB program。Data read的depth是由report program的GET event来决定的。如果node的某个field没有筛选条件,可以在report program中增加这个字段select-options在这个node的GET event中通过check来筛选。
标签: LOGICAL DATABASE
归类于 abap, abap PA 学习笔记 | 没有评论 »
ABAP FUNCTION MODULE调用logical database
星期三, 08月 19th, 2009
在4.5B版本之后,可通过FM:LDB_PROCESS来调用逻辑数据库,这样不仅在executable program中可以使用LDB,在interactive program和module pool中也可以使用逻辑数据库。而且多个LDBS可以在一个程序中使用,同一个LDB可以在一个程序中调用多次。
CALL FUNCTION ‘LDB_PROCESS’
EXPORTING
Ldbname = ‘F1S’
TABLES
Callback = call_back_tab
Selections = sel_tab.
Logical database与function module LDB_PROCESS的交互原理与get event相似,必需根据data dictionary structure ldbcb创建一个internal table和work area。这个内表收集了要处理的nodes和call back subroutines。这个internal table的各个字段的意义如下:
l LDBNODE Name of the node to process
l GET X Process the GET event of the above node
l GET_LATE X process the GET_LATE event of the above node
l CB_PROG name of the program containing the callback subroutine
l CB_FORM name of the callback subroutine
当你调用logical database时,你必须保证selection screen上输入了正确的值,必须通过dictionary structure RSPARAMS,为了使logical database可以被multiple processing,你必须在database program中添加subroutine LDB_PROCESS_INIT。如果你通过LDB_PROCESS指定了一个logical database,并且定义了额外的筛选条件,这些条件对应的该node的字段没有在dynamic selection中定义,那么必须在程序中去check当前的记录是否符合这些条件。如果check失败,你必须把subroutine的selected参数置为space。
FORM call_back_sflight.
USING name LIKE ldbn-ldbnode
Workarea TYPE sflight
Node TYPE c
Selected TYPE c.
IF NOT workarea-price IN s_price.
Selected = space.
EXIT.
ENDIF.
ENDFORM.