GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sqlparser/ast.go at master · lntoly/sqlparser Expand file tree/ Copy pathast.goMore file actions 3450 lines (3019 loc) · 73.7 KB / Copy pathast.goFile metadata and controls3450 lines (3019 loc) · 73.7 KB 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 997 998 999 1000 /* Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package sqlparser import ( "bytes" "encoding/hex" "encoding/json" "fmt" "io" "log" "strings" "github.com/xwb1989/sqlparser/dependency/querypb" "github.com/xwb1989/sqlparser/dependency/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 in full and returns a Statement, which // is the AST representation of the query. If a DDL statement // is partially parsed but still contains a syntax error, the // error is ignored and the DDL is returned anyway. func Parse(sql string) (Statement, error) { tokenizer := NewStringTokenizer(sql) if yyParse(tokenizer) != 0 { if tokenizer.partialDDL != nil { log.Printf("ignoring error parsing DDL '%s': %v", sql, tokenizer.LastError) tokenizer.ParseTree = tokenizer.partialDDL return tokenizer.ParseTree, nil } return nil, tokenizer.LastError } return tokenizer.ParseTree, nil } // ParseStrictDDL is the same as Parse except it errors on // partially parsed DDL statements. func ParseStrictDDL(sql string) (Statement, error) { tokenizer := NewStringTokenizer(sql) if yyParse(tokenizer) != 0 { return nil, tokenizer.LastError } return tokenizer.ParseTree, nil } // ParseNext parses a single SQL statement from the tokenizer // returning a Statement which is the AST representation of the query. // The tokenizer will always read up to the end of the statement, allowing for // the next call to ParseNext to parse any subsequent SQL statements. When // there are no more statements to parse, a error of io.EOF is returned. func ParseNext(tokenizer *Tokenizer) (Statement, error) { if tokenizer.lastChar == ';' { tokenizer.next() tokenizer.skipBlank() } if tokenizer.lastChar == eofChar { return nil, io.EOF } tokenizer.reset() tokenizer.multi = true if yyParse(tokenizer) != 0 { if tokenizer.partialDDL != nil { tokenizer.ParseTree = tokenizer.partialDDL return tokenizer.ParseTree, nil } return nil, tokenizer.LastError } return tokenizer.ParseTree, nil } // SplitStatement returns the first sql statement up to either a ; or EOF // and the remainder from the given buffer func SplitStatement(blob string) (string, string, error) { tokenizer := NewStringTokenizer(blob) tkn := 0 for { tkn, _ = tokenizer.Scan() if tkn == 0 || tkn == ';' || tkn == eofChar { break } } if tokenizer.LastError != nil { return "", "", tokenizer.LastError } if tkn == ';' { return blob[:tokenizer.Position-2], blob[tokenizer.Position-1:], nil } return blob, "", nil } // SplitStatementToPieces split raw sql statement that may have multi sql pieces to sql pieces // returns the sql pieces blob contains; or error if sql cannot be parsed func SplitStatementToPieces(blob string) (pieces []string, err error) { pieces = make([]string, 0, 16) tokenizer := NewStringTokenizer(blob) tkn := 0 var stmt string stmtBegin := 0 for { tkn, _ = tokenizer.Scan() if tkn == ';' { stmt = blob[stmtBegin : tokenizer.Position-2] pieces = append(pieces, stmt) stmtBegin = tokenizer.Position - 1 } else if tkn == 0 || tkn == eofChar { blobTail := tokenizer.Position - 2 if stmtBegin < blobTail { stmt = blob[stmtBegin : blobTail+1] pieces = append(pieces, stmt) } break } } err = tokenizer.LastError return } // SQLNode defines the interface for all nodes // generated by the parser. type SQLNode interface { Format(buf *TrackedBuffer) // walkSubtree calls visit on all underlying nodes // of the subtree, but not the current one. Walking // must be interrupted if visit returns an error. walkSubtree(visit Visit) error } // Visit defines the signature of a function that // can be used to visit all nodes of a parse tree. type Visit func(node SQLNode) (kontinue bool, err error) // Walk calls visit on every node. // If visit returns true, the underlying nodes // are also visited. If it returns an error, walking // is interrupted, and the error is returned. func Walk(visit Visit, nodes ...SQLNode) error { for _, node := range nodes { if node == nil { continue } kontinue, err := visit(node) if err != nil { return err } if kontinue { err = node.walkSubtree(visit) if err != nil { return err } } } return nil } // String returns a string representation of an SQLNode. func String(node SQLNode) string { if node == nil { return "<nil>" } buf := NewTrackedBuffer(nil) buf.Myprintf("%v", node) return buf.String() } // Append appends the SQLNode to the buffer. func Append(buf *bytes.Buffer, node SQLNode) { tbuf := &TrackedBuffer{ Buffer: buf, } node.Format(tbuf) } // Statement represents a statement. type Statement interface { iStatement() SQLNode } func (*Union) iStatement() {} func (*Select) iStatement() {} func (*Stream) iStatement() {} func (*Insert) iStatement() {} func (*Update) iStatement() {} func (*Delete) iStatement() {} func (*Set) iStatement() {} func (*DBDDL) iStatement() {} func (*DDL) iStatement() {} func (*Show) iStatement() {} func (*Use) iStatement() {} func (*Begin) iStatement() {} func (*Commit) iStatement() {} func (*Rollback) iStatement() {} func (*OtherRead) iStatement() {} func (*OtherAdmin) iStatement() {} // ParenSelect can actually not be a top level statement, // but we have to allow it because it's a requirement // of SelectStatement. func (*ParenSelect) iStatement() {} // SelectStatement any SELECT statement. type SelectStatement interface { iSelectStatement() iStatement() iInsertRows() AddOrder(*Order) SetLimit(*Limit) SQLNode } func (*Select) iSelectStatement() {} func (*Union) iSelectStatement() {} func (*ParenSelect) iSelectStatement() {} // Select represents a SELECT statement. type Select struct { Cache string Comments Comments Distinct string Hints string SelectExprs SelectExprs From TableExprs Where *Where GroupBy GroupBy Having *Where OrderBy OrderBy Limit *Limit Lock string } // Select.Distinct const ( DistinctStr = "distinct " StraightJoinHint = "straight_join " ) // Select.Lock const ( ForUpdateStr = " for update" ShareModeStr = " lock in share mode" ) // Select.Cache const ( SQLCacheStr = "sql_cache " SQLNoCacheStr = "sql_no_cache " ) // AddOrder adds an order by element func (node *Select) AddOrder(order *Order) { node.OrderBy = append(node.OrderBy, order) } // SetLimit sets the limit clause func (node *Select) SetLimit(limit *Limit) { node.Limit = limit } // Format formats the node. func (node *Select) Format(buf *TrackedBuffer) { buf.Myprintf("select %v%s%s%s%v from %v%v%v%v%v%v%s", node.Comments, node.Cache, node.Distinct, node.Hints, node.SelectExprs, node.From, node.Where, node.GroupBy, node.Having, node.OrderBy, node.Limit, node.Lock) } func (node *Select) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Comments, node.SelectExprs, node.From, node.Where, node.GroupBy, node.Having, node.OrderBy, node.Limit, ) } // AddWhere adds the boolean expression to the // WHERE clause as an AND condition. If the expression // is an OR clause, it parenthesizes it. Currently, // the OR operator is the only one that's lower precedence // than AND. func (node *Select) AddWhere(expr Expr) { if _, ok := expr.(*OrExpr); ok { expr = &ParenExpr{Expr: expr} } if node.Where == nil { node.Where = &Where{ Type: WhereStr, Expr: expr, } return } node.Where.Expr = &AndExpr{ Left: node.Where.Expr, Right: expr, } return } // AddHaving adds the boolean expression to the // HAVING clause as an AND condition. If the expression // is an OR clause, it parenthesizes it. Currently, // the OR operator is the only one that's lower precedence // than AND. func (node *Select) AddHaving(expr Expr) { if _, ok := expr.(*OrExpr); ok { expr = &ParenExpr{Expr: expr} } if node.Having == nil { node.Having = &Where{ Type: HavingStr, Expr: expr, } return } node.Having.Expr = &AndExpr{ Left: node.Having.Expr, Right: expr, } return } // ParenSelect is a parenthesized SELECT statement. type ParenSelect struct { Select SelectStatement } // AddOrder adds an order by element func (node *ParenSelect) AddOrder(order *Order) { panic("unreachable") } // SetLimit sets the limit clause func (node *ParenSelect) SetLimit(limit *Limit) { panic("unreachable") } // Format formats the node. func (node *ParenSelect) Format(buf *TrackedBuffer) { buf.Myprintf("(%v)", node.Select) } func (node *ParenSelect) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Select, ) } // Union represents a UNION statement. type Union struct { Type string Left, Right SelectStatement OrderBy OrderBy Limit *Limit Lock string } // Union.Type const ( UnionStr = "union" UnionAllStr = "union all" UnionDistinctStr = "union distinct" ) // AddOrder adds an order by element func (node *Union) AddOrder(order *Order) { node.OrderBy = append(node.OrderBy, order) } // SetLimit sets the limit clause func (node *Union) SetLimit(limit *Limit) { node.Limit = limit } // Format formats the node. func (node *Union) Format(buf *TrackedBuffer) { buf.Myprintf("%v %s %v%v%v%s", node.Left, node.Type, node.Right, node.OrderBy, node.Limit, node.Lock) } func (node *Union) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Left, node.Right, ) } // Stream represents a SELECT statement. type Stream struct { Comments Comments SelectExpr SelectExpr Table TableName } // Format formats the node. func (node *Stream) Format(buf *TrackedBuffer) { buf.Myprintf("stream %v%v from %v", node.Comments, node.SelectExpr, node.Table) } func (node *Stream) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Comments, node.SelectExpr, node.Table, ) } // Insert represents an INSERT or REPLACE statement. // Per the MySQL docs, http://dev.mysql.com/doc/refman/5.7/en/replace.html // Replace is the counterpart to `INSERT IGNORE`, and works exactly like a // normal INSERT except if the row exists. In that case it first deletes // the row and re-inserts with new values. For that reason we keep it as an Insert struct. // Replaces are currently disallowed in sharded schemas because // of the implications the deletion part may have on vindexes. // If you add fields here, consider adding them to calls to validateSubquerySamePlan. type Insert struct { Action string Comments Comments Ignore string Table TableName Partitions Partitions Columns Columns Rows InsertRows OnDup OnDup } // DDL strings. const ( InsertStr = "insert" ReplaceStr = "replace" ) // Format formats the node. func (node *Insert) Format(buf *TrackedBuffer) { buf.Myprintf("%s %v%sinto %v%v%v %v%v", node.Action, node.Comments, node.Ignore, node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup) } func (node *Insert) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, 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() {} func (*ParenSelect) iInsertRows() {} // Update represents an UPDATE statement. // If you add fields here, consider adding them to calls to validateSubquerySamePlan. type Update struct { Comments Comments TableExprs TableExprs Exprs UpdateExprs Where *Where OrderBy OrderBy Limit *Limit } // Format formats the node. func (node *Update) Format(buf *TrackedBuffer) { buf.Myprintf("update %v%v set %v%v%v%v", node.Comments, node.TableExprs, node.Exprs, node.Where, node.OrderBy, node.Limit) } func (node *Update) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Comments, node.TableExprs, node.Exprs, node.Where, node.OrderBy, node.Limit, ) } // Delete represents a DELETE statement. // If you add fields here, consider adding them to calls to validateSubquerySamePlan. type Delete struct { Comments Comments Targets TableNames TableExprs TableExprs Partitions Partitions Where *Where OrderBy OrderBy Limit *Limit } // Format formats the node. func (node *Delete) Format(buf *TrackedBuffer) { buf.Myprintf("delete %v", node.Comments) if node.Targets != nil { buf.Myprintf("%v ", node.Targets) } buf.Myprintf("from %v%v%v%v%v", node.TableExprs, node.Partitions, node.Where, node.OrderBy, node.Limit) } func (node *Delete) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Comments, node.Targets, node.TableExprs, node.Where, node.OrderBy, node.Limit, ) } // Set represents a SET statement. type Set struct { Comments Comments Exprs SetExprs Scope string } // Set.Scope or Show.Scope const ( SessionStr = "session" GlobalStr = "global" ) // Format formats the node. func (node *Set) Format(buf *TrackedBuffer) { if node.Scope == "" { buf.Myprintf("set %v%v", node.Comments, node.Exprs) } else { buf.Myprintf("set %v%s %v", node.Comments, node.Scope, node.Exprs) } } func (node *Set) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Comments, node.Exprs, ) } // DBDDL represents a CREATE, DROP database statement. type DBDDL struct { Action string DBName string IfExists bool Collate string Charset string } // Format formats the node. func (node *DBDDL) Format(buf *TrackedBuffer) { switch node.Action { case CreateStr: buf.WriteString(fmt.Sprintf("%s database %s", node.Action, node.DBName)) case DropStr: exists := "" if node.IfExists { exists = " if exists" } buf.WriteString(fmt.Sprintf("%s database%s %v", node.Action, exists, node.DBName)) } } // walkSubtree walks the nodes of the subtree. func (node *DBDDL) walkSubtree(visit Visit) error { return nil } // DDL represents a CREATE, ALTER, DROP, RENAME or TRUNCATE statement. // Table is set for AlterStr, DropStr, RenameStr, TruncateStr // NewName is set for AlterStr, CreateStr, RenameStr. // VindexSpec is set for CreateVindexStr, DropVindexStr, AddColVindexStr, DropColVindexStr // VindexCols is set for AddColVindexStr type DDL struct { Action string Table TableName NewName TableName IfExists bool TableSpec *TableSpec PartitionSpec *PartitionSpec VindexSpec *VindexSpec VindexCols []ColIdent } // DDL strings. const ( CreateStr = "create" AlterStr = "alter" DropStr = "drop" RenameStr = "rename" TruncateStr = "truncate" CreateVindexStr = "create vindex" AddColVindexStr = "add vindex" DropColVindexStr = "drop vindex" // Vindex DDL param to specify the owner of a vindex VindexOwnerStr = "owner" ) // Format formats the node. func (node *DDL) Format(buf *TrackedBuffer) { switch node.Action { case CreateStr: if node.TableSpec == nil { buf.Myprintf("%s table %v", node.Action, node.NewName) } else { buf.Myprintf("%s table %v %v", node.Action, node.NewName, node.TableSpec) } case DropStr: exists := "" if node.IfExists { exists = " if exists" } buf.Myprintf("%s table%s %v", node.Action, exists, node.Table) case RenameStr: buf.Myprintf("%s table %v to %v", node.Action, node.Table, node.NewName) case AlterStr: if node.PartitionSpec != nil { buf.Myprintf("%s table %v %v", node.Action, node.Table, node.PartitionSpec) } else { buf.Myprintf("%s table %v", node.Action, node.Table) } case CreateVindexStr: buf.Myprintf("%s %v %v", node.Action, node.VindexSpec.Name, node.VindexSpec) case AddColVindexStr: buf.Myprintf("alter table %v %s %v (", node.Table, node.Action, node.VindexSpec.Name) for i, col := range node.VindexCols { if i != 0 { buf.Myprintf(", %v", col) } else { buf.Myprintf("%v", col) } } buf.Myprintf(")") if node.VindexSpec.Type.String() != "" { buf.Myprintf(" %v", node.VindexSpec) } case DropColVindexStr: buf.Myprintf("alter table %v %s %v", node.Table, node.Action, node.VindexSpec.Name) default: buf.Myprintf("%s table %v", node.Action, node.Table) } } func (node *DDL) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Table, node.NewName, ) } // Partition strings const ( ReorganizeStr = "reorganize partition" ) // PartitionSpec describe partition actions (for alter and create) type PartitionSpec struct { Action string Name ColIdent Definitions []*PartitionDefinition } // Format formats the node. func (node *PartitionSpec) Format(buf *TrackedBuffer) { switch node.Action { case ReorganizeStr: buf.Myprintf("%s %v into (", node.Action, node.Name) var prefix string for _, pd := range node.Definitions { buf.Myprintf("%s%v", prefix, pd) prefix = ", " } buf.Myprintf(")") default: panic("unimplemented") } } func (node *PartitionSpec) walkSubtree(visit Visit) error { if node == nil { return nil } if err := Walk(visit, node.Name); err != nil { return err } for _, def := range node.Definitions { if err := Walk(visit, def); err != nil { return err } } return nil } // PartitionDefinition describes a very minimal partition definition type PartitionDefinition struct { Name ColIdent Limit Expr Maxvalue bool } // Format formats the node func (node *PartitionDefinition) Format(buf *TrackedBuffer) { if !node.Maxvalue { buf.Myprintf("partition %v values less than (%v)", node.Name, node.Limit) } else { buf.Myprintf("partition %v values less than (maxvalue)", node.Name) } } func (node *PartitionDefinition) walkSubtree(visit Visit) error { if node == nil { return nil } return Walk( visit, node.Name, node.Limit, ) } // TableSpec describes the structure of a table from a CREATE TABLE statement type TableSpec struct { Columns []*ColumnDefinition Indexes []*IndexDefinition Options string } // Format formats the node. func (ts *TableSpec) Format(buf *TrackedBuffer) { buf.Myprintf("(\n") for i, col := range ts.Columns { if i == 0 { buf.Myprintf("\t%v", col) } else { buf.Myprintf(",\n\t%v", col) } } for _, idx := range ts.Indexes { buf.Myprintf(",\n\t%v", idx) } buf.Myprintf("\n)%s", strings.Replace(ts.Options, ", ", ",\n ", -1)) } // AddColumn appends the given column to the list in the spec func (ts *TableSpec) AddColumn(cd *ColumnDefinition) { ts.Columns = append(ts.Columns, cd) } // AddIndex appends the given index to the list in the spec func (ts *TableSpec) AddIndex(id *IndexDefinition) { ts.Indexes = append(ts.Indexes, id) } func (ts *TableSpec) walkSubtree(visit Visit) error { if ts == nil { return nil } for _, n := range ts.Columns { if err := Walk(visit, n); err != nil { return err } } for _, n := range ts.Indexes { if err := Walk(visit, n); err != nil { return err } } return nil } // ColumnDefinition describes a column in a CREATE TABLE statement type ColumnDefinition struct { Name ColIdent Type ColumnType } // Format formats the node. func (col *ColumnDefinition) Format(buf *TrackedBuffer) { buf.Myprintf("%v %v", col.Name, &col.Type) } func (col *ColumnDefinition) walkSubtree(visit Visit) error { if col == nil { return nil } return Walk( visit, col.Name, &col.Type, ) } // ColumnType represents a sql type in a CREATE TABLE statement // All optional fields are nil if not specified type ColumnType struct { // The base type string Type string // Generic field options. NotNull BoolVal Autoincrement BoolVal Default *SQLVal OnUpdate *SQLVal Comment *SQLVal // Numeric field options Length *SQLVal Unsigned BoolVal Zerofill BoolVal Scale *SQLVal // Text field options Charset string Collate string // Enum values EnumValues []string // Key specification KeyOpt ColumnKeyOption } // Format returns a canonical string representation of the type and all relevant options func (ct *ColumnType) Format(buf *TrackedBuffer) { buf.Myprintf("%s", ct.Type) if ct.Length != nil && ct.Scale != nil { buf.Myprintf("(%v,%v)", ct.Length, ct.Scale) } else if ct.Length != nil { buf.Myprintf("(%v)", ct.Length) } if ct.EnumValues != nil { buf.Myprintf("(%s)", strings.Join(ct.EnumValues, ", ")) } opts := make([]string, 0, 16) if ct.Unsigned { opts = append(opts, keywordStrings[UNSIGNED]) } if ct.Zerofill { opts = append(opts, keywordStrings[ZEROFILL]) } if ct.Charset != "" { opts = append(opts, keywordStrings[CHARACTER], keywordStrings[SET], ct.Charset) } if ct.Collate != "" { opts = append(opts, keywordStrings[COLLATE], ct.Collate) } if ct.NotNull { opts = append(opts, keywordStrings[NOT], keywordStrings[NULL]) } if ct.Default != nil { opts = append(opts, keywordStrings[DEFAULT], String(ct.Default)) } if ct.OnUpdate != nil { opts = append(opts, keywordStrings[ON], keywordStrings[UPDATE], String(ct.OnUpdate)) } if ct.Autoincrement { opts = append(opts, keywordStrings[AUTO_INCREMENT]) } if ct.Comment != nil { opts = append(opts, keywordStrings[COMMENT_KEYWORD], String(ct.Comment)) } if ct.KeyOpt == colKeyPrimary { opts = append(opts, keywordStrings[PRIMARY], keywordStrings[KEY]) } if ct.KeyOpt == colKeyUnique { opts = append(opts, keywordStrings[UNIQUE]) } if ct.KeyOpt == colKeyUniqueKey { opts = append(opts, keywordStrings[UNIQUE], keywordStrings[KEY]) } if ct.KeyOpt == colKeySpatialKey { opts = append(opts, keywordStrings[SPATIAL], keywordStrings[KEY]) } if ct.KeyOpt == colKey { opts = append(opts, keywordStrings[KEY]) } if len(opts) != 0 { buf.Myprintf(" %s", strings.Join(opts, " ")) } } // DescribeType returns the abbreviated type information as required for // describe table func (ct *ColumnType) DescribeType() string { buf := NewTrackedBuffer(nil) buf.Myprintf("%s", ct.Type) if ct.Length != nil && ct.Scale != nil { buf.Myprintf("(%v,%v)", ct.Length, ct.Scale) } else if ct.Length != nil { buf.Myprintf("(%v)", ct.Length) } opts := make([]string, 0, 16) if ct.Unsigned { opts = append(opts, keywordStrings[UNSIGNED]) } if ct.Zerofill { opts = append(opts, keywordStrings[ZEROFILL]) } if len(opts) != 0 { buf.Myprintf(" %s", strings.Join(opts, " ")) } return buf.String() } // SQLType returns the sqltypes type code for the given column func (ct *ColumnType) SQLType() querypb.Type { |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |