package me.mrten.mysqlapi.queries;

import me.mrten.mysqlapi.utils.QueryUtils;

import java.util.LinkedHashMap;

public class InsertQuery {

    private String table;
    private LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();

    /**
     * Create an insert query.
     *
     * @param table - the table to be updated
     */
    public InsertQuery(String table) {
        this.table = table;
    }

    /**
     * Set a column to insert to and the value to be inserted.
     * @param column - the column to insert to
     * @param value - the value to be inserted
     * @return the InsertQuery object
     */
    public InsertQuery value(String column, String value) {
        values.put(column, value);
        return this;
    }

    /**
     * Set a column to insert to. Automatically sets the value to ? to be used with prepared statements.
     * @param column - the column to insert to
     * @return the InsertQuery object
     */
    public InsertQuery value(String column) {
        value(column, "?");
        return this;
    }

    /**
     * Build the query as a String.
     * @return the query as a String
     */
    public String build() {
        StringBuilder builder = new StringBuilder();
        builder.append("INSERT INTO ")
                .append(table)
                .append(" (")
                .append(QueryUtils.separate(values.keySet(), ","))
                .append(")")
                .append(" VALUES (")
                .append(QueryUtils.separate(values.values(), ","))
                .append(")");

        return builder.toString();
    }

}

@@ -109,16 +109,39 @@
 ashMap;%0D
+%0Aimport java.util.Map;%0D
 %0A%0D%0Apubli
@@ -280,16 +280,159 @@
 ing%3E();%0D
+%0A    private LinkedHashMap%3CString, String%3E duplicateValues = new LinkedHashMap%3CString, String%3E();%0D%0A    private boolean onDuplicateKey = false;%0D
 %0A%0D%0A    /
@@ -1292,504 +1292,1895 @@
   * 
-Build the query as a String.%0D%0A     * @return the query as a String%0D%0A     */%0D%0A    public String build() %7B%0D%0A        StringBuilder builder = new StringBuilder();%0D%0A        builder.append(%22INSERT INTO %22)%0D%0A                .append(table)%0D%0A                .append(%22 (%22)%0D%0A                .append(QueryUtils.separate(values.keySet(), %22,%22))%0D%0A                .append(%22)%22)%0D%0A                .append(%22 VALUES (%22)%0D%0A                .append(QueryUtils.separate(values.values(), %22,%22))%0D%0A                .append(%22)%22);
+Add on duplicate key update clause.%0D%0A     * @return the InsertQuery object%0D%0A     */%0D%0A    public InsertQuery onDuplicateKeyUpdate() %7B%0D%0A        onDuplicateKey = true;%0D%0A        return this;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Update a column to value in case of a duplicate key.%0D%0A     *%0D%0A     * @param column the column to update%0D%0A     * @param value  the new value%0D%0A     * @return the InsertQuery object%0D%0A     */%0D%0A    public InsertQuery set(String column, String value) %7B%0D%0A        duplicateValues.put(column, value);%0D%0A        return this;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Update a column to value in case of a duplicate key.%0D%0A     * %3Cp%3E%0D%0A     * Automatically inserts values(column).%0D%0A     *%0D%0A     * @param column%0D%0A     * @return%0D%0A     */%0D%0A    public InsertQuery set(String column) %7B%0D%0A        set(column, %22VALUES(%22 + column + %22)%22);%0D%0A        return this;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Build the query as a String.%0D%0A     * @return the query as a String%0D%0A     */%0D%0A    public String build() %7B%0D%0A        StringBuilder builder = new StringBuilder();%0D%0A        builder.append(%22INSERT INTO %22)%0D%0A                .append(table)%0D%0A                .append(%22 (%22)%0D%0A                .append(QueryUtils.separate(values.keySet(), %22,%22))%0D%0A                .append(%22)%22)%0D%0A                .append(%22 VALUES (%22)%0D%0A                .append(QueryUtils.separate(values.values(), %22,%22))%0D%0A                .append(%22)%22);%0D%0A%0D%0A        if (onDuplicateKey) %7B%0D%0A            builder.append(%22 ON DUPLICATE KEY UPDATE %22);%0D%0A            String separator = %22%22;%0D%0A            for (Map.Entry%3CString, String%3E entry : duplicateValues.entrySet()) %7B%0D%0A                String column = entry.getKey();%0D%0A                String value = entry.getValue();%0D%0A                builder.append(separator)%0D%0A                        .append(column)%0D%0A                        .append(%22=%22)%0D%0A                        .append(value);%0D%0A                separator = %22,%22;%0D%0A            %7D%0D%0A        %7D
 %0D%0A%0D%0A

Add on duplicate key update clause