diff --git a/query/query.go b/query/query.go index 21ea483..744ee6f 100644 --- a/query/query.go +++ b/query/query.go @@ -21,26 +21,28 @@ import ( // that extra attributes can be selected dynamically. // Each field will be selected with the requested name, so be careful to // avoid collisions. -func FieldListToSelect(fl types.FieldList, nameMap map[string]string) string { +// The tableName will be used as a prefix to the attributes field. Only one +// attributes field can be used at the same time. +func FieldListToSelect(tableName string, fl types.FieldList, nameMap map[string]string) string { res := []string{} for _, name := range fl.Fields() { if field, found := nameMap[name]; found { res = append(res, fmt.Sprintf(`%s as %s`, field, name)) } else { - res = append(res, fmt.Sprintf(`%s as %s`, nameToAttrPath(name), name)) + res = append(res, fmt.Sprintf(`%s as %s`, nameToAttrPath(tableName, name), name)) } } return strings.Join(res, ",") } // nameToAttrPath takes a dotted string and converts it into a json field path. -func nameToAttrPath(name string) string { +func nameToAttrPath(tabName, name string) string { if name == "" { - return "attributes" + return tabName + ".attributes" } parts := strings.Split(name, ".") for i, part := range parts { parts[i] = fmt.Sprintf(`'%s'`, part) } - return fmt.Sprintf("attributes->%s", strings.Join(parts, "->")) + return fmt.Sprintf("%s.attributes->%s", tabName, strings.Join(parts, "->")) }