| 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 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/xwb1989/sqlparser/dependency/sqltypes" |
| 25 |
) |
| 26 |
|
| 27 |
func TestPreview(t *testing.T) { |
| 28 |
testcases := []struct { |
| 29 |
sql string |
| 30 |
want int |
| 31 |
}{ |
| 32 |
{"select ...", StmtSelect}, |
| 33 |
{" select ...", StmtSelect}, |
| 34 |
{"(select ...", StmtSelect}, |
| 35 |
{"insert ...", StmtInsert}, |
| 36 |
{"replace ....", StmtReplace}, |
| 37 |
{" update ...", StmtUpdate}, |
| 38 |
{"Update", StmtUpdate}, |
| 39 |
{"UPDATE ...", StmtUpdate}, |
| 40 |
{"\n\t delete ...", StmtDelete}, |
| 41 |
{"", StmtUnknown}, |
| 42 |
{" ", StmtUnknown}, |
| 43 |
{"begin", StmtBegin}, |
| 44 |
{" begin", StmtBegin}, |
| 45 |
{" begin ", StmtBegin}, |
| 46 |
{"\n\t begin ", StmtBegin}, |
| 47 |
{"... begin ", StmtUnknown}, |
| 48 |
{"begin ...", StmtUnknown}, |
| 49 |
{"begin /* ... */", StmtBegin}, |
| 50 |
{"begin /* ... *//*test*/", StmtBegin}, |
| 51 |
{"start transaction", StmtBegin}, |
| 52 |
{"commit", StmtCommit}, |
| 53 |
{"commit /*...*/", StmtCommit}, |
| 54 |
{"rollback", StmtRollback}, |
| 55 |
{"rollback /*...*/", StmtRollback}, |
| 56 |
{"create", StmtDDL}, |
| 57 |
{"alter", StmtDDL}, |
| 58 |
{"rename", StmtDDL}, |
| 59 |
{"drop", StmtDDL}, |
| 60 |
{"set", StmtSet}, |
| 61 |
{"show", StmtShow}, |
| 62 |
{"use", StmtUse}, |
| 63 |
{"analyze", StmtOther}, |
| 64 |
{"describe", StmtOther}, |
| 65 |
{"desc", StmtOther}, |
| 66 |
{"explain", StmtOther}, |
| 67 |
{"repair", StmtOther}, |
| 68 |
{"optimize", StmtOther}, |
| 69 |
{"truncate", StmtDDL}, |
| 70 |
{"unknown", StmtUnknown}, |
| 71 |
|
| 72 |
{"/* leading comment */ select ...", StmtSelect}, |
| 73 |
{"/* leading comment */ (select ...", StmtSelect}, |
| 74 |
{"/* leading comment */ /* leading comment 2 */ select ...", StmtSelect}, |
| 75 |
{"/*! MySQL-specific comment */", StmtComment}, |
| 76 |
{"/*!50708 MySQL-version comment */", StmtComment}, |
| 77 |
{"-- leading single line comment \n select ...", StmtSelect}, |
| 78 |
{"-- leading single line comment \n -- leading single line comment 2\n select ...", StmtSelect}, |
| 79 |
|
| 80 |
{"/* leading comment no end select ...", StmtUnknown}, |
| 81 |
{"-- leading single line comment no end select ...", StmtUnknown}, |
| 82 |
} |
| 83 |
for _, tcase := range testcases { |
| 84 |
if got := Preview(tcase.sql); got != tcase.want { |
| 85 |
t.Errorf("Preview(%s): %v, want %v", tcase.sql, got, tcase.want) |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestIsDML(t *testing.T) { |
| 91 |
testcases := []struct { |
| 92 |
sql string |
| 93 |
want bool |
| 94 |
}{ |
| 95 |
{" update ...", true}, |
| 96 |
{"Update", true}, |
| 97 |
{"UPDATE ...", true}, |
| 98 |
{"\n\t delete ...", true}, |
| 99 |
{"insert ...", true}, |
| 100 |
{"replace ...", true}, |
| 101 |
{"select ...", false}, |
| 102 |
{" select ...", false}, |
| 103 |
{"", false}, |
| 104 |
{" ", false}, |
| 105 |
} |
| 106 |
for _, tcase := range testcases { |
| 107 |
if got := IsDML(tcase.sql); got != tcase.want { |
| 108 |
t.Errorf("IsDML(%s): %v, want %v", tcase.sql, got, tcase.want) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestGetTableName(t *testing.T) { |
| 114 |
testcases := []struct { |
| 115 |
in, out string |
| 116 |
}{{ |
| 117 |
in: "select * from t", |
| 118 |
out: "t", |
| 119 |
}, { |
| 120 |
in: "select * from t.t", |
| 121 |
out: "", |
| 122 |
}, { |
| 123 |
in: "select * from (select * from t) as tt", |
| 124 |
out: "", |
| 125 |
}} |
| 126 |
|
| 127 |
for _, tc := range testcases { |
| 128 |
tree, err := Parse(tc.in) |
| 129 |
if err != nil { |
| 130 |
t.Error(err) |
| 131 |
continue |
| 132 |
} |
| 133 |
out := GetTableName(tree.(*Select).From[0].(*AliasedTableExpr).Expr) |
| 134 |
if out.String() != tc.out { |
| 135 |
t.Errorf("GetTableName('%s'): %s, want %s", tc.in, out, tc.out) |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func TestIsColName(t *testing.T) { |
| 141 |
testcases := []struct { |
| 142 |
in Expr |
| 143 |
out bool |
| 144 |
}{{ |
| 145 |
in: &ColName{}, |
| 146 |
out: true, |
| 147 |
}, { |
| 148 |
in: newHexVal(""), |
| 149 |
}} |
| 150 |
for _, tc := range testcases { |
| 151 |
out := IsColName(tc.in) |
| 152 |
if out != tc.out { |
| 153 |
t.Errorf("IsColName(%T): %v, want %v", tc.in, out, tc.out) |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestIsValue(t *testing.T) { |
| 159 |
testcases := []struct { |
| 160 |
in Expr |
| 161 |
out bool |
| 162 |
}{{ |
| 163 |
in: newStrVal("aa"), |
| 164 |
out: true, |
| 165 |
}, { |
| 166 |
in: newHexVal("3131"), |
| 167 |
out: true, |
| 168 |
}, { |
| 169 |
in: newIntVal("1"), |
| 170 |
out: true, |
| 171 |
}, { |
| 172 |
in: newValArg(":a"), |
| 173 |
out: true, |
| 174 |
}, { |
| 175 |
in: &NullVal{}, |
| 176 |
out: false, |
| 177 |
}} |
| 178 |
for _, tc := range testcases { |
| 179 |
out := IsValue(tc.in) |
| 180 |
if out != tc.out { |
| 181 |
t.Errorf("IsValue(%T): %v, want %v", tc.in, out, tc.out) |
| 182 |
} |
| 183 |
if tc.out { |
| 184 |
|
| 185 |
if _, err := NewPlanValue(tc.in); err != nil { |
| 186 |
t.Error(err) |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestIsNull(t *testing.T) { |
| 193 |
testcases := []struct { |
| 194 |
in Expr |
| 195 |
out bool |
| 196 |
}{{ |
| 197 |
in: &NullVal{}, |
| 198 |
out: true, |
| 199 |
}, { |
| 200 |
in: newStrVal(""), |
| 201 |
}} |
| 202 |
for _, tc := range testcases { |
| 203 |
out := IsNull(tc.in) |
| 204 |
if out != tc.out { |
| 205 |
t.Errorf("IsNull(%T): %v, want %v", tc.in, out, tc.out) |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestIsSimpleTuple(t *testing.T) { |
| 211 |
testcases := []struct { |
| 212 |
in Expr |
| 213 |
out bool |
| 214 |
}{{ |
| 215 |
in: ValTuple{newStrVal("aa")}, |
| 216 |
out: true, |
| 217 |
}, { |
| 218 |
in: ValTuple{&ColName{}}, |
| 219 |
}, { |
| 220 |
in: ListArg("::a"), |
| 221 |
out: true, |
| 222 |
}, { |
| 223 |
in: &ColName{}, |
| 224 |
}} |
| 225 |
for _, tc := range testcases { |
| 226 |
out := IsSimpleTuple(tc.in) |
| 227 |
if out != tc.out { |
| 228 |
t.Errorf("IsSimpleTuple(%T): %v, want %v", tc.in, out, tc.out) |
| 229 |
} |
| 230 |
if tc.out { |
| 231 |
|
| 232 |
if _, err := NewPlanValue(tc.in); err != nil { |
| 233 |
t.Error(err) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestNewPlanValue(t *testing.T) { |
| 240 |
tcases := []struct { |
| 241 |
in Expr |
| 242 |
out sqltypes.PlanValue |
| 243 |
err string |
| 244 |
}{{ |
| 245 |
in: &SQLVal{ |
| 246 |
Type: ValArg, |
| 247 |
Val: []byte(":valarg"), |
| 248 |
}, |
| 249 |
out: sqltypes.PlanValue{Key: "valarg"}, |
| 250 |
}, { |
| 251 |
in: &SQLVal{ |
| 252 |
Type: IntVal, |
| 253 |
Val: []byte("10"), |
| 254 |
}, |
| 255 |
out: sqltypes.PlanValue{Value: sqltypes.NewInt64(10)}, |
| 256 |
}, { |
| 257 |
in: &SQLVal{ |
| 258 |
Type: IntVal, |
| 259 |
Val: []byte("1111111111111111111111111111111111111111"), |
| 260 |
}, |
| 261 |
err: "value out of range", |
| 262 |
}, { |
| 263 |
in: &SQLVal{ |
| 264 |
Type: StrVal, |
| 265 |
Val: []byte("strval"), |
| 266 |
}, |
| 267 |
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")}, |
| 268 |
}, { |
| 269 |
in: &SQLVal{ |
| 270 |
Type: HexVal, |
| 271 |
Val: []byte("3131"), |
| 272 |
}, |
| 273 |
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("11")}, |
| 274 |
}, { |
| 275 |
in: &SQLVal{ |
| 276 |
Type: HexVal, |
| 277 |
Val: []byte("313"), |
| 278 |
}, |
| 279 |
err: "odd length hex string", |
| 280 |
}, { |
| 281 |
in: ListArg("::list"), |
| 282 |
out: sqltypes.PlanValue{ListKey: "list"}, |
| 283 |
}, { |
| 284 |
in: ValTuple{ |
| 285 |
&SQLVal{ |
| 286 |
Type: ValArg, |
| 287 |
Val: []byte(":valarg"), |
| 288 |
}, |
| 289 |
&SQLVal{ |
| 290 |
Type: StrVal, |
| 291 |
Val: []byte("strval"), |
| 292 |
}, |
| 293 |
}, |
| 294 |
out: sqltypes.PlanValue{ |
| 295 |
Values: []sqltypes.PlanValue{{ |
| 296 |
Key: "valarg", |
| 297 |
}, { |
| 298 |
Value: sqltypes.NewVarBinary("strval"), |
| 299 |
}}, |
| 300 |
}, |
| 301 |
}, { |
| 302 |
in: ValTuple{ |
| 303 |
&ParenExpr{Expr: &SQLVal{ |
| 304 |
Type: ValArg, |
| 305 |
Val: []byte(":valarg"), |
| 306 |
}}, |
| 307 |
}, |
| 308 |
err: "expression is too complex", |
| 309 |
}, { |
| 310 |
in: ValTuple{ |
| 311 |
ListArg("::list"), |
| 312 |
}, |
| 313 |
err: "unsupported: nested lists", |
| 314 |
}, { |
| 315 |
in: &NullVal{}, |
| 316 |
out: sqltypes.PlanValue{}, |
| 317 |
}, { |
| 318 |
in: &ParenExpr{Expr: &SQLVal{ |
| 319 |
Type: ValArg, |
| 320 |
Val: []byte(":valarg"), |
| 321 |
}}, |
| 322 |
err: "expression is too complex", |
| 323 |
}} |
| 324 |
for _, tc := range tcases { |
| 325 |
got, err := NewPlanValue(tc.in) |
| 326 |
if err != nil { |
| 327 |
if !strings.Contains(err.Error(), tc.err) { |
| 328 |
t.Errorf("NewPlanValue(%s) error: %v, want '%s'", String(tc.in), err, tc.err) |
| 329 |
} |
| 330 |
continue |
| 331 |
} |
| 332 |
if tc.err != "" { |
| 333 |
t.Errorf("NewPlanValue(%s) error: nil, want '%s'", String(tc.in), tc.err) |
| 334 |
continue |
| 335 |
} |
| 336 |
if !reflect.DeepEqual(got, tc.out) { |
| 337 |
t.Errorf("NewPlanValue(%s): %v, want %v", String(tc.in), got, tc.out) |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func TestStringIn(t *testing.T) { |
| 343 |
testcases := []struct { |
| 344 |
in1 string |
| 345 |
in2 []string |
| 346 |
out bool |
| 347 |
}{{ |
| 348 |
in1: "v1", |
| 349 |
in2: []string{"v1", "v2"}, |
| 350 |
out: true, |
| 351 |
}, { |
| 352 |
in1: "v0", |
| 353 |
in2: []string{"v1", "v2"}, |
| 354 |
}} |
| 355 |
for _, tc := range testcases { |
| 356 |
out := StringIn(tc.in1, tc.in2...) |
| 357 |
if out != tc.out { |
| 358 |
t.Errorf("StringIn(%v,%v): %#v, want %#v", tc.in1, tc.in2, out, tc.out) |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
func TestExtractSetValues(t *testing.T) { |
| 364 |
testcases := []struct { |
| 365 |
sql string |
| 366 |
out map[SetKey]interface{} |
| 367 |
scope string |
| 368 |
err string |
| 369 |
}{{ |
| 370 |
sql: "invalid", |
| 371 |
err: "syntax error at position 8 near 'invalid'", |
| 372 |
}, { |
| 373 |
sql: "select * from t", |
| 374 |
err: "ast did not yield *sqlparser.Set: *sqlparser.Select", |
| 375 |
}, { |
| 376 |
sql: "set autocommit=1+1", |
| 377 |
err: "invalid syntax: 1 + 1", |
| 378 |
}, { |
| 379 |
sql: "set transaction_mode='single'", |
| 380 |
out: map[SetKey]interface{}{{Key: "transaction_mode", Scope: "session"}: "single"}, |
| 381 |
}, { |
| 382 |
sql: "set autocommit=1", |
| 383 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 384 |
}, { |
| 385 |
sql: "set autocommit=true", |
| 386 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 387 |
}, { |
| 388 |
sql: "set autocommit=false", |
| 389 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(0)}, |
| 390 |
}, { |
| 391 |
sql: "set autocommit=on", |
| 392 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: "on"}, |
| 393 |
}, { |
| 394 |
sql: "set autocommit=off", |
| 395 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: "off"}, |
| 396 |
}, { |
| 397 |
sql: "set @@global.autocommit=1", |
| 398 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "global"}: int64(1)}, |
| 399 |
}, { |
| 400 |
sql: "set @@global.autocommit=1", |
| 401 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "global"}: int64(1)}, |
| 402 |
}, { |
| 403 |
sql: "set @@session.autocommit=1", |
| 404 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 405 |
}, { |
| 406 |
sql: "set @@session.`autocommit`=1", |
| 407 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 408 |
}, { |
| 409 |
sql: "set @@session.'autocommit'=1", |
| 410 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 411 |
}, { |
| 412 |
sql: "set @@session.\"autocommit\"=1", |
| 413 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 414 |
}, { |
| 415 |
sql: "set @@session.'\"autocommit'=1", |
| 416 |
out: map[SetKey]interface{}{{Key: "\"autocommit", Scope: "session"}: int64(1)}, |
| 417 |
}, { |
| 418 |
sql: "set @@session.`autocommit'`=1", |
| 419 |
out: map[SetKey]interface{}{{Key: "autocommit'", Scope: "session"}: int64(1)}, |
| 420 |
}, { |
| 421 |
sql: "set AUTOCOMMIT=1", |
| 422 |
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)}, |
| 423 |
}, { |
| 424 |
sql: "SET character_set_results = NULL", |
| 425 |
out: map[SetKey]interface{}{{Key: "character_set_results", Scope: "session"}: nil}, |
| 426 |
}, { |
| 427 |
sql: "SET foo = 0x1234", |
| 428 |
err: "invalid value type: 0x1234", |
| 429 |
}, { |
| 430 |
sql: "SET names utf8", |
| 431 |
out: map[SetKey]interface{}{{Key: "names", Scope: "session"}: "utf8"}, |
| 432 |
}, { |
| 433 |
sql: "SET names ascii collate ascii_bin", |
| 434 |
out: map[SetKey]interface{}{{Key: "names", Scope: "session"}: "ascii"}, |
| 435 |
}, { |
| 436 |
sql: "SET charset default", |
| 437 |
out: map[SetKey]interface{}{{Key: "charset", Scope: "session"}: "default"}, |
| 438 |
}, { |
| 439 |
sql: "SET character set ascii", |
| 440 |
out: map[SetKey]interface{}{{Key: "charset", Scope: "session"}: "ascii"}, |
| 441 |
}, { |
| 442 |
sql: "SET SESSION wait_timeout = 3600", |
| 443 |
out: map[SetKey]interface{}{{Key: "wait_timeout", Scope: "session"}: int64(3600)}, |
| 444 |
scope: "session", |
| 445 |
}, { |
| 446 |
sql: "SET GLOBAL wait_timeout = 3600", |
| 447 |
out: map[SetKey]interface{}{{Key: "wait_timeout", Scope: "session"}: int64(3600)}, |
| 448 |
scope: "global", |
| 449 |
}, { |
| 450 |
sql: "set session transaction isolation level repeatable read", |
| 451 |
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "repeatable read"}, |
| 452 |
scope: "session", |
| 453 |
}, { |
| 454 |
sql: "set session transaction isolation level read committed", |
| 455 |
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "read committed"}, |
| 456 |
scope: "session", |
| 457 |
}, { |
| 458 |
sql: "set session transaction isolation level read uncommitted", |
| 459 |
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "read uncommitted"}, |
| 460 |
scope: "session", |
| 461 |
}, { |
| 462 |
sql: "set session transaction isolation level serializable", |
| 463 |
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "serializable"}, |
| 464 |
scope: "session", |
| 465 |
}, { |
| 466 |
sql: "set session tx_read_only = 0", |
| 467 |
out: map[SetKey]interface{}{{Key: "tx_read_only", Scope: "session"}: int64(0)}, |
| 468 |
scope: "session", |
| 469 |
}, { |
| 470 |
sql: "set session tx_read_only = 1", |
| 471 |
out: map[SetKey]interface{}{{Key: "tx_read_only", Scope: "session"}: int64(1)}, |
| 472 |
scope: "session", |
| 473 |
}, { |
| 474 |
sql: "set session sql_safe_updates = 0", |
| 475 |
out: map[SetKey]interface{}{{Key: "sql_safe_updates", Scope: "session"}: int64(0)}, |
| 476 |
scope: "session", |
| 477 |
}, { |
| 478 |
sql: "set session sql_safe_updates = 1", |
| 479 |
out: map[SetKey]interface{}{{Key: "sql_safe_updates", Scope: "session"}: int64(1)}, |
| 480 |
scope: "session", |
| 481 |
}} |
| 482 |
for _, tcase := range testcases { |
| 483 |
out, _, err := ExtractSetValues(tcase.sql) |
| 484 |
if tcase.err != "" { |
| 485 |
if err == nil || err.Error() != tcase.err { |
| 486 |
t.Errorf("ExtractSetValues(%s): %v, want '%s'", tcase.sql, err, tcase.err) |
| 487 |
} |
| 488 |
} else if err != nil { |
| 489 |
t.Errorf("ExtractSetValues(%s): %v, want no error", tcase.sql, err) |
| 490 |
} |
| 491 |
if !reflect.DeepEqual(out, tcase.out) { |
| 492 |
t.Errorf("ExtractSetValues(%s): %v, want '%v'", tcase.sql, out, tcase.out) |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
func newStrVal(in string) *SQLVal { |
| 498 |
return NewStrVal([]byte(in)) |
| 499 |
} |
| 500 |
|
| 501 |
func newIntVal(in string) *SQLVal { |
| 502 |
return NewIntVal([]byte(in)) |
| 503 |
} |
| 504 |
|
| 505 |
func newHexVal(in string) *SQLVal { |
| 506 |
return NewHexVal([]byte(in)) |
| 507 |
} |
| 508 |
|
| 509 |
func newValArg(in string) *SQLVal { |
| 510 |
return NewValArg([]byte(in)) |
| 511 |
} |