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