Knut Anders Hatlen <[email protected]> writes:

> AKA https://issues.apache.org/jira/browse/DERBY-5537 :)
>
>> The truncation behavior we're seeing may be due to the insertion of
>> implicit CASTs.
>
> The above mentioned bug has a patch attached. Would be interesting to
> see if it makes these implicit casts raise a warning too.

Confirmed. Tried the repro as modified below with that patch and saw:

:
warning:01004 java.sql.DataTruncation: Data truncation
:

Dag

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package silentvarcharargtruncation;

import java.sql.*;

/**
 *
 * @author dag
 */
public class SilentVarcharArgTruncation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException {
        Connection c = 
DriverManager.getConnection("jdbc:derby:wombat;create=true");
        Statement s = c.createStatement();
        
        try {
            s.executeUpdate("drop table t");
        } catch (SQLException e) {}
        
        try {
            s.executeUpdate("drop procedure p");
        } catch (SQLException e) {}
        
        
        s.executeUpdate("create table t(v varchar(5))");
        s.executeUpdate("create procedure p (a varchar(5)) modifies sql data " 
+ 
            "external name 
'silentvarcharargtruncation.SilentVarcharArgTruncation.testLength' " + 
            "language java parameter style java");
        CallableStatement cs = c.prepareCall("call p(?)");
        cs.setString(1, "123456");
        
        // This silently truncates
        cs.execute();
        SQLWarning warning = cs.getWarnings();
        while (warning != null) {
            System.out.println("warning:" + warning.getSQLState() + " " + 
warning);
            warning = warning.getNextWarning();
        }
        
        PreparedStatement ps = c.prepareStatement("insert into t values(?)");
        ps.setString(1, "123456");
        // This does not truncate
        ps.execute();
    }
    
    
    public static void testLength (String s) throws SQLException {
        System.out.println(s.length());
    }
}

Reply via email to