
buckelieg.simpletools.db.AbstractQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db-tools Show documentation
Show all versions of db-tools Show documentation
Simple-tools project for database: functional style programming with plain JDBC
package buckelieg.simpletools.db;
import javax.annotation.Nonnull;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;
abstract class AbstractQuery implements Query {
final S statement;
AbstractQuery(S statement) {
this.statement = Objects.requireNonNull(statement, "Statement must not be null");
}
@Nonnull
@Override
public final > Q timeout(int timeout) {
return jdbcTry(() -> statement.setQueryTimeout(timeout > 0 ? timeout : 0));
}
final void close() {
jdbcTry(statement::close); // by JDBC spec: subsequently closes all result sets opened by this statement
}
final O jdbcTry(Try action) {
O result = null;
try {
result = action.doTry();
} catch (SQLException e) {
throw new SQLRuntimeException(e);
} catch (AbstractMethodError ame) {
// ignore this possible vendor-specific JDBC driver's error.
}
return result;
}
@SuppressWarnings("unchecked")
final > Q jdbcTry(Try.Consume action) {
return jdbcTry(() -> {
action.doTry();
return (Q) this;
});
}
final PreparedStatement setParameters(PreparedStatement ps, Object... params) throws SQLException {
Objects.requireNonNull(params, "Parameters must be provided");
int pNum = 0;
for (Object p : params) {
ps.setObject(++pNum, p); // TODO introduce type conversion here...
}
return ps;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy