Split reads SQL queries from an io.Reader and returns them as a slice of strings. Each SQL query is delimited by a semicolon (;). The function handles: - Single-line comments (-- comment) - Multi-line comments (/* comment */) - Single-quoted strings ('string') - Double-quoted identifiers ("identifie
(ctx context.Context, r io.Reader)
| 16 | // - Double-quoted identifiers ("identifier") |
| 17 | // - Dollar-quoted strings ($$string$$ or $tag$string$tag$) |
| 18 | func Split(ctx context.Context, r io.Reader) ([]string, error) { |
| 19 | scanner := bufio.NewScanner(r) |
| 20 | var queries []string |
| 21 | var currentQuery strings.Builder |
| 22 | var inSingleQuote bool |
| 23 | var inDoubleQuote bool |
| 24 | var inDollarQuote bool |
| 25 | var dollarTag string |
| 26 | var inMultiLineComment bool |
| 27 | |
| 28 | for scanner.Scan() { |
| 29 | // Check context cancellation |
| 30 | select { |
| 31 | case <-ctx.Done(): |
| 32 | return nil, ctx.Err() |
| 33 | default: |
| 34 | } |
| 35 | |
| 36 | line := scanner.Text() |
| 37 | i := 0 |
| 38 | lineLen := len(line) |
| 39 | |
| 40 | for i < lineLen { |
| 41 | ch := line[i] |
| 42 | |
| 43 | // Handle multi-line comments |
| 44 | if inMultiLineComment { |
| 45 | if i+1 < lineLen && ch == '*' && line[i+1] == '/' { |
| 46 | inMultiLineComment = false |
| 47 | currentQuery.WriteString("*/") |
| 48 | i += 2 |
| 49 | continue |
| 50 | } |
| 51 | currentQuery.WriteByte(ch) |
| 52 | i++ |
| 53 | continue |
| 54 | } |
| 55 | |
| 56 | // Handle dollar-quoted strings (PostgreSQL) |
| 57 | if inDollarQuote { |
| 58 | if ch == '$' { |
| 59 | // Try to match the closing tag |
| 60 | endTag := extractDollarTag(line[i:]) |
| 61 | if endTag == dollarTag { |
| 62 | inDollarQuote = false |
| 63 | currentQuery.WriteString(endTag) |
| 64 | i += len(endTag) |
| 65 | continue |
| 66 | } |
| 67 | } |
| 68 | currentQuery.WriteByte(ch) |
| 69 | i++ |
| 70 | continue |
| 71 | } |
| 72 | |
| 73 | // Handle single-quoted strings |
| 74 | if inSingleQuote { |
| 75 | currentQuery.WriteByte(ch) |