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

com.digitolio.jdbi.AutoUpdatableByPKGenerator Maven / Gradle / Ivy

There is a newer version: 0.6.8.2
Show newest version
package com.digitolio.jdbi;

import com.digitolio.jdbi.table.Table;
import org.skife.jdbi.v2.Binding;
import org.skife.jdbi.v2.tweak.Argument;

import java.util.Map;

public class AutoUpdatableByPKGenerator implements SqlGenerator {

    private Table table;

    private String clause;


    public AutoUpdatableByPKGenerator(Table table) {
        this.table = table;
        initConstantSqls();
    }

    private void initConstantSqls() {
        clause = initAllClause();
    }


    private String initAllClause() {
        return initUpdatePart() + " %s " + initWherePart();
    }

    private String initUpdatePart() {
       return "UPDATE ".concat(table.getTableDb());
    }

    private String initWherePart() {
        Map primaryKeys = table.getPrimaryKeys();
        StringBuilder builder = new StringBuilder("WHERE ");
        for (Map.Entry entry : primaryKeys.entrySet()) {
            builder.append(entry.getValue()).append(" = :").append(entry.getKey()).append(" AND ");
        }
        return builder.substring(0,builder.length() - 5);
    }

    @Override
    public String generate(Binding params) {
        Map primaryKeys = table.getNonPrimaryKeys();
        StringBuilder builder = new StringBuilder("SET ");

        for (Map.Entry entry : primaryKeys.entrySet()) {
            String input = entry.getKey();
            Argument argument = params.forName(input);
            if(argument !=null && !"null".equals(argument.toString())){
                builder.append(entry.getValue()).append(" = :").append(input).append(" , ");
            }
        }
        String set = builder.substring(0, builder.length() - 2);
        return String.format(clause,set);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy