Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE in CoreStatement.checkIndex when no parameter has been set client side #911

Closed
jcgay opened this issue May 30, 2023 · 2 comments
Closed
Labels
bug Something isn't working released Issue has been released

Comments

@jcgay
Copy link
Contributor

jcgay commented May 30, 2023

I'm facing an issue since version v3.41.2.2.

Describe the bug
NullPointerException for org.sqlite.jdbc3.JDBC3PreparedStatement.getParameterType when no parameter has been set yet.

To Reproduce

    @Test
    void getParameterTypeTest_when_no_parameter_set() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:sqlite:");
        Statement stat = conn.createStatement();
        stat.executeUpdate("create table t_int(i INT)");

        try (PreparedStatement ps = conn.prepareStatement("INSERT INTO t_int VALUES(?)")) {
            assertThatThrownBy(() -> ps.getParameterMetaData().getParameterType(1))
                    .isInstanceOf(NullPointerException.class);
            assertThatThrownBy(() -> ps.getParameterMetaData().getParameterTypeName(1))
                    .isInstanceOf(NullPointerException.class);
        }
    }

Expected behavior
A SQLException should be thrown instead indicating that parameter type cannot be determined.

Logs

java.lang.NullPointerException
	at org.sqlite.core.CoreStatement.checkIndex(CoreStatement.java:145)
	at org.sqlite.jdbc3.JDBC3PreparedStatement.getParameterType(JDBC3PreparedStatement.java:165)
	at xxx.dao.stubbing.SqliteBinderConfiguration.getBinder(SqliteBinderConfiguration.java:25)
	at com.ninja_squad.dbsetup.operation.Insert.initializeBinders(Insert.java:286)
	at com.ninja_squad.dbsetup.operation.Insert.execute(Insert.java:210)
	at com.ninja_squad.dbsetup.operation.CompositeOperation.execute(CompositeOperation.java:92)
	at com.ninja_squad.dbsetup.DbSetup.launch(DbSetup.java:107)

Environment (please complete the following information):

  • OS: all
  • CPU architecture: x86_64, arm64
  • sqlite-jdbc version 3.41.2.2

Additional context
The bug appears in my application tests using http://dbsetup.ninja-squad.com/ when a binder try to guess the database types (http://dbsetup.ninja-squad.com/user-guide.html#data-formats).

I'm not really sure of the desired behavior here. In this driver we only rely on which setX method has been called on PreparedStatement, not on metadata returned by the database. But I assume the ParameterMetaData documentation can be interpreted this way. I have not checked implementation in other drivers.
For example we can modify this test:

    @Test
    public void getParameterTypeTest() throws SQLException {
        stat.executeUpdate("create table t_int(i VARCHAR)");

        try (PreparedStatement ps = conn.prepareStatement("INSERT INTO t_int VALUES(?)")) {
            ps.setLong(1, 100);
            assertThat(ps.getParameterMetaData().getParameterType(1)).isEqualTo(Types.BIGINT);
            assertThat(ps.getParameterMetaData().getParameterTypeName(1)).isEqualTo("BIGINT");
        }
    }

and it will pass even if the declared type in the database is not an integer.

@gotson
Copy link
Collaborator

gotson commented May 30, 2023

I'm not really sure of the desired behavior here. In this driver we only rely on which setX method has been called on PreparedStatement, not on metadata returned by the database. But I assume the ParameterMetaData documentation can be interpreted this way. I have not checked implementation in other drivers.

SQLite doesn't really have types, which makes things like this difficult.

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in 3.42.0.1 (Release Notes)

gotson pushed a commit to gotson/sqlite-jdbc that referenced this issue Oct 18, 2023
Before this commit, the code was failing with NPE when getParameterType was called
before any PreparedStatement#setX was done.

Closes: xerial#911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released Issue has been released
Projects
None yet
2 participants