среда, 25 декабря 2013 г.

Класс для динамического создания таблиц

Небольшой класс, для динамического создания таблиц по названию таблицы в словаре.
Доступные следующие поля:

  • table - ссылка на таблицу
  • line - ссылка на структуру таблицы
  • lvc_scat - структура каталога полей
  • lvc_tcat - таблица каталога полей

и методы:

  • create_with_alv_table_create - Создание таблицы через метод cl_alv_table_create=>create_dynamic_table
  • create_with_rtts - Создание таблицы через RTTS (Run Time Type Services), начиная с версии ABAP 6.40
  • create_with_gen_subr_pool - Создание таблицы через генерацию программы (GENERATE SUBROUTINE POOL). Используется в старых системах

Ниже сам инклуд.

Include ZYV_DYN_TABLE_CREATE
*&---------------------------------------------------------------------* *& Include ZYV_DYN_TABLE_CREATE *&---------------------------------------------------------------------* *& Варианты динамического создания таблицы *& create_with_alv_table_create - Используя метод *& CL_ALV_TABLE_CREATE=>create_dynamic_table *& create_with_rtts - Используя RTTS (Run Time Type Services) *& начиная с версии ABAP 6.40. *& create_with_gen_subr_pool - Используется в старых системах, через *& генерацию программы (GENERATE SUBROUTINE POOL). *----------------------------------------------------------------------* * CLASS zcl_dyn_table DEFINITION *----------------------------------------------------------------------* class zcl_dyn_table definition. public section. data: table type ref to data, " Указатель на таблицу line type ref to data, " Указатель на строку lvc_scat type lvc_s_fcat, " Структура каталога lvc_tcat type lvc_t_fcat. " Каталог полей methods: create_with_alv_table_create importing dicttabname type string, " Имя таблицы в словаре create_with_rtts importing dicttabname type string, " Имя таблицы в словаре create_with_gen_subr_pool importing dicttabname type string. " Имя таблицы в словаре protected section. data: t_structure type standard table of dfies, t_source type standard table of string, r_struct type ref to cl_abap_structdescr, r_new_type type ref to cl_abap_structdescr, r_new_tab type ref to cl_abap_tabledescr, tabname type ddobjname, s_dfies type dfies, t_comp type cl_abap_structdescr=>component_table, s_source type string, rep_name like sy-repid, form_name(30) type c, message(240) type c, nline type i, word(72) type c. methods: fill_fieldcat importing r_sdescr type ref to cl_abap_structdescr. private section. endclass. "zcl_dyn_table DEFINITION *----------------------------------------------------------------------* * CLASS zcl_dyn_table IMPLEMENTATION *----------------------------------------------------------------------* class zcl_dyn_table implementation. method create_with_alv_table_create. field-symbols: type table, type any. me->tabname = dicttabname. clear me->t_structure[]. call function 'DDIF_FIELDINFO_GET' exporting tabname = me->tabname langu = sy-langu tables dfies_tab = me->t_structure. clear: me->lvc_tcat[]. loop at me->t_structure into me->s_dfies. clear me->lvc_scat. move-corresponding me->s_dfies to me->lvc_scat. me->lvc_scat-tabname = me->tabname. me->lvc_scat-fieldname = me->s_dfies-fieldname. me->lvc_scat-ref_field = me->s_dfies-fieldname. me->lvc_scat-ref_table = me->tabname. me->lvc_scat-domname = me->s_dfies-domname. me->lvc_scat-col_pos = me->s_dfies-position. append me->lvc_scat to me->lvc_tcat. clear me->lvc_scat. endloop. clear: me->s_dfies. "create table cl_alv_table_create=>create_dynamic_table( exporting it_fieldcatalog = me->lvc_tcat importing ep_table = me->table ). assign me->table->* to . create data me->line like line of . assign me->line->* to . endmethod. "create_with_alv_table_create method create_with_rtts. " Получаем описание структуры из ABAP словаря me->tabname = dicttabname. me->r_struct ?= cl_abap_typedescr=>describe_by_name( me->tabname ). me->t_comp = me->r_struct->get_components( ). " Создаем новый тип структуры по каталогу полей. me->r_new_type = cl_abap_structdescr=>create( me->t_comp ). " Создаем новую таблицу по структуре me->r_new_tab = cl_abap_tabledescr=>create( p_line_type = me->r_new_type p_table_kind = cl_abap_tabledescr=>tablekind_std p_unique = abap_false ). " Создаем ссылочные переменные присываеваем значения create data me->line type handle me->r_new_type. create data me->table type handle me->r_new_tab. " Заполним каталог полей clear: me->lvc_tcat[]. me->fill_fieldcat( me->r_struct ). clear: me->s_dfies. endmethod. "create_with_rtts method fill_fieldcat. data: lt_comp type cl_abap_structdescr=>component_table, ls_comp like line of lt_comp, lr_edescr type ref to cl_abap_elemdescr, lr_sdescr type ref to cl_abap_structdescr. lt_comp = r_sdescr->get_components( ). loop at lt_comp into ls_comp. clear me->lvc_scat. move-corresponding me->s_dfies to me->lvc_scat. if ls_comp-as_include eq 'X'. lr_sdescr ?= ls_comp-type. me->fill_fieldcat( lr_sdescr ). else. me->lvc_scat-tabname = me->tabname. me->lvc_scat-fieldname = ls_comp-name. lr_edescr ?= ls_comp-type. clear me->s_dfies. me->s_dfies = lr_edescr->get_ddic_field( ). me->lvc_scat-ref_field = me->s_dfies-fieldname. me->lvc_scat-ref_table = me->tabname. me->lvc_scat-domname = me->s_dfies-domname. me->lvc_scat-col_pos = me->s_dfies-position. append me->lvc_scat to me->lvc_tcat. endif. clear me->lvc_scat. endloop. endmethod. "fill_fieldcat method create_with_gen_subr_pool. field-symbols: type any, type table. me->tabname = dicttabname. me->form_name = 'TABLE_CREATE'. me->rep_name = 'ZTEST_SUBROUTINE_POOL'. me->s_source = 'REPORT ZTEST_SUBROUTINE_POOL.'. append me->s_source to me->t_source. me->s_source = 'FORM TABLE_CREATE USING I_FS TYPE ANY.'. append me->s_source to me->t_source. concatenate 'DATA: LT_GENTAB TYPE' me->tabname '.' into me->s_source separated by space. append me->s_source to me->t_source. me->s_source = 'DATA: POINTER TYPE REF TO DATA.'. append me->s_source to me->t_source. me->s_source = 'CREATE DATA POINTER LIKE STANDARD TABLE OF LT_GENTAB.'. append me->s_source to me->t_source. me->s_source = 'I_FS = POINTER.'. append me->s_source to me->t_source. me->s_source = 'ENDFORM. '. append me->s_source to me->t_source. catch system-exceptions generate_subpool_dir_full = 9. generate subroutine pool me->t_source name me->rep_name message me->message line me->nline word me->word. endcatch. " В случае ошибки выведем сообщение if not me->message is initial. message e000(0k) with me->message me->nline me->word. endif. assign me->table to . " Вызываем подпрограмму из сгенерированной программы perform (me->form_name) in program (me->rep_name) using . " Получаем определение типа данных и присваиваем динамической переменной assign me->table->* to . " Создаем тип данных, как строку сформированной таблицы create data me->line like line of . " Заполним каталог полей clear me->t_structure[]. call function 'DDIF_FIELDINFO_GET' exporting tabname = me->tabname langu = sy-langu tables dfies_tab = me->t_structure. clear: me->lvc_tcat[]. loop at me->t_structure into me->s_dfies. clear me->lvc_scat. move-corresponding me->s_dfies to me->lvc_scat. me->lvc_scat-tabname = me->tabname. me->lvc_scat-fieldname = me->s_dfies-fieldname. me->lvc_scat-ref_field = me->s_dfies-fieldname. me->lvc_scat-ref_table = me->tabname. me->lvc_scat-domname = me->s_dfies-domname. me->lvc_scat-col_pos = me->s_dfies-position. append me->lvc_scat to me->lvc_tcat. clear me->lvc_scat. endloop. clear: me->s_dfies. endmethod. "create_with_gen_subr_pool endclass. "zcl_dyn_table IMPLEMENTATION

Комментариев нет:

Отправить комментарий