-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand_statement_result.go
More file actions
167 lines (143 loc) · 3.67 KB
/
command_statement_result.go
File metadata and controls
167 lines (143 loc) · 3.67 KB
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
160
161
162
163
164
165
166
167
package flink
import (
"fmt"
"net/url"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
flinkgatewayv1 "github.com/confluentinc/ccloud-sdk-go-v2/flink-gateway/v1"
"github.com/confluentinc/cli/v4/pkg/ccloudv2"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/config"
"github.com/confluentinc/cli/v4/pkg/output"
)
const defaultMaxResultRows = 100
func (c *command) newStatementResultCommand(cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "result",
Short: "Manage Flink SQL statement results.",
}
if cfg.IsCloudLogin() {
pcmd.AddCloudFlag(cmd)
pcmd.AddRegionFlagFlink(cmd, c.AuthenticatedCLICommand)
cmd.AddCommand(c.newStatementResultListCommand())
}
return cmd
}
type serializedResultOutput struct {
Columns []string `json:"columns" yaml:"columns"`
Rows []map[string]any `json:"rows" yaml:"rows"`
}
type statementResultData struct {
Headers []string
Rows [][]string
}
func printStatementResults(cmd *cobra.Command, data *statementResultData) error {
if data == nil || len(data.Rows) == 0 {
if output.GetFormat(cmd).IsSerialized() {
headers := []string{}
if data != nil {
headers = data.Headers
}
return output.SerializedOutput(cmd, &serializedResultOutput{
Columns: headers,
Rows: []map[string]any{},
})
}
fmt.Fprintln(cmd.OutOrStdout(), "No results found.")
return nil
}
if output.GetFormat(cmd).IsSerialized() {
rows := make([]map[string]any, len(data.Rows))
for i, row := range data.Rows {
rowMap := make(map[string]any)
for j, val := range row {
if j < len(data.Headers) {
rowMap[data.Headers[j]] = val
}
}
rows[i] = rowMap
}
return output.SerializedOutput(cmd, &serializedResultOutput{
Columns: data.Headers,
Rows: rows,
})
}
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetAutoFormatHeaders(false)
table.SetHeader(data.Headers)
table.SetAutoWrapText(false)
table.SetBorder(false)
for _, row := range data.Rows {
table.Append(row)
}
table.Render()
return nil
}
func fetchAllResults(client ccloudv2.GatewayClientInterface, envId, name, orgId string, schema flinkgatewayv1.SqlV1ResultSchema, maxRows int) (*statementResultData, error) {
columns := schema.GetColumns()
headers := make([]string, len(columns))
for i, col := range columns {
headers[i] = col.GetName()
}
var allRows [][]string
pageToken := ""
for {
resp, err := client.GetStatementResults(envId, name, orgId, pageToken)
if err != nil {
return nil, err
}
resultSet := resp.GetResults()
rawData := resultSet.GetData()
for _, item := range rawData {
resultItem, ok := item.(map[string]any)
if !ok {
continue
}
rowFields, _ := resultItem["row"].([]any)
row := make([]string, len(headers))
for j, field := range rowFields {
if j < len(headers) {
row[j] = fieldToString(field)
}
}
allRows = append(allRows, row)
}
if maxRows > 0 && len(allRows) >= maxRows {
allRows = allRows[:maxRows]
break
}
nextUrl := resp.Metadata.GetNext()
nextToken, err := extractResultPageToken(nextUrl)
if err != nil {
return nil, err
}
if nextToken == "" {
break
}
pageToken = nextToken
}
return &statementResultData{
Headers: headers,
Rows: allRows,
}, nil
}
func fieldToString(field any) string {
if field == nil {
return "NULL"
}
return fmt.Sprintf("%v", field)
}
func extractResultPageToken(nextUrl string) (string, error) {
if nextUrl == "" {
return "", nil
}
parsed, err := url.Parse(nextUrl)
if err != nil {
return "", err
}
params, err := url.ParseQuery(parsed.RawQuery)
if err != nil {
return "", err
}
return params.Get("page_token"), nil
}