github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/ast_format.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sqlparser 18 19 import ( 20 "strings" 21 22 "github.com/team-ide/go-dialect/vitess/sqltypes" 23 ) 24 25 // Format formats the node. 26 func (node *Select) Format(buf *TrackedBuffer) { 27 if node.With != nil { 28 buf.astPrintf(node, "%v", node.With) 29 } 30 buf.astPrintf(node, "select %v", node.Comments) 31 32 if node.Distinct { 33 buf.WriteString(DistinctStr) 34 } 35 if node.Cache != nil { 36 if *node.Cache { 37 buf.WriteString(SQLCacheStr) 38 } else { 39 buf.WriteString(SQLNoCacheStr) 40 } 41 } 42 if node.StraightJoinHint { 43 buf.WriteString(StraightJoinHint) 44 } 45 if node.SQLCalcFoundRows { 46 buf.WriteString(SQLCalcFoundRowsStr) 47 } 48 49 buf.astPrintf(node, "%v from ", node.SelectExprs) 50 51 prefix := "" 52 for _, expr := range node.From { 53 buf.astPrintf(node, "%s%v", prefix, expr) 54 prefix = ", " 55 } 56 57 buf.astPrintf(node, "%v%v%v%v%v%s%v", 58 node.Where, 59 node.GroupBy, node.Having, node.OrderBy, 60 node.Limit, node.Lock.ToString(), node.Into) 61 } 62 63 // Format formats the node. 64 func (node *Union) Format(buf *TrackedBuffer) { 65 if requiresParen(node.Left) { 66 buf.astPrintf(node, "(%v)", node.Left) 67 } else { 68 buf.astPrintf(node, "%v", node.Left) 69 } 70 71 buf.WriteString(" ") 72 if node.Distinct { 73 buf.WriteString(UnionStr) 74 } else { 75 buf.WriteString(UnionAllStr) 76 } 77 buf.WriteString(" ") 78 79 if requiresParen(node.Right) { 80 buf.astPrintf(node, "(%v)", node.Right) 81 } else { 82 buf.astPrintf(node, "%v", node.Right) 83 } 84 85 buf.astPrintf(node, "%v%v%s", node.OrderBy, node.Limit, node.Lock.ToString()) 86 } 87 88 // Format formats the node. 89 func (node *VStream) Format(buf *TrackedBuffer) { 90 buf.astPrintf(node, "vstream %v%v from %v", 91 node.Comments, node.SelectExpr, node.Table) 92 } 93 94 // Format formats the node. 95 func (node *Stream) Format(buf *TrackedBuffer) { 96 buf.astPrintf(node, "stream %v%v from %v", 97 node.Comments, node.SelectExpr, node.Table) 98 } 99 100 // Format formats the node. 101 func (node *Insert) Format(buf *TrackedBuffer) { 102 switch node.Action { 103 case InsertAct: 104 buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v", 105 InsertStr, 106 node.Comments, node.Ignore.ToString(), 107 node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup) 108 case ReplaceAct: 109 buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v", 110 ReplaceStr, 111 node.Comments, node.Ignore.ToString(), 112 node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup) 113 default: 114 buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v", 115 "Unkown Insert Action", 116 node.Comments, node.Ignore.ToString(), 117 node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup) 118 } 119 120 } 121 122 // Format formats the node. 123 func (node *With) Format(buf *TrackedBuffer) { 124 buf.astPrintf(node, "with ") 125 126 if node.Recursive { 127 buf.astPrintf(node, "recursive ") 128 } 129 ctesLength := len(node.ctes) 130 for i := 0; i < ctesLength-1; i++ { 131 buf.astPrintf(node, "%v, ", node.ctes[i]) 132 } 133 buf.astPrintf(node, "%v", node.ctes[ctesLength-1]) 134 } 135 136 // Format formats the node. 137 func (node *CommonTableExpr) Format(buf *TrackedBuffer) { 138 buf.astPrintf(node, "%v%v as %v ", node.TableID, node.Columns, node.Subquery) 139 } 140 141 // Format formats the node. 142 func (node *Update) Format(buf *TrackedBuffer) { 143 if node.With != nil { 144 buf.astPrintf(node, "%v", node.With) 145 } 146 buf.astPrintf(node, "update %v%s%v set %v%v%v%v", 147 node.Comments, node.Ignore.ToString(), node.TableExprs, 148 node.Exprs, node.Where, node.OrderBy, node.Limit) 149 } 150 151 // Format formats the node. 152 func (node *Delete) Format(buf *TrackedBuffer) { 153 if node.With != nil { 154 buf.astPrintf(node, "%v", node.With) 155 } 156 buf.astPrintf(node, "delete %v", node.Comments) 157 if node.Ignore { 158 buf.WriteString("ignore ") 159 } 160 if node.Targets != nil { 161 buf.astPrintf(node, "%v ", node.Targets) 162 } 163 buf.astPrintf(node, "from %v%v%v%v%v", node.TableExprs, node.Partitions, node.Where, node.OrderBy, node.Limit) 164 } 165 166 // Format formats the node. 167 func (node *Set) Format(buf *TrackedBuffer) { 168 buf.astPrintf(node, "set %v%v", node.Comments, node.Exprs) 169 } 170 171 // Format formats the node. 172 func (node *SetTransaction) Format(buf *TrackedBuffer) { 173 if node.Scope == ImplicitScope { 174 buf.astPrintf(node, "set %vtransaction ", node.Comments) 175 } else { 176 buf.astPrintf(node, "set %v%s transaction ", node.Comments, node.Scope.ToString()) 177 } 178 179 for i, char := range node.Characteristics { 180 if i > 0 { 181 buf.WriteString(", ") 182 } 183 buf.astPrintf(node, "%v", char) 184 } 185 } 186 187 // Format formats the node. 188 func (node *DropDatabase) Format(buf *TrackedBuffer) { 189 exists := "" 190 if node.IfExists { 191 exists = "if exists " 192 } 193 buf.astPrintf(node, "%s %vdatabase %s%v", DropStr, node.Comments, exists, node.DBName) 194 } 195 196 // Format formats the node. 197 func (node *Flush) Format(buf *TrackedBuffer) { 198 buf.astPrintf(node, "%s", FlushStr) 199 if node.IsLocal { 200 buf.WriteString(" local") 201 } 202 if len(node.FlushOptions) != 0 { 203 prefix := " " 204 for _, option := range node.FlushOptions { 205 buf.astPrintf(node, "%s%s", prefix, option) 206 prefix = ", " 207 } 208 } else { 209 buf.WriteString(" tables") 210 if len(node.TableNames) != 0 { 211 buf.astPrintf(node, " %v", node.TableNames) 212 } 213 if node.ForExport { 214 buf.WriteString(" for export") 215 } 216 if node.WithLock { 217 buf.WriteString(" with read lock") 218 } 219 } 220 } 221 222 // Format formats the node. 223 func (node *AlterVschema) Format(buf *TrackedBuffer) { 224 switch node.Action { 225 case CreateVindexDDLAction: 226 buf.astPrintf(node, "alter vschema create vindex %v %v", node.Table, node.VindexSpec) 227 case DropVindexDDLAction: 228 buf.astPrintf(node, "alter vschema drop vindex %v", node.Table) 229 case AddVschemaTableDDLAction: 230 buf.astPrintf(node, "alter vschema add table %v", node.Table) 231 case DropVschemaTableDDLAction: 232 buf.astPrintf(node, "alter vschema drop table %v", node.Table) 233 case AddColVindexDDLAction: 234 buf.astPrintf(node, "alter vschema on %v add vindex %v (", node.Table, node.VindexSpec.Name) 235 for i, col := range node.VindexCols { 236 if i != 0 { 237 buf.astPrintf(node, ", %v", col) 238 } else { 239 buf.astPrintf(node, "%v", col) 240 } 241 } 242 buf.astPrintf(node, ")") 243 if node.VindexSpec.Type.String() != "" { 244 buf.astPrintf(node, " %v", node.VindexSpec) 245 } 246 case DropColVindexDDLAction: 247 buf.astPrintf(node, "alter vschema on %v drop vindex %v", node.Table, node.VindexSpec.Name) 248 case AddSequenceDDLAction: 249 buf.astPrintf(node, "alter vschema add sequence %v", node.Table) 250 case AddAutoIncDDLAction: 251 buf.astPrintf(node, "alter vschema on %v add auto_increment %v", node.Table, node.AutoIncSpec) 252 default: 253 buf.astPrintf(node, "%s table %v", node.Action.ToString(), node.Table) 254 } 255 } 256 257 // Format formats the node. 258 func (node *AlterMigration) Format(buf *TrackedBuffer) { 259 buf.astPrintf(node, "alter vitess_migration") 260 if node.UUID != "" { 261 buf.astPrintf(node, " '%s'", node.UUID) 262 } 263 var alterType string 264 switch node.Type { 265 case RetryMigrationType: 266 alterType = "retry" 267 case CleanupMigrationType: 268 alterType = "cleanup" 269 case CompleteMigrationType: 270 alterType = "complete" 271 case CancelMigrationType: 272 alterType = "cancel" 273 case CancelAllMigrationType: 274 alterType = "cancel all" 275 } 276 buf.astPrintf(node, " %s", alterType) 277 } 278 279 // Format formats the node. 280 func (node *RevertMigration) Format(buf *TrackedBuffer) { 281 buf.astPrintf(node, "revert %vvitess_migration '%s'", node.Comments, node.UUID) 282 } 283 284 // Format formats the node. 285 func (node *ShowMigrationLogs) Format(buf *TrackedBuffer) { 286 buf.astPrintf(node, "show vitess_migration '%s' logs", node.UUID) 287 } 288 289 // Format formats the node. 290 func (node *OptLike) Format(buf *TrackedBuffer) { 291 buf.astPrintf(node, "like %v", node.LikeTable) 292 } 293 294 // Format formats the node. 295 func (node *PartitionSpec) Format(buf *TrackedBuffer) { 296 switch node.Action { 297 case ReorganizeAction: 298 buf.astPrintf(node, "%s ", ReorganizeStr) 299 for i, n := range node.Names { 300 if i != 0 { 301 buf.WriteString(", ") 302 } 303 buf.astPrintf(node, "%v", n) 304 } 305 buf.WriteString(" into (") 306 for i, pd := range node.Definitions { 307 if i != 0 { 308 buf.WriteString(", ") 309 } 310 buf.astPrintf(node, "%v", pd) 311 } 312 buf.astPrintf(node, ")") 313 case AddAction: 314 buf.astPrintf(node, "%s (%v)", AddStr, node.Definitions[0]) 315 case DropAction: 316 buf.astPrintf(node, "%s ", DropPartitionStr) 317 for i, n := range node.Names { 318 if i != 0 { 319 buf.WriteString(", ") 320 } 321 buf.astPrintf(node, "%v", n) 322 } 323 case DiscardAction: 324 buf.astPrintf(node, "%s ", DiscardStr) 325 if node.IsAll { 326 buf.WriteString("all") 327 } else { 328 prefix := "" 329 for _, n := range node.Names { 330 buf.astPrintf(node, "%s%v", prefix, n) 331 prefix = ", " 332 } 333 } 334 buf.WriteString(" tablespace") 335 case ImportAction: 336 buf.astPrintf(node, "%s ", ImportStr) 337 if node.IsAll { 338 buf.WriteString("all") 339 } else { 340 prefix := "" 341 for _, n := range node.Names { 342 buf.astPrintf(node, "%s%v", prefix, n) 343 prefix = ", " 344 } 345 } 346 buf.WriteString(" tablespace") 347 case TruncateAction: 348 buf.astPrintf(node, "%s ", TruncatePartitionStr) 349 if node.IsAll { 350 buf.WriteString("all") 351 } else { 352 prefix := "" 353 for _, n := range node.Names { 354 buf.astPrintf(node, "%s%v", prefix, n) 355 prefix = ", " 356 } 357 } 358 case CoalesceAction: 359 buf.astPrintf(node, "%s %v", CoalesceStr, node.Number) 360 case ExchangeAction: 361 buf.astPrintf(node, "%s %v with table %v", ExchangeStr, node.Names[0], node.TableName) 362 if node.WithoutValidation { 363 buf.WriteString(" without validation") 364 } 365 case AnalyzeAction: 366 buf.astPrintf(node, "%s ", AnalyzePartitionStr) 367 if node.IsAll { 368 buf.WriteString("all") 369 } else { 370 prefix := "" 371 for _, n := range node.Names { 372 buf.astPrintf(node, "%s%v", prefix, n) 373 prefix = ", " 374 } 375 } 376 case CheckAction: 377 buf.astPrintf(node, "%s ", CheckStr) 378 if node.IsAll { 379 buf.WriteString("all") 380 } else { 381 prefix := "" 382 for _, n := range node.Names { 383 buf.astPrintf(node, "%s%v", prefix, n) 384 prefix = ", " 385 } 386 } 387 case OptimizeAction: 388 buf.astPrintf(node, "%s ", OptimizeStr) 389 if node.IsAll { 390 buf.WriteString("all") 391 } else { 392 prefix := "" 393 for _, n := range node.Names { 394 buf.astPrintf(node, "%s%v", prefix, n) 395 prefix = ", " 396 } 397 } 398 case RebuildAction: 399 buf.astPrintf(node, "%s ", RebuildStr) 400 if node.IsAll { 401 buf.WriteString("all") 402 } else { 403 prefix := "" 404 for _, n := range node.Names { 405 buf.astPrintf(node, "%s%v", prefix, n) 406 prefix = ", " 407 } 408 } 409 case RepairAction: 410 buf.astPrintf(node, "%s ", RepairStr) 411 if node.IsAll { 412 buf.WriteString("all") 413 } else { 414 prefix := "" 415 for _, n := range node.Names { 416 buf.astPrintf(node, "%s%v", prefix, n) 417 prefix = ", " 418 } 419 } 420 case RemoveAction: 421 buf.WriteString(RemoveStr) 422 case UpgradeAction: 423 buf.WriteString(UpgradeStr) 424 default: 425 panic("unimplemented") 426 } 427 } 428 429 // Format formats the node 430 func (node *PartitionDefinition) Format(buf *TrackedBuffer) { 431 if !node.Maxvalue { 432 buf.astPrintf(node, "partition %v values less than (%v)", node.Name, node.Limit) 433 } else { 434 buf.astPrintf(node, "partition %v values less than (maxvalue)", node.Name) 435 } 436 } 437 438 // Format formats the node. 439 func (node *PartitionOption) Format(buf *TrackedBuffer) { 440 buf.WriteString("partition by") 441 if node.isHASH { 442 if node.Linear != "" { 443 buf.astPrintf(node, " %s", node.Linear) 444 } 445 buf.WriteString(" hash") 446 if node.Expr != nil { 447 buf.astPrintf(node, " (%v)", node.Expr) 448 } 449 } 450 if node.isKEY { 451 if node.Linear != "" { 452 buf.astPrintf(node, " %s", node.Linear) 453 } 454 buf.WriteString(" key") 455 if node.KeyAlgorithm != "" { 456 buf.astPrintf(node, " algorithm = %s", node.KeyAlgorithm) 457 } 458 if node.KeyColList != nil { 459 buf.astPrintf(node, " %v", node.KeyColList) 460 } 461 } 462 if node.RangeOrList != "" { 463 buf.astPrintf(node, " %s", node.RangeOrList) 464 buf.astPrintf(node, " %v", node.ExprOrCol) 465 } 466 if node.Partitions != "" { 467 buf.astPrintf(node, " partitions %s", node.Partitions) 468 } 469 if node.SubPartition != nil { 470 buf.astPrintf(node, " %v", node.SubPartition) 471 } 472 if node.Definitions != nil { 473 buf.WriteString(" (") 474 for i, pd := range node.Definitions { 475 if i != 0 { 476 buf.WriteString(", ") 477 } 478 buf.astPrintf(node, "%v", pd) 479 } 480 buf.WriteString(")") 481 } 482 } 483 484 // Format formats the node. 485 func (node *SubPartition) Format(buf *TrackedBuffer) { 486 buf.WriteString("subpartition by") 487 if node.isHASH { 488 if node.Linear != "" { 489 buf.astPrintf(node, " %s", node.Linear) 490 } 491 buf.WriteString(" hash") 492 if node.Expr != nil { 493 buf.astPrintf(node, " (%v)", node.Expr) 494 } 495 } 496 if node.isKEY { 497 if node.Linear != "" { 498 buf.astPrintf(node, " %s", node.Linear) 499 } 500 buf.WriteString(" key") 501 if node.KeyAlgorithm != "" { 502 buf.astPrintf(node, " algorithm = %s", node.KeyAlgorithm) 503 } 504 if node.KeyColList != nil { 505 buf.astPrintf(node, " (%v)", node.KeyColList) 506 } 507 } 508 if node.SubPartitions != "" { 509 buf.astPrintf(node, " subpartitions %s", node.SubPartitions) 510 } 511 } 512 513 // Format formats the node. 514 func (node *ExprOrColumns) Format(buf *TrackedBuffer) { 515 if node.Expr != nil { 516 buf.astPrintf(node, "(%v)", node.Expr) 517 } 518 if node.ColumnList != nil { 519 buf.astPrintf(node, "columns %v", node.ColumnList) 520 } 521 } 522 523 // Format formats the node. 524 func (ts *TableSpec) Format(buf *TrackedBuffer) { 525 buf.astPrintf(ts, "(\n") 526 for i, col := range ts.Columns { 527 if i == 0 { 528 buf.astPrintf(ts, "\t%v", col) 529 } else { 530 buf.astPrintf(ts, ",\n\t%v", col) 531 } 532 } 533 for _, idx := range ts.Indexes { 534 buf.astPrintf(ts, ",\n\t%v", idx) 535 } 536 for _, c := range ts.Constraints { 537 buf.astPrintf(ts, ",\n\t%v", c) 538 } 539 540 buf.astPrintf(ts, "\n)") 541 for i, opt := range ts.Options { 542 if i != 0 { 543 buf.WriteString(",\n ") 544 } 545 buf.astPrintf(ts, " %s", opt.Name) 546 if opt.String != "" { 547 buf.astPrintf(ts, " %s", opt.String) 548 } else if opt.Value != nil { 549 buf.astPrintf(ts, " %v", opt.Value) 550 } else { 551 buf.astPrintf(ts, " (%v)", opt.Tables) 552 } 553 } 554 if ts.PartitionOption != nil { 555 buf.astPrintf(ts, " %v", ts.PartitionOption) 556 } 557 } 558 559 // Format formats the node. 560 func (col *ColumnDefinition) Format(buf *TrackedBuffer) { 561 buf.astPrintf(col, "%v %v", col.Name, &col.Type) 562 } 563 564 // Format returns a canonical string representation of the type and all relevant options 565 func (ct *ColumnType) Format(buf *TrackedBuffer) { 566 buf.astPrintf(ct, "%s", ct.Type) 567 568 if ct.Length != nil && ct.Scale != nil { 569 buf.astPrintf(ct, "(%v,%v)", ct.Length, ct.Scale) 570 571 } else if ct.Length != nil { 572 buf.astPrintf(ct, "(%v)", ct.Length) 573 } 574 575 if ct.EnumValues != nil { 576 buf.astPrintf(ct, "(%s)", strings.Join(ct.EnumValues, ", ")) 577 } 578 579 if ct.Unsigned { 580 buf.astPrintf(ct, " %s", keywordStrings[UNSIGNED]) 581 } 582 if ct.Zerofill { 583 buf.astPrintf(ct, " %s", keywordStrings[ZEROFILL]) 584 } 585 if ct.Charset != "" { 586 buf.astPrintf(ct, " %s %s %s", keywordStrings[CHARACTER], keywordStrings[SET], ct.Charset) 587 } 588 if ct.Options != nil && ct.Options.Collate != "" { 589 buf.astPrintf(ct, " %s %s", keywordStrings[COLLATE], ct.Options.Collate) 590 } 591 if ct.Options.Null != nil && ct.Options.As == nil { 592 if *ct.Options.Null { 593 buf.astPrintf(ct, " %s", keywordStrings[NULL]) 594 } else { 595 buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL]) 596 } 597 } 598 if ct.Options.Default != nil { 599 buf.astPrintf(ct, " %s", keywordStrings[DEFAULT]) 600 if defaultRequiresParens(ct) { 601 buf.astPrintf(ct, " (%v)", ct.Options.Default) 602 } else { 603 buf.astPrintf(ct, " %v", ct.Options.Default) 604 } 605 } 606 if ct.Options.OnUpdate != nil { 607 buf.astPrintf(ct, " %s %s %v", keywordStrings[ON], keywordStrings[UPDATE], ct.Options.OnUpdate) 608 } 609 if ct.Options.As != nil { 610 buf.astPrintf(ct, " %s (%v)", keywordStrings[AS], ct.Options.As) 611 612 if ct.Options.Storage == VirtualStorage { 613 buf.astPrintf(ct, " %s", keywordStrings[VIRTUAL]) 614 } else if ct.Options.Storage == StoredStorage { 615 buf.astPrintf(ct, " %s", keywordStrings[STORED]) 616 } 617 if ct.Options.Null != nil { 618 if *ct.Options.Null { 619 buf.astPrintf(ct, " %s", keywordStrings[NULL]) 620 } else { 621 buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL]) 622 } 623 } 624 } 625 if ct.Options.Autoincrement { 626 buf.astPrintf(ct, " %s", keywordStrings[AUTO_INCREMENT]) 627 } 628 if ct.Options.Comment != nil { 629 buf.astPrintf(ct, " %s %v", keywordStrings[COMMENT_KEYWORD], ct.Options.Comment) 630 } 631 if ct.Options.KeyOpt == colKeyPrimary { 632 buf.astPrintf(ct, " %s %s", keywordStrings[PRIMARY], keywordStrings[KEY]) 633 } 634 if ct.Options.KeyOpt == colKeyUnique { 635 buf.astPrintf(ct, " %s", keywordStrings[UNIQUE]) 636 } 637 if ct.Options.KeyOpt == colKeyUniqueKey { 638 buf.astPrintf(ct, " %s %s", keywordStrings[UNIQUE], keywordStrings[KEY]) 639 } 640 if ct.Options.KeyOpt == colKeySpatialKey { 641 buf.astPrintf(ct, " %s %s", keywordStrings[SPATIAL], keywordStrings[KEY]) 642 } 643 if ct.Options.KeyOpt == colKeyFulltextKey { 644 buf.astPrintf(ct, " %s %s", keywordStrings[FULLTEXT], keywordStrings[KEY]) 645 } 646 if ct.Options.KeyOpt == colKey { 647 buf.astPrintf(ct, " %s", keywordStrings[KEY]) 648 } 649 if ct.Options.Reference != nil { 650 buf.astPrintf(ct, " %v", ct.Options.Reference) 651 } 652 } 653 654 // Format formats the node. 655 func (idx *IndexDefinition) Format(buf *TrackedBuffer) { 656 buf.astPrintf(idx, "%v (", idx.Info) 657 for i, col := range idx.Columns { 658 if i != 0 { 659 buf.astPrintf(idx, ", %v", col.Column) 660 } else { 661 buf.astPrintf(idx, "%v", col.Column) 662 } 663 if col.Length != nil { 664 buf.astPrintf(idx, "(%v)", col.Length) 665 } 666 if col.Direction == DescOrder { 667 buf.astPrintf(idx, " desc") 668 } 669 } 670 buf.astPrintf(idx, ")") 671 672 for _, opt := range idx.Options { 673 buf.astPrintf(idx, " %s", opt.Name) 674 if opt.String != "" { 675 buf.astPrintf(idx, " %s", opt.String) 676 } else { 677 buf.astPrintf(idx, " %v", opt.Value) 678 } 679 } 680 } 681 682 // Format formats the node. 683 func (ii *IndexInfo) Format(buf *TrackedBuffer) { 684 if !ii.ConstraintName.IsEmpty() { 685 buf.astPrintf(ii, "constraint %v ", ii.ConstraintName) 686 } 687 if ii.Primary { 688 buf.astPrintf(ii, "%s", ii.Type) 689 } else { 690 buf.astPrintf(ii, "%s", ii.Type) 691 if !ii.Name.IsEmpty() { 692 buf.astPrintf(ii, " %v", ii.Name) 693 } 694 } 695 } 696 697 // Format formats the node. 698 func (node *AutoIncSpec) Format(buf *TrackedBuffer) { 699 buf.astPrintf(node, "%v ", node.Column) 700 buf.astPrintf(node, "using %v", node.Sequence) 701 } 702 703 // Format formats the node. The "CREATE VINDEX" preamble was formatted in 704 // the containing DDL node Format, so this just prints the type, any 705 // parameters, and optionally the owner 706 func (node *VindexSpec) Format(buf *TrackedBuffer) { 707 buf.astPrintf(node, "using %v", node.Type) 708 709 numParams := len(node.Params) 710 if numParams != 0 { 711 buf.astPrintf(node, " with ") 712 for i, p := range node.Params { 713 if i != 0 { 714 buf.astPrintf(node, ", ") 715 } 716 buf.astPrintf(node, "%v", p) 717 } 718 } 719 } 720 721 // Format formats the node. 722 func (node VindexParam) Format(buf *TrackedBuffer) { 723 buf.astPrintf(node, "%s=%s", node.Key.String(), node.Val) 724 } 725 726 // Format formats the node. 727 func (c *ConstraintDefinition) Format(buf *TrackedBuffer) { 728 if !c.Name.IsEmpty() { 729 buf.astPrintf(c, "constraint %v ", c.Name) 730 } 731 c.Details.Format(buf) 732 } 733 734 // Format formats the node. 735 func (a ReferenceAction) Format(buf *TrackedBuffer) { 736 switch a { 737 case Restrict: 738 buf.WriteString("restrict") 739 case Cascade: 740 buf.WriteString("cascade") 741 case NoAction: 742 buf.WriteString("no action") 743 case SetNull: 744 buf.WriteString("set null") 745 case SetDefault: 746 buf.WriteString("set default") 747 } 748 } 749 750 // Format formats the node. 751 func (f *ForeignKeyDefinition) Format(buf *TrackedBuffer) { 752 buf.astPrintf(f, "foreign key %v%v %v", f.IndexName, f.Source, f.ReferenceDefinition) 753 } 754 755 // Format formats the node. 756 func (ref *ReferenceDefinition) Format(buf *TrackedBuffer) { 757 buf.astPrintf(ref, "references %v %v", ref.ReferencedTable, ref.ReferencedColumns) 758 if ref.OnDelete != DefaultAction { 759 buf.astPrintf(ref, " on delete %v", ref.OnDelete) 760 } 761 if ref.OnUpdate != DefaultAction { 762 buf.astPrintf(ref, " on update %v", ref.OnUpdate) 763 } 764 } 765 766 // Format formats the node. 767 func (c *CheckConstraintDefinition) Format(buf *TrackedBuffer) { 768 buf.astPrintf(c, "check (%v)", c.Expr) 769 if !c.Enforced { 770 buf.astPrintf(c, " not enforced") 771 } 772 } 773 774 // Format formats the node. 775 func (node *Show) Format(buf *TrackedBuffer) { 776 buf.astPrintf(node, "%v", node.Internal) 777 } 778 779 // Format formats the node. 780 func (node *ShowLegacy) Format(buf *TrackedBuffer) { 781 nodeType := strings.ToLower(node.Type) 782 if (nodeType == "tables" || nodeType == "columns" || nodeType == "fields" || nodeType == "index" || nodeType == "keys" || nodeType == "indexes" || 783 nodeType == "databases" || nodeType == "schemas" || nodeType == "keyspaces" || nodeType == "vitess_keyspaces" || nodeType == "vitess_replication_status" || 784 nodeType == "vitess_shards" || nodeType == "vitess_tablets") && node.ShowTablesOpt != nil { 785 opt := node.ShowTablesOpt 786 if node.Extended != "" { 787 buf.astPrintf(node, "show %s%s", node.Extended, nodeType) 788 } else { 789 buf.astPrintf(node, "show %s%s", opt.Full, nodeType) 790 } 791 if (nodeType == "columns" || nodeType == "fields") && node.HasOnTable() { 792 buf.astPrintf(node, " from %v", node.OnTable) 793 } 794 if (nodeType == "index" || nodeType == "keys" || nodeType == "indexes") && node.HasOnTable() { 795 buf.astPrintf(node, " from %v", node.OnTable) 796 } 797 if opt.DbName != "" { 798 buf.astPrintf(node, " from %s", opt.DbName) 799 } 800 buf.astPrintf(node, "%v", opt.Filter) 801 return 802 } 803 if node.Scope == ImplicitScope { 804 buf.astPrintf(node, "show %s", nodeType) 805 } else { 806 buf.astPrintf(node, "show %s %s", node.Scope.ToString(), nodeType) 807 } 808 if node.HasOnTable() { 809 buf.astPrintf(node, " on %v", node.OnTable) 810 } 811 if nodeType == "collation" && node.ShowCollationFilterOpt != nil { 812 buf.astPrintf(node, " where %v", node.ShowCollationFilterOpt) 813 } 814 if nodeType == "charset" && node.ShowTablesOpt != nil { 815 buf.astPrintf(node, "%v", node.ShowTablesOpt.Filter) 816 } 817 if node.HasTable() { 818 buf.astPrintf(node, " %v", node.Table) 819 } 820 } 821 822 // Format formats the node. 823 func (node *ShowFilter) Format(buf *TrackedBuffer) { 824 if node == nil { 825 return 826 } 827 if node.Like != "" { 828 buf.astPrintf(node, " like ") 829 sqltypes.BufEncodeStringSQL(buf.Builder, node.Like) 830 } else { 831 buf.astPrintf(node, " where %v", node.Filter) 832 } 833 } 834 835 // Format formats the node. 836 func (node *Use) Format(buf *TrackedBuffer) { 837 if node.DBName.v != "" { 838 buf.astPrintf(node, "use %v", node.DBName) 839 } else { 840 buf.astPrintf(node, "use") 841 } 842 } 843 844 // Format formats the node. 845 func (node *Commit) Format(buf *TrackedBuffer) { 846 buf.WriteString("commit") 847 } 848 849 // Format formats the node. 850 func (node *Begin) Format(buf *TrackedBuffer) { 851 buf.WriteString("begin") 852 } 853 854 // Format formats the node. 855 func (node *Rollback) Format(buf *TrackedBuffer) { 856 buf.WriteString("rollback") 857 } 858 859 // Format formats the node. 860 func (node *SRollback) Format(buf *TrackedBuffer) { 861 buf.astPrintf(node, "rollback to %v", node.Name) 862 } 863 864 // Format formats the node. 865 func (node *Savepoint) Format(buf *TrackedBuffer) { 866 buf.astPrintf(node, "savepoint %v", node.Name) 867 } 868 869 // Format formats the node. 870 func (node *Release) Format(buf *TrackedBuffer) { 871 buf.astPrintf(node, "release savepoint %v", node.Name) 872 } 873 874 // Format formats the node. 875 func (node *ExplainStmt) Format(buf *TrackedBuffer) { 876 format := "" 877 switch node.Type { 878 case EmptyType: 879 case AnalyzeType: 880 format = AnalyzeStr + " " 881 default: 882 format = "format = " + node.Type.ToString() + " " 883 } 884 buf.astPrintf(node, "explain %s%v", format, node.Statement) 885 } 886 887 // Format formats the node. 888 func (node *ExplainTab) Format(buf *TrackedBuffer) { 889 buf.astPrintf(node, "explain %v", node.Table) 890 if node.Wild != "" { 891 buf.astPrintf(node, " %s", node.Wild) 892 } 893 } 894 895 // Format formats the node. 896 func (node *CallProc) Format(buf *TrackedBuffer) { 897 buf.astPrintf(node, "call %v(%v)", node.Name, node.Params) 898 } 899 900 // Format formats the node. 901 func (node *OtherRead) Format(buf *TrackedBuffer) { 902 buf.WriteString("otherread") 903 } 904 905 // Format formats the node. 906 func (node *OtherAdmin) Format(buf *TrackedBuffer) { 907 buf.WriteString("otheradmin") 908 } 909 910 // Format formats the node. 911 func (node Comments) Format(buf *TrackedBuffer) { 912 for _, c := range node { 913 buf.astPrintf(node, "%s ", c) 914 } 915 } 916 917 // Format formats the node. 918 func (node SelectExprs) Format(buf *TrackedBuffer) { 919 var prefix string 920 for _, n := range node { 921 buf.astPrintf(node, "%s%v", prefix, n) 922 prefix = ", " 923 } 924 } 925 926 // Format formats the node. 927 func (node *StarExpr) Format(buf *TrackedBuffer) { 928 if !node.TableName.IsEmpty() { 929 buf.astPrintf(node, "%v.", node.TableName) 930 } 931 buf.astPrintf(node, "*") 932 } 933 934 // Format formats the node. 935 func (node *AliasedExpr) Format(buf *TrackedBuffer) { 936 buf.astPrintf(node, "%v", node.Expr) 937 if !node.As.IsEmpty() { 938 buf.astPrintf(node, " as %v", node.As) 939 } 940 } 941 942 // Format formats the node. 943 func (node *Nextval) Format(buf *TrackedBuffer) { 944 buf.astPrintf(node, "next %v values", node.Expr) 945 } 946 947 // Format formats the node. 948 func (node Columns) Format(buf *TrackedBuffer) { 949 if node == nil { 950 return 951 } 952 prefix := "(" 953 for _, n := range node { 954 buf.astPrintf(node, "%s%v", prefix, n) 955 prefix = ", " 956 } 957 buf.WriteString(")") 958 } 959 960 // Format formats the node 961 func (node Partitions) Format(buf *TrackedBuffer) { 962 if node == nil { 963 return 964 } 965 prefix := " partition (" 966 for _, n := range node { 967 buf.astPrintf(node, "%s%v", prefix, n) 968 prefix = ", " 969 } 970 buf.WriteString(")") 971 } 972 973 // Format formats the node. 974 func (node TableExprs) Format(buf *TrackedBuffer) { 975 var prefix string 976 for _, n := range node { 977 buf.astPrintf(node, "%s%v", prefix, n) 978 prefix = ", " 979 } 980 } 981 982 // Format formats the node. 983 func (node *AliasedTableExpr) Format(buf *TrackedBuffer) { 984 buf.astPrintf(node, "%v%v", node.Expr, node.Partitions) 985 if !node.As.IsEmpty() { 986 buf.astPrintf(node, " as %v", node.As) 987 if len(node.Columns) != 0 { 988 buf.astPrintf(node, "%v", node.Columns) 989 } 990 } 991 if node.Hints != nil { 992 // Hint node provides the space padding. 993 buf.astPrintf(node, "%v", node.Hints) 994 } 995 } 996 997 // Format formats the node. 998 func (node TableNames) Format(buf *TrackedBuffer) { 999 var prefix string 1000 for _, n := range node { 1001 buf.astPrintf(node, "%s%v", prefix, n) 1002 prefix = ", " 1003 } 1004 } 1005 1006 // Format formats the node. 1007 func (node TableName) Format(buf *TrackedBuffer) { 1008 if node.IsEmpty() { 1009 return 1010 } 1011 if !node.Qualifier.IsEmpty() { 1012 buf.astPrintf(node, "%v.", node.Qualifier) 1013 } 1014 buf.astPrintf(node, "%v", node.Name) 1015 } 1016 1017 // Format formats the node. 1018 func (node *ParenTableExpr) Format(buf *TrackedBuffer) { 1019 buf.astPrintf(node, "(%v)", node.Exprs) 1020 } 1021 1022 // Format formats the node. 1023 func (node *JoinCondition) Format(buf *TrackedBuffer) { 1024 if node == nil { 1025 return 1026 } 1027 if node.On != nil { 1028 buf.astPrintf(node, " on %v", node.On) 1029 } 1030 if node.Using != nil { 1031 buf.astPrintf(node, " using %v", node.Using) 1032 } 1033 } 1034 1035 // Format formats the node. 1036 func (node *JoinTableExpr) Format(buf *TrackedBuffer) { 1037 buf.astPrintf(node, "%v %s %v%v", node.LeftExpr, node.Join.ToString(), node.RightExpr, node.Condition) 1038 } 1039 1040 // Format formats the node. 1041 func (node *IndexHints) Format(buf *TrackedBuffer) { 1042 buf.astPrintf(node, " %sindex ", node.Type.ToString()) 1043 if len(node.Indexes) == 0 { 1044 buf.astPrintf(node, "()") 1045 } else { 1046 prefix := "(" 1047 for _, n := range node.Indexes { 1048 buf.astPrintf(node, "%s%v", prefix, n) 1049 prefix = ", " 1050 } 1051 buf.astPrintf(node, ")") 1052 } 1053 } 1054 1055 // Format formats the node. 1056 func (node *Where) Format(buf *TrackedBuffer) { 1057 if node == nil || node.Expr == nil { 1058 return 1059 } 1060 buf.astPrintf(node, " %s %v", node.Type.ToString(), node.Expr) 1061 } 1062 1063 // Format formats the node. 1064 func (node Exprs) Format(buf *TrackedBuffer) { 1065 var prefix string 1066 for _, n := range node { 1067 buf.astPrintf(node, "%s%v", prefix, n) 1068 prefix = ", " 1069 } 1070 } 1071 1072 // Format formats the node. 1073 func (node *AndExpr) Format(buf *TrackedBuffer) { 1074 buf.astPrintf(node, "%l and %r", node.Left, node.Right) 1075 } 1076 1077 // Format formats the node. 1078 func (node *OrExpr) Format(buf *TrackedBuffer) { 1079 buf.astPrintf(node, "%l or %r", node.Left, node.Right) 1080 } 1081 1082 // Format formats the node. 1083 func (node *XorExpr) Format(buf *TrackedBuffer) { 1084 buf.astPrintf(node, "%l xor %r", node.Left, node.Right) 1085 } 1086 1087 // Format formats the node. 1088 func (node *NotExpr) Format(buf *TrackedBuffer) { 1089 buf.astPrintf(node, "not %v", node.Expr) 1090 } 1091 1092 // Format formats the node. 1093 func (node *ComparisonExpr) Format(buf *TrackedBuffer) { 1094 buf.astPrintf(node, "%l %s %r", node.Left, node.Operator.ToString(), node.Right) 1095 if node.Escape != nil { 1096 buf.astPrintf(node, " escape %v", node.Escape) 1097 } 1098 } 1099 1100 // Format formats the node. 1101 func (node *BetweenExpr) Format(buf *TrackedBuffer) { 1102 if node.IsBetween { 1103 buf.astPrintf(node, "%v between %l and %r", node.Left, node.From, node.To) 1104 } else { 1105 buf.astPrintf(node, "%v not between %l and %r", node.Left, node.From, node.To) 1106 } 1107 } 1108 1109 // Format formats the node. 1110 func (node *IsExpr) Format(buf *TrackedBuffer) { 1111 buf.astPrintf(node, "%v %s", node.Left, node.Right.ToString()) 1112 } 1113 1114 // Format formats the node. 1115 func (node *ExistsExpr) Format(buf *TrackedBuffer) { 1116 buf.astPrintf(node, "exists %v", node.Subquery) 1117 } 1118 1119 // Format formats the node. 1120 func (node *Literal) Format(buf *TrackedBuffer) { 1121 switch node.Type { 1122 case StrVal: 1123 sqltypes.MakeTrusted(sqltypes.VarBinary, node.Bytes()).EncodeSQL(buf) 1124 case IntVal, FloatVal, DecimalVal, HexNum: 1125 buf.astPrintf(node, "%s", node.Val) 1126 case HexVal: 1127 buf.astPrintf(node, "X'%s'", node.Val) 1128 case BitVal: 1129 buf.astPrintf(node, "B'%s'", node.Val) 1130 default: 1131 panic("unexpected") 1132 } 1133 } 1134 1135 // Format formats the node. 1136 func (node Argument) Format(buf *TrackedBuffer) { 1137 buf.WriteArg(":", string(node)) 1138 } 1139 1140 // Format formats the node. 1141 func (node *NullVal) Format(buf *TrackedBuffer) { 1142 buf.astPrintf(node, "null") 1143 } 1144 1145 // Format formats the node. 1146 func (node BoolVal) Format(buf *TrackedBuffer) { 1147 if node { 1148 buf.astPrintf(node, "true") 1149 } else { 1150 buf.astPrintf(node, "false") 1151 } 1152 } 1153 1154 // Format formats the node. 1155 func (node *ColName) Format(buf *TrackedBuffer) { 1156 if !node.Qualifier.IsEmpty() { 1157 buf.astPrintf(node, "%v.", node.Qualifier) 1158 } 1159 buf.astPrintf(node, "%v", node.Name) 1160 } 1161 1162 // Format formats the node. 1163 func (node ValTuple) Format(buf *TrackedBuffer) { 1164 buf.astPrintf(node, "(%v)", Exprs(node)) 1165 } 1166 1167 // Format formats the node. 1168 func (node *Subquery) Format(buf *TrackedBuffer) { 1169 buf.astPrintf(node, "(%v)", node.Select) 1170 } 1171 1172 // Format formats the node. 1173 func (node *DerivedTable) Format(buf *TrackedBuffer) { 1174 buf.astPrintf(node, "(%v)", node.Select) 1175 } 1176 1177 // Format formats the node. 1178 func (node ListArg) Format(buf *TrackedBuffer) { 1179 buf.WriteArg("::", string(node)) 1180 } 1181 1182 // Format formats the node. 1183 func (node *BinaryExpr) Format(buf *TrackedBuffer) { 1184 buf.astPrintf(node, "%l %s %r", node.Left, node.Operator.ToString(), node.Right) 1185 } 1186 1187 // Format formats the node. 1188 func (node *UnaryExpr) Format(buf *TrackedBuffer) { 1189 if _, unary := node.Expr.(*UnaryExpr); unary { 1190 // They have same precedence so parenthesis is not required. 1191 buf.astPrintf(node, "%s %v", node.Operator.ToString(), node.Expr) 1192 return 1193 } 1194 buf.astPrintf(node, "%s%v", node.Operator.ToString(), node.Expr) 1195 } 1196 1197 // Format formats the node. 1198 func (node *IntroducerExpr) Format(buf *TrackedBuffer) { 1199 buf.astPrintf(node, "%s %v", node.CharacterSet, node.Expr) 1200 } 1201 1202 // Format formats the node. 1203 func (node *IntervalExpr) Format(buf *TrackedBuffer) { 1204 buf.astPrintf(node, "interval %v %s", node.Expr, node.Unit) 1205 } 1206 1207 // Format formats the node. 1208 func (node *TimestampFuncExpr) Format(buf *TrackedBuffer) { 1209 buf.astPrintf(node, "%s(%s, %v, %v)", node.Name, node.Unit, node.Expr1, node.Expr2) 1210 } 1211 1212 // Format formats the node. 1213 func (node *ExtractFuncExpr) Format(buf *TrackedBuffer) { 1214 buf.astPrintf(node, "extract(%s from %v)", node.IntervalTypes.ToString(), node.Expr) 1215 } 1216 1217 // Format formats the node. 1218 func (node *CurTimeFuncExpr) Format(buf *TrackedBuffer) { 1219 if node.Fsp != nil { 1220 buf.astPrintf(node, "%s(%v)", node.Name.String(), node.Fsp) 1221 } else { 1222 buf.astPrintf(node, "%s()", node.Name.String()) 1223 } 1224 } 1225 1226 // Format formats the node. 1227 func (node *CollateExpr) Format(buf *TrackedBuffer) { 1228 buf.astPrintf(node, "%v collate %s", node.Expr, node.Collation) 1229 } 1230 1231 // Format formats the node. 1232 func (node *FuncExpr) Format(buf *TrackedBuffer) { 1233 var distinct string 1234 if node.Distinct { 1235 distinct = "distinct " 1236 } 1237 if !node.Qualifier.IsEmpty() { 1238 buf.astPrintf(node, "%v.", node.Qualifier) 1239 } 1240 // Function names should not be back-quoted even 1241 // if they match a reserved word, only if they contain illegal characters 1242 funcName := node.Name.String() 1243 1244 if containEscapableChars(funcName, NoAt) { 1245 writeEscapedString(buf, funcName) 1246 } else { 1247 buf.WriteString(funcName) 1248 } 1249 buf.astPrintf(node, "(%s%v)", distinct, node.Exprs) 1250 } 1251 1252 // Format formats the node 1253 func (node *GroupConcatExpr) Format(buf *TrackedBuffer) { 1254 if node.Distinct { 1255 buf.astPrintf(node, "group_concat(%s%v%v%s%v)", DistinctStr, node.Exprs, node.OrderBy, node.Separator, node.Limit) 1256 } else { 1257 buf.astPrintf(node, "group_concat(%v%v%s%v)", node.Exprs, node.OrderBy, node.Separator, node.Limit) 1258 } 1259 } 1260 1261 // Format formats the node. 1262 func (node *ValuesFuncExpr) Format(buf *TrackedBuffer) { 1263 buf.astPrintf(node, "values(%v)", node.Name) 1264 } 1265 1266 // Format formats the node. 1267 func (node *SubstrExpr) Format(buf *TrackedBuffer) { 1268 if node.To == nil { 1269 buf.astPrintf(node, "substr(%v, %v)", node.Name, node.From) 1270 } else { 1271 buf.astPrintf(node, "substr(%v, %v, %v)", node.Name, node.From, node.To) 1272 } 1273 } 1274 1275 // Format formats the node. 1276 func (node *ConvertExpr) Format(buf *TrackedBuffer) { 1277 buf.astPrintf(node, "convert(%v, %v)", node.Expr, node.Type) 1278 } 1279 1280 // Format formats the node. 1281 func (node *ConvertUsingExpr) Format(buf *TrackedBuffer) { 1282 buf.astPrintf(node, "convert(%v using %s)", node.Expr, node.Type) 1283 } 1284 1285 // Format formats the node. 1286 func (node *ConvertType) Format(buf *TrackedBuffer) { 1287 buf.astPrintf(node, "%s", node.Type) 1288 if node.Length != nil { 1289 buf.astPrintf(node, "(%v", node.Length) 1290 if node.Scale != nil { 1291 buf.astPrintf(node, ", %v", node.Scale) 1292 } 1293 buf.astPrintf(node, ")") 1294 } 1295 if node.Charset != "" { 1296 buf.astPrintf(node, "%s %s", node.Operator.ToString(), node.Charset) 1297 } 1298 } 1299 1300 // Format formats the node 1301 func (node *MatchExpr) Format(buf *TrackedBuffer) { 1302 buf.astPrintf(node, "match(%v) against (%v%s)", node.Columns, node.Expr, node.Option.ToString()) 1303 } 1304 1305 // Format formats the node. 1306 func (node *CaseExpr) Format(buf *TrackedBuffer) { 1307 buf.astPrintf(node, "case ") 1308 if node.Expr != nil { 1309 buf.astPrintf(node, "%v ", node.Expr) 1310 } 1311 for _, when := range node.Whens { 1312 buf.astPrintf(node, "%v ", when) 1313 } 1314 if node.Else != nil { 1315 buf.astPrintf(node, "else %v ", node.Else) 1316 } 1317 buf.astPrintf(node, "end") 1318 } 1319 1320 // Format formats the node. 1321 func (node *Default) Format(buf *TrackedBuffer) { 1322 buf.astPrintf(node, "default") 1323 if node.ColName != "" { 1324 buf.WriteString("(") 1325 formatID(buf, node.ColName, NoAt) 1326 buf.WriteString(")") 1327 } 1328 } 1329 1330 // Format formats the node. 1331 func (node *When) Format(buf *TrackedBuffer) { 1332 buf.astPrintf(node, "when %v then %v", node.Cond, node.Val) 1333 } 1334 1335 // Format formats the node. 1336 func (node GroupBy) Format(buf *TrackedBuffer) { 1337 prefix := " group by " 1338 for _, n := range node { 1339 buf.astPrintf(node, "%s%v", prefix, n) 1340 prefix = ", " 1341 } 1342 } 1343 1344 // Format formats the node. 1345 func (node OrderBy) Format(buf *TrackedBuffer) { 1346 prefix := " order by " 1347 for _, n := range node { 1348 buf.astPrintf(node, "%s%v", prefix, n) 1349 prefix = ", " 1350 } 1351 } 1352 1353 // Format formats the node. 1354 func (node *Order) Format(buf *TrackedBuffer) { 1355 if node, ok := node.Expr.(*NullVal); ok { 1356 buf.astPrintf(node, "%v", node) 1357 return 1358 } 1359 if node, ok := node.Expr.(*FuncExpr); ok { 1360 if node.Name.Lowered() == "rand" { 1361 buf.astPrintf(node, "%v", node) 1362 return 1363 } 1364 } 1365 1366 buf.astPrintf(node, "%v %s", node.Expr, node.Direction.ToString()) 1367 } 1368 1369 // Format formats the node. 1370 func (node *Limit) Format(buf *TrackedBuffer) { 1371 if node == nil { 1372 return 1373 } 1374 buf.astPrintf(node, " limit ") 1375 if node.Offset != nil { 1376 buf.astPrintf(node, "%v, ", node.Offset) 1377 } 1378 buf.astPrintf(node, "%v", node.Rowcount) 1379 } 1380 1381 // Format formats the node. 1382 func (node Values) Format(buf *TrackedBuffer) { 1383 prefix := "values " 1384 for _, n := range node { 1385 buf.astPrintf(node, "%s%v", prefix, n) 1386 prefix = ", " 1387 } 1388 } 1389 1390 // Format formats the node. 1391 func (node UpdateExprs) Format(buf *TrackedBuffer) { 1392 var prefix string 1393 for _, n := range node { 1394 buf.astPrintf(node, "%s%v", prefix, n) 1395 prefix = ", " 1396 } 1397 } 1398 1399 // Format formats the node. 1400 func (node *UpdateExpr) Format(buf *TrackedBuffer) { 1401 buf.astPrintf(node, "%v = %v", node.Name, node.Expr) 1402 } 1403 1404 // Format formats the node. 1405 func (node SetExprs) Format(buf *TrackedBuffer) { 1406 var prefix string 1407 for _, n := range node { 1408 buf.astPrintf(node, "%s%v", prefix, n) 1409 prefix = ", " 1410 } 1411 } 1412 1413 // Format formats the node. 1414 func (node *SetExpr) Format(buf *TrackedBuffer) { 1415 if node.Scope != ImplicitScope { 1416 buf.WriteString(node.Scope.ToString()) 1417 buf.WriteString(" ") 1418 } 1419 // We don't have to backtick set variable names. 1420 switch { 1421 case node.Name.EqualString("charset") || node.Name.EqualString("names"): 1422 buf.astPrintf(node, "%s %v", node.Name.String(), node.Expr) 1423 case node.Name.EqualString(TransactionStr): 1424 literal := node.Expr.(*Literal) 1425 buf.astPrintf(node, "%s %s", node.Name.String(), strings.ToLower(string(literal.Val))) 1426 default: 1427 buf.astPrintf(node, "%v = %v", node.Name, node.Expr) 1428 } 1429 } 1430 1431 // Format formats the node. 1432 func (node OnDup) Format(buf *TrackedBuffer) { 1433 if node == nil { 1434 return 1435 } 1436 buf.astPrintf(node, " on duplicate key update %v", UpdateExprs(node)) 1437 } 1438 1439 // Format formats the node. 1440 func (node ColIdent) Format(buf *TrackedBuffer) { 1441 for i := NoAt; i < node.at; i++ { 1442 buf.WriteByte('@') 1443 } 1444 formatID(buf, node.val, node.at) 1445 } 1446 1447 // Format formats the node. 1448 func (node TableIdent) Format(buf *TrackedBuffer) { 1449 formatID(buf, node.v, NoAt) 1450 } 1451 1452 // Format formats the node. 1453 func (node IsolationLevel) Format(buf *TrackedBuffer) { 1454 buf.WriteString("isolation level ") 1455 switch node { 1456 case ReadUncommitted: 1457 buf.WriteString(ReadUncommittedStr) 1458 case ReadCommitted: 1459 buf.WriteString(ReadCommittedStr) 1460 case RepeatableRead: 1461 buf.WriteString(RepeatableReadStr) 1462 case Serializable: 1463 buf.WriteString(SerializableStr) 1464 default: 1465 buf.WriteString("Unknown Isolation level value") 1466 } 1467 } 1468 1469 // Format formats the node. 1470 func (node AccessMode) Format(buf *TrackedBuffer) { 1471 if node == ReadOnly { 1472 buf.WriteString(TxReadOnly) 1473 } else { 1474 buf.WriteString(TxReadWrite) 1475 } 1476 } 1477 1478 // Format formats the node. 1479 func (node *Load) Format(buf *TrackedBuffer) { 1480 buf.WriteString("AST node missing for Load type") 1481 } 1482 1483 // Format formats the node. 1484 func (node *ShowBasic) Format(buf *TrackedBuffer) { 1485 buf.WriteString("show") 1486 if node.Full { 1487 buf.WriteString(" full") 1488 } 1489 buf.astPrintf(node, "%s", node.Command.ToString()) 1490 if !node.Tbl.IsEmpty() { 1491 buf.astPrintf(node, " from %v", node.Tbl) 1492 } 1493 if !node.DbName.IsEmpty() { 1494 buf.astPrintf(node, " from %v", node.DbName) 1495 } 1496 buf.astPrintf(node, "%v", node.Filter) 1497 } 1498 1499 // Format formats the node. 1500 func (node *ShowCreate) Format(buf *TrackedBuffer) { 1501 buf.astPrintf(node, "show%s %v", node.Command.ToString(), node.Op) 1502 } 1503 1504 // Format formats the node. 1505 func (node *SelectInto) Format(buf *TrackedBuffer) { 1506 if node == nil { 1507 return 1508 } 1509 buf.astPrintf(node, "%s%s", node.Type.ToString(), node.FileName) 1510 if node.Charset != "" { 1511 buf.astPrintf(node, " character set %s", node.Charset) 1512 } 1513 buf.astPrintf(node, "%s%s%s%s", node.FormatOption, node.ExportOption, node.Manifest, node.Overwrite) 1514 } 1515 1516 // Format formats the node. 1517 func (node *CreateDatabase) Format(buf *TrackedBuffer) { 1518 buf.astPrintf(node, "create database %v", node.Comments) 1519 if node.IfNotExists { 1520 buf.WriteString("if not exists ") 1521 } 1522 buf.astPrintf(node, "%v", node.DBName) 1523 if node.CreateOptions != nil { 1524 for _, createOption := range node.CreateOptions { 1525 if createOption.IsDefault { 1526 buf.WriteString(" default") 1527 } 1528 buf.WriteString(createOption.Type.ToString()) 1529 buf.WriteString(" " + createOption.Value) 1530 } 1531 } 1532 } 1533 1534 // Format formats the node. 1535 func (node *AlterDatabase) Format(buf *TrackedBuffer) { 1536 buf.WriteString("alter database") 1537 if !node.DBName.IsEmpty() { 1538 buf.astPrintf(node, " %v", node.DBName) 1539 } 1540 if node.UpdateDataDirectory { 1541 buf.WriteString(" upgrade data directory name") 1542 } 1543 if node.AlterOptions != nil { 1544 for _, createOption := range node.AlterOptions { 1545 if createOption.IsDefault { 1546 buf.WriteString(" default") 1547 } 1548 buf.WriteString(createOption.Type.ToString()) 1549 buf.WriteString(" " + createOption.Value) 1550 } 1551 } 1552 } 1553 1554 // Format formats the node. 1555 func (node *CreateTable) Format(buf *TrackedBuffer) { 1556 buf.astPrintf(node, "create %v", node.Comments) 1557 if node.Temp { 1558 buf.WriteString("temporary ") 1559 } 1560 buf.WriteString("table ") 1561 1562 if node.IfNotExists { 1563 buf.WriteString("if not exists ") 1564 } 1565 buf.astPrintf(node, "%v", node.Table) 1566 1567 if node.OptLike != nil { 1568 buf.astPrintf(node, " %v", node.OptLike) 1569 } 1570 if node.TableSpec != nil { 1571 buf.astPrintf(node, " %v", node.TableSpec) 1572 } 1573 } 1574 1575 // Format formats the node. 1576 func (node *CreateView) Format(buf *TrackedBuffer) { 1577 buf.WriteString("create") 1578 if node.IsReplace { 1579 buf.WriteString(" or replace") 1580 } 1581 if node.Algorithm != "" { 1582 buf.astPrintf(node, " algorithm = %s", node.Algorithm) 1583 } 1584 if node.Definer != "" { 1585 buf.astPrintf(node, " definer = %s", node.Definer) 1586 } 1587 if node.Security != "" { 1588 buf.astPrintf(node, " sql security %s", node.Security) 1589 } 1590 buf.astPrintf(node, " view %v", node.ViewName) 1591 buf.astPrintf(node, "%v as %v", node.Columns, node.Select) 1592 if node.CheckOption != "" { 1593 buf.astPrintf(node, " with %s check option", node.CheckOption) 1594 } 1595 } 1596 1597 // Format formats the LockTables node. 1598 func (node *LockTables) Format(buf *TrackedBuffer) { 1599 buf.astPrintf(node, "lock tables %v %s", node.Tables[0].Table, node.Tables[0].Lock.ToString()) 1600 for i := 1; i < len(node.Tables); i++ { 1601 buf.astPrintf(node, ", %v %s", node.Tables[i].Table, node.Tables[i].Lock.ToString()) 1602 } 1603 } 1604 1605 // Format formats the UnlockTables node. 1606 func (node *UnlockTables) Format(buf *TrackedBuffer) { 1607 buf.WriteString("unlock tables") 1608 } 1609 1610 // Format formats the node. 1611 func (node *AlterView) Format(buf *TrackedBuffer) { 1612 buf.WriteString("alter") 1613 if node.Algorithm != "" { 1614 buf.astPrintf(node, " algorithm = %s", node.Algorithm) 1615 } 1616 if node.Definer != "" { 1617 buf.astPrintf(node, " definer = %s", node.Definer) 1618 } 1619 if node.Security != "" { 1620 buf.astPrintf(node, " sql security %s", node.Security) 1621 } 1622 buf.astPrintf(node, " view %v", node.ViewName) 1623 buf.astPrintf(node, "%v as %v", node.Columns, node.Select) 1624 if node.CheckOption != "" { 1625 buf.astPrintf(node, " with %s check option", node.CheckOption) 1626 } 1627 } 1628 1629 // Format formats the node. 1630 func (node *DropTable) Format(buf *TrackedBuffer) { 1631 temp := "" 1632 if node.Temp { 1633 temp = "temporary " 1634 } 1635 exists := "" 1636 if node.IfExists { 1637 exists = " if exists" 1638 } 1639 buf.astPrintf(node, "drop %v%stable%s %v", node.Comments, temp, exists, node.FromTables) 1640 } 1641 1642 // Format formats the node. 1643 func (node *DropView) Format(buf *TrackedBuffer) { 1644 exists := "" 1645 if node.IfExists { 1646 exists = " if exists" 1647 } 1648 buf.astPrintf(node, "drop view%s %v", exists, node.FromTables) 1649 } 1650 1651 // Format formats the AlterTable node. 1652 func (node *AlterTable) Format(buf *TrackedBuffer) { 1653 buf.astPrintf(node, "alter %vtable %v", node.Comments, node.Table) 1654 prefix := "" 1655 for i, option := range node.AlterOptions { 1656 if i != 0 { 1657 buf.WriteString(",") 1658 } 1659 buf.astPrintf(node, " %v", option) 1660 if node.PartitionSpec != nil && node.PartitionSpec.Action != RemoveAction { 1661 prefix = "," 1662 } 1663 } 1664 if node.PartitionSpec != nil { 1665 buf.astPrintf(node, "%s %v", prefix, node.PartitionSpec) 1666 } 1667 } 1668 1669 // Format formats the node. 1670 func (node *AddConstraintDefinition) Format(buf *TrackedBuffer) { 1671 buf.astPrintf(node, "add %v", node.ConstraintDefinition) 1672 } 1673 1674 // Format formats the node. 1675 func (node *AddIndexDefinition) Format(buf *TrackedBuffer) { 1676 buf.astPrintf(node, "add %v", node.IndexDefinition) 1677 } 1678 1679 // Format formats the node. 1680 func (node *AddColumns) Format(buf *TrackedBuffer) { 1681 1682 if len(node.Columns) == 1 { 1683 buf.astPrintf(node, "add column %v", node.Columns[0]) 1684 if node.First { 1685 buf.astPrintf(node, " first") 1686 } 1687 if node.After != nil { 1688 buf.astPrintf(node, " after %v", node.After) 1689 } 1690 } else { 1691 for i, col := range node.Columns { 1692 if i == 0 { 1693 buf.astPrintf(node, "add column (%v", col) 1694 } else { 1695 buf.astPrintf(node, ", %v", col) 1696 } 1697 } 1698 buf.WriteString(")") 1699 } 1700 } 1701 1702 // Format formats the node. 1703 func (node AlgorithmValue) Format(buf *TrackedBuffer) { 1704 buf.astPrintf(node, "algorithm = %s", string(node)) 1705 } 1706 1707 // Format formats the node 1708 func (node *AlterColumn) Format(buf *TrackedBuffer) { 1709 if node.DropDefault { 1710 buf.astPrintf(node, "alter column %v drop default", node.Column) 1711 } else { 1712 buf.astPrintf(node, "alter column %v set default", node.Column) 1713 buf.astPrintf(node, " %v", node.DefaultVal) 1714 } 1715 } 1716 1717 // Format formats the node 1718 func (node *ChangeColumn) Format(buf *TrackedBuffer) { 1719 buf.astPrintf(node, "change column %v %v", node.OldColumn, node.NewColDefinition) 1720 if node.First { 1721 buf.astPrintf(node, " first") 1722 } 1723 if node.After != nil { 1724 buf.astPrintf(node, " after %v", node.After) 1725 } 1726 } 1727 1728 // Format formats the node 1729 func (node *ModifyColumn) Format(buf *TrackedBuffer) { 1730 buf.astPrintf(node, "modify column %v", node.NewColDefinition) 1731 if node.First { 1732 buf.astPrintf(node, " first") 1733 } 1734 if node.After != nil { 1735 buf.astPrintf(node, " after %v", node.After) 1736 } 1737 } 1738 1739 // Format formats the node 1740 func (node *AlterCharset) Format(buf *TrackedBuffer) { 1741 buf.astPrintf(node, "convert to character set %s", node.CharacterSet) 1742 if node.Collate != "" { 1743 buf.astPrintf(node, " collate %s", node.Collate) 1744 } 1745 } 1746 1747 // Format formats the node 1748 func (node *KeyState) Format(buf *TrackedBuffer) { 1749 if node.Enable { 1750 buf.WriteString("enable keys") 1751 } else { 1752 buf.WriteString("disable keys") 1753 } 1754 1755 } 1756 1757 // Format formats the node 1758 func (node *TablespaceOperation) Format(buf *TrackedBuffer) { 1759 if node.Import { 1760 buf.WriteString("import tablespace") 1761 } else { 1762 buf.WriteString("discard tablespace") 1763 } 1764 } 1765 1766 // Format formats the node 1767 func (node *DropColumn) Format(buf *TrackedBuffer) { 1768 buf.astPrintf(node, "drop column %v", node.Name) 1769 } 1770 1771 // Format formats the node 1772 func (node *DropKey) Format(buf *TrackedBuffer) { 1773 buf.astPrintf(node, "drop %s", node.Type.ToString()) 1774 if !node.Name.IsEmpty() { 1775 buf.astPrintf(node, " %v", node.Name) 1776 } 1777 } 1778 1779 // Format formats the node 1780 func (node *Force) Format(buf *TrackedBuffer) { 1781 buf.WriteString("force") 1782 } 1783 1784 // Format formats the node 1785 func (node *LockOption) Format(buf *TrackedBuffer) { 1786 buf.astPrintf(node, "lock %s", node.Type.ToString()) 1787 } 1788 1789 // Format formats the node 1790 func (node *OrderByOption) Format(buf *TrackedBuffer) { 1791 buf.astPrintf(node, "order by ") 1792 prefix := "" 1793 for _, n := range node.Cols { 1794 buf.astPrintf(node, "%s%v", prefix, n) 1795 prefix = ", " 1796 } 1797 } 1798 1799 // Format formats the node 1800 func (node *RenameTableName) Format(buf *TrackedBuffer) { 1801 buf.astPrintf(node, "rename %v", node.Table) 1802 } 1803 1804 // Format formats the node 1805 func (node *RenameIndex) Format(buf *TrackedBuffer) { 1806 buf.astPrintf(node, "rename index %v to %v", node.OldName, node.NewName) 1807 } 1808 1809 // Format formats the node 1810 func (node *Validation) Format(buf *TrackedBuffer) { 1811 if node.With { 1812 buf.WriteString("with validation") 1813 } else { 1814 buf.WriteString("without validation") 1815 } 1816 } 1817 1818 // Format formats the node 1819 func (node TableOptions) Format(buf *TrackedBuffer) { 1820 for i, option := range node { 1821 if i != 0 { 1822 buf.WriteString(" ") 1823 } 1824 buf.astPrintf(node, "%s", option.Name) 1825 if option.String != "" { 1826 buf.astPrintf(node, " %s", option.String) 1827 } else if option.Value != nil { 1828 buf.astPrintf(node, " %v", option.Value) 1829 } else { 1830 buf.astPrintf(node, " (%v)", option.Tables) 1831 } 1832 } 1833 } 1834 1835 // Format formats the node 1836 func (node *TruncateTable) Format(buf *TrackedBuffer) { 1837 buf.astPrintf(node, "truncate table %v", node.Table) 1838 } 1839 1840 // Format formats the node. 1841 func (node *RenameTable) Format(buf *TrackedBuffer) { 1842 buf.astPrintf(node, "rename table") 1843 prefix := " " 1844 for _, pair := range node.TablePairs { 1845 buf.astPrintf(node, "%s%v to %v", prefix, pair.FromTable, pair.ToTable) 1846 prefix = ", " 1847 } 1848 } 1849 1850 // Format formats the node. 1851 // If an extracted subquery is still in the AST when we print it, 1852 // it will be formatted as if the subquery has been extracted, and instead 1853 // show up like argument comparisons 1854 func (node *ExtractedSubquery) Format(buf *TrackedBuffer) { 1855 node.alternative.Format(buf) 1856 }