先上效果图:
主程序:
*&---------------------------------------------------------------------*
*& Report ZOPENSQL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOPENSQL.
*INCLUDE zopensqlf08.
INCLUDE zopensqlf01.
INCLUDE zopensqltop.
INCLUDE zopensqlf02.
INCLUDE zopensqlf03.
INCLUDE zopensqlf07.
INCLUDE zopensqlf04.
INCLUDE zopensqlf05.
INCLUDE zopensqlf06.
INCLUDE zopensqlo01.
INCLUDE zopensqlo02.
INCLUDE zopensqli01.
INCLUDE zopensqli02.
START-OF-SELECTION.
CALL SCREEN c_100.
子程序1:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF01
*&---------------------------------------------------------------------*
CONSTANTS: c_line_length TYPE i VALUE 150.
*& Message
CONSTANTS :
c_sql_editor TYPE c LENGTH 10 VALUE 'SQL_EDITOR' ,
c_msg01 TYPE string VALUE
'Error while instantiating ABAP proxy of TextEdit control',
c_msg02 TYPE string VALUE
'Error in flush',
c_msg03 TYPE string VALUE
'Error while retrieving text form TextEdit control',
c_msg04 TYPE string VALUE
'Error while sending text into TextEdit control',
c_msg05 TYPE string VALUE
'Error while destroying TextEdit control'.
CONSTANTS :
c_sql_key_word_01 TYPE string
VALUE '-SELECT-FROM-INNER-JOIN-ON-LEFT-OUTER-',
c_sql_key_word_02 TYPE string
VALUE '-CLIENT-SPECIFIED-INTO-APPENDING-AND-OR-IN-BETWEEN-LIKE-',
c_sql_key_word_03 TYPE string
VALUE '-WHERE-ORDER-BY-GROUP-HAVING-ASCENDING-DESCENDING-',
c_sql_key_word_04 TYPE string
VALUE '-AS-SINGLE-DISTINCT-SUM-COUNT-MIN-MAX-AVG-',
c_select TYPE string VALUE 'SELECT',
c_sum TYPE string VALUE 'SUM',
c_count TYPE string VALUE 'COUNT',
c_count1 type string value 'COUNT(',
c_min TYPE string VALUE 'MIN',
c_max TYPE string VALUE 'MAX',
c_avg TYPE string VALUE 'AVG',
c_left TYPE string VALUE '(',
c_right TYPE string VALUE ')',
c_all_fields TYPE string VALUE '*',
c_from TYPE string VALUE 'FROM',
c_on TYPE string VALUE 'ON',
c_inner TYPE string VALUE 'INNER',
c_join TYPE string VALUE 'JOIN',
c_out TYPE string VALUE 'OUTER',
c_left_j TYPE string VALUE 'LEFT',
c_as TYPE string VALUE 'AS' ,
c_or TYPE string VALUE 'OR',
c_and TYPE string VALUE 'AND',
c_single TYPE string VALUE 'SINGLE',
c_distinct TYPE string VALUE 'DISTINCT',
c_where TYPE string VALUE 'WHERE',
c_order TYPE string VALUE 'ORDER',
c_group TYPE string VALUE 'GROUP',
c_have TYPE string VALUE 'HAVING',
c_hex TYPE c LENGTH 5 VALUE '\X\09',
c_funct TYPE string VALUE 'FUNCT',
c_separ TYPE string VALUE '-',
c_ss TYPE string VALUE '~',
c_quotes TYPE string VALUE '''',
c_line_fields TYPE i VALUE 3 ,
c_fields_blank TYPE i VALUE 1 .
CONSTANTS :
c_comment1 TYPE string VALUE '*',
c_comment2 TYPE string VALUE '"'.
******************************************************
* To Definde Structure
******************************************************
TYPES: BEGIN OF st_text,
line TYPE c LENGTH c_line_length ,
END OF st_text.
TYPES: tt_text TYPE STANDARD TABLE OF st_text .
TYPES: BEGIN OF st_element,
alias TYPE c LENGTH 40 ,
name TYPE c LENGTH 40 ,
source TYPE ddobjname ,
link TYPE ddobjname ,
label TYPE string ,
index TYPE i ,
display TYPE string ,
END OF st_element.
TYPES: BEGIN OF st_exception,
id TYPE i,
icon TYPE icon_d,
msg TYPE string,
END OF st_exception.
TYPES: tt_element TYPE STANDARD TABLE OF st_element .
TYPES: tt_exception TYPE STANDARD TABLE OF st_exception .
TYPES: tt_code TYPE TABLE OF rssource-line .
子程序2:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLTOP
*&---------------------------------------------------------------------*
******************************************************
* To Constants
******************************************************
CONSTANTS :
c_100 TYPE rs37a-fnum VALUE '100' ,
c_200 TYPE rs37a-fnum VALUE '200' .
******************************************************
* To Define Varant
******************************************************
DATA :
it_text TYPE tt_text,
g_editor TYPE REF TO cl_gui_textedit,
g_editor1 TYPE REF TO cl_gui_textedit,
g_grid TYPE REF TO cl_gui_alv_grid,
g_splitter TYPE REF TO cl_gui_easy_splitter_container,
g_splitter1 TYPE REF TO cl_gui_easy_splitter_container,
g_container TYPE REF TO cl_gui_custom_container,
g_exception TYPE tt_exception,
g_ucomm TYPE sy-ucomm,
g_repid TYPE sy-repid,
g_file TYPE c LENGTH 50,
*& Configuration Parameters
l_case_01 TYPE c VALUE '' ,
l_case_02 TYPE c VALUE '' ,
l_case_03 TYPE c VALUE 'X' ,
g_case TYPE i VALUE 3 ,
l_label_01 TYPE c VALUE 'X' ,
l_label_02 TYPE c VALUE '' ,
g_label TYPE i VALUE 1 .
*& ALV Data Must Be A Global Variant
FIELD-SYMBOLS: <table> TYPE ANY TABLE,
<line> TYPE ANY.
*& Necessary To Flush The Automation Queue
CLASS cl_gui_cfw DEFINITION LOAD.
子程序3:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF02
*&---------------------------------------------------------------------*
******************************************************
*& Form destroy_Control_object.
******************************************************
DEFINE destroy_control_object.
check &1 is not initial .
call method &1->free
exceptions
others = 1.
free : &1 .
END-OF-DEFINITION.
******************************************************
*& Form Create_ALV_Object
******************************************************
FORM create_alv_object USING p_grid TYPE REF TO cl_gui_alv_grid
p_container TYPE REF TO cl_gui_container.
CREATE OBJECT p_grid
EXPORTING i_parent = p_container.
ENDFORM . "Create_ALV_Object
******************************************************
* Form Show_Data_In_ALV
******************************************************
FORM show_data_in_alv USING p_grid TYPE REF TO cl_gui_alv_grid
p_data TYPE ANY TABLE
p_field TYPE ANY TABLE .
CALL METHOD p_grid->set_table_for_first_display
EXPORTING
i_buffer_active = 'X'
CHANGING
it_outtab = p_data
it_fieldcatalog = p_field
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ENDFORM. "Show_Data_In_ALV
******************************************************
*& Form Destroy_Grid_Object
******************************************************
FORM destroy_alv_object CHANGING p_grid TYPE REF TO cl_gui_alv_grid.
destroy_control_object p_grid .
ENDFORM . "Destroy_Object
******************************************************
*& Form prepare_alv_field_cat
******************************************************
FORM prepare_alv_field_cat USING p_fields TYPE tt_element
CHANGING p_fieldcat TYPE lvc_t_fcat .
DATA : wa_field TYPE st_element ,
wa_fieldcat TYPE LINE OF lvc_t_fcat .
CLEAR : p_fieldcat .
LOOP AT p_fields INTO wa_field .
*& Must Translate Field Name To Upper Case
TRANSLATE wa_field-alias TO UPPER CASE .
CASE g_label .
WHEN 1 .
PERFORM change_field_name USING wa_field-alias
wa_field-label
''
CHANGING p_fieldcat .
WHEN 2 .
PERFORM change_field_name USING wa_field-alias
wa_field-alias
''
CHANGING p_fieldcat .
ENDCASE .
ENDLOOP .
ENDFORM . "destroy_alv_object
******************************************************
*& Form prepare_alv_error_field_Cat
******************************************************
FORM prepare_alv_error_field_cat CHANGING p_fieldcat TYPE lvc_t_fcat .
CLEAR p_fieldcat[] .
PERFORM change_field_name USING 'ICON' 'Status' 'X'
CHANGING p_fieldcat .
PERFORM change_field_name USING 'MSG' 'Message' ''
CHANGING p_fieldcat .
ENDFORM . "prepare_alv_error_field_Cat
******************************************************
*& Form CHANGE_FIELD_NAME
******************************************************
FORM change_field_name USING p_field p_text p_icon
CHANGING p_fieldcat TYPE lvc_t_fcat .
DATA wa_fieldcat TYPE LINE OF lvc_t_fcat .
wa_fieldcat-fieldname = p_field.
wa_fieldcat-scrtext_l = p_text.
wa_fieldcat-scrtext_m = p_text.
wa_fieldcat-scrtext_s = p_text.
wa_fieldcat-icon = p_icon .
IF p_field = 'MSG'.
wa_fieldcat-outputlen = 100 .
ENDIF .
APPEND wa_fieldcat TO p_fieldcat.
ENDFORM. " CHANGE_FIELD_NAME
******************************************************
*& Form Append_Error_Message
******************************************************
FORM append_error_message USING p_id TYPE i
p_msg .
DATA : wa_exception TYPE st_exception ,
l_msg TYPE string ,
l_lines TYPE i .
CASE p_id .
WHEN 1 .
CONCATENATE 'Miss a '' at ' p_msg
INTO l_msg
SEPARATED BY space .
WHEN 2 .
CONCATENATE 'Miss a ( at ' p_msg
INTO l_msg
SEPARATED BY space .
WHEN 3 .
CONCATENATE 'After ''AS'' , need a alias at ' p_msg
INTO l_msg
SEPARATED BY space .
WHEN 4 .
l_msg = p_msg .
WHEN 5 .
CONCATENATE 'Table' p_msg 'doesn''t exist '
INTO l_msg
SEPARATED BY space .
WHEN 6 .
CONCATENATE 'Field' p_msg 'doesn''t exist '
INTO l_msg
SEPARATED BY space .
WHEN 7 .
l_msg = 'SQL with sub SQL can not be formated in this verison .' .
WHEN 8 .
l_msg = 'Please input Open SQL !'.
WHEN 9 .
CONCATENATE 'Encounter a ' p_msg 'after order by !'
INTO l_msg
SEPARATED BY space .
WHEN 10 .
l_msg = 'Can not find any field !'.
WHEN 11 .
l_msg = 'Can not find any table !'.
ENDCASE .
DESCRIBE TABLE g_exception LINES l_lines .
wa_exception-id = l_lines + 1 .
wa_exception-icon = '@0A@' .
wa_exception-msg = l_msg .
APPEND wa_exception TO g_exception .
ENDFORM. "Append_Error_Message
******************************************************
*& Form Download_Table_To_Local
******************************************************
FORM download_table_to_local TABLES p_table
USING p_file TYPE rlgrap-filename .
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = p_file
filetype = 'ASC'
TABLES
data_tab = p_table
EXCEPTIONS
file_open_error = 1
file_write_error = 2
invalid_filesize = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
no_authority = 10
OTHERS = 11.
ENDFORM . "Download_Table_To_Local
******************************************************
*& Form Get_FileName
******************************************************
FORM get_filename CHANGING p_file .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* mask = ',*.txt.'
mode = 'S'
title = 'Save to local'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
ENDFORM. "Get_FileName
子程序4:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF03
*&---------------------------------------------------------------------*
DATA : g_postion TYPE i .
******************************************************
*& Form Create_Editor_Object
******************************************************
FORM create_editor_object USING p_editor TYPE REF TO cl_gui_textedit
p_container TYPE REF TO cl_gui_container.
CREATE OBJECT p_editor
EXPORTING
parent = p_container
wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position
wordwrap_position = c_line_length
wordwrap_to_linebreak_mode = cl_gui_textedit=>true
EXCEPTIONS
OTHERS = 1.
ENDFORM . "Create_Editor_Object
******************************************************
*& Form Set_Comment_Mode
******************************************************
FORM set_comment_mode USING p_editor TYPE REF TO cl_gui_textedit .
CALL METHOD p_editor->set_comments_string.
CALL METHOD p_editor->set_highlight_comments_mode.
ENDFORM . "Set_Comment_Mode
******************************************************
*& Form Get_Selection_Index
******************************************************
FORM get_selection_index USING p_editor TYPE REF TO cl_gui_textedit
p_return TYPE i .
DATA : l_from TYPE i ,
l_to TYPE i .
CALL METHOD p_editor->get_selection_indexes
IMPORTING
from_index = l_from
to_index = l_to
EXCEPTIONS
error_cntl_call_method = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
p_return = l_to - l_from .
ENDFORM . "Get_Selection_Index
******************************************************
*& Form Save_Text_To_Table
******************************************************
FORM save_text_to_table USING p_editor TYPE REF TO cl_gui_textedit
CHANGING p_text TYPE tt_text .
DATA : l_flag TYPE i .
PERFORM get_selection_index USING g_editor l_flag.
IF l_flag = 0 .
CALL METHOD p_editor->get_text_as_r3table
IMPORTING
table = p_text
EXCEPTIONS
OTHERS = 1.
ELSE .
CALL METHOD p_editor->get_selected_text_as_r3table
IMPORTING
table = p_text
EXCEPTIONS
OTHERS = 1.
ENDIF .
IF sy-subrc NE 0.
PERFORM show_message USING c_msg03 .
ENDIF.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
PERFORM show_message USING c_msg02 .
ENDIF.
ENDFORM . "Save_TEXT_To_TABLE
******************************************************
*& Form Save_As_Local_File
******************************************************
FORM save_as_local_file USING p_editor TYPE REF TO cl_gui_textedit
p_file .
CALL METHOD p_editor->save_as_local_file
EXPORTING
file_name = p_file
EXCEPTIONS
error_cntl_call_method = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM . "Save_As_Local_File
******************************************************
*& Form set_text_as_r3table
******************************************************
FORM set_text_as_r3table USING p_editor TYPE REF TO cl_gui_textedit
CHANGING p_text TYPE tt_text.
CALL METHOD p_editor->set_text_as_r3table
EXPORTING
table = p_text
EXCEPTIONS
OTHERS = 1.
ENDFORM . "set_text_as_r3table
******************************************************
*& Form set_text_as_r3table
******************************************************
FORM set_selected_text_as_r3table USING p_editor TYPE REF TO cl_gui_textedit
CHANGING p_text TYPE tt_text.
CALL METHOD p_editor->set_selected_text_as_r3table
EXPORTING
table = p_text
EXCEPTIONS
OTHERS = 1.
ENDFORM . "set_text_as_r3table
******************************************************
*& Form Load_Text_From_Table
******************************************************
FORM load_text_from_table USING p_editor TYPE REF TO cl_gui_textedit
CHANGING p_text TYPE tt_text.
DATA : l_flag TYPE i .
PERFORM get_selection_index USING g_editor l_flag.
IF l_flag = 0 .
PERFORM set_text_as_r3table USING p_editor
CHANGING p_text .
ELSE .
PERFORM set_selected_text_as_r3table USING p_editor
CHANGING p_text .
ENDIF .
IF sy-subrc NE 0.
PERFORM show_message USING c_msg04 .
ENDIF.
ENDFORM. "Load_Text_From
******************************************************
*& Form Set_Status_Text
******************************************************
FORM set_status_text USING p_editor TYPE REF TO cl_gui_textedit
CHANGING p_text .
CALL METHOD p_editor->set_status_text
EXPORTING
status_text = p_text
EXCEPTIONS
error_cntl_call_method = 1
OTHERS = 2.
ENDFORM. "Set_Status_Text
******************************************************
*& Form Set_Toolbar_Mode
******************************************************
FORM set_toolbar_mode USING p_editor TYPE REF TO cl_gui_textedit
p_status TYPE i .
CALL METHOD p_editor->set_toolbar_mode
EXPORTING
toolbar_mode = p_status
EXCEPTIONS
error_cntl_call_method = 1
invalid_parameter = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. "Set_Toolbar_Mode
******************************************************
*& Form SET_READONLY_MODE
******************************************************
FORM set_readonly_mode USING p_editor TYPE REF TO cl_gui_textedit
p_mode TYPE i .
CALL METHOD p_editor->set_readonly_mode
EXPORTING
readonly_mode = p_mode
EXCEPTIONS
error_cntl_call_method = 1
invalid_parameter = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. "set_readonly_mode
******************************************************
*& Form Destroy_Editor_Object
******************************************************
FORM destroy_editor_object CHANGING p_editor TYPE REF TO cl_gui_textedit.
destroy_control_object p_editor .
ENDFORM . "Destroy_Object
******************************************************
*& Form Create_Container_Object
******************************************************
FORM create_container_object USING p_container TYPE REF TO cl_gui_custom_container
p_sql_editor.
CREATE OBJECT p_container
EXPORTING
container_name = p_sql_editor
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
ENDFORM . "Create_Container_Object
******************************************************
*& Form Destroy_Container_Object
******************************************************
FORM destroy_container_object CHANGING p_container TYPE REF TO cl_gui_custom_container.
destroy_control_object p_container .
ENDFORM . "Destroy_Object
******************************************************
*& Form Get_Splitter_Postion
******************************************************
FORM get_splitter_postion USING p_splitter TYPE REF TO cl_gui_easy_splitter_container.
CALL METHOD p_splitter->get_sash_position
IMPORTING
sash_position = g_postion
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM . "get_splitter_postion
******************************************************
*& Form Set_Splitter_Postion
******************************************************
FORM set_splitter_postion USING p_splitter TYPE REF TO cl_gui_easy_splitter_container
p_postion TYPE i .
*& Set Splitter Postion
CALL METHOD p_splitter->set_sash_position
EXPORTING
sash_position = p_postion.
ENDFORM . "Set_Splitter_Postion
******************************************************
*& Form Move_splitter_Postion
******************************************************
FORM move_splitter_postion USING p_splitter TYPE REF TO cl_gui_easy_splitter_container .
* PERFORM get_splitter_postion USING p_splitter .
CASE g_postion .
WHEN 0 .
g_postion = 100 .
WHEN 50.
g_postion = 0 .
WHEN 100 .
g_postion = 50 .
ENDCASE .
PERFORM set_splitter_postion USING p_splitter g_postion .
ENDFORM . "move_splitter_postion
******************************************************
*& Form Create_Splitter_Object
******************************************************
FORM create_splitter_object USING p_splitter TYPE REF TO cl_gui_easy_splitter_container
p_container
p_orientation TYPE i .
DATA : l_position TYPE i VALUE 100 .
CREATE OBJECT p_splitter
EXPORTING
parent = p_container
orientation = p_orientation.
PERFORM set_splitter_postion USING p_splitter
l_position .
ENDFORM . "Create_Splitter_Object
******************************************************
*& Form Destroy_Splitter_Object
******************************************************
FORM destroy_splitter_object CHANGING p_splitter TYPE REF TO cl_gui_easy_splitter_container.
destroy_control_object p_splitter .
ENDFORM . "Destroy_Object
子程序5:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF07
*&---------------------------------------------------------------------*
******************************************************
*& Form Format_fields
******************************************************
FORM format_fields USING value(p_fields) TYPE tt_element
p_act TYPE i
CHANGING p_select TYPE tt_text
p_text TYPE tt_text
p_type TYPE string .
DATA : wa_text TYPE st_text ,
l_line TYPE i VALUE 1 ,
l_mod TYPE i VALUE 0 ,
l_index TYPE i VALUE 0 ,
l_string TYPE c LENGTH 120,
wa_field TYPE st_element ,
l_flag TYPE i VALUE 1 .
*& Display Key Word "Single" And "Distinct"
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_single OR c_distinct .
p_type = wa_text-line .
WHEN c_from OR c_where OR c_order .
EXIT .
ENDCASE .
DELETE p_text .
ENDLOOP .
LOOP AT p_fields INTO wa_field .
IF sy-tabix = 1 .
IF p_type <> '' AND p_act <> 1 .
CONCATENATE c_select p_type wa_field-display
INTO l_string
SEPARATED BY space .
ADD 2 TO l_index .
ELSE .
CONCATENATE c_select wa_field-display
INTO l_string
SEPARATED BY space .
ADD 1 TO l_index .
ENDIF .
ELSE .
SHIFT wa_field-display BY c_fields_blank PLACES RIGHT .
CONCATENATE l_string wa_field-display
INTO l_string
SEPARATED BY space .
ADD 1 TO l_index .
ENDIF .
l_mod = l_index MOD c_line_fields .
IF l_mod = 0 .
IF l_line <> 1 .
SHIFT l_string BY 5 PLACES RIGHT.
ENDIF .
INSERT l_string INTO p_text INDEX l_line .
APPEND l_string TO p_select .
ADD 1 TO l_line .
CLEAR : l_string .
ENDIF .
AT LAST.
CHECK l_string IS NOT INITIAL .
IF l_line <> 1 .
SHIFT l_string BY 5 PLACES RIGHT.
ENDIF .
INSERT l_string INTO p_text INDEX l_line .
APPEND l_string TO p_select .
ADD 1 TO l_line .
ENDAT .
ENDLOOP .
ENDFORM . "Format_Fields
******************************************************
*& Form format_tables
******************************************************
FORM format_tables USING p_on TYPE tt_text
CHANGING p_text TYPE tt_text.
DATA : l_from_index TYPE i ,
l_to TYPE string ,
wa_text TYPE st_text ,
l_str TYPE string ,
l_delete TYPE i VALUE 1 .
CHECK p_on IS NOT INITIAL .
CONCATENATE c_separ c_where c_separ c_group
c_separ c_have c_separ c_order c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
IF wa_text-line = c_from .
l_from_index = sy-tabix .
l_delete = 0 .
ENDIF .
CONCATENATE c_separ wa_text-line c_separ
INTO wa_text-line .
IF l_to CS wa_text-line .
EXIT .
ENDIF .
IF l_delete = 0 .
DELETE p_text .
ENDIF .
ENDLOOP .
CHECK l_from_index > 0 .
INSERT LINES OF p_on INTO p_text INDEX l_from_index .
ENDFORM . "format_fields
******************************************************
*& Form Format_Where
******************************************************
FORM format_where USING p_where TYPE tt_text
CHANGING p_text TYPE tt_text .
DATA : l_index TYPE i ,
l_to TYPE string ,
wa_text TYPE st_text ,
l_delete TYPE i VALUE 1 .
CHECK p_where IS NOT INITIAL .
CONCATENATE c_separ c_group c_separ c_have
c_separ c_order c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
IF wa_text-line = c_where .
l_index = sy-tabix .
l_delete = 0 .
ENDIF .
CONCATENATE c_separ wa_text-line c_separ
INTO wa_text-line .
IF l_to CS wa_text-line .
EXIT .
ENDIF .
IF l_delete = 0 .
DELETE p_text .
ENDIF .
ENDLOOP .
CHECK l_index > 0 .
INSERT LINES OF p_where INTO p_text INDEX l_index .
ENDFORM . "Format_Where
******************************************************
*& Form Format_Group
******************************************************
FORM format_group USING p_group TYPE tt_text
CHANGING p_text TYPE tt_text .
DATA : l_index TYPE i ,
l_to TYPE string ,
wa_text TYPE st_text ,
l_delete TYPE i VALUE 1 .
CHECK p_group IS NOT INITIAL .
CONCATENATE c_separ c_have
c_separ c_order c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
IF wa_text-line = c_group .
l_index = sy-tabix .
l_delete = 0 .
ENDIF .
CONCATENATE c_separ wa_text-line c_separ
INTO wa_text-line .
IF l_to CS wa_text-line .
EXIT .
ENDIF .
IF l_delete = 0 .
DELETE p_text .
ENDIF .
ENDLOOP .
CHECK l_index > 0 .
INSERT LINES OF p_group INTO p_text INDEX l_index .
ENDFORM . "Format_Group
******************************************************
*& Form Format_have
******************************************************
FORM format_have USING p_have TYPE tt_text
CHANGING p_text TYPE tt_text .
DATA : l_index TYPE i ,
l_to TYPE string ,
wa_text TYPE st_text ,
l_delete TYPE i VALUE 1 .
CHECK p_have IS NOT INITIAL .
CONCATENATE c_separ c_order c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
IF wa_text-line = c_have .
l_index = sy-tabix .
l_delete = 0 .
ENDIF .
CONCATENATE c_separ wa_text-line c_separ
INTO wa_text-line .
IF l_to CS wa_text-line .
EXIT .
ENDIF .
IF l_delete = 0 .
DELETE p_text .
ENDIF .
ENDLOOP .
CHECK l_index > 0 .
INSERT LINES OF p_have INTO p_text INDEX l_index .
ENDFORM . "Format_have
******************************************************
*& Form format_order
******************************************************
FORM format_order USING p_order TYPE tt_text
CHANGING p_text TYPE tt_text .
DATA : l_index TYPE i ,
l_to TYPE string ,
wa_text TYPE st_text ,
l_delete TYPE i VALUE 1 .
CHECK p_order IS NOT INITIAL .
CONCATENATE c_separ c_order c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
IF wa_text-line = c_order .
l_index = sy-tabix .
l_delete = 0 .
ENDIF .
CONCATENATE c_separ wa_text-line c_separ
INTO wa_text-line .
IF l_delete = 0 .
DELETE p_text .
ENDIF .
ENDLOOP .
CHECK l_index > 0 .
INSERT LINES OF p_order INTO p_text INDEX l_index .
ENDFORM . "format_order
子程序6:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF04
*&---------------------------------------------------------------------*
******************************************************
*& Form Process_Comment2
******************************************************
FORM process_comment2 CHANGING p_text TYPE st_text .
DATA : l_temp TYPE string ,
l_amount TYPE i VALUE 0,
l_mod TYPE i VALUE 0,
l_index TYPE i ,
l_length TYPE i .
l_amount = 0 .
l_length = STRLEN( p_text-line ) .
DO l_length TIMES .
l_index = sy-index - 1 .
CASE p_text-line+l_index(1).
WHEN c_comment2 .
l_mod = l_amount MOD 2 .
IF l_mod = 0 .
IF l_index = 0 .
p_text = space .
ELSE .
p_text = p_text(l_index).
ENDIF .
EXIT .
ENDIF .
WHEN c_quotes .
ADD 1 TO l_amount .
ENDCASE .
ENDDO .
l_mod = l_amount MOD 2 .
IF l_mod <> 0 .
*& Exception "You maybe miss a single quoted symbol"
PERFORM append_error_message USING 1 p_text-line.
ENDIF .
ENDFORM. "Process_Comment2
******************************************************
*& Form Move_Comments_In_SQL
******************************************************
FORM move_comments_in_sql CHANGING p_text TYPE tt_text .
DATA : wa_text TYPE st_text .
LOOP AT p_text INTO wa_text .
IF wa_text-line(1) = c_comment1.
DELETE p_text .
CONTINUE .
ENDIF .
IF wa_text-line CS c_comment2 .
PERFORM process_comment2 CHANGING wa_text .
MODIFY p_text FROM wa_text .
ENDIF .
CONDENSE : wa_text-line .
CHECK wa_text-line = space.
DELETE p_text .
ENDLOOP .
ENDFORM . "Move_Comments_In_SQL
******************************************************
*& Form Separated_Quoted
******************************************************
FORM separated_quoted USING p_line TYPE st_text
CHANGING p_table TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_flag TYPE i VALUE 1 ,
l_length TYPE i ,
l_index TYPE i ,
l_start TYPE i VALUE 0 ,
l_end TYPE i VALUE 0 ,
l_mod TYPE i VALUE 0 ,
l_amount TYPE i VALUE 0 ,
l_str TYPE string .
l_length = STRLEN( p_line-line ) .
DO l_length TIMES .
l_index = sy-index - 1 .
CASE p_line+l_index(1).
WHEN c_quotes .
IF l_amount = 0 .
IF sy-index > 1 .
l_end = l_index - l_start .
l_str = p_line-line+l_start(l_end) .
APPEND l_str TO p_table .
l_start = l_index .
ENDIF .
ENDIF .
ADD 1 TO l_amount .
WHEN space.
IF l_amount = 0 .
l_end = l_index - l_start .
IF l_end = 0 .
l_end = 1 .
ENDIF .
l_str = p_line-line+l_start(l_end) .
APPEND l_str TO p_table .
l_start = l_index .
ENDIF .
l_mod = l_amount MOD 2 .
CHECK l_amount <> 0 AND l_mod = 0.
l_end = l_index - l_start .
l_str = p_line-line+l_start(l_end) .
APPEND l_str TO p_table .
l_start = l_index .
l_amount = 0 .
WHEN OTHERS .
l_mod = l_amount MOD 2 .
CHECK l_amount <> 0 AND l_mod = 0.
l_end = l_index - l_start .
l_str = p_line-line+l_start(l_end) .
APPEND l_str TO p_table .
l_start = l_index .
l_amount = 0 .
ENDCASE .
ENDDO .
l_str = p_line-line+l_start.
APPEND l_str TO p_table .
l_start = l_index .
l_mod = l_amount MOD 2 .
IF l_mod <> 0 .
*& Exception "You maybe miss a single quoted symbol"
PERFORM append_error_message USING 1 p_line-line.
ENDIF .
ENDFORM . "Process_Quoted
******************************************************
*& Form Separate_SQL_From_Word
******************************************************
FORM separate_from_word CHANGING p_text TYPE tt_text .
DATA : wa_text TYPE st_text ,
it_temp TYPE tt_text ,
it_split TYPE tt_text ,
l_tab TYPE c .
CLEAR : it_temp.
LOOP AT p_text INTO wa_text .
IF wa_text CS c_quotes .
CLEAR it_split .
PERFORM separated_quoted USING wa_text
CHANGING it_split .
APPEND LINES OF it_split TO it_temp .
ELSE .
CLEAR it_split .
SPLIT wa_text AT space INTO TABLE it_split .
APPEND LINES OF it_split TO it_temp .
ENDIF .
ENDLOOP .
*& Delete All Blank Line
LOOP AT it_temp INTO wa_text .
CHECK NOT wa_text CS c_quotes .
CONDENSE wa_text-line .
MODIFY it_temp FROM wa_text .
ENDLOOP .
PERFORM get_asc_code USING c_hex
CHANGING l_tab.
DELETE it_temp WHERE LINE = space OR
LINE = l_tab.
p_text[] = it_temp[] .
ENDFORM. "Separate_SQL_From_Word
******************************************************
*& Form Case_To_Upper
******************************************************
FORM case_to_upper CHANGING p_text .
TRANSLATE p_text TO UPPER CASE.
ENDFORM. "Case_To_Upper
******************************************************
*& Form Case_To_Lower
******************************************************
FORM case_to_lower CHANGING p_text .
TRANSLATE p_text TO LOWER CASE.
ENDFORM. "Case_To_Upper
******************************************************
*& Form transfer_Case
******************************************************
FORM transfer_case USING p_type TYPE i
CHANGING p_text TYPE tt_text .
DATA : wa_text TYPE st_text .
LOOP AT p_text INTO wa_text .
CASE p_type .
WHEN '1'.
PERFORM case_to_lower CHANGING wa_text-line .
WHEN '2'.
PERFORM case_to_upper CHANGING wa_text-line .
ENDCASE .
MODIFY p_text FROM wa_text .
ENDLOOP .
ENDFORM . "transfer_Case
******************************************************
*& Form Check_SQL_Key_Word
******************************************************
FORM check_sql_key_word USING p_word
CHANGING p_return TYPE i .
DATA : l_word TYPE string .
l_word = p_word .
PERFORM case_to_upper CHANGING l_word .
CONCATENATE c_separ l_word c_separ
INTO l_word .
p_return = 1.
CHECK c_sql_key_word_01 CS l_word OR
c_sql_key_word_02 CS l_word OR
c_sql_key_word_03 CS l_word OR
c_sql_key_word_04 CS l_word .
p_return = 0.
ENDFORM . "Check_SQL_Key_Word
******************************************************
*& Form Upper_All_Word_SQL Exception Quotes Value
******************************************************
FORM upper_all_word_sql CHANGING p_text TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_return TYPE i .
LOOP AT p_text INTO wa_text .
CHECK NOT wa_text-line CS c_quotes .
PERFORM check_sql_key_word USING wa_text
CHANGING l_return .
CASE l_return.
WHEN 0.
PERFORM case_to_upper CHANGING wa_text-line .
WHEN 1 .
PERFORM case_to_lower CHANGING wa_text-line .
ENDCASE .
MODIFY p_text FROM wa_text .
ENDLOOP .
ENDFORM . "Upper_All_Word_SQL
******************************************************
*& Form Process_funct_word
******************************************************
FORM process_funct_word CHANGING p_text TYPE tt_text.
DATA : wa_text TYPE st_text ,
wa_temp TYPE st_text ,
l_str_01 TYPE string ,
l_str_02 TYPE string ,
l_str_03 TYPE string ,
l_index TYPE i ,
l_temp TYPE st_text-line ,
l_step TYPE i VALUE 0.
*& SUM ( * ) AS TT
*& l_Step 1 2 3 4 5 6
LOOP AT p_text INTO wa_text .
CASE l_step .
WHEN 2 .
IF wa_text-line CS c_left .
CONCATENATE wa_temp-line c_left
INTO wa_temp-line.
DELETE p_text.
l_step = 3 .
ELSE .
PERFORM append_error_message USING 2 wa_text-line.
ENDIF .
WHEN 3 .
* CHECK NOT wa_text-line CS c_left .
IF wa_text-line CS c_right .
SPLIT wa_text-line AT c_right INTO l_str_01 l_str_02 .
IF l_str_01 IS NOT INITIAL .
CONCATENATE wa_temp-line l_str_01 c_right
INTO wa_temp-line
SEPARATED BY space.
ELSE .
CONCATENATE wa_temp-line c_right
INTO wa_temp-line
SEPARATED BY space.
ENDIF .
l_step = 4 .
IF l_str_02 = c_as.
CONCATENATE wa_temp-line l_str_02
INTO wa_temp-line
SEPARATED BY space.
l_step = 5 .
ENDIF .
ELSE .
CONCATENATE wa_temp-line wa_text-line
INTO wa_temp-line
SEPARATED BY space.
l_step = 3 .
ENDIF .
DELETE p_text.
WHEN 4 .
CHECK NOT wa_text-line CS c_right.
IF wa_text-line CS c_as .
CONCATENATE wa_temp-line wa_text-line
INTO wa_temp-line
SEPARATED BY space.
DELETE p_text.
l_step = 5 .
ELSE .
*& Assign A Alias To The Field
INSERT wa_temp INTO p_text INDEX l_index .
l_step = 0 .
ENDIF .
WHEN 5 .
CONCATENATE c_separ wa_text-line c_separ
INTO l_str_01 .
IF c_sql_key_word_01 CS l_str_01 .
** Exception : Need A Field Alias
PERFORM append_error_message USING 3 wa_text-line .
* INSERT wa_temp INTO p_text INDEX l_index .
ELSE .
CONCATENATE wa_temp-line wa_text-line
INTO wa_temp-line
SEPARATED BY space.
MODIFY p_text FROM wa_temp .
ENDIF .
l_step = 0 .
ENDCASE .
l_temp = wa_text-line .
PERFORM case_to_upper CHANGING l_temp.
IF l_temp = c_sum OR l_temp(4) = 'SUM(' OR
l_temp = c_min OR l_temp(4) = 'MIN(' OR
l_temp = c_max OR l_temp(4) = 'MAX(' OR
l_temp = c_avg OR l_temp(4) = 'AVG(' OR
l_temp = c_count OR l_temp(6) = c_count1 .
wa_text-line = l_temp.
l_index = sy-tabix . l_step = 2 .
IF wa_text-line CS c_left.
SPLIT wa_text-line AT c_left INTO l_str_01 l_str_02 .
CONCATENATE l_str_01 c_left INTO wa_temp-line .
IF l_str_02 IS INITIAL.
l_step = 3.
ELSE .
IF l_str_02 CS c_right.
SPLIT l_str_02 AT c_right INTO l_str_01 l_str_03 .
IF l_str_03 = c_as .
CONCATENATE wa_temp-line l_str_01 c_right l_str_03
INTO wa_temp-line
SEPARATED BY space.
l_step = 5 .
ELSE.
CONCATENATE wa_temp-line l_str_01 c_right
INTO wa_temp-line
SEPARATED BY space.
l_step = 4 .
ENDIF .
ELSE .
CONCATENATE wa_temp-line l_str_02
INTO wa_temp-line
SEPARATED BY space.
l_step = 3 .
ENDIF .
ENDIF .
ELSE .
wa_temp-line = l_str_01 .
l_step = 2.
ENDIF .
DELETE p_text .
CONTINUE .
ENDIF .
ENDLOOP .
ENDFORM . "Process_funct_word
******************************************************
*& Form get_all_Element_By_Key
******************************************************
FORM get_all_element_by_key USING p_text TYPE tt_text
p_all TYPE i
p_from TYPE string
CHANGING p_element TYPE tt_text .
DATA : l_key_word TYPE string .
CONCATENATE c_sql_key_word_01
c_sql_key_word_02
c_sql_key_word_03
INTO l_key_word .
PERFORM get_all_element_by_range USING p_text
p_all
p_from
l_key_word
CHANGING p_element.
ENDFORM . "Get_All_Element_By_Range
******************************************************
*& Form get_all_Element_By_range
******************************************************
FORM get_all_element_by_range USING p_text TYPE tt_text
p_all TYPE i
p_from TYPE string
p_to TYPE string
CHANGING p_element TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_flag TYPE i VALUE 1 ,
l_text TYPE string ,
l_to TYPE string .
CLEAR : p_element .
CONCATENATE c_separ p_to c_separ
INTO l_to .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN p_from.
l_flag = 0 .
CONTINUE .
WHEN OTHERS.
CHECK l_flag = 0 .
CONCATENATE c_separ wa_text-line c_separ
INTO l_text .
PERFORM case_to_upper CHANGING l_text .
IF l_to CS l_text .
IF p_all = 1 .
EXIT .
ELSE .
l_flag = 1 .
ENDIF .
ENDIF .
ENDCASE .
CHECK l_flag = 0 .
APPEND wa_text TO p_element .
ENDLOOP .
ENDFORM . "Get_All_Element_By_Range
******************************************************
*& Form Get_All_Fields_Name
******************************************************
FORM get_all_fields_name USING p_text TYPE tt_text
CHANGING p_element TYPE tt_element .
DATA : l_all TYPE i VALUE 1 ,
it_temp TYPE tt_text .
CLEAR : it_temp .
PERFORM get_all_element_by_key USING p_text l_all
c_select
CHANGING it_temp .
PERFORM get_element_alias USING it_temp
CHANGING p_element .
ENDFORM . "Get_All_Fields_Name
******************************************************
*& Form Get_All_Tables_Name
******************************************************
FORM get_all_tables_name USING p_text TYPE tt_text
CHANGING p_element TYPE tt_element .
DATA : l_all TYPE i VALUE 1 ,
it_temp TYPE tt_text ,
it_element TYPE tt_element,
wa_element TYPE st_element.
CLEAR : p_element .
l_all = 1 .
PERFORM get_all_element_by_key USING p_text l_all
c_from
CHANGING it_temp .
PERFORM get_element_alias USING it_temp
CHANGING it_element .
APPEND LINES OF it_element TO p_element .
l_all = 0 .
PERFORM get_all_element_by_key USING p_text l_all
c_join
CHANGING it_temp .
PERFORM get_element_alias USING it_temp
CHANGING it_element .
APPEND LINES OF it_element TO p_element .
*& Process Table Alias Name
LOOP AT p_element INTO wa_element .
IF wa_element-alias IS INITIAL .
wa_element-alias = wa_element-name .
ENDIF .
CONCATENATE wa_element-name c_as
wa_element-alias
INTO wa_element-display
SEPARATED BY space .
MODIFY p_element FROM wa_element .
ENDLOOP .
ENDFORM. "Get_All_Tables_Name
******************************************************
*& Form Get_Element_Alias
******************************************************
FORM get_element_alias USING p_text TYPE tt_text
CHANGING p_element TYPE tt_element .
DATA : it_temp TYPE tt_text,
wa_text TYPE st_text,
wa_element TYPE st_element,
l_flag TYPE i VALUE 1 ,
l_text TYPE string .
it_temp = p_text .
CLEAR : p_element,wa_element.
DELETE it_temp WHERE LINE = c_single OR
LINE = c_distinct .
LOOP AT it_temp INTO wa_text .
l_text = wa_text-line .
PERFORM case_to_upper CHANGING l_text .
CASE l_text .
WHEN c_as.
l_flag = 0 .
WHEN OTHERS .
IF l_flag = 0 .
IF wa_text-line CS c_right AND
wa_text-line CS c_left .
** Excpetion Nedd A Alias
PERFORM append_error_message USING 3 wa_text-line .
ELSE.
wa_element-alias = wa_text-line .
APPEND wa_element TO p_element .
ENDIF .
CLEAR : wa_element .
l_flag = 1 .
ELSE .
IF wa_element IS NOT INITIAL .
APPEND wa_element TO p_element .
ENDIF .
CLEAR : wa_element .
wa_element-name = wa_text-line .
ENDIF .
ENDCASE .
AT LAST.
IF l_flag = 0.
** Excpetion Nedd A Alias
PERFORM append_error_message USING 3 wa_text-line .
ENDIF .
IF wa_element IS NOT INITIAL .
APPEND wa_element TO p_element .
ENDIF .
ENDAT .
ENDLOOP .
ENDFORM . "Get_Element_Alias
******************************************************
*& Form Get_All_Fields_Infor
******************************************************
FORM get_all_fields_infor USING p_table TYPE tt_element
CHANGING p_fields TYPE tt_element .
*& Process Normal Fields
PERFORM get_normal_fields_infor USING p_table
CHANGING p_fields .
*& Get Aggregate Function Fields
PERFORM get_aggra_fields_infor USING p_table
CHANGING p_fields .
*& Process Symbol * For All Fields
PERFORM get_symbol_fields_infor USING p_table
CHANGING p_fields .
PERFORM process_duplic_alias CHANGING p_fields .
ENDFORM . "Get_All_Fields_Infor
******************************************************
*& Form Get_Normal_Fields_Infor
******************************************************
FORM get_normal_fields_infor USING p_tables TYPE tt_element
CHANGING p_fields TYPE tt_element .
DATA : wa_table TYPE st_element ,
wa_field TYPE st_element ,
it_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
l_table_alias TYPE dfies-fieldname,
l_field_alias TYPE dfies-fieldname,
l_field_name TYPE dfies-fieldname,
l_field_name01 TYPE dfies-fieldname.
LOOP AT p_tables INTO wa_table .
CLEAR : it_fieldcat, it_fieldcat[] .
PERFORM get_table_infor USING wa_table-name
CHANGING it_fieldcat[] .
*& Check Whether Table Exist
IF it_fieldcat[] IS INITIAL .
PERFORM append_error_message USING 5 wa_table-name .
CONTINUE.
ENDIF .
LOOP AT p_fields INTO wa_field WHERE label IS INITIAL.
*& Skip Symbol *
CHECK NOT wa_field-name CS c_all_fields .
IF wa_field-name CS c_ss .
SPLIT wa_field-name AT c_ss INTO l_table_alias l_field_name .
CHECK l_table_alias = wa_table-alias .
ELSE .
l_field_name = wa_field-name .
ENDIF .
l_field_name01 = l_field_name .
PERFORM case_to_upper CHANGING l_field_name01 .
READ TABLE it_fieldcat WITH KEY fieldname = l_field_name01 .
CHECK sy-subrc = 0 .
wa_field-name = l_field_name .
wa_field-source = wa_table-name .
wa_field-link = wa_table-alias .
wa_field-label = it_fieldcat-seltext_l .
IF wa_field-alias IS INITIAL.
wa_field-alias = wa_field-name .
ENDIF .
CONCATENATE wa_field-link c_ss wa_field-name
INTO wa_field-display .
MODIFY p_fields FROM wa_field .
ENDLOOP .
ENDLOOP .
ENDFORM . "Get_Normal_Fields_infor
******************************************************
*& Form Get_Aggra_Fields_Infor
******************************************************
FORM get_aggra_fields_infor USING p_tables TYPE tt_element
CHANGING p_fields TYPE tt_element .
DATA : wa_field TYPE st_element ,
wa_temp TYPE st_element ,
it_text TYPE tt_text ,
wa_text TYPE st_text ,
l_step TYPE i VALUE 0 ,
l_index TYPE i ,
l_str_01 TYPE string ,
l_str_02 TYPE string .
LOOP AT p_fields INTO wa_field WHERE label IS INITIAL.
IF wa_field-name CS c_sum OR wa_field-name CS c_min OR
wa_field-name CS c_max OR wa_field-name CS c_avg OR
wa_field-name CS c_count .
CLEAR : it_text .
SPLIT wa_field-name AT space INTO TABLE it_text.
DELETE it_text WHERE LINE = space .
*& Get Field Name
READ TABLE it_text INTO wa_text WITH KEY line = c_right .
l_index = sy-tabix - 1 .
CLEAR : wa_text .
READ TABLE it_text INTO wa_text INDEX l_index .
IF wa_text = c_all_fields.
wa_field-label = c_funct .
ELSE.
PERFORM get_field_infor_by_name USING p_tables
wa_text-line
CHANGING wa_field .
CONCATENATE wa_field-link c_ss wa_field-name
INTO wa_text .
ENDIF .
MODIFY it_text FROM wa_text INDEX l_index .
*& Get Field Alias
CLEAR : wa_text .
READ TABLE it_text INTO wa_text WITH KEY line = c_as .
IF sy-subrc = 0 .
l_index = sy-tabix + 1 .
CLEAR : wa_text .
READ TABLE it_text INTO wa_text INDEX l_index .
IF sy-subrc = 0 .
wa_field-alias = wa_text-line .
ELSE .
wa_field-alias = c_funct.
ENDIF .
ELSE .
wa_field-alias = c_funct.
ENDIF .
*& Set Display
CLEAR : wa_field-display .
LOOP AT it_text INTO wa_text .
IF wa_text-line = c_as .
EXIT .
ENDIF .
CONCATENATE wa_field-display wa_text-line
INTO wa_field-display
SEPARATED BY space .
ENDLOOP .
CONDENSE : wa_field-display .
MODIFY p_fields FROM wa_field .
ENDIF .
ENDLOOP .
ENDFORM. "Get_Aggra_Fields_infor
******************************************************
*& Form Get_Symbol_Fields_Infor
******************************************************
FORM get_symbol_fields_infor USING p_tables TYPE tt_element
CHANGING p_fields TYPE tt_element .
DATA : wa_table TYPE st_element ,
wa_field TYPE st_element ,
it_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
l_table_alias TYPE dfies-fieldname,
l_field_name TYPE dfies-fieldname,
it_temp TYPE tt_element ,
wa_temp TYPE st_element .
LOOP AT p_fields INTO wa_field WHERE label IS INITIAL.
CLEAR : l_table_alias, l_field_name .
IF wa_field-name CS c_ss .
SPLIT wa_field-name AT c_ss INTO l_table_alias l_field_name .
ELSE .
l_field_name = wa_field-name .
ENDIF .
CHECK l_field_name = c_all_fields .
CLEAR : it_temp[] .
LOOP AT p_tables INTO wa_table .
CLEAR : it_fieldcat, it_fieldcat[] .
CHECK l_table_alias = wa_table-alias OR
l_table_alias IS INITIAL .
PERFORM get_table_infor USING wa_table-name
CHANGING it_fieldcat[] .
LOOP AT it_fieldcat .
CLEAR : wa_temp .
wa_temp-name = it_fieldcat-fieldname .
wa_temp-alias = it_fieldcat-fieldname .
wa_temp-source = wa_table-name .
wa_temp-link = wa_table-alias .
wa_temp-label = it_fieldcat-seltext_l .
CONCATENATE wa_temp-link c_ss wa_temp-name
INTO wa_temp-display .
APPEND wa_temp TO it_temp .
ENDLOOP .
ENDLOOP .
*& Deletle The Sybmol *
DELETE p_fields INDEX sy-tabix .
INSERT LINES OF it_temp INTO p_fields INDEX sy-tabix .
ENDLOOP .
ENDFORM . "Get_Symbol_Fields_infor
******************************************************
*& Form Process_Duplic_Alias
******************************************************
FORM process_duplic_alias CHANGING p_fields TYPE tt_element.
DATA : wa_prior TYPE st_element ,
wa_field TYPE st_element ,
l_i TYPE n LENGTH 2 .
LOOP AT p_fields INTO wa_field .
wa_field-index = sy-tabix .
MODIFY p_fields FROM wa_field .
IF wa_field-display IS INITIAL .
PERFORM append_error_message USING 6 wa_field-name .
ENDIF .
ENDLOOP .
SORT p_fields BY alias index .
LOOP AT p_fields INTO wa_field.
IF wa_prior-alias = wa_field-alias .
ADD 1 TO l_i .
CONCATENATE wa_field-alias '_' l_i
INTO wa_field-alias .
ELSE .
l_i = 1 .
wa_prior = wa_field .
ENDIF .
CONCATENATE wa_field-display c_as wa_field-alias
INTO wa_field-display
SEPARATED BY space .
MODIFY p_fields FROM wa_field .
ENDLOOP .
SORT p_fields BY index .
ENDFORM . "Process_Duplic_Alias
******************************************************
*& Form Get_Field_Infor_By_Name
******************************************************
FORM get_field_infor_by_name USING p_tables TYPE tt_element
p_name
CHANGING p_field TYPE st_element.
DATA: wa_table TYPE st_element ,
l_table_alias TYPE dfies-fieldname,
l_field_name TYPE dfies-fieldname.
IF p_name CS c_ss .
SPLIT p_name AT c_ss INTO l_table_alias l_field_name .
READ TABLE p_tables INTO wa_table WITH KEY alias = l_table_alias .
CHECK sy-subrc = 0 .
PERFORM get_field_infor_in_table USING wa_table-name
l_field_name
CHANGING p_field .
p_field-link = wa_table-alias .
p_field-name = l_field_name .
ELSE .
LOOP AT p_tables INTO wa_table .
PERFORM get_field_infor_in_table USING wa_table-name
p_name
CHANGING p_field .
IF p_field-alias IS NOT INITIAL .
p_field-link = wa_table-alias .
p_field-name = p_name .
EXIT .
ENDIF .
ENDLOOP .
ENDIF .
ENDFORM . "Get_Field_Infor_By_Name
******************************************************
*& Form Get_Field_Infor_In_Table
******************************************************
FORM get_field_infor_in_table USING p_table_name
p_name
CHANGING p_field TYPE st_element.
DATA : l_field_name01 TYPE dfies-fieldname,
it_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE .
CLEAR : it_fieldcat, it_fieldcat[] .
PERFORM get_table_infor USING p_table_name
CHANGING it_fieldcat[] .
l_field_name01 = p_name.
PERFORM case_to_upper CHANGING l_field_name01 .
READ TABLE it_fieldcat WITH KEY fieldname = l_field_name01 .
CHECK sy-subrc = 0 .
p_field-name = p_name .
p_field-source = p_table_name .
p_field-label = it_fieldcat-seltext_l .
IF p_field-alias IS INITIAL.
p_field-alias = p_field-name .
ENDIF .
ENDFORM . "get_field_infor_in_table
******************************************************
*& Form Get_Table_Infor
******************************************************
FORM get_table_infor USING p_table
CHANGING p_fieldcat TYPE slis_t_fieldcat_alv .
DATA : l_table TYPE dd02l-tabname .
l_table = p_table .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = l_table
i_client_never_display = 'X'
CHANGING
ct_fieldcat = p_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
ENDFORM . "Get_Table_Infor
******************************************************
*& Form Get_asc_code
******************************************************
FORM get_asc_code USING p_hex
CHANGING p_char .
DATA: l_string(1024).
CALL FUNCTION 'STPU1_HEX_TO_CHAR'
EXPORTING
hex_string = p_hex
IMPORTING
char_string = l_string.
p_char = l_string.
ENDFORM. " get_asc_code
******************************************************
*& Form Get_Fields_And_Catalog
******************************************************
FORM get_sql_fields USING p_text TYPE tt_text
p_tables TYPE tt_element
CHANGING p_fields TYPE tt_element.
PERFORM get_all_fields_name USING p_text
CHANGING p_fields.
PERFORM get_all_fields_infor USING p_tables
CHANGING p_fields.
ENDFORM . "Get_Fields_And_Catalog
******************************************************
*& Form Get_On_Tables
******************************************************
FORM get_on_tables USING p_text TYPE tt_text
p_tables TYPE tt_element
CHANGING p_on TYPE tt_text.
DATA : wa_text TYPE st_text ,
l_str TYPE string ,
wa_table TYPE st_element,
l_step TYPE i VALUE 0,
l_index TYPE i VALUE 0,
l_join TYPE i VALUE 0,
l_on TYPE i VALUE 0,
l_mod TYPE i VALUE 0.
CLEAR: p_on .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_from .
l_step = 1 . CONTINUE .
WHEN c_inner .
l_join = 0 . CHECK l_on > 0 .
l_on = 0 . APPEND l_str TO p_on .
l_step = 0 .
WHEN c_left_j .
l_join = 1 . CHECK l_on > 0 .
l_on = 0 . APPEND l_str TO p_on .
l_step = 0 .
WHEN c_join.
l_step = 2 . CHECK l_on > 0 .
l_on = 0 . APPEND l_str TO p_on .
WHEN c_on .
l_step = 3 . CHECK l_on > 0 .
l_on = 0 . APPEND l_str TO p_on .
WHEN c_where OR c_order OR c_group OR c_have.
CHECK l_on > 0 . l_on = 0 .
APPEND l_str TO p_on .
EXIT .
ENDCASE .
CASE l_step .
WHEN 1 .
ADD 1 TO l_index .
READ TABLE p_tables INTO wa_table INDEX l_index .
CONCATENATE c_from wa_table-display
INTO l_str
SEPARATED BY space .
SHIFT l_str BY 2 PLACES RIGHT.
APPEND l_str TO p_on .
l_step = 0 .
WHEN 2 .
ADD 1 TO l_index .
READ TABLE p_tables INTO wa_table INDEX l_index .
IF l_join = 0 .
CONCATENATE c_inner c_join wa_table-display
INTO l_str
SEPARATED BY space .
SHIFT l_str BY 1 PLACES RIGHT.
APPEND l_str TO p_on .
ELSE .
CONCATENATE c_left_j c_join wa_table-display
INTO l_str
SEPARATED BY space .
SHIFT l_str BY 2 PLACES RIGHT.
APPEND l_str TO p_on .
ENDIF .
l_step = 0 .
l_join = 0 .
WHEN 3 .
ADD 1 TO l_on .
IF l_on = 1 .
CONCATENATE c_on wa_text-line
INTO l_str
SEPARATED BY space .
SHIFT l_str BY 4 PLACES RIGHT.
ELSE .
l_mod = l_on MOD 8 .
IF l_mod = 0 .
APPEND l_str TO p_on .
CLEAR : l_str .
l_str = wa_text-line .
CASE wa_text-line .
WHEN c_or .
SHIFT l_str BY 4 PLACES RIGHT.
WHEN c_and.
SHIFT l_str BY 3 PLACES RIGHT.
WHEN OTHERS.
SHIFT l_str BY 7 PLACES RIGHT.
ENDCASE .
ELSE .
CONCATENATE l_str wa_text-line
INTO l_str
SEPARATED BY space .
ENDIF .
ENDIF .
WHEN 4 .
ENDCASE .
AT LAST .
IF l_on > 0 .
l_on = 0 .
APPEND l_str TO p_on .
l_step = 0 .
ENDIF .
ENDAT .
ENDLOOP .
ENDFORM . "Get_On_Tables
******************************************************
*& Form Get_Where
******************************************************
FORM get_where USING p_text TYPE tt_text
CHANGING p_where TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_where TYPE i VALUE 0 ,
l_flag TYPE i VALUE 1 ,
l_mod TYPE i VALUE 0 ,
l_str TYPE string .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_where .
l_flag = 0 .
l_where = -1 .
WHEN c_group OR c_have OR c_order .
l_flag = 1 .
ENDCASE .
IF l_flag = 0 .
ADD 1 TO l_where .
IF l_where = 0 .
l_str = wa_text-line .
SHIFT l_str BY 1 PLACES RIGHT.
ELSE .
l_mod = l_where MOD 8 .
IF l_mod = 0 .
APPEND l_str TO p_where .
CLEAR : l_str .
l_str = wa_text-line .
CASE wa_text-line .
WHEN c_or .
SHIFT l_str BY 4 PLACES RIGHT.
WHEN c_and.
SHIFT l_str BY 3 PLACES RIGHT.
WHEN OTHERS.
SHIFT l_str BY 7 PLACES RIGHT.
ENDCASE .
ELSE .
CONCATENATE l_str wa_text-line
INTO l_str
SEPARATED BY space .
ENDIF .
ENDIF .
ENDIF .
AT LAST.
CHECK l_str IS NOT INITIAL .
APPEND l_str TO p_where .
ENDAT .
ENDLOOP .
ENDFORM . "Get_Where
******************************************************
*& Form Get_Group
******************************************************
FORM get_group USING p_text TYPE tt_text
CHANGING p_group TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_group TYPE i VALUE 0 ,
l_flag TYPE i VALUE 1 ,
l_mod TYPE i VALUE 0 ,
l_str TYPE string .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_group .
l_flag = 0 .
l_group = 0 .
WHEN c_have OR c_order .
l_flag = 1 .
ENDCASE .
IF l_flag = 0 .
ADD 1 TO l_group .
IF l_group = 1 .
l_str = wa_text-line .
SHIFT l_str BY 1 PLACES RIGHT.
ELSE .
l_mod = l_group MOD 8 .
IF l_mod = 0 .
APPEND l_str TO p_group .
CLEAR : l_str .
l_str = wa_text-line .
SHIFT l_str BY 7 PLACES RIGHT.
ELSE .
CONCATENATE l_str wa_text-line
INTO l_str
SEPARATED BY space .
ENDIF .
ENDIF .
ENDIF .
AT LAST.
CHECK l_str IS NOT INITIAL .
APPEND l_str TO p_group .
ENDAT .
ENDLOOP .
ENDFORM . "Get_Group
******************************************************
*& Form Get_Have
******************************************************
FORM get_have USING p_text TYPE tt_text
CHANGING p_have TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_have TYPE i VALUE 0 ,
l_flag TYPE i VALUE 1 ,
l_mod TYPE i VALUE 0 ,
l_str TYPE string .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_have .
l_flag = 0 .
l_have = 0 .
WHEN c_order .
l_flag = 1 .
ENDCASE .
IF l_flag = 0 .
ADD 1 TO l_have .
IF l_have = 1 .
l_str = wa_text-line .
ELSE .
l_mod = l_have MOD 8 .
IF l_mod = 0 .
APPEND l_str TO p_have .
CLEAR : l_str .
l_str = wa_text-line .
SHIFT l_str BY 7 PLACES RIGHT.
ELSE .
CONCATENATE l_str wa_text-line
INTO l_str
SEPARATED BY space .
ENDIF .
ENDIF .
ENDIF .
AT LAST.
CHECK l_str IS NOT INITIAL .
APPEND l_str TO p_have .
ENDAT .
ENDLOOP .
ENDFORM . "Get_Have
******************************************************
*& Form Get_Order
******************************************************
FORM get_order USING p_text TYPE tt_text
CHANGING p_order TYPE tt_text .
DATA : wa_text TYPE st_text ,
l_order TYPE i VALUE 0 ,
l_flag TYPE i VALUE 1 ,
l_mod TYPE i VALUE 0 ,
l_str TYPE string .
LOOP AT p_text INTO wa_text .
CASE wa_text-line .
WHEN c_order .
l_flag = 0 .
l_order = 0 .
WHEN c_select OR c_from OR c_where OR
c_have OR c_inner OR c_join .
CHECK l_flag = 0 .
PERFORM append_error_message USING 9 wa_text-line.
EXIT .
ENDCASE .
IF l_flag = 0 .
ADD 1 TO l_order .
IF l_order = 1 .
l_str = wa_text-line .
SHIFT l_str BY 1 PLACES RIGHT.
ELSE .
l_mod = l_order MOD 8 .
IF l_mod = 0 .
APPEND l_str TO p_order .
CLEAR : l_str .
l_str = wa_text-line .
SHIFT l_str BY 7 PLACES RIGHT.
ELSE .
CONCATENATE l_str wa_text-line
INTO l_str
SEPARATED BY space .
ENDIF .
ENDIF .
ENDIF .
AT LAST.
CHECK l_str IS NOT INITIAL .
APPEND l_str TO p_order .
ENDAT .
ENDLOOP .
ENDFORM . "Get_Order
******************************************************
*& Form perpare_SQL_Element
******************************************************
FORM perpare_sql_element CHANGING p_text TYPE tt_text
p_fields TYPE tt_element
p_tables TYPE tt_element
p_on TYPE tt_text
p_where TYPE tt_text
p_group TYPE tt_text
p_have TYPE tt_text
p_order TYPE tt_text .
PERFORM move_comments_in_sql CHANGING p_text .
IF p_text IS INITIAL .
PERFORM append_error_message USING 8 '' .
EXIT .
ENDIF .
PERFORM separate_from_word CHANGING p_text .
PERFORM upper_all_word_sql CHANGING p_text .
PERFORM process_funct_word CHANGING p_text .
PERFORM get_all_tables_name USING p_text
CHANGING p_tables.
IF p_tables IS INITIAL .
PERFORM append_error_message USING 10 '' .
EXIT .
ENDIF .
PERFORM get_sql_fields USING p_text
p_tables
CHANGING p_fields.
IF p_fields IS INITIAL .
PERFORM append_error_message USING 11 '' .
EXIT .
ENDIF .
PERFORM get_on_tables USING p_text
p_tables
CHANGING p_on .
PERFORM get_where USING p_text
CHANGING p_where .
PERFORM get_group USING p_text
CHANGING p_group .
PERFORM get_have USING p_text
CHANGING p_have .
PERFORM get_order USING p_text
CHANGING p_order .
ENDFORM . "Perpare_SQL_Element
******************************************************
*& Form Get_Ref_Table
******************************************************
FORM get_ref_table USING p_table_alias TYPE ddobjname
p_tables TYPE tt_element
CHANGING p_table_name TYPE ddobjname .
DATA : wa_element TYPE st_element .
READ TABLE p_tables INTO wa_element
WITH KEY alias = p_table_alias .
p_table_name = wa_element-name .
ENDFORM . "Get_Ref_Table
******************************************************
*& Form Check_SQL_With_Sub_Query
******************************************************
FORM check_sql_with_sub_query USING p_text TYPE tt_text
CHANGING p_return TYPE i.
DATA : wa_text TYPE st_text .
LOOP AT p_text INTO wa_text .
CHECK wa_text-line CP c_from .
ADD 1 TO p_return .
ENDLOOP.
ENDFORM . "Check_SQL_With_Sub_Query
******************************************************
*& Form Execute_SQL
******************************************************
FORM execute_sql USING p_text TYPE tt_text
CHANGING l_number TYPE i .
DATA: it_fields TYPE tt_element ,
it_tables TYPE tt_element ,
it_select TYPE tt_text ,
it_on TYPE tt_text ,
it_where TYPE tt_text ,
it_group TYPE tt_text ,
it_have TYPE tt_text ,
it_order TYPE tt_text ,
it_fieldcat TYPE lvc_t_fcat ,
it_temp TYPE tt_text ,
l_type TYPE string ,
l_act TYPE i VALUE 1 ,
l_return TYPE i VALUE 0 .
it_temp[] = p_text[] .
PERFORM format_sql_adapter USING l_act
CHANGING it_temp l_type
it_fields it_tables
it_select it_on
it_where it_group
it_have it_order
l_return .
* PERFORM check_sql_with_sub_query USING it_temp[]
* CHANGING l_return .
IF <table> IS ASSIGNED .
CLEAR : <table> .
UNASSIGN <table> .
ENDIF .
IF g_exception[] IS INITIAL .
CASE l_return.
*& SQL With Error
WHEN 0 .
*& Without Sub Query
WHEN 1 .
PERFORM get_data_in_dynamic_sql USING it_temp[] l_type
it_fields it_select
it_on it_where
it_group it_have
it_order .
*& With Sub Query
WHEN OTHERS .
PERFORM get_data_in_dynamic_prog USING it_temp[]
l_type
it_fields.
ENDCASE .
ENDIF .
IF g_exception IS INITIAL .
*& Show Data
PERFORM prepare_alv_field_cat USING it_fields
CHANGING it_fieldcat .
l_number = sy-dbcnt .
PERFORM show_data_in_alv USING g_grid <table>
it_fieldcat[] .
ELSE .
*& Show Exception
PERFORM prepare_alv_error_field_cat CHANGING it_fieldcat .
PERFORM show_data_in_alv USING g_grid g_exception
it_fieldcat[] .
ENDIF .
g_postion = 50 .
PERFORM set_splitter_postion USING g_splitter
g_postion .
ENDFORM . "Execute_SQL
******************************************************
*& Form Get_Stucture_Define
******************************************************
FORM get_stucture_define USING p_fields TYPE tt_element
CHANGING p_text TYPE tt_text .
DATA : wa_text TYPE st_text ,
wa_field TYPE st_element .
CLEAR : p_text[] .
wa_text = 'TYPES : BEGIN OF ST_DATA,'.
APPEND wa_text TO p_text .
LOOP AT p_fields INTO wa_field .
CLEAR : wa_text .
IF wa_field-display(6) = c_count1 .
CONCATENATE wa_field-alias 'TYPE i ,'
INTO wa_text-line
SEPARATED BY space .
ELSE .
CONCATENATE wa_field-source '-' wa_field-name
INTO wa_text-line .
CONCATENATE wa_field-alias 'TYPE' wa_text-line ','
INTO wa_text-line
SEPARATED BY space .
ENDIF .
SHIFT wa_text-line BY 10 PLACES RIGHT .
APPEND wa_text TO p_text .
ENDLOOP .
wa_text = ' END OF ST_DATA.'.
APPEND wa_text TO p_text .
ENDFORM. "Get_Stucture_Define
******************************************************
*& Form format_sql_adapter
******************************************************
FORM format_sql_adapter USING p_act TYPE i
CHANGING p_text TYPE tt_text
p_type TYPE string
p_fields TYPE tt_element
p_tables TYPE tt_element
p_select TYPE tt_text
p_on TYPE tt_text
p_where TYPE tt_text
p_group TYPE tt_text
p_have TYPE tt_text
p_order TYPE tt_text
p_return TYPE i.
DATA : it_temp TYPE tt_text .
PERFORM perpare_sql_element CHANGING p_text p_fields
p_tables p_on
p_where p_group
p_have p_order.
PERFORM check_sql_with_sub_query USING p_text
CHANGING p_return .
CASE p_return.
*& SQL With Error
WHEN 0 .
*& Without Sub Query
WHEN 1 .
*& Format Select Fields
PERFORM format_fields USING p_fields
p_act
CHANGING p_select
p_text
p_type .
*& Format Table Name
PERFORM format_tables USING p_on
CHANGING p_text .
*& Format Where Condition
PERFORM format_where USING p_where
CHANGING p_text .
*& Format Group By
PERFORM format_group USING p_group
CHANGING p_text .
*& Format Having
PERFORM format_have USING p_have
CHANGING p_text .
*& Format Order By
PERFORM format_order USING p_order
CHANGING p_text .
*& Trnasfer Case
PERFORM transfer_case USING g_case
CHANGING p_text .
WHEN OTHERS .
*& Format Select Fields
it_temp[] = p_text .
PERFORM format_fields USING p_fields
p_act
CHANGING p_select
it_temp
p_type .
IF p_act <> 1 .
PERFORM append_error_message USING 7 ''.
ENDIF .
ENDCASE .
ENDFORM . "format_sql
******************************************************
*& Form format_sql
******************************************************
FORM format_sql CHANGING p_text TYPE tt_text .
DATA: it_fields TYPE tt_element ,
it_tables TYPE tt_element ,
it_fieldcat TYPE lvc_t_fcat ,
it_select TYPE tt_text ,
it_on TYPE tt_text ,
it_where TYPE tt_text ,
it_group TYPE tt_text ,
it_have TYPE tt_text ,
it_order TYPE tt_text ,
l_act TYPE i VALUE 0 ,
l_type TYPE string ,
l_return TYPE i .
PERFORM format_sql_adapter USING l_act
CHANGING p_text l_type
it_fields it_tables
it_select it_on
it_where it_group
it_have it_order
l_return.
*& Show Exception
IF g_exception IS INITIAL AND p_text[] IS NOT INITIAL.
PERFORM load_text_from_table USING g_editor
CHANGING p_text .
g_postion = 100 .
ELSE .
PERFORM prepare_alv_error_field_cat CHANGING it_fieldcat .
PERFORM show_data_in_alv USING g_grid
g_exception
it_fieldcat[] .
g_postion = 50 .
ENDIF .
PERFORM set_splitter_postion USING g_splitter
g_postion .
ENDFORM . "format_sql
******************************************************
*& Form bud_sturcture
******************************************************
FORM bud_sturcture USING p_text TYPE tt_text
CHANGING p_temp TYPE tt_text .
DATA: it_fields TYPE tt_element ,
it_tables TYPE tt_element ,
it_fieldcat TYPE lvc_t_fcat ,
it_select TYPE tt_text ,
it_on TYPE tt_text ,
it_where TYPE tt_text ,
it_group TYPE tt_text ,
it_have TYPE tt_text ,
it_order TYPE tt_text ,
l_act TYPE i VALUE 0 ,
l_type TYPE string ,
l_temp TYPE tt_text ,
l_return TYPE i .
CLEAR : p_temp .
l_temp = p_text .
PERFORM format_sql_adapter USING l_act
CHANGING l_temp l_type
it_fields it_tables
it_select it_on
it_where it_group
it_have it_order
l_return.
*& Show Exception
IF g_exception IS INITIAL AND p_text[] IS NOT INITIAL.
PERFORM get_stucture_define USING it_fields
CHANGING p_temp .
ELSE .
PERFORM prepare_alv_error_field_cat CHANGING it_fieldcat .
PERFORM show_data_in_alv USING g_grid
g_exception
it_fieldcat[] .
g_postion = 50 .
PERFORM set_splitter_postion USING g_splitter
g_postion .
ENDIF .
g_postion = 70 .
PERFORM set_splitter_postion USING g_splitter1
g_postion .
ENDFORM . "bud_sturcture
子程序7:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF05
*&---------------------------------------------------------------------*
******************************************************
*& Form Get_Data_In_Dynamic_Program
******************************************************
FORM get_data_in_dynamic_prog USING p_sql TYPE tt_text
p_type TYPE string
p_element TYPE tt_element.
DATA : it_code TYPE tt_code ,
prog TYPE c LENGTH 8 ,
msg TYPE c LENGTH 120,
lin TYPE c LENGTH 3 ,
wrd TYPE c LENGTH 10 ,
off TYPE c LENGTH 3 ,
l_msg TYPE string ,
p_stcurt TYPE REF TO data .
PERFORM create_dynamic_program_code USING p_sql
p_element
p_type
CHANGING it_code .
GENERATE SUBROUTINE POOL it_code NAME prog
MESSAGE msg
LINE lin
WORD wrd
OFFSET off.
IF sy-subrc <> 0.
l_msg = msg .
PERFORM append_error_message USING 4 l_msg .
PERFORM download_table_to_local TABLES it_code[]
USING 'c:\11.txt'.
ELSE .
*& Get The Dynamic Structure
PERFORM get_structure IN PROGRAM (prog) CHANGING p_stcurt .
ASSIGN p_stcurt->* TO <table> .
*& Get Data With SQL
PERFORM get_data IN PROGRAM (prog) USING <table> .
ENDIF .
ENDFORM . "Get_Data_In_Dynamic_Prog
******************************************************
*& Form Create_Dynamic_Program_Code
******************************************************
FORM create_dynamic_program_code USING p_sql TYPE tt_text
p_element TYPE tt_element
p_type TYPE string
CHANGING p_code TYPE tt_code .
DATA : it_temp TYPE tt_code .
CLEAR : p_code[] .
*& Create The Start Of The Dynamic Program
PERFORM define_program_01 USING p_element
CHANGING it_temp .
APPEND LINES OF it_temp TO p_code .
*& Create The First Function
PERFORM define_program_02 CHANGING it_temp .
APPEND LINES OF it_temp TO p_code .
*& Create The Get Data Function
PERFORM define_program_03 USING p_sql
p_type
CHANGING it_temp .
APPEND LINES OF it_temp TO p_code .
ENDFORM . "Create_Dynamic_Program
******************************************************
*& Form Define_Program_01
******************************************************
FORM define_program_01 USING p_element TYPE tt_element
CHANGING p_code TYPE tt_code .
DATA : wa_element TYPE st_element ,
wa_line TYPE LINE OF tt_code,
l_text TYPE string .
CLEAR : p_code[] .
APPEND 'PROGRAM SUBPOOL. ' TO p_code .
APPEND 'TYPES:Begin OF ST_DATA, ' TO p_code .
LOOP AT p_element INTO wa_element .
IF wa_element-display CS c_count .
CONCATENATE wa_element-alias ' ' 'TYPE' ' '
'i,'
INTO wa_line
SEPARATED BY space .
ELSE .
CONCATENATE wa_element-source '-' wa_element-name
INTO l_text .
CONCATENATE wa_element-alias ' ' 'LIKE' ' '
l_text ','
INTO wa_line
SEPARATED BY space .
ENDIF .
APPEND wa_line TO p_code .
ENDLOOP .
APPEND ' End OF ST_DATA. ' TO p_code .
APPEND 'TYPES: TT_DATA TYPE STANDARD TABLE OF ST_DATA. ' TO p_code .
ENDFORM . "Define_Program_01
******************************************************
*& Form Define_Program_02
******************************************************
FORM define_program_02 CHANGING p_code TYPE tt_code .
DATA : wa_element TYPE st_element ,
wa_line TYPE LINE OF tt_code .
CLEAR : p_code[] .
APPEND 'FORM get_structure Changing p_struct TYPE REF TO data . ' TO p_code .
APPEND ' ' TO p_code .
APPEND ' CREATE DATA p_struct TYPE TT_DATA. ' TO p_code .
APPEND ' ' TO p_code .
APPEND 'ENDFORM . ' TO p_code .
ENDFORM . "Define_Program_02
******************************************************
*& Form Define_Program_03
******************************************************
FORM define_program_03 USING p_sql TYPE tt_text
p_type TYPE string
CHANGING p_code TYPE tt_code .
DATA : wa_text TYPE st_text ,
l_first TYPE i VALUE 0 .
CLEAR : p_code[] .
APPEND 'FORM GET_DATA USING P_DATA TYPE ANY TABLE . ' TO p_code .
APPEND ' DATA : IT_DATA TYPE TT_DATA . ' TO p_code .
APPEND ' DATA : WA_DATA TYPE ST_DATA . ' TO p_code .
APPEND ' ' TO p_code .
LOOP AT p_sql INTO wa_text .
IF wa_text = 'FROM' AND l_first = 0 .
IF p_type = c_single .
wa_text = 'INTO WA_DATA FROM' .
ELSE .
wa_text = 'INTO CORRESPONDING FIELDS OF TABLE IT_DATA FROM' .
ENDIF .
l_first = 1 .
ENDIF .
SHIFT wa_text-line BY 1 PLACES RIGHT .
APPEND wa_text TO p_code .
ENDLOOP .
APPEND ' . ' TO p_code .
IF p_type = c_single .
APPEND ' APPEND WA_DATA TO IT_DATA. ' TO p_code .
ENDIF .
APPEND ' P_DATA = IT_DATA[]. ' TO p_code .
APPEND ' ' TO p_code .
APPEND 'ENDFORM. ' TO p_code .
ENDFORM . "Define_Program_03
子程序8:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLF06
*&---------------------------------------------------------------------*
******************************************************
*& Form Get_Data_In_Dynamic_SQL
******************************************************
FORM get_data_in_dynamic_sql USING p_text TYPE tt_text
p_type TYPE string
p_fields TYPE tt_element
p_select TYPE tt_text
p_on TYPE tt_text
p_where TYPE tt_text
p_group TYPE tt_text
p_have TYPE tt_text
p_order TYPE tt_text .
DATA : p_all TYPE i VALUE 1 ,
wa_text TYPE st_text ,
p_table TYPE REF TO data,
p_line TYPE REF TO data,
l_to TYPE string ,
l_length TYPE i .
*& Get Field Statement
l_length = 6 .
PERFORM set_blank USING l_length
CHANGING p_select .
*& Get Table Statement
l_length = 6 .
PERFORM set_blank USING l_length
CHANGING p_on .
*& Get Where Statement
l_length = 6 .
PERFORM set_blank USING l_length
CHANGING p_where .
*& Get Group Statement
l_length = 9 .
PERFORM set_blank USING l_length
CHANGING p_group.
*& Get Have Statement
l_length = 6 .
PERFORM set_blank USING l_length
CHANGING p_have.
*& Get Order Statement
l_length = 9 .
PERFORM set_blank USING l_length
CHANGING p_order.
*& Get Dynamic Structure
PERFORM get_structure USING p_fields
CHANGING p_table .
CHECK g_exception[] IS INITIAL .
ASSIGN p_table->* TO <table> .
CREATE DATA p_line LIKE LINE OF <table>.
ASSIGN p_line->* TO <line> .
*& Check SQL Type
PERFORM get_data USING p_select p_on
p_where p_group
p_have p_order
p_type
CHANGING <table> <line>.
ENDFORM . "Get_Data_in_dynamic_SQL
******************************************************
*& Form Get_Structure
******************************************************
FORM get_structure USING p_fields TYPE tt_element
CHANGING p_ref TYPE REF TO data .
DATA: wa_component TYPE abap_componentdescr,
it_component TYPE abap_component_tab,
wa_strucdescr TYPE REF TO cl_abap_structdescr,
it_tabledescr TYPE REF TO cl_abap_tabledescr,
wa_field TYPE st_element,
l_oref TYPE REF TO cx_root ,
l_str TYPE string,
l_msg TYPE string.
LOOP AT p_fields INTO wa_field .
CLEAR : wa_component .
wa_component-name = wa_field-alias .
IF wa_field-display(6) = c_count1 .
CONCATENATE 'LVC_S_FCAT' '-' 'ROW_POS'
INTO l_str .
ELSE .
CONCATENATE wa_field-source '-' wa_field-name
INTO l_str .
ENDIF .
PERFORM case_to_upper CHANGING l_str .
PERFORM case_to_upper CHANGING wa_component-name .
TRY.
wa_component-type ?= cl_abap_typedescr=>describe_by_name( l_str ).
INSERT wa_component INTO TABLE it_component.
CATCH cx_root INTO l_oref.
l_msg = l_oref->get_text( ).
PERFORM append_error_message USING 4 l_msg .
CLEANUP.
CLEAR l_oref.
ENDTRY .
ENDLOOP .
CHECK g_exception[] IS INITIAL .
TRY.
IF it_component IS NOT INITIAL.
wa_strucdescr = cl_abap_structdescr=>create( it_component ).
it_tabledescr = cl_abap_tabledescr=>create( p_line_type = wa_strucdescr ).
ENDIF.
CREATE DATA p_ref TYPE HANDLE it_tabledescr.
CATCH cx_root INTO l_oref.
l_msg = l_oref->get_text( ).
PERFORM append_error_message USING 4 l_msg .
CLEANUP.
CLEAR l_oref.
ENDTRY .
ENDFORM . "get_structure
******************************************************
*& Form Get_Data
******************************************************
FORM get_data USING p_field TYPE tt_text
p_table TYPE tt_text
p_where TYPE tt_text
p_group TYPE tt_text
p_have TYPE tt_text
p_order TYPE tt_text
p_type TYPE string
CHANGING p_data TYPE STANDARD TABLE
p_line TYPE any .
DATA : l_oref TYPE REF TO cx_root ,
wa_text TYPE st_text ,
l_msg TYPE string .
TRY.
CASE p_type .
WHEN c_single .
SELECT SINGLE (p_field)
INTO p_line
FROM (p_table)
WHERE (p_where)
GROUP BY (p_group)
HAVING (p_have).
APPEND p_line TO p_data .
WHEN c_distinct .
SELECT DISTINCT (p_field)
INTO TABLE p_data
FROM (p_table)
WHERE (p_where)
GROUP BY (p_group)
HAVING (p_have)
ORDER BY (p_order) .
WHEN OTHERS .
SELECT (p_field)
INTO TABLE p_data
FROM (p_table)
WHERE (p_where)
GROUP BY (p_group)
HAVING (p_have)
ORDER BY (p_order) .
ENDCASE .
CATCH cx_root INTO l_oref.
l_msg = l_oref->get_text( ).
PERFORM append_error_message USING 4 l_msg .
CLEANUP.
CLEAR l_oref.
ENDTRY .
ENDFORM . "Get_Data
******************************************************
*& Form Get_Field_Statement
******************************************************
FORM get_field_statement USING p_element TYPE tt_element
CHANGING p_field TYPE tt_text .
DATA : wa_element TYPE st_element ,
wa_field TYPE st_text ,
l_temp TYPE string .
LOOP AT p_element INTO wa_element .
wa_field-line = wa_element-display .
APPEND wa_field TO p_field .
ENDLOOP .
ENDFORM . "Get_Field_Statement
******************************************************
*& Form Set_Blank
******************************************************
FORM set_blank USING l_length TYPE i
CHANGING p_text TYPE tt_text.
DATA : wa_text TYPE st_text .
CHECK p_text IS NOT INITIAL .
READ TABLE p_text INTO wa_text INDEX 1 .
wa_text-line(l_length) = '' .
SHIFT wa_text-line BY l_length PLACES RIGHT .
MODIFY p_text FROM wa_text INDEX 1 .
ENDFORM . "Set_Blank
子程序9:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLO01
*&---------------------------------------------------------------------*
******************************************************
*& Form Show_Message
******************************************************
FORM show_message USING p_message TYPE string .
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = space
txt1 = p_message.
ENDFORM. "Show_Message
******************************************************
*& Module Initial_Screen OUTPUT
******************************************************
MODULE initial_screen OUTPUT.
DATA : l_orientation TYPE i ,
l_mode TYPE i .
CHECK g_editor IS INITIAL.
g_repid = sy-repid.
*& Create Control Container
PERFORM create_container_object USING g_container
c_sql_editor.
l_orientation = 1 .
PERFORM create_splitter_object USING g_splitter1
g_container
l_orientation .
PERFORM create_editor_object USING g_editor1
g_splitter1->bottom_right_container .
l_orientation = 0 .
PERFORM create_splitter_object USING g_splitter
g_splitter1->top_left_container
l_orientation.
*& Create Text Editor
PERFORM create_editor_object USING g_editor
g_splitter->top_left_container .
*& Create ALV
PERFORM create_alv_object USING g_grid
g_splitter->bottom_right_container.
IF sy-subrc NE 0.
PERFORM show_message USING c_msg01 .
ENDIF.
*& Set SQL Editor Support Highlight Comments
PERFORM set_comment_mode USING g_editor .
PERFORM set_comment_mode USING g_editor1.
*& Set Editor Read Only
l_mode = 1 .
PERFORM set_readonly_mode USING g_editor1
l_mode .
ENDMODULE. " Initial_Screen OUTPUT
******************************************************
*& Module status_0100 OUTPUT
******************************************************
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN100'.
SET TITLEBAR 'TITLE100'.
ENDMODULE. " status_0100 OUTPUT
子程序10:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLO02
*&---------------------------------------------------------------------*
******************************************************
*& Module STATUS_0200 OUTPUT
******************************************************
MODULE status_0200 OUTPUT.
SET PF-STATUS ''.
ENDMODULE. " STATUS_0200 OUTPUT
子程序11:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLI01
*&---------------------------------------------------------------------*
******************************************************
*& Module USER_COMMAND_0100 INPUT
******************************************************
MODULE user_command_0100 INPUT.
DATA : l_start TYPE i ,
l_end TYPE i ,
it_temp TYPE tt_text ,
l_number TYPE i .
*& Initial Serval Global Variant
CLEAR : g_exception, l_number .
*& Keep Start Time
GET RUN TIME FIELD l_start.
*& Processing User Command
CASE g_ucomm.
WHEN 'EXIT' OR 'BACK' OR 'BREAK'.
PERFORM exit_program.
WHEN 'EXEC'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM execute_sql USING it_text
CHANGING l_number.
WHEN 'FORM'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM format_sql CHANGING it_text.
WHEN 'BUDG'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM bud_sturcture USING it_text
CHANGING it_temp .
PERFORM set_text_as_r3table USING g_editor1
CHANGING it_temp .
WHEN 'SAVE'.
IF g_file IS INITIAL .
PERFORM get_filename CHANGING g_file .
ENDIF .
CHECK g_file IS NOT INITIAL AND sy-subrc = 0 .
PERFORM save_as_local_file USING g_editor
g_file .
WHEN 'MOVE'.
PERFORM move_splitter_postion USING g_splitter .
WHEN 'CONF'.
CALL SCREEN c_200 STARTING AT 30 5 .
ENDCASE.
*& Set Runtime
GET RUN TIME FIELD l_end.
PERFORM set_runtime_and_lines USING l_start
l_end
l_number.
CLEAR : g_ucomm .
ENDMODULE. " USER_COMMAND_0100 INPUT
******************************************************
*& Form EXIT_PROGRAM
******************************************************
FORM exit_program.
*& Free All Memory Located By Objects
PERFORM destroy_alv_object CHANGING g_grid .
PERFORM destroy_editor_object CHANGING g_editor .
PERFORM destroy_editor_object CHANGING g_editor1 .
PERFORM destroy_splitter_object CHANGING g_splitter .
PERFORM destroy_splitter_object CHANGING g_splitter1 .
PERFORM destroy_container_object CHANGING g_container .
*& Finally Flush
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
OTHERS = 1.
LEAVE PROGRAM.
ENDFORM. " EXIT_PROGRAM
******************************************************
*& Form Set_Runtime
******************************************************
FORM set_runtime_and_lines USING p_start TYPE i
p_end TYPE i
p_number TYPE i .
DATA : l_temp TYPE string,
l_status TYPE c LENGTH 100,
l_spend TYPE p LENGTH 12 DECIMALS 3 .
*& Set Runtime
l_spend = ( p_end - p_start ) / 1000000 .
l_temp = l_spend .
CONCATENATE ' Runtime :' l_temp 'Seconds'
INTO l_status
SEPARATED BY space .
*& Set The Number Of The Total Records
l_temp = l_number .
CONCATENATE l_status ', Total Records :' l_temp
INTO l_status
SEPARATED BY space .
PERFORM set_status_text USING g_editor
l_status .
ENDFORM . "Set_Runtime
子程序12:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLI01
*&---------------------------------------------------------------------*
******************************************************
*& Module USER_COMMAND_0100 INPUT
******************************************************
MODULE user_command_0100 INPUT.
DATA : l_start TYPE i ,
l_end TYPE i ,
it_temp TYPE tt_text ,
l_number TYPE i .
*& Initial Serval Global Variant
CLEAR : g_exception, l_number .
*& Keep Start Time
GET RUN TIME FIELD l_start.
*& Processing User Command
CASE g_ucomm.
WHEN 'EXIT' OR 'BACK' OR 'BREAK'.
PERFORM exit_program.
WHEN 'EXEC'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM execute_sql USING it_text
CHANGING l_number.
WHEN 'FORM'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM format_sql CHANGING it_text.
WHEN 'BUDG'.
PERFORM save_text_to_table USING g_editor
CHANGING it_text.
PERFORM bud_sturcture USING it_text
CHANGING it_temp .
PERFORM set_text_as_r3table USING g_editor1
CHANGING it_temp .
WHEN 'SAVE'.
IF g_file IS INITIAL .
PERFORM get_filename CHANGING g_file .
ENDIF .
CHECK g_file IS NOT INITIAL AND sy-subrc = 0 .
PERFORM save_as_local_file USING g_editor
g_file .
WHEN 'MOVE'.
PERFORM move_splitter_postion USING g_splitter .
WHEN 'CONF'.
CALL SCREEN c_200 STARTING AT 30 5 .
ENDCASE.
*& Set Runtime
GET RUN TIME FIELD l_end.
PERFORM set_runtime_and_lines USING l_start
l_end
l_number.
CLEAR : g_ucomm .
ENDMODULE. " USER_COMMAND_0100 INPUT
******************************************************
*& Form EXIT_PROGRAM
******************************************************
FORM exit_program.
*& Free All Memory Located By Objects
PERFORM destroy_alv_object CHANGING g_grid .
PERFORM destroy_editor_object CHANGING g_editor .
PERFORM destroy_editor_object CHANGING g_editor1 .
PERFORM destroy_splitter_object CHANGING g_splitter .
PERFORM destroy_splitter_object CHANGING g_splitter1 .
PERFORM destroy_container_object CHANGING g_container .
*& Finally Flush
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
OTHERS = 1.
LEAVE PROGRAM.
ENDFORM. " EXIT_PROGRAM
******************************************************
*& Form Set_Runtime
******************************************************
FORM set_runtime_and_lines USING p_start TYPE i
p_end TYPE i
p_number TYPE i .
DATA : l_temp TYPE string,
l_status TYPE c LENGTH 100,
l_spend TYPE p LENGTH 12 DECIMALS 3 .
*& Set Runtime
l_spend = ( p_end - p_start ) / 1000000 .
l_temp = l_spend .
CONCATENATE ' Runtime :' l_temp 'Seconds'
INTO l_status
SEPARATED BY space .
*& Set The Number Of The Total Records
l_temp = l_number .
CONCATENATE l_status ', Total Records :' l_temp
INTO l_status
SEPARATED BY space .
PERFORM set_status_text USING g_editor
l_status .
ENDFORM . "Set_Runtime
子程序13:
*&---------------------------------------------------------------------*
*& 包括 ZOPENSQLI02
*&---------------------------------------------------------------------*
******************************************************
*& Module USER_COMMAND_0200 INPUT
******************************************************
MODULE user_command_0200 INPUT.
CASE g_ucomm .
WHEN 'CONF'.
CASE 'X'.
WHEN l_case_01 .
g_case = 1 .
WHEN l_case_02 .
g_case = 2 .
WHEN l_case_03 .
g_case = 3 .
ENDCASE .
CASE 'X'.
WHEN l_label_01 .
g_label = 1 .
WHEN l_label_02 .
g_label = 2 .
ENDCASE .
WHEN 'CANC'.
ENDCASE .
CLEAR : g_ucomm .
ENDMODULE. " USER_COMMAND_0200 INPUT