github.com/mithrandie/csvq@v1.18.1/lib/parser/ast.go (about) 1 package parser 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "time" 8 9 "github.com/mithrandie/csvq/lib/option" 10 "github.com/mithrandie/csvq/lib/value" 11 12 "github.com/mithrandie/ternary" 13 ) 14 15 const TokenUndefined = 0 16 17 type Statement interface{} 18 19 type Expression interface { 20 GetBaseExpr() *BaseExpr 21 ClearBaseExpr() 22 HasParseInfo() bool 23 Line() int 24 Char() int 25 SourceFile() string 26 } 27 28 type QueryExpression interface { 29 String() string 30 31 GetBaseExpr() *BaseExpr 32 ClearBaseExpr() 33 HasParseInfo() bool 34 Line() int 35 Char() int 36 SourceFile() string 37 } 38 39 type BaseExpr struct { 40 line int 41 char int 42 sourceFile string 43 } 44 45 func (e *BaseExpr) Line() int { 46 return e.line 47 } 48 49 func (e *BaseExpr) Char() int { 50 return e.char 51 } 52 53 func (e *BaseExpr) SourceFile() string { 54 return e.sourceFile 55 } 56 57 func (e *BaseExpr) HasParseInfo() bool { 58 if e == nil { 59 return false 60 } 61 return true 62 } 63 64 func (e *BaseExpr) GetBaseExpr() *BaseExpr { 65 return e 66 } 67 68 func (e *BaseExpr) ClearBaseExpr() { 69 e.line = 0 70 e.char = 0 71 e.sourceFile = "" 72 } 73 74 func NewBaseExpr(token Token) *BaseExpr { 75 return &BaseExpr{ 76 line: token.Line, 77 char: token.Char, 78 sourceFile: token.SourceFile, 79 } 80 } 81 82 type PrimitiveType struct { 83 *BaseExpr 84 Literal string 85 Value value.Primary 86 } 87 88 func NewStringValue(s string) PrimitiveType { 89 return PrimitiveType{ 90 Literal: s, 91 Value: value.NewString(s), 92 } 93 } 94 95 func NewIntegerValueFromString(s string) PrimitiveType { 96 return PrimitiveType{ 97 Literal: s, 98 Value: value.NewIntegerFromString(s), 99 } 100 } 101 102 func NewIntegerValue(i int64) PrimitiveType { 103 return PrimitiveType{ 104 Value: value.NewInteger(i), 105 } 106 } 107 108 func NewFloatValueFromString(s string) PrimitiveType { 109 return PrimitiveType{ 110 Literal: s, 111 Value: value.NewFloatFromString(s), 112 } 113 } 114 115 func NewFloatValue(f float64) PrimitiveType { 116 return PrimitiveType{ 117 Value: value.NewFloat(f), 118 } 119 } 120 121 func NewTernaryValueFromString(s string) PrimitiveType { 122 return PrimitiveType{ 123 Value: value.NewTernaryFromString(s), 124 } 125 } 126 127 func NewTernaryValue(t ternary.Value) PrimitiveType { 128 return PrimitiveType{ 129 Value: value.NewTernary(t), 130 } 131 } 132 133 func NewDatetimeValueFromString(s string, formats []string, location *time.Location) PrimitiveType { 134 return PrimitiveType{ 135 Literal: s, 136 Value: value.NewDatetimeFromString(s, formats, location), 137 } 138 } 139 140 func NewDatetimeValue(t time.Time) PrimitiveType { 141 return PrimitiveType{ 142 Value: value.NewDatetime(t), 143 } 144 } 145 146 func NewNullValue() PrimitiveType { 147 return PrimitiveType{ 148 Value: value.NewNull(), 149 } 150 } 151 152 func (e PrimitiveType) String() string { 153 if 0 < len(e.Literal) { 154 switch e.Value.(type) { 155 case *value.String, *value.Datetime: 156 return option.QuoteString(e.Literal) 157 default: 158 return e.Literal 159 } 160 } 161 return e.Value.String() 162 } 163 164 func (e PrimitiveType) IsInteger() bool { 165 _, ok := e.Value.(*value.Integer) 166 return ok 167 } 168 169 type Placeholder struct { 170 *BaseExpr 171 Literal string 172 Ordinal int 173 Name string 174 } 175 176 func (e Placeholder) String() string { 177 if len(e.Name) < 1 { 178 return fmt.Sprintf("%s{%d}", e.Literal, e.Ordinal) 179 } 180 return e.Literal 181 } 182 183 type Identifier struct { 184 *BaseExpr 185 Literal string 186 Quoted bool 187 } 188 189 func (i Identifier) String() string { 190 if i.Quoted { 191 return option.QuoteIdentifier(i.Literal) 192 } 193 return i.Literal 194 } 195 196 type Constant struct { 197 *BaseExpr 198 Space string 199 Name string 200 } 201 202 func (e Constant) String() string { 203 return strings.ToUpper(e.Space) + ConstantDelimiter + strings.ToUpper(e.Name) 204 } 205 206 type FieldReference struct { 207 *BaseExpr 208 View Identifier 209 Column QueryExpression 210 } 211 212 func (e FieldReference) String() string { 213 s := e.Column.String() 214 if 0 < len(e.View.Literal) { 215 s = e.View.String() + "." + s 216 } 217 return s 218 } 219 220 type ColumnNumber struct { 221 *BaseExpr 222 View Identifier 223 Number *value.Integer 224 } 225 226 func (e ColumnNumber) String() string { 227 return e.View.String() + "." + e.Number.String() 228 } 229 230 type Parentheses struct { 231 *BaseExpr 232 Expr QueryExpression 233 } 234 235 func (p Parentheses) String() string { 236 return putParentheses(p.Expr.String()) 237 } 238 239 type RowValue struct { 240 *BaseExpr 241 Value QueryExpression 242 } 243 244 func (e RowValue) String() string { 245 return e.Value.String() 246 } 247 248 type ValueList struct { 249 *BaseExpr 250 Values []QueryExpression 251 } 252 253 func (e ValueList) String() string { 254 return putParentheses(listQueryExpressions(e.Values)) 255 } 256 257 type RowValueList struct { 258 *BaseExpr 259 RowValues []QueryExpression 260 } 261 262 func (e RowValueList) String() string { 263 return putParentheses(listQueryExpressions(e.RowValues)) 264 } 265 266 type SelectQuery struct { 267 *BaseExpr 268 WithClause QueryExpression 269 SelectEntity QueryExpression 270 OrderByClause QueryExpression 271 LimitClause QueryExpression 272 Context Token 273 } 274 275 func (e SelectQuery) IsForUpdate() bool { 276 return e.Context.Token == UPDATE 277 } 278 279 func (e SelectQuery) String() string { 280 s := make([]string, 0) 281 if e.WithClause != nil { 282 s = append(s, e.WithClause.String()) 283 } 284 s = append(s, e.SelectEntity.String()) 285 if e.OrderByClause != nil { 286 s = append(s, e.OrderByClause.String()) 287 } 288 if e.LimitClause != nil { 289 s = append(s, e.LimitClause.String()) 290 } 291 if e.IsForUpdate() { 292 s = append(s, keyword(FOR), e.Context.String()) 293 } 294 return joinWithSpace(s) 295 } 296 297 type SelectSet struct { 298 *BaseExpr 299 LHS QueryExpression 300 Operator Token 301 All Token 302 RHS QueryExpression 303 } 304 305 func (e SelectSet) String() string { 306 s := []string{e.LHS.String(), e.Operator.String()} 307 if !e.All.IsEmpty() { 308 s = append(s, e.All.String()) 309 } 310 s = append(s, e.RHS.String()) 311 return joinWithSpace(s) 312 } 313 314 type SelectEntity struct { 315 *BaseExpr 316 SelectClause QueryExpression 317 IntoClause QueryExpression 318 FromClause QueryExpression 319 WhereClause QueryExpression 320 GroupByClause QueryExpression 321 HavingClause QueryExpression 322 } 323 324 func (e SelectEntity) String() string { 325 s := []string{e.SelectClause.String()} 326 if e.IntoClause != nil { 327 s = append(s, e.IntoClause.String()) 328 } 329 if e.FromClause != nil { 330 s = append(s, e.FromClause.String()) 331 } 332 if e.WhereClause != nil { 333 s = append(s, e.WhereClause.String()) 334 } 335 if e.GroupByClause != nil { 336 s = append(s, e.GroupByClause.String()) 337 } 338 if e.HavingClause != nil { 339 s = append(s, e.HavingClause.String()) 340 } 341 return joinWithSpace(s) 342 } 343 344 type SelectClause struct { 345 *BaseExpr 346 Distinct Token 347 Fields []QueryExpression 348 } 349 350 func (sc SelectClause) IsDistinct() bool { 351 return sc.Distinct.Token == DISTINCT 352 } 353 354 func (sc SelectClause) String() string { 355 s := []string{keyword(SELECT)} 356 if sc.IsDistinct() { 357 s = append(s, sc.Distinct.String()) 358 } 359 s = append(s, listQueryExpressions(sc.Fields)) 360 return joinWithSpace(s) 361 } 362 363 type IntoClause struct { 364 *BaseExpr 365 Variables []Variable 366 } 367 368 func (e IntoClause) String() string { 369 vars := make([]QueryExpression, 0, len(e.Variables)) 370 for _, v := range e.Variables { 371 vars = append(vars, v) 372 } 373 return joinWithSpace([]string{keyword(INTO), listQueryExpressions(vars)}) 374 } 375 376 type FromClause struct { 377 *BaseExpr 378 Tables []QueryExpression 379 } 380 381 func (f FromClause) String() string { 382 s := []string{keyword(FROM), listQueryExpressions(f.Tables)} 383 return joinWithSpace(s) 384 } 385 386 type WhereClause struct { 387 *BaseExpr 388 Filter QueryExpression 389 } 390 391 func (w WhereClause) String() string { 392 s := []string{keyword(WHERE), w.Filter.String()} 393 return joinWithSpace(s) 394 } 395 396 type GroupByClause struct { 397 *BaseExpr 398 Items []QueryExpression 399 } 400 401 func (gb GroupByClause) String() string { 402 s := []string{keyword(GROUP), keyword(BY), listQueryExpressions(gb.Items)} 403 return joinWithSpace(s) 404 } 405 406 type HavingClause struct { 407 *BaseExpr 408 Filter QueryExpression 409 } 410 411 func (h HavingClause) String() string { 412 s := []string{keyword(HAVING), h.Filter.String()} 413 return joinWithSpace(s) 414 } 415 416 type OrderByClause struct { 417 *BaseExpr 418 Items []QueryExpression 419 } 420 421 func (ob OrderByClause) String() string { 422 s := []string{keyword(ORDER), keyword(BY), listQueryExpressions(ob.Items)} 423 return joinWithSpace(s) 424 } 425 426 type LimitClause struct { 427 *BaseExpr 428 Type Token 429 Position Token 430 Value QueryExpression 431 Unit Token 432 Restriction Token 433 OffsetClause QueryExpression 434 } 435 436 func (e LimitClause) restrictionString() []string { 437 s := make([]string, 0, 2) 438 if e.WithTies() { 439 s = append(s, keyword(WITH)) 440 } 441 return append(s, e.Restriction.String()) 442 } 443 444 func (e LimitClause) String() string { 445 s := make([]string, 0, 6) 446 447 if e.Type.Token == LIMIT { 448 s = append(s, e.Type.String()) 449 s = append(s, e.Value.String()) 450 if !e.Unit.IsEmpty() { 451 s = append(s, e.Unit.String()) 452 } 453 if !e.Restriction.IsEmpty() { 454 s = append(s, e.restrictionString()...) 455 } 456 if e.OffsetClause != nil { 457 s = append(s, e.OffsetClause.String()) 458 } 459 } else if e.Type.Token == FETCH { 460 if e.OffsetClause != nil { 461 s = append(s, e.OffsetClause.String()) 462 } 463 s = append(s, e.Type.String()) 464 s = append(s, e.Position.String()) 465 s = append(s, e.Value.String()) 466 s = append(s, e.Unit.String()) 467 if !e.Restriction.IsEmpty() { 468 s = append(s, e.restrictionString()...) 469 } 470 } else { 471 if e.OffsetClause != nil { 472 s = append(s, e.OffsetClause.String()) 473 } 474 } 475 return joinWithSpace(s) 476 } 477 478 func (e LimitClause) Percentage() bool { 479 return e.Unit.Token == PERCENT 480 } 481 482 func (e LimitClause) WithTies() bool { 483 return e.Restriction.Token == TIES 484 } 485 486 type OffsetClause struct { 487 *BaseExpr 488 Value QueryExpression 489 Unit Token 490 } 491 492 func (e OffsetClause) String() string { 493 s := make([]string, 2, 3) 494 s[0] = keyword(OFFSET) 495 s[1] = e.Value.String() 496 if !e.Unit.IsEmpty() { 497 s = append(s, e.Unit.String()) 498 } 499 return joinWithSpace(s) 500 } 501 502 type WithClause struct { 503 *BaseExpr 504 InlineTables []QueryExpression 505 } 506 507 func (e WithClause) String() string { 508 s := []string{keyword(WITH), listQueryExpressions(e.InlineTables)} 509 return joinWithSpace(s) 510 } 511 512 type InlineTable struct { 513 *BaseExpr 514 Recursive Token 515 Name Identifier 516 Fields []QueryExpression 517 Query SelectQuery 518 } 519 520 func (e InlineTable) String() string { 521 s := make([]string, 0) 522 if !e.Recursive.IsEmpty() { 523 s = append(s, e.Recursive.String()) 524 } 525 s = append(s, e.Name.String()) 526 if e.Fields != nil { 527 s = append(s, putParentheses(listQueryExpressions(e.Fields))) 528 } 529 s = append(s, keyword(AS), putParentheses(e.Query.String())) 530 return joinWithSpace(s) 531 } 532 533 func (e InlineTable) IsRecursive() bool { 534 return !e.Recursive.IsEmpty() 535 } 536 537 type Subquery struct { 538 *BaseExpr 539 Query SelectQuery 540 } 541 542 func (e Subquery) String() string { 543 return putParentheses(e.Query.String()) 544 } 545 546 type Url struct { 547 *BaseExpr 548 Raw string 549 } 550 551 func (e Url) String() string { 552 return e.Raw 553 } 554 555 type TableFunction struct { 556 *BaseExpr 557 Name string 558 Args []QueryExpression 559 } 560 561 func (e TableFunction) String() string { 562 return strings.ToUpper(e.Name) + ConstantDelimiter + putParentheses(listQueryExpressions(e.Args)) 563 } 564 565 type FormatSpecifiedFunction struct { 566 *BaseExpr 567 Type Token 568 FormatElement QueryExpression 569 Path QueryExpression 570 Args []QueryExpression 571 } 572 573 func (e FormatSpecifiedFunction) String() string { 574 allArgs := make([]QueryExpression, 0, len(e.Args)+2) 575 if e.FormatElement != nil { 576 allArgs = append(allArgs, e.FormatElement) 577 } 578 allArgs = append(allArgs, e.Path) 579 if e.Args != nil { 580 allArgs = append(allArgs, e.Args...) 581 } 582 return e.Type.String() + putParentheses(listQueryExpressions(allArgs)) 583 } 584 585 type JsonQuery struct { 586 *BaseExpr 587 JsonQuery Token 588 Query QueryExpression 589 JsonText QueryExpression 590 } 591 592 func (e JsonQuery) String() string { 593 return e.JsonQuery.String() + putParentheses(e.Query.String()+", "+e.JsonText.String()) 594 } 595 596 type Comparison struct { 597 *BaseExpr 598 LHS QueryExpression 599 Operator Token 600 RHS QueryExpression 601 } 602 603 func (c Comparison) String() string { 604 s := []string{c.LHS.String(), c.Operator.String(), c.RHS.String()} 605 return joinWithSpace(s) 606 } 607 608 type Is struct { 609 *BaseExpr 610 LHS QueryExpression 611 RHS QueryExpression 612 Negation Token 613 } 614 615 func (i Is) IsNegated() bool { 616 return !i.Negation.IsEmpty() 617 } 618 619 func (i Is) String() string { 620 s := []string{i.LHS.String(), keyword(IS)} 621 if i.IsNegated() { 622 s = append(s, i.Negation.String()) 623 } 624 s = append(s, i.RHS.String()) 625 return joinWithSpace(s) 626 } 627 628 type Between struct { 629 *BaseExpr 630 LHS QueryExpression 631 Low QueryExpression 632 High QueryExpression 633 Negation Token 634 } 635 636 func (b Between) IsNegated() bool { 637 return !b.Negation.IsEmpty() 638 } 639 640 func (b Between) String() string { 641 s := []string{b.LHS.String()} 642 if b.IsNegated() { 643 s = append(s, b.Negation.String()) 644 } 645 s = append(s, keyword(BETWEEN), b.Low.String(), keyword(AND), b.High.String()) 646 return joinWithSpace(s) 647 } 648 649 type In struct { 650 *BaseExpr 651 LHS QueryExpression 652 Values QueryExpression 653 Negation Token 654 } 655 656 func (i In) IsNegated() bool { 657 return !i.Negation.IsEmpty() 658 } 659 660 func (i In) String() string { 661 s := []string{i.LHS.String()} 662 if i.IsNegated() { 663 s = append(s, i.Negation.String()) 664 } 665 s = append(s, keyword(IN), i.Values.String()) 666 return joinWithSpace(s) 667 } 668 669 type All struct { 670 *BaseExpr 671 LHS QueryExpression 672 Operator Token 673 Values QueryExpression 674 } 675 676 func (a All) String() string { 677 s := []string{a.LHS.String(), a.Operator.String(), keyword(ALL), a.Values.String()} 678 return joinWithSpace(s) 679 } 680 681 type Any struct { 682 *BaseExpr 683 LHS QueryExpression 684 Operator Token 685 Values QueryExpression 686 } 687 688 func (a Any) String() string { 689 s := []string{a.LHS.String(), a.Operator.String(), keyword(ANY), a.Values.String()} 690 return joinWithSpace(s) 691 } 692 693 type Like struct { 694 *BaseExpr 695 LHS QueryExpression 696 Pattern QueryExpression 697 Negation Token 698 } 699 700 func (l Like) IsNegated() bool { 701 return !l.Negation.IsEmpty() 702 } 703 704 func (l Like) String() string { 705 s := []string{l.LHS.String()} 706 if l.IsNegated() { 707 s = append(s, l.Negation.String()) 708 } 709 s = append(s, keyword(LIKE), l.Pattern.String()) 710 return joinWithSpace(s) 711 } 712 713 type Exists struct { 714 *BaseExpr 715 Query Subquery 716 } 717 718 func (e Exists) String() string { 719 s := []string{keyword(EXISTS), e.Query.String()} 720 return joinWithSpace(s) 721 } 722 723 type Arithmetic struct { 724 *BaseExpr 725 LHS QueryExpression 726 Operator Token 727 RHS QueryExpression 728 } 729 730 func (a Arithmetic) String() string { 731 s := []string{a.LHS.String(), a.Operator.String(), a.RHS.String()} 732 return joinWithSpace(s) 733 } 734 735 type UnaryArithmetic struct { 736 *BaseExpr 737 Operand QueryExpression 738 Operator Token 739 } 740 741 func (e UnaryArithmetic) String() string { 742 return e.Operator.String() + e.Operand.String() 743 } 744 745 type Logic struct { 746 *BaseExpr 747 LHS QueryExpression 748 Operator Token 749 RHS QueryExpression 750 } 751 752 func (l Logic) String() string { 753 s := []string{l.LHS.String(), l.Operator.String(), l.RHS.String()} 754 return joinWithSpace(s) 755 } 756 757 type UnaryLogic struct { 758 *BaseExpr 759 Operand QueryExpression 760 Operator Token 761 } 762 763 func (e UnaryLogic) String() string { 764 if e.Operator.Token == NOT { 765 s := []string{e.Operator.String(), e.Operand.String()} 766 return joinWithSpace(s) 767 } 768 return e.Operator.String() + e.Operand.String() 769 } 770 771 type Concat struct { 772 *BaseExpr 773 Items []QueryExpression 774 } 775 776 func (c Concat) String() string { 777 s := make([]string, len(c.Items)) 778 for i, v := range c.Items { 779 s[i] = v.String() 780 } 781 return strings.Join(s, " || ") 782 } 783 784 type Function struct { 785 *BaseExpr 786 Name string 787 Args []QueryExpression 788 From Token 789 For Token 790 } 791 792 func (e Function) String() string { 793 var args string 794 if strings.EqualFold(e.Name, keyword(SUBSTRING)) && !e.From.IsEmpty() { 795 elems := make([]string, 0, 5) 796 elems = append(elems, e.Args[0].String(), e.From.String(), e.Args[1].String()) 797 if !e.For.IsEmpty() { 798 elems = append(elems, e.For.String(), e.Args[2].String()) 799 } 800 args = joinWithSpace(elems) 801 } else { 802 args = listQueryExpressions(e.Args) 803 } 804 return strings.ToUpper(e.Name) + "(" + args + ")" 805 } 806 807 type AggregateFunction struct { 808 *BaseExpr 809 Name string 810 Distinct Token 811 Args []QueryExpression 812 } 813 814 func (e AggregateFunction) String() string { 815 s := make([]string, 0) 816 if !e.Distinct.IsEmpty() { 817 s = append(s, e.Distinct.String()) 818 } 819 s = append(s, listQueryExpressions(e.Args)) 820 821 return strings.ToUpper(e.Name) + "(" + joinWithSpace(s) + ")" 822 } 823 824 func (e AggregateFunction) IsDistinct() bool { 825 return e.Distinct.Token == DISTINCT 826 } 827 828 type Table struct { 829 *BaseExpr 830 Lateral Token 831 Object QueryExpression 832 As Token 833 Alias QueryExpression 834 } 835 836 func (e Table) String() string { 837 s := make([]string, 0, 4) 838 if !e.Lateral.IsEmpty() { 839 s = append(s, e.Lateral.String()) 840 } 841 s = append(s, e.Object.String()) 842 if !e.As.IsEmpty() { 843 s = append(s, e.As.String()) 844 } 845 if e.Alias != nil { 846 s = append(s, e.Alias.String()) 847 } 848 return joinWithSpace(s) 849 } 850 851 type Join struct { 852 *BaseExpr 853 Table QueryExpression 854 JoinTable QueryExpression 855 Natural Token 856 JoinType Token 857 Direction Token 858 Condition QueryExpression 859 } 860 861 func (j Join) String() string { 862 s := []string{j.Table.String()} 863 if !j.Natural.IsEmpty() { 864 s = append(s, j.Natural.String()) 865 } 866 if !j.Direction.IsEmpty() { 867 s = append(s, j.Direction.String()) 868 } 869 if !j.JoinType.IsEmpty() { 870 s = append(s, j.JoinType.String()) 871 } 872 s = append(s, keyword(JOIN), j.JoinTable.String()) 873 if j.Condition != nil { 874 s = append(s, j.Condition.String()) 875 } 876 return joinWithSpace(s) 877 } 878 879 type JoinCondition struct { 880 *BaseExpr 881 On QueryExpression 882 Using []QueryExpression 883 } 884 885 func (jc JoinCondition) String() string { 886 var s []string 887 if jc.On != nil { 888 s = []string{keyword(ON), jc.On.String()} 889 } else { 890 s = []string{keyword(USING), putParentheses(listQueryExpressions(jc.Using))} 891 } 892 893 return joinWithSpace(s) 894 } 895 896 type Field struct { 897 *BaseExpr 898 Object QueryExpression 899 As Token 900 Alias QueryExpression 901 } 902 903 func (f Field) String() string { 904 s := []string{f.Object.String()} 905 if !f.As.IsEmpty() { 906 s = append(s, f.As.String()) 907 } 908 if f.Alias != nil { 909 s = append(s, f.Alias.String()) 910 } 911 return joinWithSpace(s) 912 } 913 914 func (f Field) Name() string { 915 if f.Alias != nil { 916 return f.Alias.(Identifier).Literal 917 } 918 if t, ok := f.Object.(PrimitiveType); ok { 919 return t.Literal 920 } 921 if fr, ok := f.Object.(FieldReference); ok { 922 if col, ok := fr.Column.(Identifier); ok { 923 return col.Literal 924 } 925 } 926 return f.Object.String() 927 } 928 929 type AllColumns struct { 930 *BaseExpr 931 } 932 933 func (ac AllColumns) String() string { 934 return "*" 935 } 936 937 type Dual struct { 938 *BaseExpr 939 } 940 941 func (d Dual) String() string { 942 return keyword(DUAL) 943 } 944 945 type Stdin struct { 946 *BaseExpr 947 } 948 949 func (si Stdin) String() string { 950 return keyword(STDIN) 951 } 952 953 type OrderItem struct { 954 *BaseExpr 955 Value QueryExpression 956 Direction Token 957 NullsPosition Token 958 } 959 960 func (e OrderItem) String() string { 961 s := []string{e.Value.String()} 962 if !e.Direction.IsEmpty() { 963 s = append(s, e.Direction.String()) 964 } 965 if !e.NullsPosition.IsEmpty() { 966 s = append(s, keyword(NULLS), e.NullsPosition.String()) 967 } 968 return joinWithSpace(s) 969 } 970 971 type CaseExpr struct { 972 *BaseExpr 973 Value QueryExpression 974 When []QueryExpression 975 Else QueryExpression 976 } 977 978 func (e CaseExpr) String() string { 979 s := []string{keyword(CASE)} 980 if e.Value != nil { 981 s = append(s, e.Value.String()) 982 } 983 for _, v := range e.When { 984 s = append(s, v.String()) 985 } 986 if e.Else != nil { 987 s = append(s, e.Else.String()) 988 } 989 s = append(s, keyword(END)) 990 return joinWithSpace(s) 991 } 992 993 type CaseExprWhen struct { 994 *BaseExpr 995 Condition QueryExpression 996 Result QueryExpression 997 } 998 999 func (e CaseExprWhen) String() string { 1000 s := []string{keyword(WHEN), e.Condition.String(), keyword(THEN), e.Result.String()} 1001 return joinWithSpace(s) 1002 } 1003 1004 type CaseExprElse struct { 1005 *BaseExpr 1006 Result QueryExpression 1007 } 1008 1009 func (e CaseExprElse) String() string { 1010 s := []string{keyword(ELSE), e.Result.String()} 1011 return joinWithSpace(s) 1012 } 1013 1014 type ListFunction struct { 1015 *BaseExpr 1016 Name string 1017 Distinct Token 1018 Args []QueryExpression 1019 OrderBy QueryExpression 1020 } 1021 1022 func (e ListFunction) String() string { 1023 args := make([]string, 0, 3) 1024 if !e.Distinct.IsEmpty() { 1025 args = append(args, e.Distinct.String()) 1026 } 1027 args = append(args, listQueryExpressions(e.Args)) 1028 1029 s := []string{strings.ToUpper(e.Name) + "(" + joinWithSpace(args) + ")"} 1030 if e.OrderBy != nil { 1031 s = append(s, keyword(WITHIN), keyword(GROUP), "("+e.OrderBy.String()+")") 1032 } 1033 return joinWithSpace(s) 1034 } 1035 1036 func (e ListFunction) IsDistinct() bool { 1037 return !e.Distinct.IsEmpty() 1038 } 1039 1040 type AnalyticFunction struct { 1041 *BaseExpr 1042 Name string 1043 Distinct Token 1044 Args []QueryExpression 1045 IgnoreType Token 1046 AnalyticClause AnalyticClause 1047 } 1048 1049 func (e AnalyticFunction) String() string { 1050 args := make([]string, 0, 6) 1051 if !e.Distinct.IsEmpty() { 1052 args = append(args, e.Distinct.String()) 1053 } 1054 if e.Args != nil { 1055 args = append(args, listQueryExpressions(e.Args)) 1056 } 1057 if !e.IgnoreType.IsEmpty() { 1058 args = append(args, keyword(IGNORE), e.IgnoreType.String()) 1059 } 1060 1061 s := []string{ 1062 strings.ToUpper(e.Name) + "(" + joinWithSpace(args) + ")", 1063 keyword(OVER), 1064 "(" + e.AnalyticClause.String() + ")", 1065 } 1066 return joinWithSpace(s) 1067 } 1068 1069 func (e AnalyticFunction) IsDistinct() bool { 1070 return !e.Distinct.IsEmpty() 1071 } 1072 1073 func (e AnalyticFunction) IgnoreNulls() bool { 1074 return e.IgnoreType.Token == NULLS 1075 } 1076 1077 type AnalyticClause struct { 1078 *BaseExpr 1079 PartitionClause QueryExpression 1080 OrderByClause QueryExpression 1081 WindowingClause QueryExpression 1082 } 1083 1084 func (e AnalyticClause) String() string { 1085 s := make([]string, 0) 1086 if e.PartitionClause != nil { 1087 s = append(s, e.PartitionClause.String()) 1088 } 1089 if e.OrderByClause != nil { 1090 s = append(s, e.OrderByClause.String()) 1091 } 1092 if e.WindowingClause != nil { 1093 s = append(s, e.WindowingClause.String()) 1094 } 1095 return joinWithSpace(s) 1096 } 1097 1098 func (e AnalyticClause) PartitionValues() []QueryExpression { 1099 if e.PartitionClause == nil { 1100 return nil 1101 } 1102 return e.PartitionClause.(PartitionClause).Values 1103 } 1104 1105 type PartitionClause struct { 1106 *BaseExpr 1107 Values []QueryExpression 1108 } 1109 1110 func (e PartitionClause) String() string { 1111 s := []string{keyword(PARTITION), keyword(BY), listQueryExpressions(e.Values)} 1112 return joinWithSpace(s) 1113 } 1114 1115 type WindowingClause struct { 1116 *BaseExpr 1117 FrameLow QueryExpression 1118 FrameHigh QueryExpression 1119 } 1120 1121 func (e WindowingClause) String() string { 1122 s := []string{keyword(ROWS)} 1123 if e.FrameHigh == nil { 1124 s = append(s, e.FrameLow.String()) 1125 } else { 1126 s = append(s, keyword(BETWEEN), e.FrameLow.String(), keyword(AND), e.FrameHigh.String()) 1127 } 1128 return joinWithSpace(s) 1129 } 1130 1131 type WindowFramePosition struct { 1132 *BaseExpr 1133 Direction Token 1134 Unbounded Token 1135 Offset int 1136 } 1137 1138 func (e WindowFramePosition) String() string { 1139 s := make([]string, 0, 2) 1140 if e.Direction.Token == CURRENT { 1141 s = append(s, keyword(CURRENT), keyword(ROW)) 1142 } else if !e.Unbounded.IsEmpty() { 1143 s = append(s, e.Unbounded.String(), e.Direction.String()) 1144 } else { 1145 s = append(s, strconv.Itoa(e.Offset), e.Direction.String()) 1146 } 1147 return joinWithSpace(s) 1148 } 1149 1150 type Variable struct { 1151 *BaseExpr 1152 Name string 1153 } 1154 1155 func (v Variable) String() string { 1156 return string(VariableSign) + v.Name 1157 } 1158 1159 type VariableSubstitution struct { 1160 *BaseExpr 1161 Variable Variable 1162 Value QueryExpression 1163 } 1164 1165 func (vs VariableSubstitution) String() string { 1166 return joinWithSpace([]string{vs.Variable.String(), SubstitutionOperator, vs.Value.String()}) 1167 } 1168 1169 type VariableAssignment struct { 1170 *BaseExpr 1171 Variable Variable 1172 Value QueryExpression 1173 } 1174 1175 type VariableDeclaration struct { 1176 *BaseExpr 1177 Assignments []VariableAssignment 1178 } 1179 1180 type DisposeVariable struct { 1181 *BaseExpr 1182 Variable Variable 1183 } 1184 1185 type EnvironmentVariable struct { 1186 *BaseExpr 1187 Name string 1188 Quoted bool 1189 } 1190 1191 func (e EnvironmentVariable) String() string { 1192 name := e.Name 1193 if e.Quoted { 1194 name = option.QuoteIdentifier(name) 1195 } 1196 1197 return string(VariableSign) + string(EnvironmentVariableSign) + name 1198 } 1199 1200 type RuntimeInformation struct { 1201 *BaseExpr 1202 Name string 1203 } 1204 1205 func (e RuntimeInformation) String() string { 1206 return string(VariableSign) + string(RuntimeInformationSign) + strings.ToUpper(e.Name) 1207 } 1208 1209 type Flag struct { 1210 *BaseExpr 1211 Name string 1212 } 1213 1214 func (e Flag) String() string { 1215 return string(VariableSign) + string(VariableSign) + strings.ToUpper(e.Name) 1216 } 1217 1218 type SetEnvVar struct { 1219 *BaseExpr 1220 EnvVar EnvironmentVariable 1221 Value QueryExpression 1222 } 1223 1224 type UnsetEnvVar struct { 1225 *BaseExpr 1226 EnvVar EnvironmentVariable 1227 } 1228 1229 type InsertQuery struct { 1230 *BaseExpr 1231 WithClause QueryExpression 1232 Table Table 1233 Fields []QueryExpression 1234 ValuesList []QueryExpression 1235 Query QueryExpression 1236 } 1237 1238 type UpdateQuery struct { 1239 *BaseExpr 1240 WithClause QueryExpression 1241 Tables []QueryExpression 1242 SetList []UpdateSet 1243 FromClause QueryExpression 1244 WhereClause QueryExpression 1245 } 1246 1247 type UpdateSet struct { 1248 *BaseExpr 1249 Field QueryExpression 1250 Value QueryExpression 1251 } 1252 1253 type ReplaceQuery struct { 1254 *BaseExpr 1255 WithClause QueryExpression 1256 Table Table 1257 Fields []QueryExpression 1258 Keys []QueryExpression 1259 ValuesList []QueryExpression 1260 Query QueryExpression 1261 } 1262 1263 type DeleteQuery struct { 1264 *BaseExpr 1265 WithClause QueryExpression 1266 Tables []QueryExpression 1267 FromClause FromClause 1268 WhereClause QueryExpression 1269 } 1270 1271 type CreateTable struct { 1272 *BaseExpr 1273 Table Identifier 1274 Fields []QueryExpression 1275 Query QueryExpression 1276 IfNotExists bool 1277 } 1278 1279 type AddColumns struct { 1280 *BaseExpr 1281 Table QueryExpression 1282 Columns []ColumnDefault 1283 Position Expression 1284 } 1285 1286 type ColumnDefault struct { 1287 *BaseExpr 1288 Column Identifier 1289 Value QueryExpression 1290 } 1291 1292 type ColumnPosition struct { 1293 *BaseExpr 1294 Position Token 1295 Column QueryExpression 1296 } 1297 1298 type DropColumns struct { 1299 *BaseExpr 1300 Table QueryExpression 1301 Columns []QueryExpression 1302 } 1303 1304 type RenameColumn struct { 1305 *BaseExpr 1306 Table QueryExpression 1307 Old QueryExpression 1308 New Identifier 1309 } 1310 1311 type SetTableAttribute struct { 1312 *BaseExpr 1313 Table QueryExpression 1314 Attribute Identifier 1315 Value QueryExpression 1316 } 1317 1318 type FunctionDeclaration struct { 1319 *BaseExpr 1320 Name Identifier 1321 Parameters []VariableAssignment 1322 Statements []Statement 1323 } 1324 1325 type AggregateDeclaration struct { 1326 *BaseExpr 1327 Name Identifier 1328 Cursor Identifier 1329 Parameters []VariableAssignment 1330 Statements []Statement 1331 } 1332 1333 type DisposeFunction struct { 1334 *BaseExpr 1335 Name Identifier 1336 } 1337 1338 type Return struct { 1339 *BaseExpr 1340 Value QueryExpression 1341 } 1342 1343 type Echo struct { 1344 *BaseExpr 1345 Value QueryExpression 1346 } 1347 1348 type Print struct { 1349 *BaseExpr 1350 Value QueryExpression 1351 } 1352 1353 type Printf struct { 1354 *BaseExpr 1355 Format QueryExpression 1356 Values []QueryExpression 1357 } 1358 1359 type Source struct { 1360 *BaseExpr 1361 FilePath QueryExpression 1362 } 1363 1364 type Chdir struct { 1365 *BaseExpr 1366 DirPath QueryExpression 1367 } 1368 1369 type Pwd struct { 1370 *BaseExpr 1371 } 1372 1373 type Reload struct { 1374 *BaseExpr 1375 Type Identifier 1376 } 1377 1378 type Execute struct { 1379 *BaseExpr 1380 Statements QueryExpression 1381 Values []QueryExpression 1382 } 1383 1384 type Syntax struct { 1385 *BaseExpr 1386 Keywords []QueryExpression 1387 } 1388 1389 type SetFlag struct { 1390 *BaseExpr 1391 Flag Flag 1392 Value QueryExpression 1393 } 1394 1395 type AddFlagElement struct { 1396 *BaseExpr 1397 Flag Flag 1398 Value QueryExpression 1399 } 1400 1401 type RemoveFlagElement struct { 1402 *BaseExpr 1403 Flag Flag 1404 Value QueryExpression 1405 } 1406 1407 type ShowFlag struct { 1408 *BaseExpr 1409 Flag Flag 1410 } 1411 1412 type ShowObjects struct { 1413 *BaseExpr 1414 Type Identifier 1415 } 1416 1417 type ShowFields struct { 1418 *BaseExpr 1419 Type Identifier 1420 Table QueryExpression 1421 } 1422 1423 type If struct { 1424 *BaseExpr 1425 Condition QueryExpression 1426 Statements []Statement 1427 ElseIf []ElseIf 1428 Else Else 1429 } 1430 1431 type ElseIf struct { 1432 *BaseExpr 1433 Condition QueryExpression 1434 Statements []Statement 1435 } 1436 1437 type Else struct { 1438 *BaseExpr 1439 Statements []Statement 1440 } 1441 1442 type Case struct { 1443 *BaseExpr 1444 Value QueryExpression 1445 When []CaseWhen 1446 Else CaseElse 1447 } 1448 1449 type CaseWhen struct { 1450 *BaseExpr 1451 Condition QueryExpression 1452 Statements []Statement 1453 } 1454 1455 type CaseElse struct { 1456 *BaseExpr 1457 Statements []Statement 1458 } 1459 1460 type While struct { 1461 *BaseExpr 1462 Condition QueryExpression 1463 Statements []Statement 1464 } 1465 1466 type WhileInCursor struct { 1467 *BaseExpr 1468 WithDeclaration bool 1469 Variables []Variable 1470 Cursor Identifier 1471 Statements []Statement 1472 } 1473 1474 type CursorDeclaration struct { 1475 *BaseExpr 1476 Cursor Identifier 1477 Query SelectQuery 1478 Statement Identifier 1479 } 1480 1481 type OpenCursor struct { 1482 *BaseExpr 1483 Cursor Identifier 1484 Values []ReplaceValue 1485 } 1486 1487 type CloseCursor struct { 1488 *BaseExpr 1489 Cursor Identifier 1490 } 1491 1492 type DisposeCursor struct { 1493 *BaseExpr 1494 Cursor Identifier 1495 } 1496 1497 type FetchCursor struct { 1498 *BaseExpr 1499 Position FetchPosition 1500 Cursor Identifier 1501 Variables []Variable 1502 } 1503 1504 type FetchPosition struct { 1505 *BaseExpr 1506 Position Token 1507 Number QueryExpression 1508 } 1509 1510 type CursorStatus struct { 1511 *BaseExpr 1512 Cursor Identifier 1513 Negation Token 1514 Type Token 1515 } 1516 1517 func (e CursorStatus) String() string { 1518 s := []string{keyword(CURSOR), e.Cursor.String(), keyword(IS)} 1519 if !e.Negation.IsEmpty() { 1520 s = append(s, e.Negation.String()) 1521 } 1522 if e.Type.Token == RANGE { 1523 s = append(s, keyword(IN)) 1524 } 1525 s = append(s, e.Type.String()) 1526 return joinWithSpace(s) 1527 } 1528 1529 type CursorAttrebute struct { 1530 *BaseExpr 1531 Cursor Identifier 1532 Attrebute Token 1533 } 1534 1535 func (e CursorAttrebute) String() string { 1536 s := []string{keyword(CURSOR), e.Cursor.String(), e.Attrebute.String()} 1537 return joinWithSpace(s) 1538 } 1539 1540 type ViewDeclaration struct { 1541 *BaseExpr 1542 View Identifier 1543 Fields []QueryExpression 1544 Query QueryExpression 1545 } 1546 1547 type DisposeView struct { 1548 *BaseExpr 1549 View QueryExpression 1550 } 1551 1552 type StatementPreparation struct { 1553 *BaseExpr 1554 Name Identifier 1555 Statement *value.String 1556 } 1557 1558 type ReplaceValue struct { 1559 *BaseExpr 1560 Value QueryExpression 1561 Name Identifier 1562 } 1563 1564 type ExecuteStatement struct { 1565 *BaseExpr 1566 Name Identifier 1567 Values []ReplaceValue 1568 } 1569 1570 type DisposeStatement struct { 1571 *BaseExpr 1572 Name Identifier 1573 } 1574 1575 type TransactionControl struct { 1576 *BaseExpr 1577 Token int 1578 } 1579 1580 type FlowControl struct { 1581 *BaseExpr 1582 Token int 1583 } 1584 1585 type Trigger struct { 1586 *BaseExpr 1587 Event Identifier 1588 Message QueryExpression 1589 Code value.Primary 1590 } 1591 1592 type Exit struct { 1593 *BaseExpr 1594 Code value.Primary 1595 } 1596 1597 type ExternalCommand struct { 1598 *BaseExpr 1599 Command string 1600 } 1601 1602 func putParentheses(s string) string { 1603 return "(" + s + ")" 1604 } 1605 1606 func joinWithSpace(s []string) string { 1607 return strings.Join(s, " ") 1608 } 1609 1610 func listQueryExpressions(exprs []QueryExpression) string { 1611 s := make([]string, len(exprs)) 1612 for i, v := range exprs { 1613 s[i] = v.String() 1614 } 1615 return strings.Join(s, ", ") 1616 } 1617 1618 func keyword(token int) string { 1619 s, _ := KeywordLiteral(token) 1620 return s 1621 }