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

org.fluentjdbc.DbSelectContext Maven / Gradle / Ivy

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

import org.fluentjdbc.DatabaseTable.RowMapper;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.sql.Connection;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class DbSelectContext implements DbListableSelectContext {

    private DbTableContext dbTableContext;
    private DatabaseTableQueryBuilder queryBuilder;

    public DbSelectContext(DbTableContext dbTableContext) {
        this.dbTableContext = dbTableContext;
        queryBuilder = new DatabaseTableQueryBuilder(dbTableContext.getTable());
    }

    @Override
    public DbSelectContext query() {
        return this;
    }

    @Override
    public DbSelectContext whereOptional(String fieldName, @Nullable Object value) {
        queryBuilder.whereOptional(fieldName, value);
        return this;
    }

    @Override
    public DbSelectContext whereExpression(String expression) {
        queryBuilder.whereExpression(expression);
        return this;
    }

    @Override
    public DbSelectContext whereExpression(String expression, Object value) {
        queryBuilder.whereExpression(expression, value);
        return this;
    }

    @Override
    public DbSelectContext whereIn(String fieldName, Collection parameters) {
        queryBuilder.whereIn(fieldName, parameters);
        return this;
    }

    @Override
    public DbSelectContext whereExpressionWithMultipleParameters(String expression, Collection parameters) {
        queryBuilder.whereExpressionWithMultipleParameters(expression, parameters);
        return this;
    }

    @Override
    public DbSelectContext whereAll(List fields, List values) {
        queryBuilder.whereAll(fields, values);
        return this;
    }

    public DbSelectContext orderBy(String orderByClause) {
        queryBuilder.orderBy(orderByClause);
        return this;
    }

    public DbSelectContext unordered() {
        queryBuilder.unordered();
        return this;
    }

    private Connection getConnection() {
        return dbTableContext.getConnection();
    }

    public int executeDelete() {
        return queryBuilder.delete(getConnection());
    }

    public  Stream stream(RowMapper mapper) {
        return queryBuilder.stream(getConnection(), mapper);
    }

    @Override
    public  List list(RowMapper mapper) {
        return queryBuilder.list(getConnection(), mapper);
    }

    @Override
    public int getCount() {
        return queryBuilder.getCount(getConnection());
    }

    @Nonnull
    @Override
    public  Optional singleObject(RowMapper mapper) {
        return queryBuilder.singleObject(getConnection(), mapper);
    }

    @Nonnull
    @Override
    public Optional singleString(String fieldName) {
        return queryBuilder.singleString(getConnection(), fieldName);
    }

    @Nonnull
    @Override
    public Optional singleLong(String fieldName) {
        return queryBuilder.singleLong(getConnection(), fieldName);
    }

    @Nonnull
    @Override
    public Optional singleInstant(String fieldName) {
        return queryBuilder.singleInstant(getConnection(), fieldName);
    }

    @Override
    public void forEach(DatabaseTable.RowConsumer row) {
        queryBuilder.forEach(getConnection(), row);
    }

    public DbContextUpdateBuilder update() {
        return new DbContextUpdateBuilder(this.dbTableContext, queryBuilder.update());
    }

}