1// Copyright 2012, Google Inc. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package sqlparser 6 7import ( 8 "errors" 9 "fmt" 10 "strconv" 11 12 "github.com/dinedal/textql/sqlparser/sqltypes" 13) 14 15// Instructions for creating new types: If a type 16// needs to satisfy an interface, declare that function 17// along with that interface. This will help users 18// identify the list of types to which they can assert 19// those interfaces. 20// If the member of a type has a string with a predefined 21// list of values, declare those values as const following 22// the type. 23// For interfaces that define dummy functions to consolidate 24// a set of types, define the function as ITypeName. 25// This will help avoid name collisions. 26 27// Parse parses the sql and returns a Statement, which 28// is the AST representation of the query. 29func Parse(sql string) (Statement, error) { 30 tokenizer := NewStringTokenizer(sql) 31 if yyParse(tokenizer) != 0 { 32 return nil, errors.New(tokenizer.LastError) 33 } 34 return tokenizer.ParseTree, nil 35} 36 37// SQLNode defines the interface for all nodes 38// generated by the parser. 39type SQLNode interface { 40 Format(buf *TrackedBuffer) 41} 42 43// String returns a string representation of an SQLNode. 44func String(node SQLNode) string { 45 buf := NewTrackedBuffer(nil) 46 buf.Myprintf("%v", node) 47 return buf.String() 48} 49 50// Statement represents a statement. 51type Statement interface { 52 IStatement() 53 SQLNode 54} 55 56func (*Union) IStatement() {} 57func (*Select) IStatement() {} 58func (*Insert) IStatement() {} 59func (*Update) IStatement() {} 60func (*Delete) IStatement() {} 61func (*Set) IStatement() {} 62func (*DDL) IStatement() {} 63func (*Other) IStatement() {} 64 65// SelectStatement any SELECT statement. 66type SelectStatement interface { 67 ISelectStatement() 68 IStatement() 69 IInsertRows() 70 SQLNode 71} 72 73func (*Select) ISelectStatement() {} 74func (*Union) ISelectStatement() {} 75 76// Select represents a SELECT statement. 77type Select struct { 78 Comments Comments 79 Distinct string 80 SelectExprs SelectExprs 81 From *From 82 Where *Where 83 GroupBy GroupBy 84 Having *Where 85 OrderBy OrderBy 86 Limit *Limit 87 Lock string 88} 89 90// Select.Distinct 91const ( 92 AST_DISTINCT = "distinct " 93) 94 95// Select.Lock 96const ( 97 AST_FOR_UPDATE = " for update" 98 AST_SHARE_MODE = " lock in share mode" 99) 100 101func (node *Select) Format(buf *TrackedBuffer) { 102 buf.Myprintf("select %v%s%v from %v%v%v%v%v%v%s", 103 node.Comments, node.Distinct, node.SelectExprs, 104 node.From, node.Where, 105 node.GroupBy, node.Having, node.OrderBy, 106 node.Limit, node.Lock) 107} 108 109// Union represents a UNION statement. 110type Union struct { 111 Type string 112 Left, Right SelectStatement 113} 114 115// Union.Type 116const ( 117 AST_UNION = "union" 118 AST_UNION_ALL = "union all" 119 AST_SET_MINUS = "minus" 120 AST_EXCEPT = "except" 121 AST_INTERSECT = "intersect" 122) 123 124func (node *Union) Format(buf *TrackedBuffer) { 125 buf.Myprintf("%v %s %v", node.Left, node.Type, node.Right) 126} 127 128// Insert represents an INSERT statement. 129type Insert struct { 130 Comments Comments 131 Table *TableName 132 Columns Columns 133 Rows InsertRows 134 OnDup OnDup 135} 136 137func (node *Insert) Format(buf *TrackedBuffer) { 138 buf.Myprintf("insert %vinto %v%v %v%v", 139 node.Comments, 140 node.Table, node.Columns, node.Rows, node.OnDup) 141} 142 143// InsertRows represents the rows for an INSERT statement. 144type InsertRows interface { 145 IInsertRows() 146 SQLNode 147} 148 149func (*Select) IInsertRows() {} 150func (*Union) IInsertRows() {} 151func (Values) IInsertRows() {} 152 153// Update represents an UPDATE statement. 154type Update struct { 155 Comments Comments 156 Table *TableName 157 Exprs UpdateExprs 158 Where *Where 159 OrderBy OrderBy 160 Limit *Limit 161} 162 163func (node *Update) Format(buf *TrackedBuffer) { 164 buf.Myprintf("update %v%v set %v%v%v%v", 165 node.Comments, node.Table, 166 node.Exprs, node.Where, node.OrderBy, node.Limit) 167} 168 169// Delete represents a DELETE statement. 170type Delete struct { 171 Comments Comments 172 Table *TableName 173 Where *Where 174 OrderBy OrderBy 175 Limit *Limit 176} 177 178func (node *Delete) Format(buf *TrackedBuffer) { 179 buf.Myprintf("delete %vfrom %v%v%v%v", 180 node.Comments, 181 node.Table, node.Where, node.OrderBy, node.Limit) 182} 183 184// Set represents a SET statement. 185type Set struct { 186 Comments Comments 187 Exprs UpdateExprs 188} 189 190func (node *Set) Format(buf *TrackedBuffer) { 191 buf.Myprintf("set %v%v", node.Comments, node.Exprs) 192} 193 194// DDL represents a CREATE, ALTER, DROP or RENAME statement. 195// Table is set for AST_ALTER, AST_DROP, AST_RENAME. 196// NewName is set for AST_ALTER, AST_CREATE, AST_RENAME. 197type DDL struct { 198 Action string 199 Table []byte 200 NewName []byte 201} 202 203const ( 204 AST_CREATE = "create" 205 AST_ALTER = "alter" 206 AST_DROP = "drop" 207 AST_RENAME = "rename" 208) 209 210func (node *DDL) Format(buf *TrackedBuffer) { 211 switch node.Action { 212 case AST_CREATE: 213 buf.Myprintf("%s table %s", node.Action, node.NewName) 214 case AST_RENAME: 215 buf.Myprintf("%s table %s %s", node.Action, node.Table, node.NewName) 216 default: 217 buf.Myprintf("%s table %s", node.Action, node.Table) 218 } 219} 220 221// Other represents a SHOW, DESCRIBE, or EXPLAIN statement. 222// It should be used only as an indicator. It does not contain 223// the full AST for the statement. 224type Other struct{} 225 226func (node *Other) Format(buf *TrackedBuffer) { 227 buf.WriteString("other") 228} 229 230// Comments represents a list of comments. 231type Comments [][]byte 232 233func (node Comments) Format(buf *TrackedBuffer) { 234 for _, c := range node { 235 buf.Myprintf("%s ", c) 236 } 237} 238 239// SelectExprs represents SELECT expressions. 240type SelectExprs []SelectExpr 241 242func (node SelectExprs) Format(buf *TrackedBuffer) { 243 var prefix string 244 for _, n := range node { 245 buf.Myprintf("%s%v", prefix, n) 246 prefix = ", " 247 } 248} 249 250// SelectExpr represents a SELECT expression. 251type SelectExpr interface { 252 ISelectExpr() 253 SQLNode 254} 255 256func (*StarExpr) ISelectExpr() {} 257func (*NonStarExpr) ISelectExpr() {} 258 259// StarExpr defines a '*' or 'table.*' expression. 260type StarExpr struct { 261 TableName []byte 262} 263 264func (node *StarExpr) Format(buf *TrackedBuffer) { 265 if node.TableName != nil { 266 buf.Myprintf("%s.", node.TableName) 267 } 268 buf.Myprintf("*") 269} 270 271// NonStarExpr defines a non-'*' select expr. 272type NonStarExpr struct { 273 Expr Expr 274 As []byte 275} 276 277func (node *NonStarExpr) Format(buf *TrackedBuffer) { 278 buf.Myprintf("%v", node.Expr) 279 if node.As != nil { 280 buf.Myprintf(" as %s", node.As) 281 } 282} 283 284// Columns represents an insert column list. 285// The syntax for Columns is a subset of SelectExprs. 286// So, it's castable to a SelectExprs and can be analyzed 287// as such. 288type Columns []SelectExpr 289 290func (node Columns) Format(buf *TrackedBuffer) { 291 if node == nil { 292 return 293 } 294 buf.Myprintf("(%v)", SelectExprs(node)) 295} 296 297// TableExprs represents a list of table expressions. 298type TableExprs []TableExpr 299 300func (node TableExprs) Format(buf *TrackedBuffer) { 301 var prefix string 302 for _, n := range node { 303 buf.Myprintf("%s%v", prefix, n) 304 prefix = ", " 305 } 306} 307 308// TableExpr represents a table expression. 309type TableExpr interface { 310 ITableExpr() 311 SQLNode 312} 313 314func (*AliasedTableExpr) ITableExpr() {} 315func (*ParenTableExpr) ITableExpr() {} 316func (*JoinTableExpr) ITableExpr() {} 317 318// AliasedTableExpr represents a table expression 319// coupled with an optional alias or index hint. 320type AliasedTableExpr struct { 321 Expr SimpleTableExpr 322 As []byte 323 Hints *IndexHints 324} 325 326func (node *AliasedTableExpr) Format(buf *TrackedBuffer) { 327 buf.Myprintf("%v", node.Expr) 328 if node.As != nil { 329 buf.Myprintf(" as %s", node.As) 330 } 331 if node.Hints != nil { 332 // Hint node provides the space padding. 333 buf.Myprintf("%v", node.Hints) 334 } 335} 336 337// SimpleTableExpr represents a simple table expression. 338type SimpleTableExpr interface { 339 ISimpleTableExpr() 340 SQLNode 341} 342 343func (*TableName) ISimpleTableExpr() {} 344func (*Subquery) ISimpleTableExpr() {} 345 346// TableName represents a table name. 347type TableName struct { 348 Name, Qualifier []byte 349} 350 351func (node *TableName) Format(buf *TrackedBuffer) { 352 if node.Qualifier != nil { 353 escape(buf, node.Qualifier) 354 buf.Myprintf(".") 355 } 356 escape(buf, node.Name) 357} 358 359// ParenTableExpr represents a parenthesized TableExpr. 360type ParenTableExpr struct { 361 Expr TableExpr 362} 363 364func (node *ParenTableExpr) Format(buf *TrackedBuffer) { 365 buf.Myprintf("(%v)", node.Expr) 366} 367 368// JoinTableExpr represents a TableExpr that's a JOIN operation. 369type JoinTableExpr struct { 370 LeftExpr TableExpr 371 Join string 372 RightExpr TableExpr 373 On BoolExpr 374} 375 376// JoinTableExpr.Join 377const ( 378 AST_JOIN = "join" 379 AST_STRAIGHT_JOIN = "straight_join" 380 AST_LEFT_JOIN = "left join" 381 AST_RIGHT_JOIN = "right join" 382 AST_CROSS_JOIN = "cross join" 383 AST_NATURAL_JOIN = "natural join" 384) 385 386func (node *JoinTableExpr) Format(buf *TrackedBuffer) { 387 buf.Myprintf("%v %s %v", node.LeftExpr, node.Join, node.RightExpr) 388 if node.On != nil { 389 buf.Myprintf(" on %v", node.On) 390 } 391} 392 393// IndexHints represents a list of index hints. 394type IndexHints struct { 395 Type string 396 Indexes [][]byte 397} 398 399const ( 400 AST_USE = "use" 401 AST_IGNORE = "ignore" 402 AST_FORCE = "force" 403) 404 405func (node *IndexHints) Format(buf *TrackedBuffer) { 406 buf.Myprintf(" %s index ", node.Type) 407 prefix := "(" 408 for _, n := range node.Indexes { 409 buf.Myprintf("%s%s", prefix, n) 410 prefix = ", " 411 } 412 buf.Myprintf(")") 413} 414 415// Where represents a WHERE or HAVING clause. 416type Where struct { 417 Type string 418 Expr BoolExpr 419} 420 421// Where.Type 422const ( 423 AST_WHERE = "where" 424 AST_HAVING = "having" 425) 426 427// NewWhere creates a WHERE or HAVING clause out 428// of a BoolExpr. If the expression is nil, it returns nil. 429func NewWhere(typ string, expr BoolExpr) *Where { 430 if expr == nil { 431 return nil 432 } 433 return &Where{Type: typ, Expr: expr} 434} 435 436func (node *Where) Format(buf *TrackedBuffer) { 437 if node == nil || node.Expr == nil { 438 return 439 } 440 buf.Myprintf(" %s %v", node.Type, node.Expr) 441} 442 443// From represents a FROM clause 444type From struct { 445 Type string 446 Expr TableExprs 447} 448 449// From.Type 450const ( 451 AST_FROM = "from" 452) 453 454// NewFrom creates a FROM clause 455// of a table list expression. If the expression is nil, it returns nil. 456func NewFrom(typ string, expr TableExprs) *From { 457 if expr == nil { 458 return nil 459 } 460 return &From{Type: typ, Expr: expr} 461} 462 463func (node *From) Format(buf *TrackedBuffer) { 464 if node == nil || node.Expr == nil { 465 return 466 } 467 buf.Myprintf("%v", node.Expr) 468} 469 470// Expr represents an expression. 471type Expr interface { 472 IExpr() 473 SQLNode 474} 475 476func (*AndExpr) IExpr() {} 477func (*OrExpr) IExpr() {} 478func (*NotExpr) IExpr() {} 479func (*ParenBoolExpr) IExpr() {} 480func (*ComparisonExpr) IExpr() {} 481func (*RangeCond) IExpr() {} 482func (*NullCheck) IExpr() {} 483func (*ExistsExpr) IExpr() {} 484func (*KeyrangeExpr) IExpr() {} 485func (StrVal) IExpr() {} 486func (NumVal) IExpr() {} 487func (ValArg) IExpr() {} 488func (*NullVal) IExpr() {} 489func (*ColName) IExpr() {} 490func (ValTuple) IExpr() {} 491func (*Subquery) IExpr() {} 492func (ListArg) IExpr() {} 493func (*BinaryExpr) IExpr() {} 494func (*UnaryExpr) IExpr() {} 495func (*FuncExpr) IExpr() {} 496func (*CaseExpr) IExpr() {} 497 498// BoolExpr represents a boolean expression. 499type BoolExpr interface { 500 IBoolExpr() 501 Expr 502} 503 504func (*AndExpr) IBoolExpr() {} 505func (*OrExpr) IBoolExpr() {} 506func (*NotExpr) IBoolExpr() {} 507func (*ParenBoolExpr) IBoolExpr() {} 508func (*ComparisonExpr) IBoolExpr() {} 509func (*RangeCond) IBoolExpr() {} 510func (*NullCheck) IBoolExpr() {} 511func (*ExistsExpr) IBoolExpr() {} 512func (*KeyrangeExpr) IBoolExpr() {} 513 514// AndExpr represents an AND expression. 515type AndExpr struct { 516 Left, Right BoolExpr 517} 518 519func (node *AndExpr) Format(buf *TrackedBuffer) { 520 buf.Myprintf("%v and %v", node.Left, node.Right) 521} 522 523// OrExpr represents an OR expression. 524type OrExpr struct { 525 Left, Right BoolExpr 526} 527 528func (node *OrExpr) Format(buf *TrackedBuffer) { 529 buf.Myprintf("%v or %v", node.Left, node.Right) 530} 531 532// NotExpr represents a NOT expression. 533type NotExpr struct { 534 Expr BoolExpr 535} 536 537func (node *NotExpr) Format(buf *TrackedBuffer) { 538 buf.Myprintf("not %v", node.Expr) 539} 540 541// ParenBoolExpr represents a parenthesized boolean expression. 542type ParenBoolExpr struct { 543 Expr BoolExpr 544} 545 546func (node *ParenBoolExpr) Format(buf *TrackedBuffer) { 547 buf.Myprintf("(%v)", node.Expr) 548} 549 550// ComparisonExpr represents a two-value comparison expression. 551type ComparisonExpr struct { 552 Operator string 553 Left, Right ValExpr 554} 555 556// ComparisonExpr.Operator 557const ( 558 AST_EQ = "=" 559 AST_LT = "<" 560 AST_GT = ">" 561 AST_LE = "<=" 562 AST_GE = ">=" 563 AST_NE = "!=" 564 AST_NSE = "<=>" 565 AST_IN = "in" 566 AST_NOT_IN = "not in" 567 AST_LIKE = "like" 568 AST_NOT_LIKE = "not like" 569) 570 571func (node *ComparisonExpr) Format(buf *TrackedBuffer) { 572 buf.Myprintf("%v %s %v", node.Left, node.Operator, node.Right) 573} 574 575// RangeCond represents a BETWEEN or a NOT BETWEEN expression. 576type RangeCond struct { 577 Operator string 578 Left ValExpr 579 From, To ValExpr 580} 581 582// RangeCond.Operator 583const ( 584 AST_BETWEEN = "between" 585 AST_NOT_BETWEEN = "not between" 586) 587 588func (node *RangeCond) Format(buf *TrackedBuffer) { 589 buf.Myprintf("%v %s %v and %v", node.Left, node.Operator, node.From, node.To) 590} 591 592// NullCheck represents an IS NULL or an IS NOT NULL expression. 593type NullCheck struct { 594 Operator string 595 Expr ValExpr 596} 597 598// NullCheck.Operator 599const ( 600 AST_IS_NULL = "is null" 601 AST_IS_NOT_NULL = "is not null" 602) 603 604func (node *NullCheck) Format(buf *TrackedBuffer) { 605 buf.Myprintf("%v %s", node.Expr, node.Operator) 606} 607 608// ExistsExpr represents an EXISTS expression. 609type ExistsExpr struct { 610 Subquery *Subquery 611} 612 613func (node *ExistsExpr) Format(buf *TrackedBuffer) { 614 buf.Myprintf("exists %v", node.Subquery) 615} 616 617// KeyrangeExpr represents a KEYRANGE expression. 618type KeyrangeExpr struct { 619 Start, End ValExpr 620} 621 622func (node *KeyrangeExpr) Format(buf *TrackedBuffer) { 623 buf.Myprintf("keyrange(%v, %v)", node.Start, node.End) 624} 625 626// ValExpr represents a value expression. 627type ValExpr interface { 628 IValExpr() 629 Expr 630} 631 632func (StrVal) IValExpr() {} 633func (NumVal) IValExpr() {} 634func (ValArg) IValExpr() {} 635func (*NullVal) IValExpr() {} 636func (*ColName) IValExpr() {} 637func (ValTuple) IValExpr() {} 638func (*Subquery) IValExpr() {} 639func (ListArg) IValExpr() {} 640func (*BinaryExpr) IValExpr() {} 641func (*UnaryExpr) IValExpr() {} 642func (*FuncExpr) IValExpr() {} 643func (*CaseExpr) IValExpr() {} 644 645// StrVal represents a string value. 646type StrVal []byte 647 648func (node StrVal) Format(buf *TrackedBuffer) { 649 s := sqltypes.MakeString([]byte(node)) 650 s.EncodeSql(buf) 651} 652 653// NumVal represents a number. 654type NumVal []byte 655 656func (node NumVal) Format(buf *TrackedBuffer) { 657 buf.Myprintf("%s", []byte(node)) 658} 659 660// ValArg represents a named bind var argument. 661type ValArg []byte 662 663func (node ValArg) Format(buf *TrackedBuffer) { 664 buf.WriteArg(string(node)) 665} 666 667// NullVal represents a NULL value. 668type NullVal struct{} 669 670func (node *NullVal) Format(buf *TrackedBuffer) { 671 buf.Myprintf("null") 672} 673 674// ColName represents a column name. 675type ColName struct { 676 Name, Qualifier []byte 677} 678 679func (node *ColName) Format(buf *TrackedBuffer) { 680 if node.Qualifier != nil { 681 escape(buf, node.Qualifier) 682 buf.Myprintf(".") 683 } 684 escape(buf, node.Name) 685} 686 687func escape(buf *TrackedBuffer, name []byte) { 688 if _, ok := keywords[string(name)]; ok { 689 buf.Myprintf("`%s`", name) 690 } else { 691 buf.Myprintf("%s", name) 692 } 693} 694 695// ColTuple represents a list of column values. 696// It can be ValTuple, Subquery, ListArg. 697type ColTuple interface { 698 IColTuple() 699 ValExpr 700} 701 702func (ValTuple) IColTuple() {} 703func (*Subquery) IColTuple() {} 704func (ListArg) IColTuple() {} 705 706// ValTuple represents a tuple of actual values. 707type ValTuple ValExprs 708 709func (node ValTuple) Format(buf *TrackedBuffer) { 710 buf.Myprintf("(%v)", ValExprs(node)) 711} 712 713// ValExprs represents a list of value expressions. 714// It's not a valid expression because it's not parenthesized. 715type ValExprs []ValExpr 716 717func (node ValExprs) Format(buf *TrackedBuffer) { 718 var prefix string 719 for _, n := range node { 720 buf.Myprintf("%s%v", prefix, n) 721 prefix = ", " 722 } 723} 724 725// Subquery represents a subquery. 726type Subquery struct { 727 Select SelectStatement 728} 729 730func (node *Subquery) Format(buf *TrackedBuffer) { 731 buf.Myprintf("(%v)", node.Select) 732} 733 734// ListArg represents a named list argument. 735type ListArg []byte 736 737func (node ListArg) Format(buf *TrackedBuffer) { 738 buf.WriteArg(string(node)) 739} 740 741// BinaryExpr represents a binary value expression. 742type BinaryExpr struct { 743 Operator byte 744 Left, Right Expr 745} 746 747// BinaryExpr.Operator 748const ( 749 AST_BITAND = '&' 750 AST_BITOR = '|' 751 AST_BITXOR = '^' 752 AST_PLUS = '+' 753 AST_MINUS = '-' 754 AST_MULT = '*' 755 AST_DIV = '/' 756 AST_MOD = '%' 757) 758 759func (node *BinaryExpr) Format(buf *TrackedBuffer) { 760 buf.Myprintf("%v%c%v", node.Left, node.Operator, node.Right) 761} 762 763// UnaryExpr represents a unary value expression. 764type UnaryExpr struct { 765 Operator byte 766 Expr Expr 767} 768 769// UnaryExpr.Operator 770const ( 771 AST_UPLUS = '+' 772 AST_UMINUS = '-' 773 AST_TILDA = '~' 774) 775 776func (node *UnaryExpr) Format(buf *TrackedBuffer) { 777 buf.Myprintf("%c%v", node.Operator, node.Expr) 778} 779 780// FuncExpr represents a function call. 781type FuncExpr struct { 782 Name []byte 783 Distinct bool 784 Exprs SelectExprs 785} 786 787func (node *FuncExpr) Format(buf *TrackedBuffer) { 788 var distinct string 789 if node.Distinct { 790 distinct = "distinct " 791 } 792 buf.Myprintf("%s(%s%v)", node.Name, distinct, node.Exprs) 793} 794 795// Aggregates is a map of all aggregate functions. 796var Aggregates = map[string]bool{ 797 "avg": true, 798 "bit_and": true, 799 "bit_or": true, 800 "bit_xor": true, 801 "count": true, 802 "group_concat": true, 803 "max": true, 804 "min": true, 805 "std": true, 806 "stddev_pop": true, 807 "stddev_samp": true, 808 "stddev": true, 809 "sum": true, 810 "var_pop": true, 811 "var_samp": true, 812 "variance": true, 813} 814 815func (node *FuncExpr) IsAggregate() bool { 816 return Aggregates[string(node.Name)] 817} 818 819// CaseExpr represents a CASE expression. 820type CaseExpr struct { 821 Expr ValExpr 822 Whens []*When 823 Else ValExpr 824} 825 826func (node *CaseExpr) Format(buf *TrackedBuffer) { 827 buf.Myprintf("case ") 828 if node.Expr != nil { 829 buf.Myprintf("%v ", node.Expr) 830 } 831 for _, when := range node.Whens { 832 buf.Myprintf("%v ", when) 833 } 834 if node.Else != nil { 835 buf.Myprintf("else %v ", node.Else) 836 } 837 buf.Myprintf("end") 838} 839 840// When represents a WHEN sub-expression. 841type When struct { 842 Cond BoolExpr 843 Val ValExpr 844} 845 846func (node *When) Format(buf *TrackedBuffer) { 847 buf.Myprintf("when %v then %v", node.Cond, node.Val) 848} 849 850// GroupBy represents a GROUP BY clause. 851type GroupBy []ValExpr 852 853func (node GroupBy) Format(buf *TrackedBuffer) { 854 prefix := " group by " 855 for _, n := range node { 856 buf.Myprintf("%s%v", prefix, n) 857 prefix = ", " 858 } 859} 860 861// OrderBy represents an ORDER By clause. 862type OrderBy []*Order 863 864func (node OrderBy) Format(buf *TrackedBuffer) { 865 prefix := " order by " 866 for _, n := range node { 867 buf.Myprintf("%s%v", prefix, n) 868 prefix = ", " 869 } 870} 871 872// Order represents an ordering expression. 873type Order struct { 874 Expr ValExpr 875 Direction string 876} 877 878// Order.Direction 879const ( 880 AST_ASC = "asc" 881 AST_DESC = "desc" 882) 883 884func (node *Order) Format(buf *TrackedBuffer) { 885 buf.Myprintf("%v %s", node.Expr, node.Direction) 886} 887 888// Limit represents a LIMIT clause. 889type Limit struct { 890 Offset, Rowcount ValExpr 891} 892 893func (node *Limit) Format(buf *TrackedBuffer) { 894 if node == nil { 895 return 896 } 897 buf.Myprintf(" limit ") 898 if node.Offset != nil { 899 buf.Myprintf("%v, ", node.Offset) 900 } 901 buf.Myprintf("%v", node.Rowcount) 902} 903 904// Limits returns the values of the LIMIT clause as interfaces. 905// The returned values can be nil for absent field, string for 906// bind variable names, or int64 for an actual number. 907// Otherwise, it's an error. 908func (node *Limit) Limits() (offset, rowcount interface{}, err error) { 909 if node == nil { 910 return nil, nil, nil 911 } 912 switch v := node.Offset.(type) { 913 case NumVal: 914 o, err := strconv.ParseInt(string(v), 0, 64) 915 if err != nil { 916 return nil, nil, err 917 } 918 if o < 0 { 919 return nil, nil, fmt.Errorf("negative offset: %d", o) 920 } 921 offset = o 922 case ValArg: 923 offset = string(v) 924 case nil: 925 // pass 926 default: 927 return nil, nil, fmt.Errorf("unexpected node for offset: %+v", v) 928 } 929 switch v := node.Rowcount.(type) { 930 case NumVal: 931 rc, err := strconv.ParseInt(string(v), 0, 64) 932 if err != nil { 933 return nil, nil, err 934 } 935 if rc < 0 { 936 return nil, nil, fmt.Errorf("negative limit: %d", rc) 937 } 938 rowcount = rc 939 case ValArg: 940 rowcount = string(v) 941 default: 942 return nil, nil, fmt.Errorf("unexpected node for rowcount: %+v", v) 943 } 944 return offset, rowcount, nil 945} 946 947// Values represents a VALUES clause. 948type Values []RowTuple 949 950func (node Values) Format(buf *TrackedBuffer) { 951 prefix := "values " 952 for _, n := range node { 953 buf.Myprintf("%s%v", prefix, n) 954 prefix = ", " 955 } 956} 957 958// RowTuple represents a row of values. It can be ValTuple, Subquery. 959type RowTuple interface { 960 IRowTuple() 961 ValExpr 962} 963 964func (ValTuple) IRowTuple() {} 965func (*Subquery) IRowTuple() {} 966 967// UpdateExprs represents a list of update expressions. 968type UpdateExprs []*UpdateExpr 969 970func (node UpdateExprs) Format(buf *TrackedBuffer) { 971 var prefix string 972 for _, n := range node { 973 buf.Myprintf("%s%v", prefix, n) 974 prefix = ", " 975 } 976} 977 978// UpdateExpr represents an update expression. 979type UpdateExpr struct { 980 Name *ColName 981 Expr ValExpr 982} 983 984func (node *UpdateExpr) Format(buf *TrackedBuffer) { 985 buf.Myprintf("%v = %v", node.Name, node.Expr) 986} 987 988// OnDup represents an ON DUPLICATE KEY clause. 989type OnDup UpdateExprs 990 991func (node OnDup) Format(buf *TrackedBuffer) { 992 if node == nil { 993 return 994 } 995 buf.Myprintf(" on duplicate key update %v", UpdateExprs(node)) 996} 997