Package org.fluentjdbc
Class DbContextSelectBuilder
- java.lang.Object
-
- org.fluentjdbc.DbContextSelectBuilder
-
- All Implemented Interfaces:
DatabaseQueryable<DbContextSelectBuilder>
,DbContextListableSelect<DbContextSelectBuilder>
public class DbContextSelectBuilder extends Object implements DbContextListableSelect<DbContextSelectBuilder>
GenerateSELECT
statements by collectingWHERE
expressions and parameters.Example:DbContextTable
table = context.table("database_test_table"); try (DbContextConnection ignored = context.startConnection(dataSource)) { List<Person> result = table .where("firstName", firstName) .whereExpression("lastName like ?", "joh%") .whereIn("status", statuses) .orderBy("lastName") .list(row -> new Person(row)); }- See Also:
DatabaseTableQueryBuilder
-
-
Constructor Summary
Constructors Constructor Description DbContextSelectBuilder(DbContextTable dbContextTable)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
executeDelete()
ExecutesDELETE FROM tableName WHERE ....
void
forEach(DatabaseResult.RowConsumer consumer)
Executes theSELECT * FROM ...
statement and calls back toDatabaseResult.RowConsumer
for each returned rowint
getCount()
ExecutesSELECT count(*) FROM ...
on the query and returns the result<T> List<T>
list(DatabaseResult.RowMapper<T> mapper)
Execute the query and map each return value over theDatabaseResult.RowMapper
function to return a list.DbContextSelectBuilder
orderBy(String orderByClause)
AddsORDER BY ...
clause to theSELECT
statementDbContextSelectBuilder
query()
Returns this.<T> Optional<T>
singleObject(DatabaseResult.RowMapper<T> mapper)
If the query returns no rows, returnsOptional.empty()
, if exactly one row is returned, maps it and return it, if more than one is returned, throws `IllegalStateException`Optional<String>
singleString(String fieldName)
Returns a string from the specified column nameDbContextSelectBuilder
skipAndLimit(int offset, int rowCount)
AddsOFFSET ...
<T> Stream<T>
stream(DatabaseResult.RowMapper<T> mapper)
Execute the query and map each return value over theDatabaseResult.RowMapper
function to return a stream.DbContextSelectBuilder
unordered()
If you haven't calledorderBy(java.lang.String)
, the results oflist(org.fluentjdbc.DatabaseResult.RowMapper<T>)
will be unpredictable.DbContextUpdateBuilder
update()
Creates aDbContextUpdateBuilder
object to fluently generate aUPDATE ...
statementDbContextSelectBuilder
whereExpressionWithParameterList(String expression, Collection<?> parameters)
Adds the expression to the WHERE-clause and all the values to the parameter list.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.fluentjdbc.DatabaseQueryable
where, whereAll, whereExpression, whereExpression, whereIn, whereOptional
-
Methods inherited from interface org.fluentjdbc.DbContextListableSelect
limit, listLongs, listStrings, singleInputStream, singleInstant, singleLong, singleReader
-
-
-
-
Constructor Detail
-
DbContextSelectBuilder
public DbContextSelectBuilder(DbContextTable dbContextTable)
-
-
Method Detail
-
query
public DbContextSelectBuilder query()
Returns this. Needed to makeDbContextSelectBuilder
interchangeable withDbContextTable
- Specified by:
query
in interfaceDatabaseQueryable<DbContextSelectBuilder>
-
whereExpressionWithParameterList
public DbContextSelectBuilder whereExpressionWithParameterList(String expression, Collection<?> parameters)
Adds the expression to the WHERE-clause and all the values to the parameter list. E.g.whereExpressionWithParameterList("created_at between ? and ?", List.of(earliestDate, latestDate))
- Specified by:
whereExpressionWithParameterList
in interfaceDatabaseQueryable<DbContextSelectBuilder>
-
orderBy
public DbContextSelectBuilder orderBy(String orderByClause)
AddsORDER BY ...
clause to theSELECT
statement- Specified by:
orderBy
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
unordered
@CheckReturnValue public DbContextSelectBuilder unordered()
If you haven't calledorderBy(java.lang.String)
, the results oflist(org.fluentjdbc.DatabaseResult.RowMapper<T>)
will be unpredictable. Callunordered()
if you are okay with this.
-
skipAndLimit
public DbContextSelectBuilder skipAndLimit(int offset, int rowCount)
AddsOFFSET ... ROWS FETCH ... ROWS ONLY
clause to theSELECT
statement. FETCH FIRST was introduced in SQL:2008 and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.- Specified by:
skipAndLimit
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
executeDelete
public int executeDelete()
ExecutesDELETE FROM tableName WHERE ....
-
stream
public <T> Stream<T> stream(DatabaseResult.RowMapper<T> mapper)
Execute the query and map each return value over theDatabaseResult.RowMapper
function to return a stream. Example:table.where("status", status).stream(row -> row.getInstant("created_at"))
- Specified by:
stream
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
list
public <T> List<T> list(DatabaseResult.RowMapper<T> mapper)
Execute the query and map each return value over theDatabaseResult.RowMapper
function to return a list. Example:List<Instant> creationTimes = table.where("status", status).list(row -> row.getInstant("created_at"))
- Specified by:
list
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
getCount
public int getCount()
ExecutesSELECT count(*) FROM ...
on the query and returns the result- Specified by:
getCount
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
singleObject
@Nonnull public <T> Optional<T> singleObject(DatabaseResult.RowMapper<T> mapper)
If the query returns no rows, returnsOptional.empty()
, if exactly one row is returned, maps it and return it, if more than one is returned, throws `IllegalStateException`- Specified by:
singleObject
in interfaceDbContextListableSelect<DbContextSelectBuilder>
- Parameters:
mapper
- Function object to map a single returned row to a object- Returns:
- the mapped row if one row is returned, Optional.empty otherwise
- Throws:
IllegalStateException
- if more than one row was matched the the query
-
singleString
@Nonnull public Optional<String> singleString(String fieldName)
Returns a string from the specified column name- Specified by:
singleString
in interfaceDbContextListableSelect<DbContextSelectBuilder>
- Returns:
- the mapped row if one row is returned, Optional.empty otherwise
- Throws:
IllegalStateException
- if more than one row was matched the the query
-
forEach
public void forEach(DatabaseResult.RowConsumer consumer)
Executes theSELECT * FROM ...
statement and calls back toDatabaseResult.RowConsumer
for each returned row- Specified by:
forEach
in interfaceDbContextListableSelect<DbContextSelectBuilder>
-
update
@CheckReturnValue public DbContextUpdateBuilder update()
Creates aDbContextUpdateBuilder
object to fluently generate aUPDATE ...
statement
-
-