Skip to content

ResultSet with ownInsertsAreVisible can't read row after insert #818

@prrvchr

Description

@prrvchr

Hi,

I am trying to implement in LibreOffice Base ResultSet that support the visibility of inserts. During this implementation I realized that it was not possible to reread a new inserted row (in fact we get the last row before the insert)

Here is the code to reproduce the error:

    public void testInsertVisibility(java.sql.Connection connection) {
        String select = "select * from t1";
        int rstype = java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;

        try {
            populateTable(connection);

            java.sql.PreparedStatement ps = connection.prepareStatement(select,
                                                                        rstype,
                                                                        java.sql.ResultSet.CONCUR_UPDATABLE);

            java.sql.ResultSet rs = ps.executeQuery();

            boolean visible = connection.getMetaData().ownInsertsAreVisible(rstype);
            System.out.println("testInsertVisibility() ResultSet Inserts are Visible: " + visible);

            if (rs.last()) {
                System.out.println("testInsertVisibility() Before insert last row is: " + rs.getRow());
            }
            rs.moveToInsertRow();
            rs.updateInt(1, 10);
            rs.updateString(2, "s10");
            rs.insertRow();
            rs.moveToCurrentRow();
            if (rs.last()) {
                System.out.println("testInsertVisibility() After insert last row is: " + rs.getRow());
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    Object value = rs.getObject(i);
                    if (rs.wasNull()) {
                        System.out.println("testInsertVisibility() Column Index: " + i + " Value was Null");
                    }
                    else {
                        System.out.println("testInsertVisibility() Column Index: " + i + " Value: " + value.toString());
                    }
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    void populateTable(java.sql.Connection connection) throws SQLException {
        java.sql.Statement statement = connection.createStatement();
        statement.execute("recreate table t1 (i int primary key, v varchar(10))");
        statement.close();

        String insert = "insert into t1 values(?, ?)";
        java.sql.PreparedStatement ps = connection.prepareStatement(insert);

        for (int i = 1; i < 10; i++) {
            ps.setInt(1, i);
            ps.setString(2, "s" + String.valueOf(i));
            ps.execute();
        }

        ps.close();
    }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions