Package org.ujorm.tools.sql
Class SqlParamBuilder
java.lang.Object
org.ujorm.tools.sql.SqlParamBuilder
- All Implemented Interfaces:
AutoCloseable
Very light class to simplify work with JDBC.
Original source: GitHub
Licence: Apache License, Version 2.0
Sample of usage
try (var builder = new SqlParamBuilder(dbConnection)) {
List<Employee> employees = builder.sql("""
SELECT t.id, t.name, t.created
FROM employee t
WHERE t.id > :id
AND t.code IN (:code)
ORDER BY t.id
""")
.bind("id", 10)
.bind("code", "T", "V")
.streamMap(rs -> new Employee(
rs.getInt("id"),
rs.getString("name"),
rs.getObject("created", LocalDate.class)))
.toList();
}
- Since:
- 2.26
- Author:
- Pavel Ponec, https://github.com/pponec
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbind(boolean enabled, @NotNull String key, BigDecimal... values) bind(boolean enabled, @NotNull String key, LocalDateTime... values) Bind BytesBind IngegersBind LongsBind ShortsBind Stringbind(@NotNull String key, BigDecimal... values) Bind BigDecimalBind LocalDatesbind(@NotNull String key, LocalDateTime... values) Bind LocalDateTimesbindObject(boolean enabled, @NotNull String key, JDBCType jdbcType, Object... values) Assigns SQL parameter values.bindObject(@NotNull String key, Object... values) Bind Objectsprotected @NotNull Stringvoidclose()The method closes a PreparedStatement object with related objects, not the database connection.intexecute()intFor INSERT operations used before calling method#generatedKeysRs.voidforEach(@NotNull SqlConsumer consumer) Iterate executed select<R> @NotNull Stream<R>generatedKeys(SqlFunction<ResultSet, ? extends R> mapper) Method for retrieving the primary keys of an INSERT statement.protected @Nullable ResultSet@NotNull PreparedStatementprepareStatement(int autoGeneratedKeys) Build (or reuse) a PreparedStatement object with SQL argumentsClose an old statement (if any) and assign the new SQL templateprotected static SQLExceptionsqlException(@NotNull String messages, @Nullable Exception ex) protected static SQLExceptionsqlException(@Nullable SQLException ex) @NotNull String<R> @NotNull Stream<R>streamMap(SqlFunction<ResultSet, ? extends R> mapper) @NotNull StringtoString()
-
Field Details
-
sqlTemplate
-
-
Constructor Details
-
SqlParamBuilder
-
-
Method Details
-
sql
Close an old statement (if any) and assign the new SQL template -
bind
-
bind
-
bind
Bind Bytes -
bind
-
bind
Bind Shorts -
bind
-
bind
Bind Ingegers -
bind
-
bind
Bind Longs -
bind
-
bind
Bind BigDecimal -
bind
-
bind
Bind String -
bind
-
bind
Bind LocalDates -
bind
-
bind
Bind LocalDateTimes -
bind
-
bindObject
Bind Objects -
bindObject
public SqlParamBuilder bindObject(boolean enabled, @NotNull @NotNull String key, JDBCType jdbcType, Object... values) Assigns SQL parameter values. If reusing a statement, ensure the same number of parameters is set. -
execute
public int execute() -
executeInsert
public int executeInsert()For INSERT operations used before calling method#generatedKeysRs. -
forEach
Iterate executed select- Throws:
SQLException
-
streamMap
-
close
public void close()The method closes a PreparedStatement object with related objects, not the database connection.- Specified by:
closein interfaceAutoCloseable
-
prepareStatement
Build (or reuse) a PreparedStatement object with SQL arguments- Parameters:
autoGeneratedKeys- For example:Statement.RETURN_GENERATED_KEYS
-
generatedKeysRs
-
generatedKeys
Method for retrieving the primary keys of an INSERT statement. Only one call per INSERT is allowed.
Usage:builder.generatedKeys(rs -> rs.getInt(1)).findFirst() -
buildSql
-
sqlTemplate
-
sqlException
-
sqlException
protected static SQLException sqlException(@NotNull @NotNull String messages, @Nullable @Nullable Exception ex) -
toString
-
toStringLine
-