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

org.fluentjdbc.DbTableContext Maven / Gradle / Ivy

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

import javax.annotation.Nullable;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DbTableContext implements DatabaseQueriable {

    private DatabaseTable table;
    private DbContext dbContext;

    public DbTableContext(DatabaseTable databaseTable, DbContext dbContext) {
        this.table = databaseTable;
        this.dbContext = dbContext;
    }

    public DbInsertContext insert() {
        return new DbInsertContext(this);
    }

    public DatabaseTable getTable() {
        return table;
    }

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

    public DbSelectContext whereOptional(String fieldName, Object value) {
        return new DbSelectContext(this).whereOptional(fieldName, value);
    }

    public DbSelectContext whereExpression(String expression) {
        return new DbSelectContext(this).whereExpression(expression);
    }

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

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

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

    public DbSelectContext unordered() {
        return new DbSelectContext(this);
    }

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

    public Connection getConnection() {
        return dbContext.getThreadConnection();
    }

    public DbSaveBuilderContext newSaveBuilder(String idColumn, Long idValue) {
        return save(table.newSaveBuilder(idColumn, idValue));
    }

    public DbSaveBuilderContext newSaveBuilderWithUUID(String field, UUID uuid) {
        return save(table.newSaveBuilderWithUUID(field, uuid));
    }

    public  DbSaveBuilderContext save(DatabaseSaveBuilder saveBuilder) {
        return new DbSaveBuilderContext<>(this, saveBuilder);
    }

    public  Optional cache(KEY key, RetrieveMethod retriever) {
        return DbContext.cache(getTable().getTableName(), key, retriever);
    }

    public DbTableAliasContext alias(String alias) {
        return new DbTableAliasContext(this, alias);
    }

    public DbContext getDbContext() {
        return dbContext;
    }

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

    public  DbSyncBuilderContext synch(List entities) {
        return new DbSyncBuilderContext<>(this, entities);
    }

    public  DbBulkInsertContext bulkInsert(Stream objects) {
        return bulkInsert(objects.collect(Collectors.toList()));
    }

    public  DbBulkInsertContext bulkInsert(Iterable objects) {
        return new DbBulkInsertContext<>(this, table.bulkInsert(objects));
    }

    public  DbBulkDeleteContext bulkDelete(Stream objects) {
        return bulkDelete(objects.collect(Collectors.toList()));
    }

    public  DbBulkDeleteContext bulkDelete(Iterable objects) {
        return new DbBulkDeleteContext<>(this, table.bulkDelete(objects));
    }

    public  DbBulkUpdateContext bulkUpdate(Stream objects) {
        return bulkUpdate(objects.collect(Collectors.toList()));
    }

    private  DbBulkUpdateContext bulkUpdate(List objects) {
        return new DbBulkUpdateContext<>(this, table.bulkUpdate(objects));
    }
}