Author: Not specified Language: text
Description: Not specified Timestamp: 2018-06-21 11:49:31 +0000
View raw paste Reply
*&---------------------------------------------------------------------*
*& Report z_pj_tests
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_pj_tests.

*DATA:
*  lt_test1 TYPE TABLE OF REF TO object, "TABLE OF REF TO object,
*  lt_test2 TYPE TABLE OF REF TO /gicom/cl_negotiation.
*
*FIELD-SYMBOLS:
*    <lt_test3> TYPE STANDARD TABLE.
*
*
*APPEND NEW /gicom/cl_negotiation( ) TO <lt_test3>.
*
*lt_test2 = <lt_test3>.

INTERFACE if_where_element.
  METHODS:
    expand
      RETURNING
        VALUE(rv_result) TYPE string.
ENDINTERFACE.

TYPES tty_where_element TYPE TABLE OF REF TO if_where_element WITH DEFAULT KEY.

CLASS cl_where_symbol DEFINITION.
  PUBLIC SECTION.
    INTERFACES:
      if_where_element.
    METHODS:
      constructor
        IMPORTING
          iv_left_operand  TYPE string
          iv_operator      TYPE string
          iv_right_operand TYPE string.
  PRIVATE SECTION.
    DATA:
      gv_left_operand  TYPE string,
      gv_operator      TYPE string,
      gv_right_operand TYPE string.
ENDCLASS.

CLASS cl_where_symbol IMPLEMENTATION.
  METHOD if_where_element~expand.
    IF me->gv_right_operand IS NOT INITIAL.
      rv_result = me->gv_left_operand && ` ` && me->gv_operator && ` ` && me->gv_right_operand.
    ENDIF.
  ENDMETHOD.

  METHOD constructor.
    me->gv_left_operand = iv_left_operand.
    me->gv_operator = iv_operator.
    me->gv_right_operand = iv_right_operand.
  ENDMETHOD.
ENDCLASS.

CLASS cl_where_part DEFINITION.
  PUBLIC SECTION.
    INTERFACES:
      if_where_element.
    METHODS:
      constructor
        IMPORTING
          it_operands TYPE tty_where_element
          iv_operator TYPE string OPTIONAL.
  PRIVATE SECTION.
    DATA:
      gt_operands TYPE tty_where_element,
      gv_operator TYPE string.
ENDCLASS.

CLASS cl_where_part IMPLEMENTATION.
  METHOD if_where_element~expand.
    DATA lv_at_first TYPE abap_bool VALUE abap_true.

    rv_result = rv_result && '(' && ` `.

    LOOP AT me->gt_operands INTO DATA(lo_operand).
      DATA(ls_part) = lo_operand->expand( ).
      IF ls_part IS NOT INITIAL.
        IF lv_at_first = abap_true.
          lv_at_first = abap_false.
        ELSE.
          rv_result = rv_result && ` ` && me->gv_operator && ` `.
        ENDIF.

        rv_result = rv_result && ls_part.
      ENDIF.
    ENDLOOP.

    rv_result = rv_result && ` ` && ')'.
  ENDMETHOD.

  METHOD constructor.
    me->gt_operands = it_operands.
    me->gv_operator = iv_operator.
  ENDMETHOD.
ENDCLASS.

CLASS cl_query_builder DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      build_where_clause
        IMPORTING
          io_where_root          TYPE REF TO if_where_element
        RETURNING
          VALUE(rv_where_clause) TYPE string.
ENDCLASS.

CLASS cl_query_builder IMPLEMENTATION.
  METHOD build_where_clause.
    rv_where_clause = io_where_root->expand( ).
    IF rv_where_clause IS INITIAL.
      rv_where_clause = '1 = 1'.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA(lo_where) = NEW cl_where_part(
      it_operands = VALUE #(
          ( NEW cl_where_symbol( iv_left_operand = 'field1' iv_operator = '=' iv_right_operand = 'field2' ) )
          ( NEW cl_where_symbol( iv_left_operand = 'field2' iv_operator = '=' iv_right_operand = 'field2' ) )
          ( NEW cl_where_part(
            it_operands = VALUE #(
                ( NEW cl_where_symbol( iv_left_operand = 'field3' iv_operator = '=' iv_right_operand = 'field4' ) )
                ( NEW cl_where_symbol( iv_left_operand = 'field5' iv_operator = '=' iv_right_operand = 'field6' ) )
            )
            iv_operator = 'AND'
          ) )
      )
      iv_operator = 'OR'
  ).

  WRITE cl_query_builder=>build_where_clause( lo_where ).
View raw paste Reply