-
Notifications
You must be signed in to change notification settings - Fork 10
/
clean.go
58 lines (45 loc) · 1.69 KB
/
clean.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
/*
Package clean cleans SQL queries.
This includes making identifiers lower case.
The walker in this package implements all the tree.Walker methods, even if it
doesn't do anything. This is to ensure that if we need to add more cleaning / validation
rules, we know that we've covered all the nodes.
For example, EnterDelete does nothing, but if we later set a limit on the amount of
CTEs allowed, then we would add it there.
*/
package clean
import (
"fmt"
"regexp"
"strings"
"github.com/kwilteam/kwil-db/internal/engine/sqlanalyzer/utils"
)
// checks that the string only contains alphanumeric characters and underscores
var identifierRegexp = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
// cleanIdentifier checks that the identifier is a valid identifier and returns
// it in lower case
func cleanIdentifier(identifier string) (string, error) {
res := strings.ToLower(identifier)
if !identifierRegexp.MatchString(res) {
return "", wrapErr(ErrInvalidIdentifier, fmt.Errorf(`identifier must start with letter and only contain alphanumeric characters or underscores, received: "%s"`, identifier))
}
return res, nil
}
// cleanIdentifiers checks several identifiers and returns them in lower case
func cleanIdentifiers(identifiers []string) ([]string, error) {
res := make([]string, len(identifiers))
for i, identifier := range identifiers {
var err error
res[i], err = cleanIdentifier(identifier)
if err != nil {
return nil, err
}
}
return res, nil
}
// checkLiteral checks that the literal is a valid literal.
// It either must be guarded with single quotes, or it must be a number.
func checkLiteral(literal string) error {
_, err := utils.IsLiteral(literal)
return wrapErr(ErrInvalidLiteral, err)
}