[CONJ-236] Using a parametrized query with a smallint -1 does return the unsigned value Created: 2015-12-21  Updated: 2015-12-26  Resolved: 2015-12-26

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 1.3.3
Fix Version/s: 1.4.0

Type: Bug Priority: Critical
Reporter: Frank S Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None


 Description   

Consider following testcase

/*
 
CREATE DATABASE test;
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost' IDENTIFIED BY 'test';
flush PRIVILEGES;
*/
 
import java.sql.*;
 
import static org.junit.Assert.assertEquals;
 
 
public class Test {
 
    @org.junit.Test
    public void test() throws Exception {
        Class.forName("org.mariadb.jdbc.Driver");
 
        String createStmt = "CREATE TABLE table1(" +
                "`id` int(11) NOT NULL AUTO_INCREMENT, value1 int default NULL, value2 smallint(6) default null, " +
                "PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=56840 DEFAULT CHARSET=utf8";
 
        try (Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost/test", "test", "test")) {
            try {
 
                connection.prepareStatement(createStmt).execute();
 
                PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO table1(value1, value2) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS);
                prepareStatement.setObject(1, -1, java.sql.Types.INTEGER);
                prepareStatement.setObject(2, -1, java.sql.Types.INTEGER);
                prepareStatement.execute();
                ResultSet rs = prepareStatement.getGeneratedKeys();
//                    if (rs.next()) {
//                        System.out.println(rs.getLong(1));
//                    }
                
                try (PreparedStatement statement = connection.prepareStatement("SELECT value1, value2 from table1")) {
                    statement.executeQuery();
                    ResultSet resultSet = statement.getResultSet();
                    while (resultSet.next()) {
                        assertEquals(new Integer(-1), new Integer(resultSet.getInt("value1")));
                        assertEquals(new Integer(-1), new Integer(resultSet.getShort("value2")));
                        assertEquals(new Integer(-1), new Integer(resultSet.getInt("value2")));
                    }
                }
 
                try (PreparedStatement statement = connection.prepareStatement("SELECT value1, value2 from table1 where value1 = ?")) {
                    statement.setInt(1, -1);
                    statement.executeQuery();
                    ResultSet resultSet = statement.getResultSet();
                    while (resultSet.next()) {
                        assertEquals(new Integer(-1), new Integer(resultSet.getInt("value1")));
                        assertEquals(new Integer(-1), new Integer(resultSet.getShort("value2")));
                        assertEquals(new Integer(-1), new Integer(resultSet.getInt("value2")));
                    }
                }
            } finally {
                connection.prepareStatement("drop table table1").execute();
            }
        }
    }
}

Executing the select statement with the "?" does return the unsigned value of -1 from value2 (smallint column).
It has sth to do with the using of MariaDbServerPreparedStatement which is used by the paramterized query and the setting of the binaryFlag which is used in MariaDBValueObject.getInt()
which is set to false their. So the Value is returned by getSmallInt. In MariaDbClientPreparedStatement the binaryFlag is set to true, so the value is returned via parseIn in MariaDBValueObject and this returns the correct (signed) value.



 Comments   
Comment by Diego Dupin [ 2015-12-26 ]

problem reproduced :
Using a resultset.getInt() / resultset.getLong(), resultset.getBigDecimal() on a preparedStatement on an signed smallInt negative value.

Comment by Diego Dupin [ 2015-12-26 ]

correction : https://github.com/MariaDB/mariadb-connector-j/commit/88c9f30d04eaf707d2698ee5e2ebad1b82cc8f3a

Generated at Thu Feb 08 03:14:11 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.