public class JdbcBuilder extends Object implements Serializable
JdbcBuilder sql = new JdbcBuilder()
.write("SELECT")
.column("t.id")
.column("t.name")
.write("FROM testTable t WHERE")
.andCondition("t.name", "=", "Test")
.andCondition("t.created", ">=", someDate);
for (ResultSet rs : sql.executeSelect(dbConnection)) {
int id = rs.getInt(1);
String name = rs.getString(2);
}
JdbcBuilder sql = new JdbcBuilder()
.write("INSERT INTO testTable (")
.columnInsert("id", 10)
.columnInsert("name", "Test")
.columnInsert("date", someDate)
.write(")");
sql.executeUpdate(dbConnection);
JdbcBuilder sql = new JdbcBuilder()
.write("UPDATE testTable SET")
.columnUpdate("name", "Test")
.columnUpdate("date", SOME_DATE)
.write("WHERE")
.andCondition("id", "IN", 10, 20, 30)
.andCondition("created BETWEEN ? AND ?", null, someDate, someDate.plusMonths(1))
.andCondition("name", "IS NOT NULL")
sql.executeUpdate(dbConnection);
For more information see a jUnit test.| Modifier and Type | Class and Description |
|---|---|
protected static class |
JdbcBuilder.MarkerEnvelope
A value marker envelope
|
protected static class |
JdbcBuilder.SqlEnvelope
A SQL fragment
|
| Modifier and Type | Field and Description |
|---|---|
protected @NotNull List<Object> |
arguments
Argument list
|
protected int |
columnCounter
Column counter
|
protected int |
conditionCounter
Condition counter
|
protected boolean |
insertMode
An insert sign for different rendering
|
static JdbcBuilder.SqlEnvelope |
ITEM_SEPARATOR
Separator of database columns
|
protected static char |
SPACE
A value marker for SQL
|
protected @NotNull List<CharSequence> |
sql
SQL string fragments
|
protected static String |
VALUE_MARKER
A value marker for SQL
|
| Constructor and Description |
|---|
JdbcBuilder()
Default constructor
|
JdbcBuilder(@NotNull List<CharSequence> sql,
@NotNull List<Object> arguments)
Default constructor
|
| Modifier and Type | Method and Description |
|---|---|
@NotNull JdbcBuilder |
addArguments(Object... values)
Add argument values with no SAPARATOR and no MARKER (for a common use)
|
protected @NotNull JdbcBuilder |
addValue(@Nullable Object value)
Add a value to SQL (inlucing MARKER)
|
@NotNull JdbcBuilder |
andCondition(@NotNull CharSequence sqlCondition,
@NotNull String operator,
Object... values)
Add a condition for a multivalue argument joined by AND operator
|
@NotNull JdbcBuilder |
andCondition(@NotNull CharSequence sqlCondition,
@Nullable String operator,
@Nullable Object value)
Add a condition for a valid argument joined by AND operator
|
@NotNull JdbcBuilder |
column(@NotNull CharSequence column)
Add new column
|
@NotNull JdbcBuilder |
columnInsert(@NotNull CharSequence column,
@NotNull Object value)
Set new value to column by template {@code name = ?
|
@NotNull JdbcBuilder |
columnUpdate(@NotNull CharSequence column,
@NotNull Object value)
Set new value to column by template {@code name = ?
|
@NotNull JdbcBuilder |
condition(@Nullable CharSequence sqlCondition,
@Nullable String operator,
@NotNull Object value)
Add a condition for an argument with length
|
protected static @NotNull ValuePrinter |
createValuePrinter(@NotNull Appendable result)
Create a value printer
|
@NotNull LoopingIterator<ResultSet> |
executeSelect(@NotNull Connection connection)
Create an iterator ready to a loop statement
for ( ; ; )
Supported SQL statements are: INSERT, UPDATE, DELETE . |
<T> @NotNull List<T> |
executeSelect(@NotNull Connection connection,
JdbcFunction<T> function)
Create a new result list
|
int |
executeUpdate(@NotNull Connection connection)
Create statement and call
PreparedStatement.executeUpdate() . |
@NotNull Object[] |
getArguments()
Returns an array of all JDBC arguments
|
protected int |
getBufferSizeEstimation(boolean preview)
Estimate a buffer size in characters
|
@NotNull String |
getSql()
Returns a SQL statement
|
@NotNull String |
getSql(boolean preview)
Returns a SQL text
|
@NotNull JdbcBuilder |
orCondition(@NotNull CharSequence sqlCondition,
@NotNull String operator,
Object... values)
Add a condition for a multivalue argument joined by OR operator
|
@NotNull JdbcBuilder |
orCondition(@NotNull CharSequence sqlCondition,
@Nullable String operator,
@Nullable Object value)
Add a condition for a valid argument joined by OR operator
|
@NotNull PreparedStatement |
prepareStatement(@NotNull Connection connection)
Build the PreparedStatement with arguments
|
@NotNull String |
toString()
Returns a SQL preview including values
|
<T> T |
uniqueValue(@NotNull Class<T> resultType,
@NotNull Connection connection)
Return the first column value of a unique resultset, else returns
null value |
<T> @NotNull Optional<T> |
uniqueValueOptional(@NotNull Class<T> resultType,
@NotNull Connection connection)
Return the first column value of a unique resultset
|
@NotNull JdbcBuilder |
value(@NotNull Object value)
Add an argument value (including a SEPARATOR and a MARKER) for buidling a SQL INSERT statement
|
@NotNull JdbcBuilder |
write(@Nullable CharSequence sqlFragment)
Write a sql fragment including a space before
|
@NotNull JdbcBuilder |
write(@NotNull JdbcBuilder builder)
Add a another statement to the end of this statement.
|
@NotNull JdbcBuilder |
writeMany(CharSequence... sqlFragments)
Write many sql fragments including a space before
|
@NotNull JdbcBuilder |
writeManyNoSpace(CharSequence... sqlFragments)
Write many sql fragments with no space before
|
@NotNull JdbcBuilder |
writeNoSpace(@NotNull CharSequence sqlFragment)
Write a sql fragment with no space before
|
protected void |
writeOperator(@Nullable boolean andOperator,
boolean enabled)
Write an opetaror AND / OR
|
public static final JdbcBuilder.SqlEnvelope ITEM_SEPARATOR
protected static final String VALUE_MARKER
protected static final char SPACE
@NotNull protected final @NotNull List<CharSequence> sql
protected int conditionCounter
protected int columnCounter
protected boolean insertMode
public JdbcBuilder()
public JdbcBuilder(@NotNull
@NotNull List<CharSequence> sql,
@NotNull
@NotNull List<Object> arguments)
@NotNull public @NotNull JdbcBuilder write(@NotNull @NotNull JdbcBuilder builder)
@NotNull public @NotNull JdbcBuilder write(@Nullable @Nullable CharSequence sqlFragment)
sqlFragment - An empty or null value is ignored.@NotNull public @NotNull JdbcBuilder writeNoSpace(@NotNull @NotNull CharSequence sqlFragment)
sqlFragment - An empty or null fragment is ignored.@NotNull public @NotNull JdbcBuilder writeMany(@NotNull CharSequence... sqlFragments)
@NotNull public @NotNull JdbcBuilder writeManyNoSpace(@NotNull CharSequence... sqlFragments)
@NotNull public @NotNull JdbcBuilder column(@NotNull @NotNull CharSequence column)
@NotNull public @NotNull JdbcBuilder columnUpdate(@NotNull @NotNull CharSequence column, @NotNull @NotNull Object value)
@NotNull public @NotNull JdbcBuilder columnInsert(@NotNull @NotNull CharSequence column, @NotNull @NotNull Object value)
@NotNull public @NotNull JdbcBuilder andCondition(@NotNull @NotNull CharSequence sqlCondition, @Nullable @Nullable String operator, @Nullable @Nullable Object value)
sqlCondition - A condition in the SQL format like the next: "table.id = ?"operator - An optional operator is followed by the VALUE_MARKER automaticallyvalue - Add the value to arguments including a markup to the SQL statement. To ignore the value, send a null.@NotNull public @NotNull JdbcBuilder andCondition(@NotNull @NotNull CharSequence sqlCondition, @NotNull @NotNull String operator, @Nullable Object... values)
sqlCondition - A condition in the SQL format like the next: "table.id = ?"operator - An optional operator is followed by the VALUE_MARKER automaticallyvalues - The value of the condition (a replacement for the question character)@NotNull public @NotNull JdbcBuilder orCondition(@NotNull @NotNull CharSequence sqlCondition, @Nullable @Nullable String operator, @Nullable @Nullable Object value)
sqlCondition - A condition in the SQL format like the next: "table.id = ?"operator - An optional operator is followed by the VALUE_MARKER automaticallyvalue - Add the value to arguments including a markup to the SQL statement. To ignore the value, send a null.@NotNull public @NotNull JdbcBuilder orCondition(@NotNull @NotNull CharSequence sqlCondition, @NotNull @NotNull String operator, @Nullable Object... values)
sqlCondition - A condition in the SQL format like the next: "table.id = ?"operator - An optional operator is followed by the VALUE_MARKER automaticallyvalues - The value of the condition (a replacement for the question character)@NotNull public @NotNull JdbcBuilder condition(@Nullable @Nullable CharSequence sqlCondition, @Nullable @Nullable String operator, @NotNull @NotNull Object value)
sqlCondition - A condition in the SQL format like the next: "table.id = ?". Send a null value to ignore the method.operator - An optional operator is followed by the VALUE_MARKER automaticallyvalue - Add a value to arguments including a markup to the SQL statement. To ignore the value, send a null. An array is supportedprotected void writeOperator(@Nullable
@Nullable boolean andOperator,
boolean enabled)
andOperator - enabled - @NotNull public @NotNull JdbcBuilder value(@NotNull @NotNull Object value)
addArguments(java.lang.Object...)@NotNull protected @NotNull JdbcBuilder addValue(@Nullable @Nullable Object value)
value - A null value is ignoredaddArguments(java.lang.Object...)@NotNull public @NotNull JdbcBuilder addArguments(@NotNull Object... values)
@NotNull public @NotNull Object[] getArguments()
@NotNull public @NotNull PreparedStatement prepareStatement(@NotNull @NotNull Connection connection) throws SQLException
SQLException@NotNull public @NotNull LoopingIterator<ResultSet> executeSelect(@NotNull @NotNull Connection connection) throws IllegalStateException, SQLException
for ( ; ; )
Supported SQL statements are: INSERT, UPDATE, DELETE .IllegalStateExceptionSQLException@NotNull public <T> @NotNull List<T> executeSelect(@NotNull @NotNull Connection connection, JdbcFunction<T> function) throws SQLException
SQLExceptionpublic int executeUpdate(@NotNull
@NotNull Connection connection)
throws IllegalStateException
PreparedStatement.executeUpdate() .
Supported SQL statements are: INSERT, UPDATE, DELETE .IllegalStateException@Nullable
public <T> T uniqueValue(@NotNull
@NotNull Class<T> resultType,
@NotNull
@NotNull Connection connection)
null value@NotNull public <T> @NotNull Optional<T> uniqueValueOptional(@NotNull @NotNull Class<T> resultType, @NotNull @NotNull Connection connection)
@NotNull public @NotNull String getSql(boolean preview)
@NotNull protected static @NotNull ValuePrinter createValuePrinter(@NotNull @NotNull Appendable result)
protected int getBufferSizeEstimation(boolean preview)
@NotNull public @NotNull String getSql()
Copyright 2015, Pavel Ponec