org.fluentjdbc.DbContextSelectBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-jdbc Show documentation
Show all versions of fluent-jdbc Show documentation
A Java library used to execute JDBC statements and build SQL
package org.fluentjdbc;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Generate SELECT
statements by collecting WHERE
expressions and parameters.Example:
*
*
* {@link 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 org.fluentjdbc.DatabaseTableQueryBuilder
*/
public class DbContextSelectBuilder implements DbContextListableSelect {
private final DbContextTable dbContextTable;
private final DatabaseTableQueryBuilder queryBuilder;
public DbContextSelectBuilder(DbContextTable dbContextTable) {
this.dbContextTable = dbContextTable;
queryBuilder = new DatabaseTableQueryBuilder(dbContextTable.getTable());
}
/**
* Returns this. Needed to make {@link DbContextSelectBuilder} interchangeable with {@link DbContextTable}
*/
@Override
public DbContextSelectBuilder query() {
return this;
}
@CheckReturnValue
private DbContextSelectBuilder query(DatabaseTableQueryBuilder builder) {
return this;
}
/**
* 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))
*/
@Override
public DbContextSelectBuilder whereExpressionWithParameterList(String expression, Collection> parameters) {
return query(queryBuilder.whereExpressionWithParameterList(expression, parameters));
}
/**
* Adds ORDER BY ...
clause to the SELECT
statement
*/
@Override
public DbContextSelectBuilder orderBy(String orderByClause) {
return query(queryBuilder.orderBy(orderByClause));
}
/**
* If you haven't called {@link #orderBy}, the results of {@link #list}
* will be unpredictable. Call unordered()
if you are okay with this.
*/
@CheckReturnValue
public DbContextSelectBuilder unordered() {
return query(queryBuilder.unordered());
}
/**
* Adds OFFSET ... ROWS FETCH ... ROWS ONLY
clause to the SELECT
* 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.
*/
@Override
public DbContextSelectBuilder skipAndLimit(int offset, int rowCount) {
return query(queryBuilder.skipAndLimit(offset, rowCount));
}
/**
* Executes DELETE FROM tableName WHERE ....
*/
public int executeDelete() {
return queryBuilder.delete(getConnection());
}
/**
* Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a stream. Example:
*
* table.where("status", status).stream(row -> row.getInstant("created_at"))
*
*/
@Override
public Stream stream(DatabaseResult.RowMapper mapper) {
return queryBuilder.stream(getConnection(), mapper);
}
/**
* Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a list. Example:
*
* List<Instant> creationTimes = table.where("status", status).list(row -> row.getInstant("created_at"))
*
*/
@Override
public List list(DatabaseResult.RowMapper mapper) {
return queryBuilder.list(getConnection(), mapper);
}
/**
* Executes SELECT count(*) FROM ...
on the query and returns the result
*/
@Override
public int getCount() {
return queryBuilder.getCount(getConnection());
}
/**
* If the query returns no rows, returns {@link Optional#empty()}, if exactly one row is returned, maps it and return it,
* if more than one is returned, throws `IllegalStateException`
*
* @param mapper Function object to map a single returned row to a object
* @return the mapped row if one row is returned, Optional.empty otherwise
* @throws IllegalStateException if more than one row was matched the the query
*/
@Nonnull
@Override
public Optional singleObject(DatabaseResult.RowMapper mapper) {
return queryBuilder.singleObject(getConnection(), mapper);
}
/**
* Returns a string from the specified column name
*
* @return the mapped row if one row is returned, Optional.empty otherwise
* @throws IllegalStateException if more than one row was matched the the query
*/
@Nonnull
@Override
public Optional singleString(String fieldName) {
return queryBuilder.singleString(getConnection(), fieldName);
}
/**
* Executes the SELECT * FROM ...
statement and calls back to
* {@link DatabaseResult.RowConsumer} for each returned row
*/
@Override
public void forEach(DatabaseResult.RowConsumer consumer) {
queryBuilder.forEach(getConnection(), consumer);
}
/**
* Creates a {@link DbContextUpdateBuilder} object to fluently generate a UPDATE ...
statement
*/
@CheckReturnValue
public DbContextUpdateBuilder update() {
return new DbContextUpdateBuilder(this.dbContextTable, queryBuilder.update());
}
@CheckReturnValue
private Connection getConnection() {
return dbContextTable.getConnection();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy