diff --git a/pkg/client/client.go b/pkg/client/client.go index e3a93b4..318437d 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1,9 +1,6 @@ package client import ( - "bytes" - "encoding/csv" - "encoding/json" "fmt" "reflect" @@ -22,13 +19,6 @@ type Client struct { ConnectionString string } -type Row []interface{} - -type Result struct { - Columns []string `json:"columns"` - Rows []Row `json:"rows"` -} - // Struct to hold table rows browsing options type RowsOptions struct { Limit int // Number of rows to fetch @@ -206,66 +196,6 @@ func (client *Client) query(query string, args ...interface{}) (*Result, error) return &result, nil } -func (res *Result) Format() []map[string]interface{} { - var items []map[string]interface{} - - for _, row := range res.Rows { - item := make(map[string]interface{}) - - for i, c := range res.Columns { - item[c] = row[i] - } - - items = append(items, item) - } - - return items -} - -func (res *Result) CSV() []byte { - buff := &bytes.Buffer{} - writer := csv.NewWriter(buff) - - writer.Write(res.Columns) - - for _, row := range res.Rows { - record := make([]string, len(res.Columns)) - - for i, item := range row { - if item != nil { - record[i] = fmt.Sprintf("%v", item) - } else { - record[i] = "" - } - } - - err := writer.Write(record) - - if err != nil { - fmt.Println(err) - break - } - } - - writer.Flush() - return buff.Bytes() -} - -func (res *Result) JSON() []byte { - records := []map[string]interface{}{} - - for _, row := range res.Rows { - record := map[string]interface{}{} - for i, col := range res.Columns { - record[col] = row[i] - } - records = append(records, record) - } - - data, _ := json.Marshal(records) - return data -} - // Close database connection func (client *Client) Close() error { if client.db != nil { diff --git a/pkg/client/result.go b/pkg/client/result.go new file mode 100644 index 0000000..893228f --- /dev/null +++ b/pkg/client/result.go @@ -0,0 +1,75 @@ +package client + +import ( + "bytes" + "encoding/csv" + "encoding/json" + "fmt" +) + +type Row []interface{} + +type Result struct { + Columns []string `json:"columns"` + Rows []Row `json:"rows"` +} + +func (res *Result) Format() []map[string]interface{} { + var items []map[string]interface{} + + for _, row := range res.Rows { + item := make(map[string]interface{}) + + for i, c := range res.Columns { + item[c] = row[i] + } + + items = append(items, item) + } + + return items +} + +func (res *Result) CSV() []byte { + buff := &bytes.Buffer{} + writer := csv.NewWriter(buff) + + writer.Write(res.Columns) + + for _, row := range res.Rows { + record := make([]string, len(res.Columns)) + + for i, item := range row { + if item != nil { + record[i] = fmt.Sprintf("%v", item) + } else { + record[i] = "" + } + } + + err := writer.Write(record) + + if err != nil { + fmt.Println(err) + break + } + } + + writer.Flush() + return buff.Bytes() +} + +func (res *Result) JSON() []byte { + records := []map[string]interface{}{} + + for _, row := range res.Rows { + record := map[string]interface{}{} + for i, col := range res.Columns { + record[col] = row[i] + } + records = append(records, record) + } + + data, _ := json.Marshal(records) + return data +}