Query Service - Callable Statement

CallableStatment는 표준 DDL, DML이 아닌 DB의 Stored Procedure, Function 등을 호출할 때 사용한다. Stored Procedure는 쿼리문을 하나의 파일 형태로 만들거나 DB에 저장해두고 함수처럼 호출해서 사용하는 것으로, 성능, 코드의 독립성, 보안성 등의 다양한 이점을 제공한다.

Query 서비스 속성 정의 파일 Sample

다음은 Query 서비스를 정의한 applicationContext-query-oracle.xml 과 Query 서비스에서 읽어들일 매핑 XML 파일의 위치를 정의한 applicationContext-query-sqlloader.xml 파일의 일부이다.
<bean name="oracle_queryservice" class="anyframe.core.query.impl.QueryServiceImpl">
	<property name="jdbcTemplate" ref="oracle_jdbcTemplate"/>		
	<property name="sqlRepository" ref="sqlLoader"/>		
	<!-- 중략 -->
</bean>	
<bean id="oracle_jdbcTemplate" class="anyframe.core.query.impl.jdbc.PagingJdbcTemplate">
    <property name="dataSource" ref="oracle_datasource" />
</bean>   
<!-- 중략 -->

<bean name="sqlLoader" class="anyframe.core.query.impl.config.loader.SQLLoader">
   	<config:configuration>
 		    <filename>
 		      classpath:/services/query/mappings/mapping-callablestatement-query.xml
 		    </filename>
 		    <!-- 중략 -->
	</config:configuration>		
</bean> 
<!-- 중략 -->

매핑 XML 파일 Sample

다음은 앞서 정의한 Query 서비스를 통해 로드된 mapping-callablestatement-query.xml 로, DB에 정의되어 있는 test_function이라는 Function을 실행시키기 위한 쿼리문을 포함하고 있다.
<queryservice>
  	<queries>	
		<query id="callFunction">
			<statement>{? = call test_function(?)}</statement>
			<param type="NUMERIC" binding="OUT" name="outVal" />
			<param type="NUMERIC" binding="IN" name="inVal" />
		</query>
    </queries>    
</queryservice>

Query 서비스 테스트 코드 Sample

다음은 앞서 언급한 매핑 XML 파일에 정의된 Function을 호출하기 위한 쿼리문을 실행하는 소스 코드 QueryServiceTestCallableStatement.java 이다. Query 서비스를 통해 Function을 실행시키기 위해서는 사용하는 DB에 해당 Function이 미리 생성되어 있어야 한다.
/**
 * 매핑 XML 파일에 정의되어 있는 query id를 이용해 Function을 호출한다.
 */
public void testCallableStatementFunction() throws Exception {
	IQueryService queryService = (IQueryService) context.getBean("oracle_queryservice");
	
	HashMap inVal = new HashMap();
	inVal.put("inVal", new Integer(10));
	
	Map results = queryService.execute("callFunction", inVal);
	BigDecimal rtVal = (BigDecimal) results.get("outVal");
	
	if (rtVal.intValue() != 1 ){
		throw new Exception("testCallableStatementProcedure failed");
	}
}

/**
 * 매핑 XML 파일에 정의되어 있지 않은 경우에도 특정 Function을 호출할 수 있다.
 * Map executeBySQL(String sql, String[] types, String[] names, String[] bindings, Map values) 
 */
public void testCallableStatementBySQL() throws Exception {
	IQueryService queryService = (IQueryService) context.getBean("oracle_queryservice");
	
	String sql = "{? = call test_function(?)}";
	String[] types = { "NUMERIC", "NUMERIC" };
	String[] bindings = { "OUT", "IN" };
	String[] names = { "outVal", "inVal" };
	
	HashMap inVal = new HashMap();
	inVal.put("inVal", new Integer(10));
	
	Map results = queryService.executeBySQL(sql, types, names, bindings, inVal);
	BigDecimal rtVal = (BigDecimal) results.get("outVal");

	if (rtVal.intValue() != 1 ){
		throw new Exception("testCallableStatementBySQL failed");
	}
}	

/**
 * 매핑 XML 파일에 정의되어 있지 않은 경우에도 특정 Function을 호출할 수 있다.
 * 이 메소드에서는 페이징 처리된 조회 결과를 얻고자 한다.
 * Map executeBySQL(String sql, String[] types, String[] names, 
 * 					String[] bindings, Map values, int pageIndex, int pageSize) 
 */
public void testCallableStatementBySQLWithPaging() throws Exception {
	IQueryService queryService = (IQueryService) context.getBean("oracle_queryservice");
	
	String sql = "{? = call test_function(?)}";
	String[] types = { "NUMERIC", "NUMERIC" };
	String[] bindings = { "OUT", "IN" };
	String[] names = { "outVal", "inVal" };
	
	HashMap inVal = new HashMap();
	inVal.put("inVal", new Integer(10));
	
	Map results = queryService.executeBySQL(sql, types, names, bindings, inVal, 1, 1);
	BigDecimal rtVal = (BigDecimal) results.get("outVal");

	if (rtVal.intValue() != 1 ){
		throw new Exception("testCallableStatementBySQLWithPaging failed");
	}
}
앞서 소개된 샘플 테스트 코드를 포함하여 QueryService 소개 페이지에서 제공하는 모든 샘플 테스트 코드는 HSQL DB를 기반으로 실행된다. ( 단, ※ CallableStatement, LOB의 경우는 Oracle 9i, 10g를 기반으로 함. )

Resources

  • 다운로드
  • 샘플 테스트 코드를 포함하고 있는 anyframe-querytest-src.zip 파일을 다운받은 후, 테스트 환경 설정 을 참조하여 위에서 제시한 예제 코드를 실행해 볼 수 있다.
    Name
    Download
    anyframe-querytest-src.zip
    Download