package com.mjm.solarInsertDB;

import java.util.Arrays;
import java.util.List;

public class A {

    public static String makeUpdateCommand(List<String> fieldName, List<String> fieldValue , List<String> conditionName , List<String> conditionValue){
//        String command = "update " + databaseInformation.getDbName() + ".dbo." + databaseInformation.getDbTableName() + " set ";
        String command = "update " + "myDb" + ".dbo." + "myTable" + " set ";
        for (int i = 0; i < fieldName.size(); i++) {
            command = command + fieldName.get(i) +"='"+fieldValue.get(i) +"',";
        }
        command = command.substring(0, command.length() - 1)+" where ";
        for (int i = 0; i < conditionName.size(); i++) {
            command = command + conditionName.get(i) +"='"+conditionValue.get(i)+"' and ";
        }
        command = command.substring(0, command.length() - 4) + " ;";
        System.out.println("update command: " + command);
        return command;
    }

    /**
     *
     * @param fieldName 要修改的属性
     * @param fieldValue 修改后值
     * @return
     */
    private static String makeUpdateCommand2(List<String> fieldName, List<String> fieldValue){
        String command = "update " + "myDb" + ".dbo." + "myTable" + " set ";
        for (int i = 0; i < fieldName.size(); i++) {
            command = command + fieldName.get(i) +"='"+fieldValue.get(i) +"',";
        }
        command = command.substring(0, command.length()-1 ) + " ;";
        System.out.println("update command: " + command);
        return command;
    }

    public static void main(String[] args) {
        List<String> fieldName = Arrays.asList(new String[]{"name","age","sex"});
        List<String> fieldValue =Arrays.asList(new String[]{"bill","33","man"});
//        List<String> conditionName =Arrays.asList(new String[]{"color","pet"});
//        List<String> conditionValue =Arrays.asList(new String[]{"red","dog"});
        makeUpdateCommand2(fieldName,fieldValue);
    }
}
