| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestSplitComments(t *testing.T) { |
| 25 |
var testCases = []struct { |
| 26 |
input, outSQL, outLeadingComments, outTrailingComments string |
| 27 |
}{{ |
| 28 |
input: "/", |
| 29 |
outSQL: "/", |
| 30 |
outLeadingComments: "", |
| 31 |
outTrailingComments: "", |
| 32 |
}, { |
| 33 |
input: "*/", |
| 34 |
outSQL: "*/", |
| 35 |
outLeadingComments: "", |
| 36 |
outTrailingComments: "", |
| 37 |
}, { |
| 38 |
input: "/*/", |
| 39 |
outSQL: "/*/", |
| 40 |
outLeadingComments: "", |
| 41 |
outTrailingComments: "", |
| 42 |
}, { |
| 43 |
input: "a*/", |
| 44 |
outSQL: "a*/", |
| 45 |
outLeadingComments: "", |
| 46 |
outTrailingComments: "", |
| 47 |
}, { |
| 48 |
input: "*a*/", |
| 49 |
outSQL: "*a*/", |
| 50 |
outLeadingComments: "", |
| 51 |
outTrailingComments: "", |
| 52 |
}, { |
| 53 |
input: "**a*/", |
| 54 |
outSQL: "**a*/", |
| 55 |
outLeadingComments: "", |
| 56 |
outTrailingComments: "", |
| 57 |
}, { |
| 58 |
input: "/*b**a*/", |
| 59 |
outSQL: "", |
| 60 |
outLeadingComments: "", |
| 61 |
outTrailingComments: "/*b**a*/", |
| 62 |
}, { |
| 63 |
input: "/*a*/", |
| 64 |
outSQL: "", |
| 65 |
outLeadingComments: "", |
| 66 |
outTrailingComments: "/*a*/", |
| 67 |
}, { |
| 68 |
input: "/**/", |
| 69 |
outSQL: "", |
| 70 |
outLeadingComments: "", |
| 71 |
outTrailingComments: "/**/", |
| 72 |
}, { |
| 73 |
input: "/*b*/ /*a*/", |
| 74 |
outSQL: "", |
| 75 |
outLeadingComments: "", |
| 76 |
outTrailingComments: "/*b*/ /*a*/", |
| 77 |
}, { |
| 78 |
input: "/* before */ foo /* bar */", |
| 79 |
outSQL: "foo", |
| 80 |
outLeadingComments: "/* before */ ", |
| 81 |
outTrailingComments: " /* bar */", |
| 82 |
}, { |
| 83 |
input: "/* before1 */ /* before2 */ foo /* after1 */ /* after2 */", |
| 84 |
outSQL: "foo", |
| 85 |
outLeadingComments: "/* before1 */ /* before2 */ ", |
| 86 |
outTrailingComments: " /* after1 */ /* after2 */", |
| 87 |
}, { |
| 88 |
input: "/** before */ foo /** bar */", |
| 89 |
outSQL: "foo", |
| 90 |
outLeadingComments: "/** before */ ", |
| 91 |
outTrailingComments: " /** bar */", |
| 92 |
}, { |
| 93 |
input: "/*** before */ foo /*** bar */", |
| 94 |
outSQL: "foo", |
| 95 |
outLeadingComments: "/*** before */ ", |
| 96 |
outTrailingComments: " /*** bar */", |
| 97 |
}, { |
| 98 |
input: "/** before **/ foo /** bar **/", |
| 99 |
outSQL: "foo", |
| 100 |
outLeadingComments: "/** before **/ ", |
| 101 |
outTrailingComments: " /** bar **/", |
| 102 |
}, { |
| 103 |
input: "/*** before ***/ foo /*** bar ***/", |
| 104 |
outSQL: "foo", |
| 105 |
outLeadingComments: "/*** before ***/ ", |
| 106 |
outTrailingComments: " /*** bar ***/", |
| 107 |
}, { |
| 108 |
input: " /*** before ***/ foo /*** bar ***/ ", |
| 109 |
outSQL: "foo", |
| 110 |
outLeadingComments: "/*** before ***/ ", |
| 111 |
outTrailingComments: " /*** bar ***/", |
| 112 |
}, { |
| 113 |
input: "*** bar ***/", |
| 114 |
outSQL: "*** bar ***/", |
| 115 |
outLeadingComments: "", |
| 116 |
outTrailingComments: "", |
| 117 |
}, { |
| 118 |
input: " foo ", |
| 119 |
outSQL: "foo", |
| 120 |
outLeadingComments: "", |
| 121 |
outTrailingComments: "", |
| 122 |
}} |
| 123 |
for _, testCase := range testCases { |
| 124 |
gotSQL, gotComments := SplitMarginComments(testCase.input) |
| 125 |
gotLeadingComments, gotTrailingComments := gotComments.Leading, gotComments.Trailing |
| 126 |
|
| 127 |
if gotSQL != testCase.outSQL { |
| 128 |
t.Errorf("test input: '%s', got SQL\n%+v, want\n%+v", testCase.input, gotSQL, testCase.outSQL) |
| 129 |
} |
| 130 |
if gotLeadingComments != testCase.outLeadingComments { |
| 131 |
t.Errorf("test input: '%s', got LeadingComments\n%+v, want\n%+v", testCase.input, gotLeadingComments, testCase.outLeadingComments) |
| 132 |
} |
| 133 |
if gotTrailingComments != testCase.outTrailingComments { |
| 134 |
t.Errorf("test input: '%s', got TrailingComments\n%+v, want\n%+v", testCase.input, gotTrailingComments, testCase.outTrailingComments) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func TestStripLeadingComments(t *testing.T) { |
| 140 |
var testCases = []struct { |
| 141 |
input, outSQL string |
| 142 |
}{{ |
| 143 |
input: "/", |
| 144 |
outSQL: "/", |
| 145 |
}, { |
| 146 |
input: "*/", |
| 147 |
outSQL: "*/", |
| 148 |
}, { |
| 149 |
input: "/*/", |
| 150 |
outSQL: "/*/", |
| 151 |
}, { |
| 152 |
input: "/*a", |
| 153 |
outSQL: "/*a", |
| 154 |
}, { |
| 155 |
input: "/*a*", |
| 156 |
outSQL: "/*a*", |
| 157 |
}, { |
| 158 |
input: "/*a**", |
| 159 |
outSQL: "/*a**", |
| 160 |
}, { |
| 161 |
input: "/*b**a*/", |
| 162 |
outSQL: "", |
| 163 |
}, { |
| 164 |
input: "/*a*/", |
| 165 |
outSQL: "", |
| 166 |
}, { |
| 167 |
input: "/**/", |
| 168 |
outSQL: "", |
| 169 |
}, { |
| 170 |
input: "/*!*/", |
| 171 |
outSQL: "/*!*/", |
| 172 |
}, { |
| 173 |
input: "/*!a*/", |
| 174 |
outSQL: "/*!a*/", |
| 175 |
}, { |
| 176 |
input: "/*b*/ /*a*/", |
| 177 |
outSQL: "", |
| 178 |
}, { |
| 179 |
input: `/*b*/ --foo |
| 180 |
bar`, |
| 181 |
outSQL: "bar", |
| 182 |
}, { |
| 183 |
input: "foo /* bar */", |
| 184 |
outSQL: "foo /* bar */", |
| 185 |
}, { |
| 186 |
input: "/* foo */ bar", |
| 187 |
outSQL: "bar", |
| 188 |
}, { |
| 189 |
input: "-- /* foo */ bar", |
| 190 |
outSQL: "-- /* foo */ bar", |
| 191 |
}, { |
| 192 |
input: "foo -- bar */", |
| 193 |
outSQL: "foo -- bar */", |
| 194 |
}, { |
| 195 |
input: `/* |
| 196 |
foo */ bar`, |
| 197 |
outSQL: "bar", |
| 198 |
}, { |
| 199 |
input: `-- foo bar |
| 200 |
a`, |
| 201 |
outSQL: "a", |
| 202 |
}, { |
| 203 |
input: `-- foo bar`, |
| 204 |
outSQL: "-- foo bar", |
| 205 |
}} |
| 206 |
for _, testCase := range testCases { |
| 207 |
gotSQL := StripLeadingComments(testCase.input) |
| 208 |
|
| 209 |
if gotSQL != testCase.outSQL { |
| 210 |
t.Errorf("test input: '%s', got SQL\n%+v, want\n%+v", testCase.input, gotSQL, testCase.outSQL) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestExtractMysqlComment(t *testing.T) { |
| 216 |
var testCases = []struct { |
| 217 |
input, outSQL, outVersion string |
| 218 |
}{{ |
| 219 |
input: "/*!50708SET max_execution_time=5000 */", |
| 220 |
outSQL: "SET max_execution_time=5000", |
| 221 |
outVersion: "50708", |
| 222 |
}, { |
| 223 |
input: "/*!50708 SET max_execution_time=5000*/", |
| 224 |
outSQL: "SET max_execution_time=5000", |
| 225 |
outVersion: "50708", |
| 226 |
}, { |
| 227 |
input: "/*!50708* from*/", |
| 228 |
outSQL: "* from", |
| 229 |
outVersion: "50708", |
| 230 |
}, { |
| 231 |
input: "/*! SET max_execution_time=5000*/", |
| 232 |
outSQL: "SET max_execution_time=5000", |
| 233 |
outVersion: "", |
| 234 |
}} |
| 235 |
for _, testCase := range testCases { |
| 236 |
gotVersion, gotSQL := ExtractMysqlComment(testCase.input) |
| 237 |
|
| 238 |
if gotVersion != testCase.outVersion { |
| 239 |
t.Errorf("test input: '%s', got version\n%+v, want\n%+v", testCase.input, gotVersion, testCase.outVersion) |
| 240 |
} |
| 241 |
if gotSQL != testCase.outSQL { |
| 242 |
t.Errorf("test input: '%s', got SQL\n%+v, want\n%+v", testCase.input, gotSQL, testCase.outSQL) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestExtractCommentDirectives(t *testing.T) { |
| 248 |
var testCases = []struct { |
| 249 |
input string |
| 250 |
vals CommentDirectives |
| 251 |
}{{ |
| 252 |
input: "", |
| 253 |
vals: nil, |
| 254 |
}, { |
| 255 |
input: "/* not a vt comment */", |
| 256 |
vals: nil, |
| 257 |
}, { |
| 258 |
input: "/*vt+ */", |
| 259 |
vals: CommentDirectives{}, |
| 260 |
}, { |
| 261 |
input: "/*vt+ SINGLE_OPTION */", |
| 262 |
vals: CommentDirectives{ |
| 263 |
"SINGLE_OPTION": true, |
| 264 |
}, |
| 265 |
}, { |
| 266 |
input: "/*vt+ ONE_OPT TWO_OPT */", |
| 267 |
vals: CommentDirectives{ |
| 268 |
"ONE_OPT": true, |
| 269 |
"TWO_OPT": true, |
| 270 |
}, |
| 271 |
}, { |
| 272 |
input: "/*vt+ ONE_OPT */ /* other comment */ /*vt+ TWO_OPT */", |
| 273 |
vals: CommentDirectives{ |
| 274 |
"ONE_OPT": true, |
| 275 |
"TWO_OPT": true, |
| 276 |
}, |
| 277 |
}, { |
| 278 |
input: "/*vt+ ONE_OPT=abc TWO_OPT=def */", |
| 279 |
vals: CommentDirectives{ |
| 280 |
"ONE_OPT": "abc", |
| 281 |
"TWO_OPT": "def", |
| 282 |
}, |
| 283 |
}, { |
| 284 |
input: "/*vt+ ONE_OPT=true TWO_OPT=false */", |
| 285 |
vals: CommentDirectives{ |
| 286 |
"ONE_OPT": true, |
| 287 |
"TWO_OPT": false, |
| 288 |
}, |
| 289 |
}, { |
| 290 |
input: "/*vt+ ONE_OPT=true TWO_OPT=\"false\" */", |
| 291 |
vals: CommentDirectives{ |
| 292 |
"ONE_OPT": true, |
| 293 |
"TWO_OPT": "\"false\"", |
| 294 |
}, |
| 295 |
}, { |
| 296 |
input: "/*vt+ RANGE_OPT=[a:b] ANOTHER ANOTHER_WITH_VALEQ=val= AND_ONE_WITH_EQ== */", |
| 297 |
vals: CommentDirectives{ |
| 298 |
"RANGE_OPT": "[a:b]", |
| 299 |
"ANOTHER": true, |
| 300 |
"ANOTHER_WITH_VALEQ": "val=", |
| 301 |
"AND_ONE_WITH_EQ": "=", |
| 302 |
}, |
| 303 |
}} |
| 304 |
|
| 305 |
for _, testCase := range testCases { |
| 306 |
sql := "select " + testCase.input + " 1 from dual" |
| 307 |
stmt, _ := Parse(sql) |
| 308 |
comments := stmt.(*Select).Comments |
| 309 |
vals := ExtractCommentDirectives(comments) |
| 310 |
|
| 311 |
if !reflect.DeepEqual(vals, testCase.vals) { |
| 312 |
t.Errorf("test input: '%v', got vals:\n%+v, want\n%+v", testCase.input, vals, testCase.vals) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
d := CommentDirectives{ |
| 317 |
"ONE_OPT": true, |
| 318 |
"TWO_OPT": false, |
| 319 |
"three": 1, |
| 320 |
"four": 2, |
| 321 |
"five": 0, |
| 322 |
"six": "true", |
| 323 |
} |
| 324 |
|
| 325 |
if !d.IsSet("ONE_OPT") { |
| 326 |
t.Errorf("d.IsSet(ONE_OPT) should be true") |
| 327 |
} |
| 328 |
|
| 329 |
if d.IsSet("TWO_OPT") { |
| 330 |
t.Errorf("d.IsSet(TWO_OPT) should be false") |
| 331 |
} |
| 332 |
|
| 333 |
if !d.IsSet("three") { |
| 334 |
t.Errorf("d.IsSet(three) should be true") |
| 335 |
} |
| 336 |
|
| 337 |
if d.IsSet("four") { |
| 338 |
t.Errorf("d.IsSet(four) should be false") |
| 339 |
} |
| 340 |
|
| 341 |
if d.IsSet("five") { |
| 342 |
t.Errorf("d.IsSet(five) should be false") |
| 343 |
} |
| 344 |
|
| 345 |
if d.IsSet("six") { |
| 346 |
t.Errorf("d.IsSet(six) should be false") |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestSkipQueryPlanCacheDirective(t *testing.T) { |
| 351 |
stmt, _ := Parse("insert /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ into user(id) values (1), (2)") |
| 352 |
if !SkipQueryPlanCacheDirective(stmt) { |
| 353 |
t.Errorf("d.SkipQueryPlanCacheDirective(stmt) should be true") |
| 354 |
} |
| 355 |
|
| 356 |
stmt, _ = Parse("insert into user(id) values (1), (2)") |
| 357 |
if SkipQueryPlanCacheDirective(stmt) { |
| 358 |
t.Errorf("d.SkipQueryPlanCacheDirective(stmt) should be false") |
| 359 |
} |
| 360 |
|
| 361 |
stmt, _ = Parse("update /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ users set name=1") |
| 362 |
if !SkipQueryPlanCacheDirective(stmt) { |
| 363 |
t.Errorf("d.SkipQueryPlanCacheDirective(stmt) should be true") |
| 364 |
} |
| 365 |
|
| 366 |
stmt, _ = Parse("select /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ * from users") |
| 367 |
if !SkipQueryPlanCacheDirective(stmt) { |
| 368 |
t.Errorf("d.SkipQueryPlanCacheDirective(stmt) should be true") |
| 369 |
} |
| 370 |
|
| 371 |
stmt, _ = Parse("delete /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ from users") |
| 372 |
if !SkipQueryPlanCacheDirective(stmt) { |
| 373 |
t.Errorf("d.SkipQueryPlanCacheDirective(stmt) should be true") |
| 374 |
} |
| 375 |
} |