2010-04-09 4 views
3

저는 오늘 pluto-test-framework으로 놀았으며, 기존 기능을 테스트 하니스로 가져 가고 싶습니다.두 오라클 유형의 각 열의 값 비교

이 유형의 사양에는 많은 기능이 있습니다.

FUNCTION DO_SOME_STUFF (pOldSchedule  IN  SCHEDULE_OBJ, 
          pNewSchedule   OUT SCHEDULE_OBJ, 
          pLoggerContext  IN OUT LOGGER_CONTEXT_OBJ) 
    RETURN NUMBER; 

pOldSchedule을 사용하고 일부 작업을 수행 한 다음 pNewSchedule을 반환합니다. logger_context는 단지 로깅을 수행합니다.

테스트의 일환으로 개별 IF 문을 작성하지 않고도 형식의 각 열의 값을 비교할 수 있기를 바랍니다.

pOldSchedule과 pNewSchedule이 일치하는지 여부를 나타 내기 위해서는 부울 값을 반환해야합니다.

아이디어가 있으십니까?

답변

6

솔직한 평등 테스트는 중첩 된 테이블에 대해 작업 :

SQL> declare 
    2  type nt is table of number; 
    3  nt1 nt; 
    4  nt2 nt; 
    5  nt3 nt; 
    6 begin 
    7  nt1 := nt(1,2,3); 
    8  nt2 := nt(1,2,3); 
    9  if nt1 = nt2 then 
10   dbms_output.put_line('NT2 is the same nested table as NT1'); 
11  else 
12   dbms_output.put_line('NT2 is a different nested table from NT1'); 
13  end if; 
14  nt2 := nt(1,2,3,4); 
15  if nt1 = nt3 then 
16   dbms_output.put_line('NT3 is the same nested table as NT1'); 
17  else 
18   dbms_output.put_line('E3 is a different nested table from NT1'); 
19  end if; 
20 end; 
21/
NT2 is the same nested table as NT1 
E3 is a different nested table from NT1 

PL/SQL procedure successfully completed. 

SQL> 

을 동일-전체의 개체의 사실이 아니다 그러나 :

SQL> create or replace type new_emp as object (
    2  ename varchar2(10) 
    3  , sal number 
    4  , deptno number 
    5  , job varchar2(10)) 
    6/

Type created. 

SQL> declare 
    2  e1 new_emp; 
    3  e2 new_emp; 
    4 begin 
    5  e1 := new_emp('KESTELYN', 3700, 30, 'MARKETING'); 
    6  e2 := new_emp('KESTELYN', 3700, 30, 'MARKETING'); 
    7  if e1 = e2 then 
    8   dbms_output.put_line('E2 is the same as E1'); 
    9  else 
10   dbms_output.put_line('E2 is different from E1'); 
11  end if; 
12 end; 
13/
    if e1 = e2 then 
      * 
ERROR at line 7: 
ORA-06550: line 7, column 11: 
PLS-00526: A MAP or ORDER function is required for comparing objects in PL/SQL. 


SQL> 

우리는 명시 적으로 비교를 실행하기위한 멤버 함수를 정의 할 필요가있다. 그래서 여기에 MAP 기능을 가진 동일한 객체가 있습니다. 예제 구현은 해시 된 문자열을 생성합니다.이 값은 나중에 비교할 값을 저장하려는 경우 유용하지만 연결 문자열 만 반환 할 수 있습니다 (특히 DBMS_CRYPTO의 EXECUTE는 기본적으로 부여되지 않습니다). NVL() 함수는 (null, value) 및 (value, null)이 동일한 것으로 평가되는 것을 피하기 위해 필요합니다. 마법 값을 사용할 때 항상 위험이 있으므로 신중하게 선택해야합니다. 오라클은 기본적으로이 작업을 수행하지 않는 이유 당신은 궁금 할 것이다

SQL> declare 
    2  e1 new_emp; 
    3  e2 new_emp; 
    4 begin 
    5  e1 := new_emp('KESTELYN', 3700, 30, 'MARKETING'); 
    6  e2 := new_emp('KESTELYN', 3700, 30, 'MARKETING'); 
    7  if e1 = e2 then 
    8   dbms_output.put_line('E2 is the same as E1'); 
    9  else 
10   dbms_output.put_line('E2 is different from E1'); 
11  end if; 
12 end; 
13/
E2 is the same as E1 

PL/SQL procedure successfully completed. 

SQL>  

:

SQL> create or replace type new_emp as object (
    2  ename varchar2(10) 
    3  , sal number 
    4  , deptno number 
    5  , job varchar2(10) 
    6  , map member function equals return raw) 
    7/

Type created. 

SQL> create or replace type body new_emp as 
    2  map member function equals return raw 
    3  is 
    4  begin 
    5   return dbms_crypto.hash(
    6      utl_raw.cast_to_raw(nvl(self.ename,'***')|| 
    7           nvl(self.sal,-99)|| 
    8           nvl(self.deptno,-99)|| 
    9           nvl(self.job,'***') 
10          ) 
11         , 1); 
12  end equals; 
13 end; 
14/

Type body created. 

SQL> 

이제 우리는 우리의 객체의 인스턴스를 비교하기위한 기준이있다. 글쎄, TYPE 구현은 하나의 비교 메소드 만 허용합니다 (우리가 MAP 함수를 가지고 있다면 ORDER 함수를 가질 수 없습니다). 그래서 우리는 우리 자신의 평등 정의를 선택할 수있는 능력이 필요합니다. 예를 들어, rectangle이라는 유형은 을 리턴하는 area()이라는 MAP 기능을 가질 수 있습니다.