github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/ast/expressions.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package ast 15 16 import ( 17 "fmt" 18 "io" 19 "regexp" 20 "strconv" 21 "strings" 22 23 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/model" 24 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/mysql" 25 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/types" 26 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/parser/opcode" 27 ) 28 29 var ( 30 _ ExprNode = &BetweenExpr{} 31 _ ExprNode = &BinaryOperationExpr{} 32 _ ExprNode = &CaseExpr{} 33 _ ExprNode = &ColumnNameExpr{} 34 _ ExprNode = &CompareSubqueryExpr{} 35 _ ExprNode = &DefaultExpr{} 36 _ ExprNode = &ExistsSubqueryExpr{} 37 _ ExprNode = &IsNullExpr{} 38 _ ExprNode = &IsTruthExpr{} 39 _ ExprNode = &ParamMarkerExpr{} 40 _ ExprNode = &ParenthesesExpr{} 41 _ ExprNode = &PatternInExpr{} 42 _ ExprNode = &PatternLikeExpr{} 43 _ ExprNode = &PatternRegexpExpr{} 44 _ ExprNode = &PositionExpr{} 45 _ ExprNode = &RowExpr{} 46 _ ExprNode = &SubqueryExpr{} 47 _ ExprNode = &UnaryOperationExpr{} 48 _ ExprNode = &ValueExpr{} 49 _ ExprNode = &ValuesExpr{} 50 _ ExprNode = &VariableExpr{} 51 52 _ Node = &ColumnName{} 53 _ Node = &WhenClause{} 54 ) 55 56 // ValueExpr is the simple value expression. 57 type ValueExpr struct { 58 exprNode 59 projectionOffset int 60 } 61 62 // Format the ExprNode into a Writer. 63 func (n *ValueExpr) Format(w io.Writer) { 64 var s string 65 switch n.Kind() { 66 case types.KindNull: 67 s = "NULL" 68 case types.KindInt64: 69 if n.Type.Flag&mysql.IsBooleanFlag != 0 { 70 if n.GetInt64() > 0 { 71 s = "TRUE" 72 } else { 73 s = "FALSE" 74 } 75 } else { 76 s = strconv.FormatInt(n.GetInt64(), 10) 77 } 78 case types.KindUint64: 79 s = strconv.FormatUint(n.GetUint64(), 10) 80 case types.KindFloat32: 81 s = strconv.FormatFloat(n.GetFloat64(), 'e', -1, 32) 82 case types.KindFloat64: 83 s = strconv.FormatFloat(n.GetFloat64(), 'e', -1, 64) 84 case types.KindString, types.KindBytes: 85 s = strconv.Quote(n.GetString()) 86 case types.KindMysqlDecimal: 87 s = n.GetMysqlDecimal().String() 88 case types.KindBinaryLiteral: 89 if n.Type.Flag&mysql.UnsignedFlag != 0 { 90 s = fmt.Sprintf("x'%x'", n.GetBytes()) 91 } else { 92 s = n.GetBinaryLiteral().ToBitLiteralString(true) 93 } 94 default: 95 panic("Can't format to string") 96 } 97 fmt.Fprint(w, s) 98 } 99 100 // NewValueExpr creates a ValueExpr with value, and sets default field type. 101 func NewValueExpr(value interface{}) *ValueExpr { 102 if ve, ok := value.(*ValueExpr); ok { 103 return ve 104 } 105 ve := &ValueExpr{} 106 ve.SetValue(value) 107 types.DefaultTypeForValue(value, &ve.Type) 108 ve.projectionOffset = -1 109 return ve 110 } 111 112 // SetProjectionOffset sets ValueExpr.projectionOffset for logical plan builder. 113 func (n *ValueExpr) SetProjectionOffset(offset int) { 114 n.projectionOffset = offset 115 } 116 117 // GetProjectionOffset returns ValueExpr.projectionOffset. 118 func (n *ValueExpr) GetProjectionOffset() int { 119 return n.projectionOffset 120 } 121 122 // Accept implements Node interface. 123 func (n *ValueExpr) Accept(v Visitor) (Node, bool) { 124 newNode, skipChildren := v.Enter(n) 125 if skipChildren { 126 return v.Leave(newNode) 127 } 128 n = newNode.(*ValueExpr) 129 return v.Leave(n) 130 } 131 132 // BetweenExpr is for "between and" or "not between and" expression. 133 type BetweenExpr struct { 134 exprNode 135 // Expr is the expression to be checked. 136 Expr ExprNode 137 // Left is the expression for minimal value in the range. 138 Left ExprNode 139 // Right is the expression for maximum value in the range. 140 Right ExprNode 141 // Not is true, the expression is "not between and". 142 Not bool 143 } 144 145 // Format the ExprNode into a Writer. 146 func (n *BetweenExpr) Format(w io.Writer) { 147 n.Expr.Format(w) 148 fmt.Fprint(w, " BETWEEN ") 149 n.Left.Format(w) 150 fmt.Fprint(w, " AND ") 151 n.Right.Format(w) 152 } 153 154 // Accept implements Node interface. 155 func (n *BetweenExpr) Accept(v Visitor) (Node, bool) { 156 newNode, skipChildren := v.Enter(n) 157 if skipChildren { 158 return v.Leave(newNode) 159 } 160 161 n = newNode.(*BetweenExpr) 162 node, ok := n.Expr.Accept(v) 163 if !ok { 164 return n, false 165 } 166 n.Expr = node.(ExprNode) 167 168 node, ok = n.Left.Accept(v) 169 if !ok { 170 return n, false 171 } 172 n.Left = node.(ExprNode) 173 174 node, ok = n.Right.Accept(v) 175 if !ok { 176 return n, false 177 } 178 n.Right = node.(ExprNode) 179 180 return v.Leave(n) 181 } 182 183 // BinaryOperationExpr is for binary operation like `1 + 1`, `1 - 1`, etc. 184 type BinaryOperationExpr struct { 185 exprNode 186 // Op is the operator code for BinaryOperation. 187 Op opcode.Op 188 // L is the left expression in BinaryOperation. 189 L ExprNode 190 // R is the right expression in BinaryOperation. 191 R ExprNode 192 } 193 194 // Format the ExprNode into a Writer. 195 func (n *BinaryOperationExpr) Format(w io.Writer) { 196 n.L.Format(w) 197 fmt.Fprint(w, " ") 198 n.Op.Format(w) 199 fmt.Fprint(w, " ") 200 n.R.Format(w) 201 } 202 203 // Accept implements Node interface. 204 func (n *BinaryOperationExpr) Accept(v Visitor) (Node, bool) { 205 newNode, skipChildren := v.Enter(n) 206 if skipChildren { 207 return v.Leave(newNode) 208 } 209 210 n = newNode.(*BinaryOperationExpr) 211 node, ok := n.L.Accept(v) 212 if !ok { 213 return n, false 214 } 215 n.L = node.(ExprNode) 216 217 node, ok = n.R.Accept(v) 218 if !ok { 219 return n, false 220 } 221 n.R = node.(ExprNode) 222 223 return v.Leave(n) 224 } 225 226 // WhenClause is the when clause in Case expression for "when condition then result". 227 type WhenClause struct { 228 node 229 // Expr is the condition expression in WhenClause. 230 Expr ExprNode 231 // Result is the result expression in WhenClause. 232 Result ExprNode 233 } 234 235 // Accept implements Node Accept interface. 236 func (n *WhenClause) Accept(v Visitor) (Node, bool) { 237 newNode, skipChildren := v.Enter(n) 238 if skipChildren { 239 return v.Leave(newNode) 240 } 241 242 n = newNode.(*WhenClause) 243 node, ok := n.Expr.Accept(v) 244 if !ok { 245 return n, false 246 } 247 n.Expr = node.(ExprNode) 248 249 node, ok = n.Result.Accept(v) 250 if !ok { 251 return n, false 252 } 253 n.Result = node.(ExprNode) 254 return v.Leave(n) 255 } 256 257 // CaseExpr is the case expression. 258 type CaseExpr struct { 259 exprNode 260 // Value is the compare value expression. 261 Value ExprNode 262 // WhenClauses is the condition check expression. 263 WhenClauses []*WhenClause 264 // ElseClause is the else result expression. 265 ElseClause ExprNode 266 } 267 268 // Format the ExprNode into a Writer. 269 func (n *CaseExpr) Format(w io.Writer) { 270 fmt.Fprint(w, "CASE ") 271 n.Value.Format(w) 272 fmt.Fprint(w, " ") 273 for _, clause := range n.WhenClauses { 274 fmt.Fprint(w, "WHEN ") 275 clause.Expr.Format(w) 276 fmt.Fprint(w, " THEN ") 277 clause.Result.Format(w) 278 } 279 if n.ElseClause != nil { 280 fmt.Fprint(w, " ELSE ") 281 n.ElseClause.Format(w) 282 } 283 fmt.Fprint(w, " END") 284 } 285 286 // Accept implements Node Accept interface. 287 func (n *CaseExpr) Accept(v Visitor) (Node, bool) { 288 newNode, skipChildren := v.Enter(n) 289 if skipChildren { 290 return v.Leave(newNode) 291 } 292 293 n = newNode.(*CaseExpr) 294 if n.Value != nil { 295 node, ok := n.Value.Accept(v) 296 if !ok { 297 return n, false 298 } 299 n.Value = node.(ExprNode) 300 } 301 for i, val := range n.WhenClauses { 302 node, ok := val.Accept(v) 303 if !ok { 304 return n, false 305 } 306 n.WhenClauses[i] = node.(*WhenClause) 307 } 308 if n.ElseClause != nil { 309 node, ok := n.ElseClause.Accept(v) 310 if !ok { 311 return n, false 312 } 313 n.ElseClause = node.(ExprNode) 314 } 315 return v.Leave(n) 316 } 317 318 // SubqueryExec represents a subquery executor interface. 319 // This interface is implemented in executor and used in plan/evaluator. 320 // It will execute the subselect and get the result. 321 type SubqueryExec interface { 322 // ColumnCount returns column count for the sub query. 323 ColumnCount() (int, error) 324 } 325 326 // SubqueryExpr represents a subquery. 327 type SubqueryExpr struct { 328 exprNode 329 // Query is the query SelectNode. 330 Query ResultSetNode 331 SubqueryExec SubqueryExec 332 Evaluated bool 333 Correlated bool 334 MultiRows bool 335 Exists bool 336 } 337 338 // Format the ExprNode into a Writer. 339 func (n *SubqueryExpr) Format(w io.Writer) { 340 panic("Not implemented") 341 } 342 343 // Accept implements Node Accept interface. 344 func (n *SubqueryExpr) Accept(v Visitor) (Node, bool) { 345 newNode, skipChildren := v.Enter(n) 346 if skipChildren { 347 return v.Leave(newNode) 348 } 349 n = newNode.(*SubqueryExpr) 350 if n.SubqueryExec != nil { 351 return v.Leave(n) 352 } 353 node, ok := n.Query.Accept(v) 354 if !ok { 355 return n, false 356 } 357 n.Query = node.(ResultSetNode) 358 return v.Leave(n) 359 } 360 361 // CompareSubqueryExpr is the expression for "expr cmp (select ...)". 362 // See https://dev.mysql.com/doc/refman/5.7/en/comparisons-using-subqueries.html 363 // See https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html 364 // See https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html 365 type CompareSubqueryExpr struct { 366 exprNode 367 // L is the left expression 368 L ExprNode 369 // Op is the comparison opcode. 370 Op opcode.Op 371 // R is the subquery for right expression, may be rewritten to other type of expression. 372 R ExprNode 373 // All is true, we should compare all records in subquery. 374 All bool 375 } 376 377 // Format the ExprNode into a Writer. 378 func (n *CompareSubqueryExpr) Format(w io.Writer) { 379 panic("Not implemented") 380 } 381 382 // Accept implements Node Accept interface. 383 func (n *CompareSubqueryExpr) Accept(v Visitor) (Node, bool) { 384 newNode, skipChildren := v.Enter(n) 385 if skipChildren { 386 return v.Leave(newNode) 387 } 388 n = newNode.(*CompareSubqueryExpr) 389 node, ok := n.L.Accept(v) 390 if !ok { 391 return n, false 392 } 393 n.L = node.(ExprNode) 394 node, ok = n.R.Accept(v) 395 if !ok { 396 return n, false 397 } 398 n.R = node.(ExprNode) 399 return v.Leave(n) 400 } 401 402 // ColumnName represents column name. 403 type ColumnName struct { 404 node 405 Schema model.CIStr 406 Table model.CIStr 407 Name model.CIStr 408 } 409 410 // Accept implements Node Accept interface. 411 func (n *ColumnName) Accept(v Visitor) (Node, bool) { 412 newNode, skipChildren := v.Enter(n) 413 if skipChildren { 414 return v.Leave(newNode) 415 } 416 n = newNode.(*ColumnName) 417 return v.Leave(n) 418 } 419 420 // String implements Stringer interface. 421 func (n *ColumnName) String() string { 422 result := n.Name.L 423 if n.Table.L != "" { 424 result = n.Table.L + "." + result 425 } 426 if n.Schema.L != "" { 427 result = n.Schema.L + "." + result 428 } 429 return result 430 } 431 432 // OrigColName returns the full original column name. 433 func (n *ColumnName) OrigColName() (ret string) { 434 ret = n.Name.O 435 if n.Table.O == "" { 436 return 437 } 438 ret = n.Table.O + "." + ret 439 if n.Schema.O == "" { 440 return 441 } 442 ret = n.Schema.O + "." + ret 443 return 444 } 445 446 // ColumnNameExpr represents a column name expression. 447 type ColumnNameExpr struct { 448 exprNode 449 450 // Name is the referenced column name. 451 Name *ColumnName 452 453 // Refer is the result field the column name refers to. 454 // The value of Refer.Expr is used as the value of the expression. 455 Refer *ResultField 456 } 457 458 // Format the ExprNode into a Writer. 459 func (n *ColumnNameExpr) Format(w io.Writer) { 460 name := strings.Replace(n.Name.String(), ".", "`.`", -1) 461 fmt.Fprintf(w, "`%s`", name) 462 } 463 464 // Accept implements Node Accept interface. 465 func (n *ColumnNameExpr) Accept(v Visitor) (Node, bool) { 466 newNode, skipChildren := v.Enter(n) 467 if skipChildren { 468 return v.Leave(newNode) 469 } 470 n = newNode.(*ColumnNameExpr) 471 node, ok := n.Name.Accept(v) 472 if !ok { 473 return n, false 474 } 475 n.Name = node.(*ColumnName) 476 return v.Leave(n) 477 } 478 479 // DefaultExpr is the default expression using default value for a column. 480 type DefaultExpr struct { 481 exprNode 482 // Name is the column name. 483 Name *ColumnName 484 } 485 486 // Format the ExprNode into a Writer. 487 func (n *DefaultExpr) Format(w io.Writer) { 488 panic("Not implemented") 489 } 490 491 // Accept implements Node Accept interface. 492 func (n *DefaultExpr) Accept(v Visitor) (Node, bool) { 493 newNode, skipChildren := v.Enter(n) 494 if skipChildren { 495 return v.Leave(newNode) 496 } 497 n = newNode.(*DefaultExpr) 498 if n.Name != nil { 499 node, ok := n.Name.Accept(v) 500 if !ok { 501 return n, false 502 } 503 n.Name = node.(*ColumnName) 504 } 505 return v.Leave(n) 506 } 507 508 // ExistsSubqueryExpr is the expression for "exists (select ...)". 509 // See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html 510 type ExistsSubqueryExpr struct { 511 exprNode 512 // Sel is the subquery, may be rewritten to other type of expression. 513 Sel ExprNode 514 } 515 516 // Format the ExprNode into a Writer. 517 func (n *ExistsSubqueryExpr) Format(w io.Writer) { 518 panic("Not implemented") 519 } 520 521 // Accept implements Node Accept interface. 522 func (n *ExistsSubqueryExpr) Accept(v Visitor) (Node, bool) { 523 newNode, skipChildren := v.Enter(n) 524 if skipChildren { 525 return v.Leave(newNode) 526 } 527 n = newNode.(*ExistsSubqueryExpr) 528 node, ok := n.Sel.Accept(v) 529 if !ok { 530 return n, false 531 } 532 n.Sel = node.(ExprNode) 533 return v.Leave(n) 534 } 535 536 // PatternInExpr is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)". 537 type PatternInExpr struct { 538 exprNode 539 // Expr is the value expression to be compared. 540 Expr ExprNode 541 // List is the list expression in compare list. 542 List []ExprNode 543 // Not is true, the expression is "not in". 544 Not bool 545 // Sel is the subquery, may be rewritten to other type of expression. 546 Sel ExprNode 547 } 548 549 // Format the ExprNode into a Writer. 550 func (n *PatternInExpr) Format(w io.Writer) { 551 n.Expr.Format(w) 552 fmt.Fprint(w, " IN (") 553 for i, expr := range n.List { 554 expr.Format(w) 555 if i != len(n.List)-1 { 556 fmt.Fprint(w, ",") 557 } 558 } 559 fmt.Fprint(w, ")") 560 } 561 562 // Accept implements Node Accept interface. 563 func (n *PatternInExpr) Accept(v Visitor) (Node, bool) { 564 newNode, skipChildren := v.Enter(n) 565 if skipChildren { 566 return v.Leave(newNode) 567 } 568 n = newNode.(*PatternInExpr) 569 node, ok := n.Expr.Accept(v) 570 if !ok { 571 return n, false 572 } 573 n.Expr = node.(ExprNode) 574 for i, val := range n.List { 575 node, ok = val.Accept(v) 576 if !ok { 577 return n, false 578 } 579 n.List[i] = node.(ExprNode) 580 } 581 if n.Sel != nil { 582 node, ok = n.Sel.Accept(v) 583 if !ok { 584 return n, false 585 } 586 n.Sel = node.(ExprNode) 587 } 588 return v.Leave(n) 589 } 590 591 // IsNullExpr is the expression for null check. 592 type IsNullExpr struct { 593 exprNode 594 // Expr is the expression to be checked. 595 Expr ExprNode 596 // Not is true, the expression is "is not null". 597 Not bool 598 } 599 600 // Format the ExprNode into a Writer. 601 func (n *IsNullExpr) Format(w io.Writer) { 602 n.Expr.Format(w) 603 if n.Not { 604 fmt.Fprint(w, " IS NOT NULL") 605 return 606 } 607 fmt.Fprint(w, " IS NULL") 608 } 609 610 // Accept implements Node Accept interface. 611 func (n *IsNullExpr) Accept(v Visitor) (Node, bool) { 612 newNode, skipChildren := v.Enter(n) 613 if skipChildren { 614 return v.Leave(newNode) 615 } 616 n = newNode.(*IsNullExpr) 617 node, ok := n.Expr.Accept(v) 618 if !ok { 619 return n, false 620 } 621 n.Expr = node.(ExprNode) 622 return v.Leave(n) 623 } 624 625 // IsTruthExpr is the expression for true/false check. 626 type IsTruthExpr struct { 627 exprNode 628 // Expr is the expression to be checked. 629 Expr ExprNode 630 // Not is true, the expression is "is not true/false". 631 Not bool 632 // True indicates checking true or false. 633 True int64 634 } 635 636 // Format the ExprNode into a Writer. 637 func (n *IsTruthExpr) Format(w io.Writer) { 638 n.Expr.Format(w) 639 if n.Not { 640 fmt.Fprint(w, " IS NOT") 641 } else { 642 fmt.Fprint(w, " IS") 643 } 644 if n.True > 0 { 645 fmt.Fprint(w, " TRUE") 646 } else { 647 fmt.Fprint(w, " FALSE") 648 } 649 } 650 651 // Accept implements Node Accept interface. 652 func (n *IsTruthExpr) Accept(v Visitor) (Node, bool) { 653 newNode, skipChildren := v.Enter(n) 654 if skipChildren { 655 return v.Leave(newNode) 656 } 657 n = newNode.(*IsTruthExpr) 658 node, ok := n.Expr.Accept(v) 659 if !ok { 660 return n, false 661 } 662 n.Expr = node.(ExprNode) 663 return v.Leave(n) 664 } 665 666 // PatternLikeExpr is the expression for like operator, e.g, expr like "%123%" 667 type PatternLikeExpr struct { 668 exprNode 669 // Expr is the expression to be checked. 670 Expr ExprNode 671 // Pattern is the like expression. 672 Pattern ExprNode 673 // Not is true, the expression is "not like". 674 Not bool 675 676 Escape byte 677 678 PatChars []byte 679 PatTypes []byte 680 } 681 682 // Format the ExprNode into a Writer. 683 func (n *PatternLikeExpr) Format(w io.Writer) { 684 n.Expr.Format(w) 685 fmt.Fprint(w, " LIKE ") 686 n.Pattern.Format(w) 687 if n.Escape != '\\' { 688 fmt.Fprint(w, " ESCAPE ") 689 fmt.Fprintf(w, "'%c'", n.Escape) 690 } 691 } 692 693 // Accept implements Node Accept interface. 694 func (n *PatternLikeExpr) Accept(v Visitor) (Node, bool) { 695 newNode, skipChildren := v.Enter(n) 696 if skipChildren { 697 return v.Leave(newNode) 698 } 699 n = newNode.(*PatternLikeExpr) 700 if n.Expr != nil { 701 node, ok := n.Expr.Accept(v) 702 if !ok { 703 return n, false 704 } 705 n.Expr = node.(ExprNode) 706 } 707 if n.Pattern != nil { 708 node, ok := n.Pattern.Accept(v) 709 if !ok { 710 return n, false 711 } 712 n.Pattern = node.(ExprNode) 713 } 714 return v.Leave(n) 715 } 716 717 // ParamMarkerExpr expression holds a place for another expression. 718 // Used in parsing prepare statement. 719 type ParamMarkerExpr struct { 720 exprNode 721 Offset int 722 Order int 723 } 724 725 // Format the ExprNode into a Writer. 726 func (n *ParamMarkerExpr) Format(w io.Writer) { 727 panic("Not implemented") 728 } 729 730 // Accept implements Node Accept interface. 731 func (n *ParamMarkerExpr) Accept(v Visitor) (Node, bool) { 732 newNode, skipChildren := v.Enter(n) 733 if skipChildren { 734 return v.Leave(newNode) 735 } 736 n = newNode.(*ParamMarkerExpr) 737 return v.Leave(n) 738 } 739 740 // ParenthesesExpr is the parentheses expression. 741 type ParenthesesExpr struct { 742 exprNode 743 // Expr is the expression in parentheses. 744 Expr ExprNode 745 } 746 747 // Format the ExprNode into a Writer. 748 func (n *ParenthesesExpr) Format(w io.Writer) { 749 fmt.Fprint(w, "(") 750 n.Expr.Format(w) 751 fmt.Fprint(w, ")") 752 } 753 754 // Accept implements Node Accept interface. 755 func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) { 756 newNode, skipChildren := v.Enter(n) 757 if skipChildren { 758 return v.Leave(newNode) 759 } 760 n = newNode.(*ParenthesesExpr) 761 if n.Expr != nil { 762 node, ok := n.Expr.Accept(v) 763 if !ok { 764 return n, false 765 } 766 n.Expr = node.(ExprNode) 767 } 768 return v.Leave(n) 769 } 770 771 // PositionExpr is the expression for order by and group by position. 772 // MySQL use position expression started from 1, it looks a little confused inner. 773 // maybe later we will use 0 at first. 774 type PositionExpr struct { 775 exprNode 776 // N is the position, started from 1 now. 777 N int 778 // Refer is the result field the position refers to. 779 Refer *ResultField 780 } 781 782 // Format the ExprNode into a Writer. 783 func (n *PositionExpr) Format(w io.Writer) { 784 panic("Not implemented") 785 } 786 787 // Accept implements Node Accept interface. 788 func (n *PositionExpr) Accept(v Visitor) (Node, bool) { 789 newNode, skipChildren := v.Enter(n) 790 if skipChildren { 791 return v.Leave(newNode) 792 } 793 n = newNode.(*PositionExpr) 794 return v.Leave(n) 795 } 796 797 // PatternRegexpExpr is the pattern expression for pattern match. 798 type PatternRegexpExpr struct { 799 exprNode 800 // Expr is the expression to be checked. 801 Expr ExprNode 802 // Pattern is the expression for pattern. 803 Pattern ExprNode 804 // Not is true, the expression is "not rlike", 805 Not bool 806 807 // Re is the compiled regexp. 808 Re *regexp.Regexp 809 // Sexpr is the string for Expr expression. 810 Sexpr *string 811 } 812 813 // Format the ExprNode into a Writer. 814 func (n *PatternRegexpExpr) Format(w io.Writer) { 815 n.Expr.Format(w) 816 if n.Not { 817 fmt.Fprint(w, " NOT REGEXP ") 818 } else { 819 fmt.Fprint(w, " REGEXP ") 820 } 821 n.Pattern.Format(w) 822 } 823 824 // Accept implements Node Accept interface. 825 func (n *PatternRegexpExpr) Accept(v Visitor) (Node, bool) { 826 newNode, skipChildren := v.Enter(n) 827 if skipChildren { 828 return v.Leave(newNode) 829 } 830 n = newNode.(*PatternRegexpExpr) 831 node, ok := n.Expr.Accept(v) 832 if !ok { 833 return n, false 834 } 835 n.Expr = node.(ExprNode) 836 node, ok = n.Pattern.Accept(v) 837 if !ok { 838 return n, false 839 } 840 n.Pattern = node.(ExprNode) 841 return v.Leave(n) 842 } 843 844 // RowExpr is the expression for row constructor. 845 // See https://dev.mysql.com/doc/refman/5.7/en/row-subqueries.html 846 type RowExpr struct { 847 exprNode 848 849 Values []ExprNode 850 } 851 852 // Format the ExprNode into a Writer. 853 func (n *RowExpr) Format(w io.Writer) { 854 panic("Not implemented") 855 } 856 857 // Accept implements Node Accept interface. 858 func (n *RowExpr) Accept(v Visitor) (Node, bool) { 859 newNode, skipChildren := v.Enter(n) 860 if skipChildren { 861 return v.Leave(newNode) 862 } 863 n = newNode.(*RowExpr) 864 for i, val := range n.Values { 865 node, ok := val.Accept(v) 866 if !ok { 867 return n, false 868 } 869 n.Values[i] = node.(ExprNode) 870 } 871 return v.Leave(n) 872 } 873 874 // UnaryOperationExpr is the expression for unary operator. 875 type UnaryOperationExpr struct { 876 exprNode 877 // Op is the operator opcode. 878 Op opcode.Op 879 // V is the unary expression. 880 V ExprNode 881 } 882 883 // Format the ExprNode into a Writer. 884 func (n *UnaryOperationExpr) Format(w io.Writer) { 885 n.Op.Format(w) 886 n.V.Format(w) 887 } 888 889 // Accept implements Node Accept interface. 890 func (n *UnaryOperationExpr) Accept(v Visitor) (Node, bool) { 891 newNode, skipChildren := v.Enter(n) 892 if skipChildren { 893 return v.Leave(newNode) 894 } 895 n = newNode.(*UnaryOperationExpr) 896 node, ok := n.V.Accept(v) 897 if !ok { 898 return n, false 899 } 900 n.V = node.(ExprNode) 901 return v.Leave(n) 902 } 903 904 // ValuesExpr is the expression used in INSERT VALUES. 905 type ValuesExpr struct { 906 exprNode 907 // Column is column name. 908 Column *ColumnNameExpr 909 } 910 911 // Format the ExprNode into a Writer. 912 func (n *ValuesExpr) Format(w io.Writer) { 913 panic("Not implemented") 914 } 915 916 // Accept implements Node Accept interface. 917 func (n *ValuesExpr) Accept(v Visitor) (Node, bool) { 918 newNode, skipChildren := v.Enter(n) 919 if skipChildren { 920 return v.Leave(newNode) 921 } 922 n = newNode.(*ValuesExpr) 923 node, ok := n.Column.Accept(v) 924 if !ok { 925 return n, false 926 } 927 n.Column = node.(*ColumnNameExpr) 928 return v.Leave(n) 929 } 930 931 // VariableExpr is the expression for variable. 932 type VariableExpr struct { 933 exprNode 934 // Name is the variable name. 935 Name string 936 // IsGlobal indicates whether this variable is global. 937 IsGlobal bool 938 // IsSystem indicates whether this variable is a system variable in current session. 939 IsSystem bool 940 // Value is the variable value. 941 Value ExprNode 942 } 943 944 // Format the ExprNode into a Writer. 945 func (n *VariableExpr) Format(w io.Writer) { 946 panic("Not implemented") 947 } 948 949 // Accept implements Node Accept interface. 950 func (n *VariableExpr) Accept(v Visitor) (Node, bool) { 951 newNode, skipChildren := v.Enter(n) 952 if skipChildren { 953 return v.Leave(newNode) 954 } 955 n = newNode.(*VariableExpr) 956 if n.Value == nil { 957 return v.Leave(n) 958 } 959 960 node, ok := n.Value.Accept(v) 961 if !ok { 962 return n, false 963 } 964 n.Value = node.(ExprNode) 965 return v.Leave(n) 966 }