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

Bring back updateReturning #18

Open
14mRh4X0r opened this issue May 15, 2018 · 2 comments
Open

Bring back updateReturning #18

14mRh4X0r opened this issue May 15, 2018 · 2 comments

Comments

@14mRh4X0r
Copy link

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
@bendem
Copy link
Owner

bendem commented May 17, 2018

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.
    }
}

@14mRh4X0r
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants