Add ability to export table to JSON and XML

This commit is contained in:
Dan Sosedoff
2016-01-04 18:03:53 -06:00
parent bc1f876fb5
commit 73a97893e9
6 changed files with 74 additions and 36 deletions

View File

@@ -3,6 +3,7 @@ package client
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"reflect"
@@ -250,6 +251,21 @@ func (res *Result) CSV() []byte {
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 {