package model import ( "encoding/json" "fmt" "strconv" "strings" cuej "git.loafle.net/commons_go/util/encoding/json" ) type ResultSetRow struct { *ResultSet } func (rsr *ResultSetRow) setMeta() { meta := make([]string, 0) arrayColumns := rsr.Item.MappingInfo.ArrayColumns keyColumns := rsr.Item.MappingInfo.KeyColumns valueColumn := rsr.Item.MappingInfo.ValueColumn if nil != arrayColumns { for _, c := range arrayColumns { meta = append(meta, c) } } if nil != keyColumns { for _, c := range keyColumns { meta = append(meta, c) } } if "" != valueColumn { meta = append(meta, valueColumn) } if nil == rsr.Meta { rsr.Meta = make(map[string]json.Number, 0) } for i := 0; i < len(meta); i++ { rsr.Meta[meta[i]] = json.Number(strconv.Itoa(i)) } } func (rsr *ResultSetRow) GetData() map[string]string { return rsr.parse() } func (rsr *ResultSetRow) parse() map[string]string { valueColumn := rsr.Item.MappingInfo.ValueColumn rm := make(map[string]string, 0) for _, row := range rsr.Rows { key := rsr.makeKey(row) if "" == key { continue } idx, _ := cuej.NumberToInt(rsr.Meta[valueColumn]) rm[key] = row[idx] } return rm } func (rsr *ResultSetRow) makeKey(data []string) string { metrics := rsr.Item.Keys arrayColumns := rsr.Item.MappingInfo.ArrayColumns keyColumns := rsr.Item.MappingInfo.KeyColumns keys := rsr.Item.Keys findIndex := -1 Loop: for _, keyColumn := range keyColumns { idx, _ := cuej.NumberToInt(rsr.Meta[keyColumn]) row := data[idx] for i := 0; i < len(keys); i++ { if row == keys[i].Key { findIndex = i break Loop } } } if 0 > findIndex { return "" } metric := metrics[findIndex].Metric convertChar := "$" if nil != arrayColumns { for i := 0; i < len(arrayColumns); i++ { convertStr := fmt.Sprintf("%s%d", convertChar, i) idx, _ := cuej.NumberToInt(rsr.Meta[arrayColumns[i]]) replaceString := data[idx] metric = strings.Replace(metric, convertStr, "'"+replaceString+"'", -1) } } return metric }