All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentjdbc.DbContextStatement Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
package org.fluentjdbc;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Used to execute an arbitrary statement. Create with {@link DbContext#statement(String, List)}.
 *
 * 

Example

* *
 * dbContext.statement(
 *      "update orders set quantity = quantity + 1 WHERE customer_id = ?",
 *      Arrays.asList(customerId)
 * ).executeUpdate();
 * 
*/ public class DbContextStatement { private final DbContext dbContext; private final DatabaseStatement statement; public DbContextStatement(DbContext dbContext, String statement, List parameters) { this.dbContext = dbContext; this.statement = dbContext.getStatementFactory().newStatement("*", "*", statement, parameters); } /** * 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 @CheckReturnValue public Optional singleObject(DatabaseResult.RowMapper mapper) { return statement.singleObject(dbContext.getThreadConnection(), mapper); } /** * 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"))
     * 
*/ @CheckReturnValue public Stream stream(DatabaseResult.RowMapper mapper) { return statement.stream(dbContext.getThreadConnection(), 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"))
     * 
*/ @CheckReturnValue public List list(DatabaseResult.RowMapper mapper) { return stream(mapper).collect(Collectors.toList()); } /** * Calls {@link Connection#prepareStatement(String)} with the statement, * {@link DatabaseStatement#bindParameters(PreparedStatement, List)}, converting each parameter in the process * and executes the statement */ public int executeUpdate(Connection connection) { return statement.executeUpdate(connection); } }