增量更新clickhouse

package main

import "fmt"
import "strings"
// import   "encoding/json"
import "strconv"



func main() {
    types := make(map[string]string)
    types["double_value"] = "int"
    types["float_value"] = "int"
    types["id"] = "string"
    list := make([]map[string]interface{},0)
    for index := 0; index < 2; index++ {
        obj := make(map[string]interface{})     
        if(index == 0){
            obj["double_value"] =  strconv.Itoa(index+4)
            obj["float_value"] =  strconv.Itoa(index+4)
            obj["id"] =  "1"
        }else{
            obj["float_value"] =  strconv.Itoa(index+4)
            obj["id"] =  "2"
        }
        list = append(list,obj)
    }
    // sql := BatchUpdateType("codec_example","id",types,list)
    sql,args := BatchUpdate("codec_example","id",list)
    fmt.Println(sql)
    fmt.Println("%v",args)
}

func BatchUpdateType(table string, primaryKey string,types map[string]string, list []map[string]string) (string) {
    if table == "" || primaryKey == "" || list == nil {
        return ""
    }
    sql := "alter table " + table
    con := make(map[int]string, 0)
    conSql := make(map[int]string, 0)
    fields := make([]string, 0)
    for key, value := range list {
        x := 0
        for k, _ := range value {
            if(types[k] == "string"){
                list[key][k] = "'"+value[k]+"'"
            }
        }
        for k, _ := range value {
            _, ok := con[x]
            if k != primaryKey {
                if !ok {
                    if x == 0 {
                        con[x] = " update " + k + " = " + "multiIf("
                    } else {
                        con[x] = k + " = " + "multiIf("
                    }
                    conSql[x] = ""
                }
                temp := value[primaryKey]
                conSql[x] = conSql[x] + primaryKey+" == " + temp + " ," + value[k] + ","
                x++

            }
        }
        temp := value[primaryKey]
        fields = append(fields, temp)

    }
    conNum := len(con) - 1
    for key, value := range con {
        if key < conNum {
            sql = sql + value + conSql[key] + "NULL),"
        } else if key == conNum {
            sql = sql + value + conSql[key] + "NULL)"
        }
    }
    sql = sql + " where " + primaryKey + " in(" + strings.Join(fields, ",") + ")"
    return sql
}

func BatchUpdate(table string, primaryKey string, list []map[string]interface{}) (string,[]interface{}) {
    args := make([]interface{},0)
    if table == "" || primaryKey == "" || list == nil {
        return "",args
    }
    sql := "alter table " + table
    con := make(map[string]string, 0)
    conSql := make(map[string]string, 0)
    fields := make([]interface{}, 0)
    fieldsTemp := make([]string, 0)
    for _, value := range list {
        x := 0
        for k, _ := range value {
            _, ok := con[k]
            if k != primaryKey {
                if !ok {
                    if x == 0 {
                        con[k] = " update " + k + " = " + "multiIf("
                    } else {
                        con[k] = k + " = " + "multiIf("
                    }
                    conSql[k] = ""
                }
                temp := value[primaryKey]
                args = append(args,temp)
                args = append(args,value[k])
                conSql[k] = conSql[k] + primaryKey+" == ? ,?,"
                x++

            }
        }
        temp := value[primaryKey]
        fields = append(fields, temp)
        fieldsTemp = append(fieldsTemp, "?")

    }
    conNum := len(con)
    i := 0
    for key, value := range con {
        i++
        if i < conNum {
            sql = sql + value + conSql[key] + key+"),"
        } else if i == conNum {
            sql = sql + value + conSql[key] + key+")"
        }
    }
    for _, value := range fields {
        args = append(args,value)
    }
    sql = sql + " where " + primaryKey + " in(" + strings.Join(fieldsTemp, ",") + ")"
    return sql,args
}


alter table codec_example update double_value = multiIf(id == ? ,?,double_value),float_value = multiIf(id == ? ,?,id == ? ,?,float_value) where id in(?,?)

你可能感兴趣的:(增量更新clickhouse)