package mariadb import ( "fmt" "regexp" "strings" ) // Recovery covers the selected schema, so external trigger effects are refused. func validateSnapshotTriggerReferences(trigger snapshotTrigger, schema string) error { body := trigger.Body quoted := `'(?:''|\\.|[^'\\])*'` if strings.Contains(trigger.SQLMode, "NO_BACKSLASH_ESCAPES") { quoted = `'(?:''|[^'])*'` } body = regexp.MustCompile(quoted).ReplaceAllString(body, " ") if !strings.Contains(trigger.SQLMode, "ANSI_QUOTES") { double := `"(?:""|\\.|[^"\\])*"` if strings.Contains(trigger.SQLMode, "NO_BACKSLASH_ESCAPES") { double = `"(?:""|[^"])*"` } body = regexp.MustCompile(double).ReplaceAllString(body, " ") } if strings.Contains(body, "/*!") || strings.Contains(body, "/*M!") { return fmt.Errorf("trigger %s has unsupported executable comments", trigger.Name) } body = regexp.MustCompile(`(?s)/\*.*?\*/|--[\t ][^\n]*|#[^\n]*`).ReplaceAllString(body, " ") if regexp.MustCompile(`(?i)\bCALL\s+`).MatchString(body) { return fmt.Errorf("trigger %s calls a routine outside supported recovery", trigger.Name) } identifier := "(?:`(?:``|[^`])+`|\"(?:\"\"|[^\"])+\"|[\\pL_$][\\pL\\pN_$]*)" for _, pattern := range []string{ "(?i)\\b(?:FROM|JOIN|UPDATE|INTO)\\s+(?:(?:LOW_PRIORITY|HIGH_PRIORITY|DELAYED|IGNORE)\\s+)*(" + identifier + ")\\s*\\.", "(?i)(" + identifier + ")\\s*\\.\\s*" + identifier + "\\s*\\(", } { for _, match := range regexp.MustCompile(pattern).FindAllStringSubmatch(body, -1) { if !strings.EqualFold(strings.Trim(match[1], "`\""), schema) { return fmt.Errorf("trigger %s references another schema outside supported recovery", trigger.Name) } } } return nil }