forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlparse.go
63 lines (57 loc) · 1.93 KB
/
sqlparse.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package sqlparse
import (
"regexp"
"strings"
newrelic "github.com/newrelic/go-agent"
)
func extractTable(s string) string {
s = extractTableRegex.ReplaceAllString(s, "")
if idx := strings.Index(s, "."); idx > 0 {
s = s[idx+1:]
}
return s
}
var (
basicTable = `[^)(\]\[\}\{\s,;]+`
enclosedTable = `[\[\(\{]` + `\s*` + basicTable + `\s*` + `[\]\)\}]`
tablePattern = `(` + `\s+` + basicTable + `|` + `\s*` + enclosedTable + `)`
extractTableRegex = regexp.MustCompile(`[\s` + "`" + `"'\(\)\{\}\[\]]*`)
updateRegex = regexp.MustCompile(`(?is)^update(?:\s+(?:low_priority|ignore|or|rollback|abort|replace|fail|only))*` + tablePattern)
sqlOperations = map[string]*regexp.Regexp{
"select": regexp.MustCompile(`(?is)^.*?\sfrom` + tablePattern),
"delete": regexp.MustCompile(`(?is)^.*?\sfrom` + tablePattern),
"insert": regexp.MustCompile(`(?is)^.*?\sinto?` + tablePattern),
"update": updateRegex,
"call": nil,
"create": nil,
"drop": nil,
"show": nil,
"set": nil,
"exec": nil,
"execute": nil,
"alter": nil,
"commit": nil,
"rollback": nil,
}
firstWordRegex = regexp.MustCompile(`^\w+`)
cCommentRegex = regexp.MustCompile(`(?is)/\*.*?\*/`)
lineCommentRegex = regexp.MustCompile(`(?im)(?:--|#).*?$`)
sqlPrefixRegex = regexp.MustCompile(`^[\s;]*`)
)
// ParseQuery parses table and operation from the SQL query string.
func ParseQuery(segment *newrelic.DatastoreSegment, query string) {
s := cCommentRegex.ReplaceAllString(query, "")
s = lineCommentRegex.ReplaceAllString(s, "")
s = sqlPrefixRegex.ReplaceAllString(s, "")
op := strings.ToLower(firstWordRegex.FindString(s))
if rg, ok := sqlOperations[op]; ok {
segment.Operation = op
if nil != rg {
if m := rg.FindStringSubmatch(s); len(m) > 1 {
segment.Collection = extractTable(m[1])
}
}
}
}