2018-06-05 23:48:16 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-02-21 03:20:27 +03:00
|
|
|
"regexp"
|
2018-06-05 23:48:16 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-02-25 20:43:04 +03:00
|
|
|
var (
|
|
|
|
// List of keywords that are not allowed in read-only mode
|
|
|
|
reRestrictedKeywords = regexp.MustCompile(`(?mi)\s?(CREATE|INSERT|DROP|DELETE|TRUNCATE|GRANT|OPEN|IMPORT|COPY)\s`)
|
|
|
|
|
|
|
|
// Comment regular expressions
|
|
|
|
reSlashComment = regexp.MustCompile(`(?m)/\*.+\*/`)
|
|
|
|
reDashComment = regexp.MustCompile(`(?m)--.+`)
|
|
|
|
)
|
2019-02-21 03:20:27 +03:00
|
|
|
|
2018-06-05 23:48:16 +03:00
|
|
|
// Get short version from the string
|
|
|
|
// Example: 10.2.3.1 -> 10.2
|
|
|
|
func getMajorMinorVersion(str string) string {
|
|
|
|
chunks := strings.Split(str, ".")
|
|
|
|
if len(chunks) == 0 {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
return strings.Join(chunks[0:2], ".")
|
|
|
|
}
|
2019-02-21 03:20:27 +03:00
|
|
|
|
|
|
|
// containsRestrictedKeywords returns true if given keyword is not allowed in read-only mode
|
|
|
|
func containsRestrictedKeywords(str string) bool {
|
2019-02-25 20:43:04 +03:00
|
|
|
str = reSlashComment.ReplaceAllString(str, "")
|
|
|
|
str = reDashComment.ReplaceAllString(str, "")
|
|
|
|
|
|
|
|
return reRestrictedKeywords.MatchString(str)
|
2019-02-21 03:20:27 +03:00
|
|
|
}
|