/*
 * 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 org.itscool.commons.connection.Record;
import org.itscool.commons.connection.Records;
import org.itscool.commons.dao.PreparedStatementWrapper;
import org.itscool.commons.connection.TransactionUtil;
import org.itscool.weber.testcase.WeberDbTestCase;
import org.itscool.weber.user.Role;

/**
 * PreparedStatementのテスト（非ORマッピング）
 */
public class PrepareTest extends WeberDbTestCase{
    public PrepareTest(){
        this.initTables("test/org/itscool/commons/dao/preperd/test-db-data.xml");
    }
    
    public void testSelect(){
        String sql = "SELECT * FROM USER, ROLE WHERE USER.ROLE_ID = ROLE.ID AND ROLE.ID = ?";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        mapper.setValue(1, 2);
        Records result = mapper.select();
        for( int i=0; i<result.size(); i++){
            Record record = result.getRecord(i);
            System.out.print("uer-id="+record.getInt("USER.ID"));
            System.out.println("user-name="+record.getString("USER.NAME"));
            System.out.print("role-id="+record.getInt("ROLE.ID"));
            System.out.println("role-name="+record.getString("ROLE.NAME"));
        }
    }
    
    public void testInsert(){
        String sql = "INSERT INTO ROLE ( ID, NAME )VALUES( ?, ? )";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        
        Role role = new Role();
        role.setId(99);
        role.setName("insert row1");
        
        mapper.setValue(1, role.getId());
        mapper.setValue(2, role.getName());
        
        int result = mapper.executeUpdate();
        assertEquals("insert count", 1, result);
        
        TransactionUtil.commitTransaction();
    }
}
