public class SqlBuilder extends JdbcBuilder
SqlBuilder sql = new SqlBuilder() .select("t.id", "t.name") .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); }
SqlBuilder sql = new SqlBuilder() .insert("testTable") .write("(") .columnInsert("id", 10) .columnInsert("name", "Test") .columnInsert("date", someDate) .super.write(")"); sql.executeUpdate(dbConnection);
SqlBuilder sql = new SqlBuilder() .update("testTable") .columnUpdate("name", "Test") .columnUpdate("date", SOME_DATE) .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.
JdbcBuilder.MarkerEnvelope, JdbcBuilder.SqlEnvelope
arguments, columnCounter, conditionCounter, insertMode, ITEM_SEPARATOR, SPACE, sql, VALUE_MARKER
Constructor and Description |
---|
SqlBuilder() |
SqlBuilder(List<CharSequence> sql,
List<Object> arguments) |
Modifier and Type | Method and Description |
---|---|
SqlBuilder |
addArguments(Object... values)
Add argument values with no SAPARATOR and no MARKER (for a common use)
|
protected SqlBuilder |
addValue(Object value)
Add a value to SQL (inlucing MARKER)
|
SqlBuilder |
andCondition(CharSequence sqlCondition,
String operator,
Object... values)
Add a condition for a multivalue argument joined by AND operator
|
SqlBuilder |
andCondition(CharSequence sqlCondition,
String operator,
Object value)
Add a condition for a valid argument joined by AND operator
|
SqlBuilder |
column(CharSequence column)
Add new column
|
SqlBuilder |
columnInsert(CharSequence column,
Object value)
Set new value to column by template {@code name = ?
|
SqlBuilder |
columnUpdate(CharSequence column,
Object value)
Set new value to column by template {@code name = ?
|
SqlBuilder |
condition(CharSequence sqlCondition,
String operator,
Object value)
Add a condition for an argument with length
|
SqlBuilder |
delete(CharSequence table)
Write WHERE
|
SqlBuilder |
from(CharSequence... tables)
Write FORM tables
|
SqlBuilder |
insert(CharSequence table)
Write WHERE
|
SqlBuilder |
orCondition(CharSequence sqlCondition,
String operator,
Object... values)
Add a condition for a multivalue argument joined by OR operator
|
SqlBuilder |
orCondition(CharSequence sqlCondition,
String operator,
Object value)
Add a condition for a valid argument joined by OR operator
|
SqlBuilder |
select(CharSequence... columns)
Write SELECT columns
|
SqlBuilder |
update(CharSequence table)
Write WHERE
|
SqlBuilder |
value(Object value)
Add an argument value (including a SEPARATOR and a MARKER) for buidling a SQL INSERT statement
|
SqlBuilder |
where()
Write WHERE
|
SqlBuilder |
write(CharSequence sqlFragment)
Write a sql fragment including a space before
|
SqlBuilder |
write(SqlBuilder builder) |
SqlBuilder |
writeMany(CharSequence... sqlFragments)
Write many sql fragments including a space before
|
SqlBuilder |
writeManyNoSpace(CharSequence... sqlFragments)
Write many sql fragments with no space before
|
SqlBuilder |
writeNoSpace(CharSequence sqlFragment)
Write a sql fragment with no space before
|
createValuePrinter, executeSelect, executeSelect, executeUpdate, getArguments, getBufferSizeEstimation, getSql, getSql, prepareStatement, toString, uniqueValue, uniqueValueOptional, write, writeOperator
public SqlBuilder()
public SqlBuilder(List<CharSequence> sql, List<Object> arguments)
public SqlBuilder select(@Nonnull CharSequence... columns)
public SqlBuilder from(@Nonnull CharSequence... tables)
public SqlBuilder where()
public SqlBuilder insert(@Nonnull CharSequence table)
public SqlBuilder update(@Nonnull CharSequence table)
public SqlBuilder delete(@Nonnull CharSequence table)
public SqlBuilder addArguments(Object... values)
JdbcBuilder
addArguments
in class JdbcBuilder
JdbcBuilder.value(java.lang.Object)
,
JdbcBuilder.writeNoSpace(java.lang.CharSequence)
protected SqlBuilder addValue(Object value)
JdbcBuilder
addValue
in class JdbcBuilder
value
- A null
value is ignoredJdbcBuilder.addArguments(java.lang.Object...)
public SqlBuilder value(Object value)
JdbcBuilder
value
in class JdbcBuilder
JdbcBuilder.addArguments(java.lang.Object...)
public SqlBuilder condition(CharSequence sqlCondition, String operator, Object value)
JdbcBuilder
condition
in class JdbcBuilder
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 JdbcBuilder.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 supportedpublic SqlBuilder orCondition(CharSequence sqlCondition, String operator, Object... values)
JdbcBuilder
orCondition
in class JdbcBuilder
sqlCondition
- A condition in the SQL format like the next: "table.id = ?"
operator
- An optional operator is followed by the JdbcBuilder.VALUE_MARKER
automaticallyvalues
- The value of the condition (a replacement for the question character)public SqlBuilder orCondition(CharSequence sqlCondition, String operator, Object value)
JdbcBuilder
orCondition
in class JdbcBuilder
sqlCondition
- A condition in the SQL format like the next: "table.id = ?"
operator
- An optional operator is followed by the JdbcBuilder.VALUE_MARKER
automaticallyvalue
- Add the value to arguments including a markup to the SQL statement. To ignore the value, send a null
.public SqlBuilder andCondition(CharSequence sqlCondition, String operator, Object... values)
JdbcBuilder
andCondition
in class JdbcBuilder
sqlCondition
- A condition in the SQL format like the next: "table.id = ?"
operator
- An optional operator is followed by the JdbcBuilder.VALUE_MARKER
automaticallyvalues
- The value of the condition (a replacement for the question character)public SqlBuilder andCondition(CharSequence sqlCondition, String operator, Object value)
JdbcBuilder
andCondition
in class JdbcBuilder
sqlCondition
- A condition in the SQL format like the next: "table.id = ?"
operator
- An optional operator is followed by the JdbcBuilder.VALUE_MARKER
automaticallyvalue
- Add the value to arguments including a markup to the SQL statement. To ignore the value, send a null
.public SqlBuilder columnInsert(CharSequence column, Object value)
JdbcBuilder
columnInsert
in class JdbcBuilder
public SqlBuilder columnUpdate(CharSequence column, Object value)
JdbcBuilder
columnUpdate
in class JdbcBuilder
public final SqlBuilder column(CharSequence column)
JdbcBuilder
column
in class JdbcBuilder
public final SqlBuilder writeManyNoSpace(CharSequence... sqlFragments)
JdbcBuilder
writeManyNoSpace
in class JdbcBuilder
public final SqlBuilder writeMany(CharSequence... sqlFragments)
JdbcBuilder
writeMany
in class JdbcBuilder
public final SqlBuilder writeNoSpace(CharSequence sqlFragment)
JdbcBuilder
writeNoSpace
in class JdbcBuilder
sqlFragment
- An empty or null fragment is ignored.public final SqlBuilder write(CharSequence sqlFragment)
JdbcBuilder
write
in class JdbcBuilder
sqlFragment
- An empty or null
value is ignored.public final SqlBuilder write(SqlBuilder builder)
Copyright 2019-2022, Pavel Ponec