| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"strconv" |
| 21 |
"strings" |
| 22 |
"unicode" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
|
| 27 |
|
| 28 |
DirectiveMultiShardAutocommit = "MULTI_SHARD_AUTOCOMMIT" |
| 29 |
|
| 30 |
DirectiveSkipQueryPlanCache = "SKIP_QUERY_PLAN_CACHE" |
| 31 |
|
| 32 |
DirectiveQueryTimeout = "QUERY_TIMEOUT_MS" |
| 33 |
) |
| 34 |
|
| 35 |
func isNonSpace(r rune) bool { |
| 36 |
return !unicode.IsSpace(r) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func leadingCommentEnd(text string) (end int) { |
| 42 |
hasComment := false |
| 43 |
pos := 0 |
| 44 |
for pos < len(text) { |
| 45 |
|
| 46 |
|
| 47 |
nextVisibleOffset := strings.IndexFunc(text[pos:], isNonSpace) |
| 48 |
if nextVisibleOffset < 0 { |
| 49 |
break |
| 50 |
} |
| 51 |
pos += nextVisibleOffset |
| 52 |
remainingText := text[pos:] |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if len(remainingText) < 4 || remainingText[:2] != "/*" { |
| 57 |
break |
| 58 |
} |
| 59 |
commentLength := 4 + strings.Index(remainingText[2:], "*/") |
| 60 |
if commentLength < 4 { |
| 61 |
|
| 62 |
break |
| 63 |
} |
| 64 |
|
| 65 |
hasComment = true |
| 66 |
pos += commentLength |
| 67 |
} |
| 68 |
|
| 69 |
if hasComment { |
| 70 |
return pos |
| 71 |
} |
| 72 |
return 0 |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func trailingCommentStart(text string) (start int) { |
| 78 |
hasComment := false |
| 79 |
reducedLen := len(text) |
| 80 |
for reducedLen > 0 { |
| 81 |
|
| 82 |
|
| 83 |
nextReducedLen := strings.LastIndexFunc(text[:reducedLen], isNonSpace) + 1 |
| 84 |
if nextReducedLen == 0 { |
| 85 |
break |
| 86 |
} |
| 87 |
reducedLen = nextReducedLen |
| 88 |
if reducedLen < 4 || text[reducedLen-2:reducedLen] != "*/" { |
| 89 |
break |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
startCommentPos := strings.LastIndex(text[:reducedLen-2], "/*") |
| 94 |
if startCommentPos < 0 { |
| 95 |
|
| 96 |
break |
| 97 |
} |
| 98 |
|
| 99 |
hasComment = true |
| 100 |
reducedLen = startCommentPos |
| 101 |
} |
| 102 |
|
| 103 |
if hasComment { |
| 104 |
return reducedLen |
| 105 |
} |
| 106 |
return len(text) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
type MarginComments struct { |
| 111 |
Leading string |
| 112 |
Trailing string |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func SplitMarginComments(sql string) (query string, comments MarginComments) { |
| 118 |
trailingStart := trailingCommentStart(sql) |
| 119 |
leadingEnd := leadingCommentEnd(sql[:trailingStart]) |
| 120 |
comments = MarginComments{ |
| 121 |
Leading: strings.TrimLeftFunc(sql[:leadingEnd], unicode.IsSpace), |
| 122 |
Trailing: strings.TrimRightFunc(sql[trailingStart:], unicode.IsSpace), |
| 123 |
} |
| 124 |
return strings.TrimFunc(sql[leadingEnd:trailingStart], unicode.IsSpace), comments |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
func StripLeadingComments(sql string) string { |
| 129 |
sql = strings.TrimFunc(sql, unicode.IsSpace) |
| 130 |
|
| 131 |
for hasCommentPrefix(sql) { |
| 132 |
switch sql[0] { |
| 133 |
case '/': |
| 134 |
|
| 135 |
index := strings.Index(sql, "*/") |
| 136 |
if index <= 1 { |
| 137 |
return sql |
| 138 |
} |
| 139 |
|
| 140 |
if len(sql) > 2 && sql[2] == '!' { |
| 141 |
return sql |
| 142 |
} |
| 143 |
sql = sql[index+2:] |
| 144 |
case '-': |
| 145 |
|
| 146 |
index := strings.Index(sql, "\n") |
| 147 |
if index == -1 { |
| 148 |
return sql |
| 149 |
} |
| 150 |
sql = sql[index+1:] |
| 151 |
} |
| 152 |
|
| 153 |
sql = strings.TrimFunc(sql, unicode.IsSpace) |
| 154 |
} |
| 155 |
|
| 156 |
return sql |
| 157 |
} |
| 158 |
|
| 159 |
func hasCommentPrefix(sql string) bool { |
| 160 |
return len(sql) > 1 && ((sql[0] == '/' && sql[1] == '*') || (sql[0] == '-' && sql[1] == '-')) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
func ExtractMysqlComment(sql string) (version string, innerSQL string) { |
| 166 |
sql = sql[3 : len(sql)-2] |
| 167 |
|
| 168 |
digitCount := 0 |
| 169 |
endOfVersionIndex := strings.IndexFunc(sql, func(c rune) bool { |
| 170 |
digitCount++ |
| 171 |
return !unicode.IsDigit(c) || digitCount == 6 |
| 172 |
}) |
| 173 |
version = sql[0:endOfVersionIndex] |
| 174 |
innerSQL = strings.TrimFunc(sql[endOfVersionIndex:], unicode.IsSpace) |
| 175 |
|
| 176 |
return version, innerSQL |
| 177 |
} |
| 178 |
|
| 179 |
const commentDirectivePreamble = "/*vt+" |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
type CommentDirectives map[string]interface{} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func ExtractCommentDirectives(comments Comments) CommentDirectives { |
| 192 |
if comments == nil { |
| 193 |
return nil |
| 194 |
} |
| 195 |
|
| 196 |
var vals map[string]interface{} |
| 197 |
|
| 198 |
for _, comment := range comments { |
| 199 |
commentStr := string(comment) |
| 200 |
if commentStr[0:5] != commentDirectivePreamble { |
| 201 |
continue |
| 202 |
} |
| 203 |
|
| 204 |
if vals == nil { |
| 205 |
vals = make(map[string]interface{}) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
directives := strings.Fields(commentStr) |
| 211 |
for i := 1; i < len(directives)-1; i++ { |
| 212 |
directive := directives[i] |
| 213 |
sep := strings.IndexByte(directive, '=') |
| 214 |
|
| 215 |
|
| 216 |
if sep == -1 { |
| 217 |
vals[directive] = true |
| 218 |
continue |
| 219 |
} |
| 220 |
|
| 221 |
strVal := directive[sep+1:] |
| 222 |
directive = directive[:sep] |
| 223 |
|
| 224 |
intVal, err := strconv.Atoi(strVal) |
| 225 |
if err == nil { |
| 226 |
vals[directive] = intVal |
| 227 |
continue |
| 228 |
} |
| 229 |
|
| 230 |
boolVal, err := strconv.ParseBool(strVal) |
| 231 |
if err == nil { |
| 232 |
vals[directive] = boolVal |
| 233 |
continue |
| 234 |
} |
| 235 |
|
| 236 |
vals[directive] = strVal |
| 237 |
} |
| 238 |
} |
| 239 |
return vals |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (d CommentDirectives) IsSet(key string) bool { |
| 245 |
if d == nil { |
| 246 |
return false |
| 247 |
} |
| 248 |
|
| 249 |
val, ok := d[key] |
| 250 |
if !ok { |
| 251 |
return false |
| 252 |
} |
| 253 |
|
| 254 |
boolVal, ok := val.(bool) |
| 255 |
if ok { |
| 256 |
return boolVal |
| 257 |
} |
| 258 |
|
| 259 |
intVal, ok := val.(int) |
| 260 |
if ok { |
| 261 |
return intVal == 1 |
| 262 |
} |
| 263 |
return false |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
func SkipQueryPlanCacheDirective(stmt Statement) bool { |
| 268 |
switch stmt := stmt.(type) { |
| 269 |
case *Select: |
| 270 |
directives := ExtractCommentDirectives(stmt.Comments) |
| 271 |
if directives.IsSet(DirectiveSkipQueryPlanCache) { |
| 272 |
return true |
| 273 |
} |
| 274 |
case *Insert: |
| 275 |
directives := ExtractCommentDirectives(stmt.Comments) |
| 276 |
if directives.IsSet(DirectiveSkipQueryPlanCache) { |
| 277 |
return true |
| 278 |
} |
| 279 |
case *Update: |
| 280 |
directives := ExtractCommentDirectives(stmt.Comments) |
| 281 |
if directives.IsSet(DirectiveSkipQueryPlanCache) { |
| 282 |
return true |
| 283 |
} |
| 284 |
case *Delete: |
| 285 |
directives := ExtractCommentDirectives(stmt.Comments) |
| 286 |
if directives.IsSet(DirectiveSkipQueryPlanCache) { |
| 287 |
return true |
| 288 |
} |
| 289 |
default: |
| 290 |
return false |
| 291 |
} |
| 292 |
return false |
| 293 |
} |