package models import ( "fmt" "strings" ) // splitSQLStatements supports MariaDB literals, quoted identifiers and comments. func splitSQLStatements(script string) ([]string, error) { var statements []string var current strings.Builder var quote byte for i := 0; i < len(script); i++ { ch := script[i] if quote != 0 { current.WriteByte(ch) if ch == '\\' && quote != '`' && i+1 < len(script) { i++ current.WriteByte(script[i]) } else if ch == quote { if i+1 < len(script) && script[i+1] == quote { i++ current.WriteByte(script[i]) } else { quote = 0 } } continue } switch { case ch == '\'' || ch == '"' || ch == '`': quote = ch current.WriteByte(ch) case ch == '#' || (ch == '-' && i+2 < len(script) && script[i+1] == '-' && script[i+2] <= ' '): for i < len(script) && script[i] != '\n' { i++ } current.WriteByte('\n') case ch == '/' && i+1 < len(script) && script[i+1] == '*': if strings.HasPrefix(script[i:], "/*!") || strings.HasPrefix(script[i:], "/*M!") { return nil, fmt.Errorf("executable SQL comments are unsupported") } end := strings.Index(script[i+2:], "*/") if end < 0 { return nil, fmt.Errorf("unterminated SQL comment") } i += end + 3 current.WriteByte(' ') case ch == ';': if stmt := strings.TrimSpace(current.String()); stmt != "" { statements = append(statements, stmt) } current.Reset() default: current.WriteByte(ch) } } if quote != 0 { return nil, fmt.Errorf("unterminated SQL quote") } if stmt := strings.TrimSpace(current.String()); stmt != "" { statements = append(statements, stmt) } for _, stmt := range statements { if strings.HasPrefix(strings.ToUpper(stmt), "DELIMITER ") { return nil, fmt.Errorf("DELIMITER directives are unsupported") } } return statements, nil }