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

org.fluentjdbc.DatabaseInsertBuilder Maven / Gradle / Ivy

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

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class DatabaseInsertBuilder extends DatabaseStatement implements DatabaseUpdateable {

    private List fieldNames = new ArrayList<>();
    private List parameters = new ArrayList<>();
    private String table;

    public DatabaseInsertBuilder(String table) {
        this.table = table;
    }

    List getParameters() {
        return parameters;
    }

    @Override
    public DatabaseInsertBuilder setField(String fieldName, @Nullable Object parameter) {
        this.fieldNames.add(fieldName);
        this.parameters.add(parameter);
        return this;
    }

    @Override
    public DatabaseInsertBuilder setFields(Collection fieldNames, Collection parameters) {
        this.fieldNames.addAll(fieldNames);
        this.parameters.addAll(parameters);
        return this;
    }

    public int execute(Connection connection) {
        return executeUpdate(createInsertStatement(), parameters, connection);
    }

    String createInsertStatement() {
        return createInsertSql(table, fieldNames);
    }

    // TODO: This doesn't work for Android when idValue is null
    public  DatabaseInsertWithPkBuilder setPrimaryKey(String idField, @Nullable T idValue) {
        if (idValue != null) {
            setField(idField, idValue);
        }
        return new DatabaseInsertWithPkBuilder<>(this, idValue);
    }

}