dim/zone_list.go
Gibheer c7ead94934 convert query result to dim result
This modifies the zone list command in such a way, that a query result
could be directly returned to the response.

With a bit of work, large query results could be rendered with a
streaming json renderer to the output.
2021-05-03 19:39:41 +02:00

52 lines
1.2 KiB
Go

package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
ZoneListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
zoneListMap = map[string]string{
"name": "z.name",
"modified_by": "z.modified_by",
"modified_at": "z.modified_at",
"created_by": "z.modified_by",
"created_at": "z.modified_at",
}
)
func zoneList(c *Context, req Request, res *Response) error {
options := ZoneListOptions{
Attributes: types.NewFieldList("name"),
}
if err := req.ParseAtLeast(0, &options); err != nil {
res.AddMessage(LevelError, "could not parse options: %s", err)
return nil
}
selClause := query.FieldListToSelect("z", options.Attributes, zoneListMap)
from := "zones z"
queryStr := fmt.Sprintf(`select %s from %s`, selClause, from)
rows, err := c.tx.Query(queryStr)
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get zone list: %s - query: %s", err, queryStr)
}
defer rows.Close()
res.Result, err = query.RowsToMap(rows)
if err != nil {
res.Result = nil
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse zone list: %v", err)
}
return nil
}