/*
 * SamplePrepare.java
 *
 * Created on 2007/04/12, 13:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package test.org.itscool.commons.dao.preperd;

import java.util.List;
import org.itscool.commons.connection.TransactionUtil;
import org.itscool.commons.dao.PreparedStatementWrapper;
import org.itscool.weber.testcase.WeberDbTestCase;

/**
 * PreparedStatementのテスト（ORマッピング）
 */
public class PrepareTest3 extends WeberDbTestCase{
    public PrepareTest3(){
        this.initTables("test/org/itscool/commons/dao/preperd/test-db-data.xml");
    }
    
    /**
     * 複数のテーブルを単一のクラスにマッピングするテスト
     */
    public void testSelect(){
        //オブジェクトのメンバとカラムに指定したAS句でマッピング
        String sql = "SELECT USER.ID, USER.NAME, ROLE.NAME AS ROLE_NAME "
                + "FROM USER, ROLE WHERE USER.ROLE_ID = ROLE.ID ORDER BY USER.ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List list = mapper.select(UserDetail.class, PreparedStatementWrapper.MAP_KIND_QUERY_ONE_CLASS);
        assertEquals("list num", 4, list.size());
        UserDetail detail = (UserDetail)list.get(0);
        assertEquals("user id", 1, detail.getId());
        assertEquals("user name", "admin1", detail.getName());
        //外部参照テーブルのカラムがセットされていることを確認する
        assertEquals("user role's name", "admin", detail.getRoleName());
    }
    
    public void testSelect2(){
        StringBuffer sql = new StringBuffer();
        sql.append("select ");
        sql.append("	customer.id, ");
        sql.append("	role.name as role_name, ");
        sql.append("	user.name as user_name, ");
        sql.append("	customer.name ");
        sql.append("from role, user, customer ");
        sql.append("where user.role_id = role.id ");
        sql.append("and role.id = ? ");
        sql.append("and user.id = customer.user_id ");
        
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql.toString());
        mapper.setValue(1, 2);
        
        List list = mapper.select(CustomerDetail.class, PreparedStatementWrapper.MAP_KIND_QUERY_ONE_CLASS);
        assertEquals("list num", 2, list.size());
    }
}
