|
| 1 | +package parse |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "unicode" |
| 6 | +) |
| 7 | + |
| 8 | +// Copyright (c) 2020-2026 Open Text. |
| 9 | + |
| 10 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +// you may not use this file except in compliance with the License. |
| 12 | +// You may obtain a copy of the License at |
| 13 | + |
| 14 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + |
| 16 | +// Unless required by applicable law or agreed to in writing, software |
| 17 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +// See the License for the specific language governing permissions and |
| 20 | +// limitations under the License. |
| 21 | + |
| 22 | +// SplitStatements breaks a SQL string into individual statements separated by semicolons |
| 23 | +// that are not contained within literals or comments. |
| 24 | +func SplitStatements(query string) []string { |
| 25 | + trimmed := strings.TrimSpace(query) |
| 26 | + if trimmed == "" { |
| 27 | + return nil |
| 28 | + } |
| 29 | + |
| 30 | + var statements []string |
| 31 | + var current strings.Builder |
| 32 | + // Track our current lexical state so we can ignore semicolons that live |
| 33 | + // inside literals or comments. |
| 34 | + inSingleQuote := false |
| 35 | + inDoubleQuote := false |
| 36 | + inLineComment := false |
| 37 | + inBlockComment := false |
| 38 | + var dollarTag string |
| 39 | + statementHasContent := false |
| 40 | + |
| 41 | + markNonWhitespace := func(b byte) { |
| 42 | + if !unicode.IsSpace(rune(b)) { |
| 43 | + statementHasContent = true |
| 44 | + } |
| 45 | + } |
| 46 | + flush := func() { |
| 47 | + statement := strings.TrimSpace(current.String()) |
| 48 | + current.Reset() |
| 49 | + if statement != "" && statementHasContent { |
| 50 | + statements = append(statements, statement) |
| 51 | + } |
| 52 | + statementHasContent = false |
| 53 | + } |
| 54 | + |
| 55 | + i := 0 |
| 56 | + for i < len(query) { |
| 57 | + ch := query[i] |
| 58 | + |
| 59 | + if inLineComment { |
| 60 | + // Swallow comment text but keep the newline terminator so tokens remain separated. |
| 61 | + if ch == '\n' || ch == '\r' { |
| 62 | + current.WriteByte(ch) |
| 63 | + inLineComment = false |
| 64 | + } |
| 65 | + i++ |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + if inBlockComment { |
| 70 | + // Traditional /* ... */ comments block statement splitting |
| 71 | + // until the closing marker is found. |
| 72 | + current.WriteByte(ch) |
| 73 | + if ch == '*' && i+1 < len(query) && query[i+1] == '/' { |
| 74 | + current.WriteByte('/') |
| 75 | + i += 2 |
| 76 | + inBlockComment = false |
| 77 | + continue |
| 78 | + } |
| 79 | + i++ |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + if inSingleQuote { |
| 84 | + // Stay inside the literal, handling doubled single quotes. |
| 85 | + current.WriteByte(ch) |
| 86 | + markNonWhitespace(ch) |
| 87 | + if ch == '\'' { |
| 88 | + if i+1 < len(query) && query[i+1] == '\'' { |
| 89 | + current.WriteByte('\'') |
| 90 | + markNonWhitespace('\'') |
| 91 | + i += 2 |
| 92 | + continue |
| 93 | + } |
| 94 | + inSingleQuote = false |
| 95 | + } |
| 96 | + i++ |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if inDoubleQuote { |
| 101 | + // Identifiers can be quoted with double quotes; treat them like |
| 102 | + // strings for splitter purposes. |
| 103 | + current.WriteByte(ch) |
| 104 | + markNonWhitespace(ch) |
| 105 | + if ch == '"' { |
| 106 | + if i+1 < len(query) && query[i+1] == '"' { |
| 107 | + current.WriteByte('"') |
| 108 | + markNonWhitespace('"') |
| 109 | + i += 2 |
| 110 | + continue |
| 111 | + } |
| 112 | + inDoubleQuote = false |
| 113 | + } |
| 114 | + i++ |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + if dollarTag != "" { |
| 119 | + statementHasContent = true |
| 120 | + // Inside a dollar-quoted literal; exit only when the exact tag is |
| 121 | + // observed again. |
| 122 | + if i+len(dollarTag) <= len(query) && query[i:i+len(dollarTag)] == dollarTag { |
| 123 | + current.WriteString(dollarTag) |
| 124 | + markNonWhitespace(dollarTag[0]) |
| 125 | + i += len(dollarTag) |
| 126 | + dollarTag = "" |
| 127 | + continue |
| 128 | + } |
| 129 | + current.WriteByte(ch) |
| 130 | + i++ |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + if ch == '\'' { |
| 135 | + inSingleQuote = true |
| 136 | + current.WriteByte(ch) |
| 137 | + markNonWhitespace(ch) |
| 138 | + i++ |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + if ch == '"' { |
| 143 | + inDoubleQuote = true |
| 144 | + current.WriteByte(ch) |
| 145 | + markNonWhitespace(ch) |
| 146 | + i++ |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + if ch == '-' && i+1 < len(query) && query[i+1] == '-' { |
| 151 | + i += 2 |
| 152 | + inLineComment = true |
| 153 | + continue |
| 154 | + } |
| 155 | + |
| 156 | + if ch == '/' && i+1 < len(query) { |
| 157 | + next := query[i+1] |
| 158 | + if next == '*' { |
| 159 | + current.WriteByte('/') |
| 160 | + current.WriteByte('*') |
| 161 | + i += 2 |
| 162 | + inBlockComment = true |
| 163 | + continue |
| 164 | + } |
| 165 | + if next == '/' { |
| 166 | + i += 2 |
| 167 | + inLineComment = true |
| 168 | + continue |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if ch == '$' { |
| 173 | + if tag, length, ok := readDollarTag(query, i); ok { |
| 174 | + dollarTag = tag |
| 175 | + current.WriteString(tag) |
| 176 | + markNonWhitespace(tag[0]) |
| 177 | + i += length |
| 178 | + continue |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if ch == ';' { |
| 183 | + flush() |
| 184 | + i++ |
| 185 | + continue |
| 186 | + } |
| 187 | + |
| 188 | + current.WriteByte(ch) |
| 189 | + markNonWhitespace(ch) |
| 190 | + i++ |
| 191 | + } |
| 192 | + |
| 193 | + flush() |
| 194 | + return statements |
| 195 | +} |
| 196 | + |
| 197 | +func readDollarTag(query string, start int) (string, int, bool) { |
| 198 | + if query[start] != '$' { |
| 199 | + return "", 0, false |
| 200 | + } |
| 201 | + |
| 202 | + end := start + 1 |
| 203 | + for end < len(query) { |
| 204 | + r := rune(query[end]) |
| 205 | + if query[end] == '$' { |
| 206 | + return query[start : end+1], end + 1 - start, true |
| 207 | + } |
| 208 | + if !isDollarTagRune(r) { |
| 209 | + break |
| 210 | + } |
| 211 | + end++ |
| 212 | + } |
| 213 | + |
| 214 | + return "", 0, false |
| 215 | +} |
| 216 | + |
| 217 | +func isDollarTagRune(r rune) bool { |
| 218 | + return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' |
| 219 | +} |
0 commit comments