-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateSelectableColumns.go
88 lines (73 loc) · 2.18 KB
/
validateSelectableColumns.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
package validation
import (
"fmt"
"github.com/MarioLegenda/cig/pkg"
"sort"
"strings"
)
func validSelectableColumns(tokens []string) (int, []SelectableColumn, error) {
if tokens[1] == "*" {
return 1, []SelectableColumn{
{
Alias: "",
Column: "*",
Original: tokens[1],
},
}, nil
}
selectableColumns := make([]SelectableColumn, 0)
columnNamesToValidate := make([]string, 0)
nextToSkip := 0
columnMode := true
commaMode := false
for i := 1; i < len(tokens); i++ {
token := tokens[i]
nextToSkip++
if token == "" {
return -1, nil, fmt.Errorf("Selectable column is invalid. Expected column, got something else: %w", pkg.InvalidSelectableColumns)
}
if columnMode {
if !isEnclosedInQuote(token) {
return -1, nil, fmt.Errorf("Selectable columns should be enclosed inside single quotes: %w", pkg.InvalidSelectableColumns)
}
// check proper column with alias
columnOnly := token[1 : len(token)-1]
splitted := strings.Split(columnOnly, ".")
if len(splitted) != 2 {
return -1, nil, fmt.Errorf("Selectable columns have to be in form {alias}.{columnName}: %w", pkg.InvalidSelectableColumns)
}
columnNamesToValidate = append(columnNamesToValidate, splitted[1])
selectableColumns = append(selectableColumns, SelectableColumn{
Alias: splitted[0],
Column: splitted[1],
Original: columnOnly,
})
nextPossibleColumn := i + 2
// the next column is not a "column" but something else, stop validating selectable columns
if tokens[i+1] == "," && isEnclosedInQuote(tokens[nextPossibleColumn]) {
commaMode = true
columnMode = false
continue
} else {
break
}
}
if commaMode {
if token != "," {
return -1, nil, fmt.Errorf("Invalid column separator. Expected comma (,), got something else: %w", pkg.InvalidSelectableColumns)
}
columnMode = true
commaMode = false
}
}
sort.Strings(columnNamesToValidate)
for i, s := range columnNamesToValidate {
if i < len(columnNamesToValidate)-1 {
next := columnNamesToValidate[i+1]
if next == s {
return -1, nil, fmt.Errorf("Duplicate column found: %w", pkg.InvalidDuplicatedColumn)
}
}
}
return nextToSkip, selectableColumns, nil
}