构建CQL查询以从map [string]接口{}

时间:2019-06-11 12:20:44

标签: go cassandra

我是Go&CQL的新手,我需要以下帮助(在平台上找不到我的问题的答案): 我有一个map [string] interface {},对于此地图中的每个k,v对,我需要检查字段切片中是否存在以k表示的列...如果不存在,请将其添加为新的表的列(具有特定类型,基于值类型)以及值。我正在努力寻找一种获取值类型,将其转换为Cassandra数据类型并将其作为列类型包含在查询字符串中的方法。到目前为止,我的代码如下:

//takes as arguments the map and a slice that holds the columns that are already in the table. Is there 
//A fucntion that returns a string to be executed later by //session.Query(...).Exec() to add columns that are missing in the table
func AddColumns(myInterfaceMap map[string]string, fields []string) string {

    var columnsToBeAdded []string

    var sb strings.Builder
    sb.WriteString("ALTER TABLE sample_demo.main ADD (")
    for k := range myInterfaceMap {

        if !contains(fields, k) {
            columnsToBeAdded = append(columnsToBeAdded, k)
            fields = append(Fields, k)
        }
    }
    for idx, column := range columnsToBeAdded {
        if idx == (len(columnsToBeAdded) - 1) {
            sb.WriteString(column)
            //I need to dynamically specify the column type, depending on value   for the 
            //concrete key...need help how to do it
            sb.WriteString(" text)") 

        } else {
            sb.WriteString(column)
            sb.WriteString(" text, ")
        }
    }
    log.Println(sb.String())
    log.Println(fields)
    return sb.String()
}

如果有人可以提供帮助,那将不胜感激:)

0 个答案:

没有答案