55 "unicode"
66)
77
8- // Copyright (c) 2020-2024 Open Text.
8+ // Copyright (c) 2020-2026 Open Text.
99
1010// Licensed under the Apache License, Version 2.0 (the "License");
1111// you may not use this file except in compliance with the License.
@@ -36,23 +36,30 @@ func SplitStatements(query string) []string {
3636 inLineComment := false
3737 inBlockComment := false
3838 var dollarTag string
39+ statementHasContent := false
3940
41+ markNonWhitespace := func (b byte ) {
42+ if ! unicode .IsSpace (rune (b )) {
43+ statementHasContent = true
44+ }
45+ }
4046 flush := func () {
4147 statement := strings .TrimSpace (current .String ())
4248 current .Reset ()
43- if statement != "" {
49+ if statement != "" && statementHasContent {
4450 statements = append (statements , statement )
4551 }
52+ statementHasContent = false
4653 }
4754
4855 i := 0
4956 for i < len (query ) {
5057 ch := query [i ]
5158
5259 if inLineComment {
53- // Consume everything until the newline terminator.
54- current .WriteByte (ch )
60+ // Swallow comment text but keep the newline terminator so tokens remain separated.
5561 if ch == '\n' || ch == '\r' {
62+ current .WriteByte (ch )
5663 inLineComment = false
5764 }
5865 i ++
@@ -76,9 +83,11 @@ func SplitStatements(query string) []string {
7683 if inSingleQuote {
7784 // Stay inside the literal, handling doubled single quotes.
7885 current .WriteByte (ch )
86+ markNonWhitespace (ch )
7987 if ch == '\'' {
8088 if i + 1 < len (query ) && query [i + 1 ] == '\'' {
8189 current .WriteByte ('\'' )
90+ markNonWhitespace ('\'' )
8291 i += 2
8392 continue
8493 }
@@ -92,9 +101,11 @@ func SplitStatements(query string) []string {
92101 // Identifiers can be quoted with double quotes; treat them like
93102 // strings for splitter purposes.
94103 current .WriteByte (ch )
104+ markNonWhitespace (ch )
95105 if ch == '"' {
96106 if i + 1 < len (query ) && query [i + 1 ] == '"' {
97107 current .WriteByte ('"' )
108+ markNonWhitespace ('"' )
98109 i += 2
99110 continue
100111 }
@@ -105,10 +116,12 @@ func SplitStatements(query string) []string {
105116 }
106117
107118 if dollarTag != "" {
119+ statementHasContent = true
108120 // Inside a dollar-quoted literal; exit only when the exact tag is
109121 // observed again.
110122 if i + len (dollarTag ) <= len (query ) && query [i :i + len (dollarTag )] == dollarTag {
111123 current .WriteString (dollarTag )
124+ markNonWhitespace (dollarTag [0 ])
112125 i += len (dollarTag )
113126 dollarTag = ""
114127 continue
@@ -121,37 +134,46 @@ func SplitStatements(query string) []string {
121134 if ch == '\'' {
122135 inSingleQuote = true
123136 current .WriteByte (ch )
137+ markNonWhitespace (ch )
124138 i ++
125139 continue
126140 }
127141
128142 if ch == '"' {
129143 inDoubleQuote = true
130144 current .WriteByte (ch )
145+ markNonWhitespace (ch )
131146 i ++
132147 continue
133148 }
134149
135150 if ch == '-' && i + 1 < len (query ) && query [i + 1 ] == '-' {
136- current .WriteByte ('-' )
137- current .WriteByte ('-' )
138151 i += 2
139152 inLineComment = true
140153 continue
141154 }
142155
143- if ch == '/' && i + 1 < len (query ) && query [i + 1 ] == '*' {
144- current .WriteByte ('/' )
145- current .WriteByte ('*' )
146- i += 2
147- inBlockComment = true
148- continue
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+ }
149170 }
150171
151172 if ch == '$' {
152173 if tag , length , ok := readDollarTag (query , i ); ok {
153174 dollarTag = tag
154175 current .WriteString (tag )
176+ markNonWhitespace (tag [0 ])
155177 i += length
156178 continue
157179 }
@@ -164,6 +186,7 @@ func SplitStatements(query string) []string {
164186 }
165187
166188 current .WriteByte (ch )
189+ markNonWhitespace (ch )
167190 i ++
168191 }
169192
0 commit comments