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

org.fluentjdbc.DbInsertContext Maven / Gradle / Ivy

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

import java.sql.SQLException;
import java.util.Collection;

import org.fluentjdbc.util.ExceptionUtil;

public class DbInsertContext implements DatabaseUpdateable {

    public class DbInsertContextWithPk {

        private DatabaseInsertWithPkBuilder builder2;

        public DbInsertContextWithPk(DatabaseInsertWithPkBuilder builder) {
            builder2 = builder;
        }

        public DbInsertContextWithPk setField(String fieldName, Object parameter) {
            builder2.setField(fieldName, parameter);
            return this;
        }

        public T execute() {
            try {
                return builder2.execute(dbTableContext.getConnection());
            } catch (SQLException e) {
                throw ExceptionUtil.softenCheckedException(e);
            }
        }
    }

    private DatabaseInsertBuilder builder;
    private DbTableContext dbTableContext;

    public DbInsertContext(DbTableContext dbTableContext) {
        this.dbTableContext = dbTableContext;
        builder = dbTableContext.getTable().insert();
    }

    public  DbInsertContextWithPk setPrimaryKey(String idField, T idValue) {
        DatabaseInsertWithPkBuilder setPrimaryKey = builder.setPrimaryKey(idField, idValue);
        return new DbInsertContextWithPk<>(setPrimaryKey);
    }

    public DbInsertContext setField(String fieldName, Object parameter) {
        builder.setField(fieldName, parameter);
        return this;
    }

    @Override
    public DbInsertContext setFields(Collection fields, Collection values) {
        builder.setFields(fields, values);
        return this;
    }

    public int execute() {
        return builder.execute(dbTableContext.getConnection());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy