/
output.go
159 lines (143 loc) · 3.69 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/service/athena/types"
"github.com/jedib0t/go-pretty/v6/table"
)
func ToJson(rows []types.Row, columns []types.ColumnInfo, qryType string, jsonMode string, outFile string) error {
dict := []map[string]interface{}{}
output := ""
// we skip the first row (column names) unless it is a utility output
start := 1
if qryType == "UTILITY" {
start = 0
}
for _, row := range rows[start:] {
data := map[string]interface{}{}
// for UTILITY type we need to split the first column by tabs first...
if qryType == "UTILITY" {
for i, col := range strings.Split(*row.Data[0].VarCharValue, "\t") {
if col != "" {
data[*columns[i].Name] = col
} else {
data[*columns[i].Name] = ""
}
}
} else {
for i, col := range row.Data {
if col.VarCharValue != nil {
data[*columns[i].Name] = *col.VarCharValue
} else {
data[*columns[i].Name] = ""
}
}
}
dict = append(dict, data)
}
if jsonMode == "serde" {
for _, row := range dict {
j, err := json.Marshal(row)
if err != nil {
return err
}
output = output + string(j) + "\n"
}
} else {
j, err := json.Marshal(dict)
if err != nil {
return err
}
output = string(j) + "\n"
}
if outFile == "" {
fmt.Print(output)
} else {
// we are writing to a file
_, writeErr := WriteToFile(outFile, output)
if writeErr != nil {
return writeErr
}
}
return nil
}
func OutputResults(rows []types.Row, columns []types.ColumnInfo, csv bool, header bool, outFile string, qryType string) error {
t := table.NewWriter()
//t.SetOutputMirror(os.Stdout)
if header {
var header table.Row
header = append(header, "#")
for _, col := range columns {
header = append(header, *col.Name)
}
t.AppendHeader(header)
}
// we skip the first row (column names) unless it is a utility output
start := 1
if qryType == "UTILITY" {
start = 0
}
for i, row := range rows[start:] {
var data table.Row
data = append(data, i)
// for UTILITY type we need to split the first column by tabs first...
if qryType == "UTILITY" {
for _, col := range strings.Split(*row.Data[0].VarCharValue, "\t") {
if col != "" {
data = append(data, col)
} else {
data = append(data, "null")
}
}
} else {
for _, col := range row.Data {
if col.VarCharValue != nil {
data = append(data, *col.VarCharValue)
} else {
data = append(data, "null")
}
}
}
t.AppendRow(data)
}
var output = ""
if csv {
output = t.RenderCSV()
} else {
output = t.Render()
}
if outFile == "" {
fmt.Println(output)
} else {
// we are writing to a file
_, writeErr := WriteToFile(outFile, output)
if writeErr != nil {
return writeErr
}
}
return nil
}
func WriteToFile(fileName string, output string) (bool, error) {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return false, err
}
file.WriteString(output + "\n")
file.Close()
return true, nil
}
func DisplayHelp() {
fmt.Println(".ddl\t\tEnable or disable DDL statements 'CREATE', 'ALTER' and 'DROP'")
fmt.Println(".exit\t\tSynonym for quit")
fmt.Println(".file\t\tRun the commands in the file specified")
fmt.Println(".header\t\tTurn on or off display of result set headers (column names)")
fmt.Println(".help\t\tDisplay this message")
fmt.Println(".mode\t\tChange output mode")
fmt.Println(".output\t\tOutput to stdout or a file, if blank it uses stdout")
fmt.Println(".save\t\tSave the default work-group and database for next time")
fmt.Println(".schema\t\tPrint the schema of the database")
fmt.Println(".stats\t\tDisplay query stats")
fmt.Println(".quit\t\tExit this utility")
}