-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand.go
More file actions
88 lines (77 loc) · 2.27 KB
/
Copy pathcommand.go
File metadata and controls
88 lines (77 loc) · 2.27 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
package main
import (
"fmt"
"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/apstndb/execspansql/jqresult"
"github.com/apstndb/execspansql/params"
"github.com/wader/gojq"
)
// preparedCommand holds validated options and resolved inputs. Preparation does
// not authenticate, create a client, or open output files.
type preparedCommand struct {
opts
statement spanner.Statement
queryOptions spanner.QueryOptions
mode queryMode
jqMode jqresult.InputMode
jqCode *gojq.Code
}
func prepareCommand(o opts) (*preparedCommand, error) {
jqMode, err := jqresult.ParseInputMode(o.JqInputMode)
if err != nil {
return nil, err
}
if err := jqMode.ValidateFormat(o.Format); err != nil {
return nil, err
}
if err := validateJqOutputOptions(o, jqMode); err != nil {
return nil, err
}
var jqCode *gojq.Code
if !o.TryPartitionQuery && o.Format != "experimental_csv" {
jqFilter, err := readFileOrDefault(o.JqFromFile, o.JqFilter)
if err != nil {
return nil, err
}
if jqFilter == "" {
jqFilter = jqresult.DefaultFilter(jqMode)
}
jqCode, err = jqresult.Compile(jqFilter, jqMode)
if err != nil {
return nil, err
}
}
mode := sppb.ExecuteSqlRequest_QueryMode(sppb.ExecuteSqlRequest_QueryMode_value[o.QueryMode])
queryOpts := queryOptionsFor(mode, o.Priority)
query, err := readFileOrDefault(o.SqlFile, o.Sql)
if err != nil {
return nil, err
}
tb, err := parseTimestampBound(o.TimestampBound.ReadTimestamp)
if err != nil {
return nil, fmt.Errorf("--read-timestamp is supplied but wrong: %w", err)
}
m := queryModeForQuery(query, o.EnablePartitionedDML, tb)
if err := validateExecutionOptions(o, m); err != nil {
return nil, err
}
// Freeze the statement (SQL and parameters) before any interactive step so
// a parameter file edited during a browser login cannot change what runs.
paramStrMap, err := o.mergedParams()
if err != nil {
return nil, err
}
paramMap, err := params.GenerateParams(paramStrMap, mode == sppb.ExecuteSqlRequest_PLAN)
if err != nil {
return nil, err
}
return &preparedCommand{
opts: o,
statement: spanner.Statement{SQL: query, Params: paramMap},
queryOptions: queryOpts,
mode: m,
jqMode: jqMode,
jqCode: jqCode,
}, nil
}