sql-streams icon indicating copy to clipboard operation
sql-streams copied to clipboard

Bring back updateReturning

Open 14mRh4X0r opened this issue 7 years ago • 2 comments

Some JDBC providers, e.g. H2, don't allow INSERT with executeQuery:

Caused by: org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
INSERT INTO players (uuid, name, first_login, last_login) VALUES (?, ?, ?, ?) [90002-196]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.message.DbException.get(DbException.java:179) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.message.DbException.get(DbException.java:155) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.message.DbException.get(DbException.java:144) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.command.Prepared.query(Prepared.java:213) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.command.CommandContainer.query(CommandContainer.java:113) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.command.Command.executeQuery(Command.java:201) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:111) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[spongevanilla-1.12.2-7.1.0-BETA-33.jar:1.12.2-7.1.0-BETA-33]
	at be.bendem.sqlstreams.util.Wrap.get(Wrap.java:41) ~[Wrap.class:0.0]
	... 14 more

14mRh4X0r avatar May 15 '18 15:05 14mRh4X0r

I have reopened #6.

Can you confirm that this works correctly with h2?

SqlFunction<Connection, PreparedStatement> preparer = connection -> connection.prepareStatement("insert into table(column) values (?) returning *", Statement.RETURN_GENERATED_KEYS);
try (Update update = sql.update(preparer).with(x)) {
    rejectIf(() -> update.count() != 1);
    try (ResultSet rs = update.getStatement().getGeneratedKeys()) {
        // deal with generated values here.
    }
}

bendem avatar May 17 '18 08:05 bendem

Yup, that works as expected, after adding a catch clause for the SQLException thrown by ResultSet#close

14mRh4X0r avatar May 17 '18 12:05 14mRh4X0r