import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.util.BaseBugReport;

public class BugReport1 extends BaseBugReport {
	Connection con;
	Statement stm;
	
	public BugReport1() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void setUp() throws Exception {
		con = DriverManager.getConnection(getUrl());
		stm = con.createStatement();
		// table creation
		stm.executeUpdate("CREATE TABLE bug1 (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,value BIGINT UNSIGNED NULL DEFAULT 0,PRIMARY KEY(id))ENGINE=InnoDB;");
		// insert 1 line
		stm.executeUpdate("INSERT INTO bug1(value) VALUES(1)");
	}

	public void tearDown() throws Exception {
		con = DriverManager.getConnection(getUrl());
		stm = con.createStatement();
		stm.executeUpdate("DROP TABLE bug1");
		stm.close();
	}

	public void runTest() throws Exception {
		con = DriverManager.getConnection(getUrl());
		stm = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
		ResultSet rs = null;
		String value = "1";
		
		// testing reading and updating from ResultSet
		try {
			rs = stm.executeQuery("SELECT * from bug1");
			if (rs.next()) {
				// updating a BigInteger as a String works
				rs.updateString("value", new BigInteger("2").toString());
				rs.updateRow();
				// if correctly updated, now value="2"
				value = rs.getString("value");
			}
			rs.close();
		} catch (SQLException e) {}
		assertTrue("String value 2 wasn't updated correctly to an UNSIGNED BIGINT", (value.equals("2")));
		
		// testing reading and updating from ResultSet
		try {
			rs = stm.executeQuery("SELECT * from bug1");
			if (rs.next()) {
				// updating a BigInteger fails
				rs.updateObject("value", new BigInteger("3"));
				rs.updateRow();
				// if correctly updated, now value="3"
				value = rs.getString("value");
			}
			rs.close();
		} catch (SQLException e) {}
		assertTrue("BigInteger value 3 wasn't updated correctly to an UNSIGNED BIGINT", (value.equals("3")));
	}

	public static void main(String[] args) throws Exception {
		new BugReport1().run();
	}

}
