github.com/vedadiyan/sqlparser@v1.0.0/pkg/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  	"github.com/vedadiyan/sqlparser/pkg/sqltypes"
    21  )
    22  
    23  // Format formats the node.
    24  func (node *Select) Format(buf *TrackedBuffer) {
    25  	if node.With != nil {
    26  		buf.astPrintf(node, "%v", node.With)
    27  	}
    28  	buf.astPrintf(node, "select %v", node.Comments)
    29  
    30  	if node.Distinct {
    31  		buf.literal(DistinctStr)
    32  	}
    33  	if node.Cache != nil {
    34  		if *node.Cache {
    35  			buf.literal(SQLCacheStr)
    36  		} else {
    37  			buf.literal(SQLNoCacheStr)
    38  		}
    39  	}
    40  	if node.StraightJoinHint {
    41  		buf.literal(StraightJoinHint)
    42  	}
    43  	if node.SQLCalcFoundRows {
    44  		buf.literal(SQLCalcFoundRowsStr)
    45  	}
    46  
    47  	buf.astPrintf(node, "%v from ", node.SelectExprs)
    48  
    49  	prefix := ""
    50  	for _, expr := range node.From {
    51  		buf.astPrintf(node, "%s%v", prefix, expr)
    52  		prefix = ", "
    53  	}
    54  
    55  	buf.astPrintf(node, "%v%v%v",
    56  		node.Where,
    57  		node.GroupBy, node.Having)
    58  
    59  	if node.Windows != nil {
    60  		buf.astPrintf(node, " %v", node.Windows)
    61  	}
    62  
    63  	buf.astPrintf(node, "%v%v%s%v",
    64  		node.OrderBy,
    65  		node.Limit, node.Lock.ToString(), node.Into)
    66  }
    67  
    68  // Format formats the node.
    69  func (node *CommentOnly) Format(buf *TrackedBuffer) {
    70  	for _, comment := range node.Comments {
    71  		buf.WriteString(comment)
    72  	}
    73  }
    74  
    75  // Format formats the node.
    76  func (node *Union) Format(buf *TrackedBuffer) {
    77  	if requiresParen(node.Left) {
    78  		buf.astPrintf(node, "(%v)", node.Left)
    79  	} else {
    80  		buf.astPrintf(node, "%v", node.Left)
    81  	}
    82  
    83  	buf.WriteByte(' ')
    84  	if node.Distinct {
    85  		buf.literal(UnionStr)
    86  	} else {
    87  		buf.literal(UnionAllStr)
    88  	}
    89  	buf.WriteByte(' ')
    90  
    91  	if requiresParen(node.Right) {
    92  		buf.astPrintf(node, "(%v)", node.Right)
    93  	} else {
    94  		buf.astPrintf(node, "%v", node.Right)
    95  	}
    96  
    97  	buf.astPrintf(node, "%v%v%s", node.OrderBy, node.Limit, node.Lock.ToString())
    98  }
    99  
   100  // Format formats the node.
   101  func (node *VStream) Format(buf *TrackedBuffer) {
   102  	buf.astPrintf(node, "vstream %v%v from %v",
   103  		node.Comments, node.SelectExpr, node.Table)
   104  }
   105  
   106  // Format formats the node.
   107  func (node *Stream) Format(buf *TrackedBuffer) {
   108  	buf.astPrintf(node, "stream %v%v from %v",
   109  		node.Comments, node.SelectExpr, node.Table)
   110  }
   111  
   112  // Format formats the node.
   113  func (node *Insert) Format(buf *TrackedBuffer) {
   114  	switch node.Action {
   115  	case InsertAct:
   116  		buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
   117  			InsertStr,
   118  			node.Comments, node.Ignore.ToString(),
   119  			node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup)
   120  	case ReplaceAct:
   121  		buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
   122  			ReplaceStr,
   123  			node.Comments, node.Ignore.ToString(),
   124  			node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup)
   125  	default:
   126  		buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
   127  			"Unkown Insert Action",
   128  			node.Comments, node.Ignore.ToString(),
   129  			node.Table, node.Partitions, node.Columns, node.Rows, node.OnDup)
   130  	}
   131  
   132  }
   133  
   134  // Format formats the node.
   135  func (node *With) Format(buf *TrackedBuffer) {
   136  	buf.astPrintf(node, "with ")
   137  
   138  	if node.Recursive {
   139  		buf.astPrintf(node, "recursive ")
   140  	}
   141  	ctesLength := len(node.Ctes)
   142  	for i := 0; i < ctesLength-1; i++ {
   143  		buf.astPrintf(node, "%v, ", node.Ctes[i])
   144  	}
   145  	buf.astPrintf(node, "%v", node.Ctes[ctesLength-1])
   146  }
   147  
   148  // Format formats the node.
   149  func (node *CommonTableExpr) Format(buf *TrackedBuffer) {
   150  	buf.astPrintf(node, "%v%v as %v ", node.ID, node.Columns, node.Subquery)
   151  }
   152  
   153  // Format formats the node.
   154  func (node *Update) Format(buf *TrackedBuffer) {
   155  	if node.With != nil {
   156  		buf.astPrintf(node, "%v", node.With)
   157  	}
   158  	buf.astPrintf(node, "update %v%s%v set %v%v%v%v",
   159  		node.Comments, node.Ignore.ToString(), node.TableExprs,
   160  		node.Exprs, node.Where, node.OrderBy, node.Limit)
   161  }
   162  
   163  // Format formats the node.
   164  func (node *Delete) Format(buf *TrackedBuffer) {
   165  	if node.With != nil {
   166  		buf.astPrintf(node, "%v", node.With)
   167  	}
   168  	buf.astPrintf(node, "delete %v", node.Comments)
   169  	if node.Ignore {
   170  		buf.literal("ignore ")
   171  	}
   172  	if node.Targets != nil {
   173  		buf.astPrintf(node, "%v ", node.Targets)
   174  	}
   175  	buf.astPrintf(node, "from %v%v%v%v%v", node.TableExprs, node.Partitions, node.Where, node.OrderBy, node.Limit)
   176  }
   177  
   178  // Format formats the node.
   179  func (node *Set) Format(buf *TrackedBuffer) {
   180  	buf.astPrintf(node, "set %v%v", node.Comments, node.Exprs)
   181  }
   182  
   183  // Format formats the node.
   184  func (node *DropDatabase) Format(buf *TrackedBuffer) {
   185  	exists := ""
   186  	if node.IfExists {
   187  		exists = "if exists "
   188  	}
   189  	buf.astPrintf(node, "%s %vdatabase %s%v", DropStr, node.Comments, exists, node.DBName)
   190  }
   191  
   192  // Format formats the node.
   193  func (node *Flush) Format(buf *TrackedBuffer) {
   194  	buf.astPrintf(node, "%s", FlushStr)
   195  	if node.IsLocal {
   196  		buf.literal(" local")
   197  	}
   198  	if len(node.FlushOptions) != 0 {
   199  		prefix := " "
   200  		for _, option := range node.FlushOptions {
   201  			buf.astPrintf(node, "%s%s", prefix, option)
   202  			prefix = ", "
   203  		}
   204  	} else {
   205  		buf.literal(" tables")
   206  		if len(node.TableNames) != 0 {
   207  			buf.astPrintf(node, " %v", node.TableNames)
   208  		}
   209  		if node.ForExport {
   210  			buf.literal(" for export")
   211  		}
   212  		if node.WithLock {
   213  			buf.literal(" with read lock")
   214  		}
   215  	}
   216  }
   217  
   218  // Format formats the node.
   219  func (node *AlterVschema) Format(buf *TrackedBuffer) {
   220  	switch node.Action {
   221  	case CreateVindexDDLAction:
   222  		buf.astPrintf(node, "alter vschema create vindex %v %v", node.Table, node.VindexSpec)
   223  	case DropVindexDDLAction:
   224  		buf.astPrintf(node, "alter vschema drop vindex %v", node.Table)
   225  	case AddVschemaTableDDLAction:
   226  		buf.astPrintf(node, "alter vschema add table %v", node.Table)
   227  	case DropVschemaTableDDLAction:
   228  		buf.astPrintf(node, "alter vschema drop table %v", node.Table)
   229  	case AddColVindexDDLAction:
   230  		buf.astPrintf(node, "alter vschema on %v add vindex %v (", node.Table, node.VindexSpec.Name)
   231  		for i, col := range node.VindexCols {
   232  			if i != 0 {
   233  				buf.astPrintf(node, ", %v", col)
   234  			} else {
   235  				buf.astPrintf(node, "%v", col)
   236  			}
   237  		}
   238  		buf.astPrintf(node, ")")
   239  		if node.VindexSpec.Type.String() != "" {
   240  			buf.astPrintf(node, " %v", node.VindexSpec)
   241  		}
   242  	case DropColVindexDDLAction:
   243  		buf.astPrintf(node, "alter vschema on %v drop vindex %v", node.Table, node.VindexSpec.Name)
   244  	case AddSequenceDDLAction:
   245  		buf.astPrintf(node, "alter vschema add sequence %v", node.Table)
   246  	case AddAutoIncDDLAction:
   247  		buf.astPrintf(node, "alter vschema on %v add auto_increment %v", node.Table, node.AutoIncSpec)
   248  	default:
   249  		buf.astPrintf(node, "%s table %v", node.Action.ToString(), node.Table)
   250  	}
   251  }
   252  
   253  // Format formats the node.
   254  func (node *AlterMigration) Format(buf *TrackedBuffer) {
   255  	buf.astPrintf(node, "alter vitess_migration")
   256  	if node.UUID != "" {
   257  		buf.astPrintf(node, " '%s'", node.UUID)
   258  	}
   259  	var alterType string
   260  	switch node.Type {
   261  	case RetryMigrationType:
   262  		alterType = "retry"
   263  	case CleanupMigrationType:
   264  		alterType = "cleanup"
   265  	case LaunchMigrationType:
   266  		alterType = "launch"
   267  	case LaunchAllMigrationType:
   268  		alterType = "launch all"
   269  	case CompleteMigrationType:
   270  		alterType = "complete"
   271  	case CompleteAllMigrationType:
   272  		alterType = "complete all"
   273  	case CancelMigrationType:
   274  		alterType = "cancel"
   275  	case CancelAllMigrationType:
   276  		alterType = "cancel all"
   277  	case ThrottleMigrationType:
   278  		alterType = "throttle"
   279  	case ThrottleAllMigrationType:
   280  		alterType = "throttle all"
   281  	case UnthrottleMigrationType:
   282  		alterType = "unthrottle"
   283  	case UnthrottleAllMigrationType:
   284  		alterType = "unthrottle all"
   285  	}
   286  	buf.astPrintf(node, " %s", alterType)
   287  	if node.Expire != "" {
   288  		buf.astPrintf(node, " expire '%s'", node.Expire)
   289  	}
   290  	if node.Ratio != nil {
   291  		buf.astPrintf(node, " ratio %v", node.Ratio)
   292  	}
   293  	if node.Shards != "" {
   294  		buf.astPrintf(node, " vitess_shards '%s'", node.Shards)
   295  	}
   296  }
   297  
   298  // Format formats the node.
   299  func (node *RevertMigration) Format(buf *TrackedBuffer) {
   300  	buf.astPrintf(node, "revert %vvitess_migration '%#s'", node.Comments, node.UUID)
   301  }
   302  
   303  // Format formats the node.
   304  func (node *ShowMigrationLogs) Format(buf *TrackedBuffer) {
   305  	buf.astPrintf(node, "show vitess_migration '%#s' logs", node.UUID)
   306  }
   307  
   308  // Format formats the node.
   309  func (node *ShowThrottledApps) Format(buf *TrackedBuffer) {
   310  	buf.astPrintf(node, "show vitess_throttled_apps")
   311  }
   312  
   313  // Format formats the node.
   314  func (node *ShowThrottlerStatus) Format(buf *TrackedBuffer) {
   315  	buf.astPrintf(node, "show vitess_throttler status")
   316  }
   317  
   318  // Format formats the node.
   319  func (node *OptLike) Format(buf *TrackedBuffer) {
   320  	buf.astPrintf(node, "like %v", node.LikeTable)
   321  }
   322  
   323  // Format formats the node.
   324  func (node *PartitionSpec) Format(buf *TrackedBuffer) {
   325  	switch node.Action {
   326  	case ReorganizeAction:
   327  		buf.astPrintf(node, "%s ", ReorganizeStr)
   328  		for i, n := range node.Names {
   329  			if i != 0 {
   330  				buf.literal(", ")
   331  			}
   332  			buf.astPrintf(node, "%v", n)
   333  		}
   334  		buf.literal(" into (")
   335  		for i, pd := range node.Definitions {
   336  			if i != 0 {
   337  				buf.literal(", ")
   338  			}
   339  			buf.astPrintf(node, "%v", pd)
   340  		}
   341  		buf.astPrintf(node, ")")
   342  	case AddAction:
   343  		buf.astPrintf(node, "%s (%v)", AddStr, node.Definitions[0])
   344  	case DropAction:
   345  		buf.astPrintf(node, "%s ", DropPartitionStr)
   346  		for i, n := range node.Names {
   347  			if i != 0 {
   348  				buf.literal(", ")
   349  			}
   350  			buf.astPrintf(node, "%v", n)
   351  		}
   352  	case DiscardAction:
   353  		buf.astPrintf(node, "%s ", DiscardStr)
   354  		if node.IsAll {
   355  			buf.literal("all")
   356  		} else {
   357  			prefix := ""
   358  			for _, n := range node.Names {
   359  				buf.astPrintf(node, "%s%v", prefix, n)
   360  				prefix = ", "
   361  			}
   362  		}
   363  		buf.literal(" tablespace")
   364  	case ImportAction:
   365  		buf.astPrintf(node, "%s ", ImportStr)
   366  		if node.IsAll {
   367  			buf.literal("all")
   368  		} else {
   369  			prefix := ""
   370  			for _, n := range node.Names {
   371  				buf.astPrintf(node, "%s%v", prefix, n)
   372  				prefix = ", "
   373  			}
   374  		}
   375  		buf.literal(" tablespace")
   376  	case TruncateAction:
   377  		buf.astPrintf(node, "%s ", TruncatePartitionStr)
   378  		if node.IsAll {
   379  			buf.literal("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 CoalesceAction:
   388  		buf.astPrintf(node, "%s %v", CoalesceStr, node.Number)
   389  	case ExchangeAction:
   390  		buf.astPrintf(node, "%s %v with table %v", ExchangeStr, node.Names[0], node.TableName)
   391  		if node.WithoutValidation {
   392  			buf.literal(" without validation")
   393  		}
   394  	case AnalyzeAction:
   395  		buf.astPrintf(node, "%s ", AnalyzePartitionStr)
   396  		if node.IsAll {
   397  			buf.literal("all")
   398  		} else {
   399  			prefix := ""
   400  			for _, n := range node.Names {
   401  				buf.astPrintf(node, "%s%v", prefix, n)
   402  				prefix = ", "
   403  			}
   404  		}
   405  	case CheckAction:
   406  		buf.astPrintf(node, "%s ", CheckStr)
   407  		if node.IsAll {
   408  			buf.literal("all")
   409  		} else {
   410  			prefix := ""
   411  			for _, n := range node.Names {
   412  				buf.astPrintf(node, "%s%v", prefix, n)
   413  				prefix = ", "
   414  			}
   415  		}
   416  	case OptimizeAction:
   417  		buf.astPrintf(node, "%s ", OptimizeStr)
   418  		if node.IsAll {
   419  			buf.literal("all")
   420  		} else {
   421  			prefix := ""
   422  			for _, n := range node.Names {
   423  				buf.astPrintf(node, "%s%v", prefix, n)
   424  				prefix = ", "
   425  			}
   426  		}
   427  	case RebuildAction:
   428  		buf.astPrintf(node, "%s ", RebuildStr)
   429  		if node.IsAll {
   430  			buf.literal("all")
   431  		} else {
   432  			prefix := ""
   433  			for _, n := range node.Names {
   434  				buf.astPrintf(node, "%s%v", prefix, n)
   435  				prefix = ", "
   436  			}
   437  		}
   438  	case RepairAction:
   439  		buf.astPrintf(node, "%s ", RepairStr)
   440  		if node.IsAll {
   441  			buf.literal("all")
   442  		} else {
   443  			prefix := ""
   444  			for _, n := range node.Names {
   445  				buf.astPrintf(node, "%s%v", prefix, n)
   446  				prefix = ", "
   447  			}
   448  		}
   449  	case RemoveAction:
   450  		buf.literal(RemoveStr)
   451  	case UpgradeAction:
   452  		buf.literal(UpgradeStr)
   453  	default:
   454  		panic("unimplemented")
   455  	}
   456  }
   457  
   458  // Format formats the node
   459  func (node *PartitionDefinition) Format(buf *TrackedBuffer) {
   460  	buf.astPrintf(node, "partition %v", node.Name)
   461  	buf.astPrintf(node, "%v", node.Options)
   462  }
   463  
   464  // Format formats the node
   465  func (node *PartitionDefinitionOptions) Format(buf *TrackedBuffer) {
   466  	if node.ValueRange != nil {
   467  		buf.astPrintf(node, " %v", node.ValueRange)
   468  	}
   469  	if node.Engine != nil {
   470  		buf.astPrintf(node, " %v", node.Engine)
   471  	}
   472  	if node.Comment != nil {
   473  		buf.astPrintf(node, " comment %v", node.Comment)
   474  	}
   475  	if node.DataDirectory != nil {
   476  		buf.astPrintf(node, " data directory %v", node.DataDirectory)
   477  	}
   478  	if node.IndexDirectory != nil {
   479  		buf.astPrintf(node, " index directory %v", node.IndexDirectory)
   480  	}
   481  	if node.MaxRows != nil {
   482  		buf.astPrintf(node, " max_rows %d", *node.MaxRows)
   483  	}
   484  	if node.MinRows != nil {
   485  		buf.astPrintf(node, " min_rows %d", *node.MinRows)
   486  	}
   487  	if node.TableSpace != "" {
   488  		buf.astPrintf(node, " tablespace %s", node.TableSpace)
   489  	}
   490  	if node.SubPartitionDefinitions != nil {
   491  		buf.astPrintf(node, " (%v)", node.SubPartitionDefinitions)
   492  	}
   493  }
   494  
   495  // Format formats the node
   496  func (node SubPartitionDefinitions) Format(buf *TrackedBuffer) {
   497  	var prefix string
   498  	for _, n := range node {
   499  		buf.astPrintf(node, "%s%v", prefix, n)
   500  		prefix = ", "
   501  	}
   502  }
   503  
   504  // Format formats the node
   505  func (node *SubPartitionDefinition) Format(buf *TrackedBuffer) {
   506  	buf.astPrintf(node, "subpartition %v", node.Name)
   507  	buf.astPrintf(node, "%v", node.Options)
   508  }
   509  
   510  // Format formats the node
   511  func (node *SubPartitionDefinitionOptions) Format(buf *TrackedBuffer) {
   512  	if node.Engine != nil {
   513  		buf.astPrintf(node, " %v", node.Engine)
   514  	}
   515  	if node.Comment != nil {
   516  		buf.astPrintf(node, " comment %v", node.Comment)
   517  	}
   518  	if node.DataDirectory != nil {
   519  		buf.astPrintf(node, " data directory %v", node.DataDirectory)
   520  	}
   521  	if node.IndexDirectory != nil {
   522  		buf.astPrintf(node, " index directory %v", node.IndexDirectory)
   523  	}
   524  	if node.MaxRows != nil {
   525  		buf.astPrintf(node, " max_rows %d", *node.MaxRows)
   526  	}
   527  	if node.MinRows != nil {
   528  		buf.astPrintf(node, " min_rows %d", *node.MinRows)
   529  	}
   530  	if node.TableSpace != "" {
   531  		buf.astPrintf(node, " tablespace %s", node.TableSpace)
   532  	}
   533  }
   534  
   535  // Format formats the node
   536  func (node *PartitionValueRange) Format(buf *TrackedBuffer) {
   537  	buf.astPrintf(node, "values %s", node.Type.ToString())
   538  	if node.Maxvalue {
   539  		buf.literal(" maxvalue")
   540  	} else {
   541  		buf.astPrintf(node, " %v", node.Range)
   542  	}
   543  }
   544  
   545  // Format formats the node
   546  func (node *PartitionEngine) Format(buf *TrackedBuffer) {
   547  	if node.Storage {
   548  		buf.astPrintf(node, "%s", "storage ")
   549  	}
   550  	buf.astPrintf(node, "%s", "engine ")
   551  	buf.astPrintf(node, "%#s", node.Name)
   552  }
   553  
   554  // Format formats the node.
   555  func (node *PartitionOption) Format(buf *TrackedBuffer) {
   556  	buf.literal("\npartition by")
   557  	if node.IsLinear {
   558  		buf.literal(" linear")
   559  	}
   560  
   561  	switch node.Type {
   562  	case HashType:
   563  		buf.astPrintf(node, " hash (%v)", node.Expr)
   564  	case KeyType:
   565  		buf.literal(" key")
   566  		if node.KeyAlgorithm != 0 {
   567  			buf.astPrintf(node, " algorithm = %d", node.KeyAlgorithm)
   568  		}
   569  		if len(node.ColList) == 0 {
   570  			buf.literal(" ()")
   571  		} else {
   572  			buf.astPrintf(node, " %v", node.ColList)
   573  		}
   574  	case RangeType, ListType:
   575  		buf.astPrintf(node, " %s", node.Type.ToString())
   576  		if node.Expr != nil {
   577  			buf.astPrintf(node, " (%v)", node.Expr)
   578  		} else {
   579  			buf.astPrintf(node, " columns %v", node.ColList)
   580  		}
   581  	}
   582  
   583  	if node.Partitions != -1 {
   584  		buf.astPrintf(node, " partitions %d", node.Partitions)
   585  	}
   586  	if node.SubPartition != nil {
   587  		buf.astPrintf(node, " %v", node.SubPartition)
   588  	}
   589  	if node.Definitions != nil {
   590  		buf.literal("\n(")
   591  		for i, pd := range node.Definitions {
   592  			if i != 0 {
   593  				buf.literal(",\n ")
   594  			}
   595  			buf.astPrintf(node, "%v", pd)
   596  		}
   597  		buf.WriteByte(')')
   598  	}
   599  }
   600  
   601  // Format formats the node.
   602  func (node *SubPartition) Format(buf *TrackedBuffer) {
   603  	buf.literal("subpartition by")
   604  	if node.IsLinear {
   605  		buf.literal(" linear")
   606  	}
   607  
   608  	switch node.Type {
   609  	case HashType:
   610  		buf.astPrintf(node, " hash (%v)", node.Expr)
   611  	case KeyType:
   612  		buf.literal(" key")
   613  		if node.KeyAlgorithm != 0 {
   614  			buf.astPrintf(node, " algorithm = %d", node.KeyAlgorithm)
   615  		}
   616  		if len(node.ColList) == 0 {
   617  			buf.literal(" ()")
   618  		} else {
   619  			buf.astPrintf(node, " %v", node.ColList)
   620  		}
   621  	}
   622  
   623  	if node.SubPartitions != -1 {
   624  		buf.astPrintf(node, " subpartitions %d", node.SubPartitions)
   625  	}
   626  }
   627  
   628  // Format formats the node.
   629  func (ts *TableSpec) Format(buf *TrackedBuffer) {
   630  	buf.astPrintf(ts, "(\n")
   631  	for i, col := range ts.Columns {
   632  		if i == 0 {
   633  			buf.astPrintf(ts, "\t%v", col)
   634  		} else {
   635  			buf.astPrintf(ts, ",\n\t%v", col)
   636  		}
   637  	}
   638  	for _, idx := range ts.Indexes {
   639  		buf.astPrintf(ts, ",\n\t%v", idx)
   640  	}
   641  	for _, c := range ts.Constraints {
   642  		buf.astPrintf(ts, ",\n\t%v", c)
   643  	}
   644  
   645  	buf.astPrintf(ts, "\n)")
   646  	for i, opt := range ts.Options {
   647  		if i != 0 {
   648  			buf.literal(",\n ")
   649  		}
   650  		buf.astPrintf(ts, " %s", opt.Name)
   651  		if opt.String != "" {
   652  			if opt.CaseSensitive {
   653  				buf.astPrintf(ts, " %#s", opt.String)
   654  			} else {
   655  				buf.astPrintf(ts, " %s", opt.String)
   656  			}
   657  		} else if opt.Value != nil {
   658  			buf.astPrintf(ts, " %v", opt.Value)
   659  		} else {
   660  			buf.astPrintf(ts, " (%v)", opt.Tables)
   661  		}
   662  	}
   663  	if ts.PartitionOption != nil {
   664  		buf.astPrintf(ts, "%v", ts.PartitionOption)
   665  	}
   666  }
   667  
   668  // Format formats the node.
   669  func (col *ColumnDefinition) Format(buf *TrackedBuffer) {
   670  	buf.astPrintf(col, "%v %v", col.Name, col.Type)
   671  }
   672  
   673  // Format returns a canonical string representation of the type and all relevant options
   674  func (ct *ColumnType) Format(buf *TrackedBuffer) {
   675  	buf.astPrintf(ct, "%#s", ct.Type)
   676  
   677  	if ct.Length != nil && ct.Scale != nil {
   678  		buf.astPrintf(ct, "(%v,%v)", ct.Length, ct.Scale)
   679  
   680  	} else if ct.Length != nil {
   681  		buf.astPrintf(ct, "(%v)", ct.Length)
   682  	}
   683  
   684  	if ct.EnumValues != nil {
   685  		buf.WriteString("(")
   686  		for i, enum := range ct.EnumValues {
   687  			if i > 0 {
   688  				buf.WriteString(", ")
   689  			}
   690  			buf.astPrintf(ct, "%#s", enum)
   691  		}
   692  		buf.WriteString(")")
   693  	}
   694  
   695  	if ct.Unsigned {
   696  		buf.astPrintf(ct, " %#s", keywordStrings[UNSIGNED])
   697  	}
   698  	if ct.Zerofill {
   699  		buf.astPrintf(ct, " %#s", keywordStrings[ZEROFILL])
   700  	}
   701  	if ct.Charset.Name != "" {
   702  		buf.astPrintf(ct, " %s %s %#s", keywordStrings[CHARACTER], keywordStrings[SET], ct.Charset.Name)
   703  	}
   704  	if ct.Charset.Binary {
   705  		buf.astPrintf(ct, " %#s", keywordStrings[BINARY])
   706  	}
   707  	if ct.Options != nil {
   708  		if ct.Options.Collate != "" {
   709  			buf.astPrintf(ct, " %s %#s", keywordStrings[COLLATE], ct.Options.Collate)
   710  		}
   711  		if ct.Options.Null != nil && ct.Options.As == nil {
   712  			if *ct.Options.Null {
   713  				buf.astPrintf(ct, " %s", keywordStrings[NULL])
   714  			} else {
   715  				buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL])
   716  			}
   717  		}
   718  		if ct.Options.Default != nil {
   719  			buf.astPrintf(ct, " %s", keywordStrings[DEFAULT])
   720  			if defaultRequiresParens(ct) {
   721  				buf.astPrintf(ct, " (%v)", ct.Options.Default)
   722  			} else {
   723  				buf.astPrintf(ct, " %v", ct.Options.Default)
   724  			}
   725  		}
   726  		if ct.Options.OnUpdate != nil {
   727  			buf.astPrintf(ct, " %s %s %v", keywordStrings[ON], keywordStrings[UPDATE], ct.Options.OnUpdate)
   728  		}
   729  		if ct.Options.As != nil {
   730  			buf.astPrintf(ct, " %s (%v)", keywordStrings[AS], ct.Options.As)
   731  
   732  			if ct.Options.Storage == VirtualStorage {
   733  				buf.astPrintf(ct, " %s", keywordStrings[VIRTUAL])
   734  			} else if ct.Options.Storage == StoredStorage {
   735  				buf.astPrintf(ct, " %s", keywordStrings[STORED])
   736  			}
   737  			if ct.Options.Null != nil {
   738  				if *ct.Options.Null {
   739  					buf.astPrintf(ct, " %s", keywordStrings[NULL])
   740  				} else {
   741  					buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL])
   742  				}
   743  			}
   744  		}
   745  		if ct.Options.Autoincrement {
   746  			buf.astPrintf(ct, " %s", keywordStrings[AUTO_INCREMENT])
   747  		}
   748  		if ct.Options.Comment != nil {
   749  			buf.astPrintf(ct, " %s %v", keywordStrings[COMMENT_KEYWORD], ct.Options.Comment)
   750  		}
   751  		if ct.Options.Invisible != nil {
   752  			if *ct.Options.Invisible {
   753  				buf.astPrintf(ct, " %s", keywordStrings[INVISIBLE])
   754  			} else {
   755  				buf.astPrintf(ct, " %s", keywordStrings[VISIBLE])
   756  			}
   757  		}
   758  		if ct.Options.Format != UnspecifiedFormat {
   759  			buf.astPrintf(ct, " %s %s", keywordStrings[COLUMN_FORMAT], ct.Options.Format.ToString())
   760  		}
   761  		if ct.Options.EngineAttribute != nil {
   762  			buf.astPrintf(ct, " %s %v", keywordStrings[ENGINE_ATTRIBUTE], ct.Options.EngineAttribute)
   763  		}
   764  		if ct.Options.SecondaryEngineAttribute != nil {
   765  			buf.astPrintf(ct, " %s %v", keywordStrings[SECONDARY_ENGINE_ATTRIBUTE], ct.Options.SecondaryEngineAttribute)
   766  		}
   767  		if ct.Options.KeyOpt == ColKeyPrimary {
   768  			buf.astPrintf(ct, " %s %s", keywordStrings[PRIMARY], keywordStrings[KEY])
   769  		}
   770  		if ct.Options.KeyOpt == ColKeyUnique {
   771  			buf.astPrintf(ct, " %s", keywordStrings[UNIQUE])
   772  		}
   773  		if ct.Options.KeyOpt == ColKeyUniqueKey {
   774  			buf.astPrintf(ct, " %s %s", keywordStrings[UNIQUE], keywordStrings[KEY])
   775  		}
   776  		if ct.Options.KeyOpt == ColKeySpatialKey {
   777  			buf.astPrintf(ct, " %s %s", keywordStrings[SPATIAL], keywordStrings[KEY])
   778  		}
   779  		if ct.Options.KeyOpt == ColKeyFulltextKey {
   780  			buf.astPrintf(ct, " %s %s", keywordStrings[FULLTEXT], keywordStrings[KEY])
   781  		}
   782  		if ct.Options.KeyOpt == ColKey {
   783  			buf.astPrintf(ct, " %s", keywordStrings[KEY])
   784  		}
   785  		if ct.Options.Reference != nil {
   786  			buf.astPrintf(ct, " %v", ct.Options.Reference)
   787  		}
   788  		if ct.Options.SRID != nil {
   789  			buf.astPrintf(ct, " %s %v", keywordStrings[SRID], ct.Options.SRID)
   790  		}
   791  	}
   792  }
   793  
   794  // Format formats the node.
   795  func (idx *IndexDefinition) Format(buf *TrackedBuffer) {
   796  	buf.astPrintf(idx, "%v (", idx.Info)
   797  	for i, col := range idx.Columns {
   798  		if i != 0 {
   799  			buf.astPrintf(idx, ", ")
   800  		}
   801  		if col.Expression != nil {
   802  			buf.astPrintf(idx, "(%v)", col.Expression)
   803  		} else {
   804  			buf.astPrintf(idx, "%v", col.Column)
   805  			if col.Length != nil {
   806  				buf.astPrintf(idx, "(%v)", col.Length)
   807  			}
   808  		}
   809  		if col.Direction == DescOrder {
   810  			buf.astPrintf(idx, " desc")
   811  		}
   812  	}
   813  	buf.astPrintf(idx, ")")
   814  
   815  	for _, opt := range idx.Options {
   816  		buf.astPrintf(idx, " %s", opt.Name)
   817  		if opt.String != "" {
   818  			buf.astPrintf(idx, " %#s", opt.String)
   819  		} else if opt.Value != nil {
   820  			buf.astPrintf(idx, " %v", opt.Value)
   821  		}
   822  	}
   823  }
   824  
   825  // Format formats the node.
   826  func (ii *IndexInfo) Format(buf *TrackedBuffer) {
   827  	if !ii.ConstraintName.IsEmpty() {
   828  		buf.astPrintf(ii, "constraint %v ", ii.ConstraintName)
   829  	}
   830  	if ii.Primary {
   831  		buf.astPrintf(ii, "%s", ii.Type)
   832  	} else {
   833  		buf.astPrintf(ii, "%s", ii.Type)
   834  		if !ii.Name.IsEmpty() {
   835  			buf.astPrintf(ii, " %v", ii.Name)
   836  		}
   837  	}
   838  }
   839  
   840  // Format formats the node.
   841  func (node *AutoIncSpec) Format(buf *TrackedBuffer) {
   842  	buf.astPrintf(node, "%v ", node.Column)
   843  	buf.astPrintf(node, "using %v", node.Sequence)
   844  }
   845  
   846  // Format formats the node. The "CREATE VINDEX" preamble was formatted in
   847  // the containing DDL node Format, so this just prints the type, any
   848  // parameters, and optionally the owner
   849  func (node *VindexSpec) Format(buf *TrackedBuffer) {
   850  	buf.astPrintf(node, "using %v", node.Type)
   851  
   852  	numParams := len(node.Params)
   853  	if numParams != 0 {
   854  		buf.astPrintf(node, " with ")
   855  		for i, p := range node.Params {
   856  			if i != 0 {
   857  				buf.astPrintf(node, ", ")
   858  			}
   859  			buf.astPrintf(node, "%v", p)
   860  		}
   861  	}
   862  }
   863  
   864  // Format formats the node.
   865  func (node VindexParam) Format(buf *TrackedBuffer) {
   866  	buf.astPrintf(node, "%s=%s", node.Key.String(), node.Val)
   867  }
   868  
   869  // Format formats the node.
   870  func (c *ConstraintDefinition) Format(buf *TrackedBuffer) {
   871  	if !c.Name.IsEmpty() {
   872  		buf.astPrintf(c, "constraint %v ", c.Name)
   873  	}
   874  	c.Details.Format(buf)
   875  }
   876  
   877  // Format formats the node.
   878  func (a ReferenceAction) Format(buf *TrackedBuffer) {
   879  	switch a {
   880  	case Restrict:
   881  		buf.literal("restrict")
   882  	case Cascade:
   883  		buf.literal("cascade")
   884  	case NoAction:
   885  		buf.literal("no action")
   886  	case SetNull:
   887  		buf.literal("set null")
   888  	case SetDefault:
   889  		buf.literal("set default")
   890  	}
   891  }
   892  
   893  // Format formats the node.
   894  func (a MatchAction) Format(buf *TrackedBuffer) {
   895  	switch a {
   896  	case Full:
   897  		buf.literal("full")
   898  	case Simple:
   899  		buf.literal("simple")
   900  	case Partial:
   901  		buf.literal("partial")
   902  	}
   903  }
   904  
   905  // Format formats the node.
   906  func (f *ForeignKeyDefinition) Format(buf *TrackedBuffer) {
   907  	buf.astPrintf(f, "foreign key %v%v %v", f.IndexName, f.Source, f.ReferenceDefinition)
   908  }
   909  
   910  // Format formats the node.
   911  func (ref *ReferenceDefinition) Format(buf *TrackedBuffer) {
   912  	buf.astPrintf(ref, "references %v %v", ref.ReferencedTable, ref.ReferencedColumns)
   913  	if ref.Match != DefaultMatch {
   914  		buf.astPrintf(ref, " match %v", ref.Match)
   915  	}
   916  	if ref.OnDelete != DefaultAction {
   917  		buf.astPrintf(ref, " on delete %v", ref.OnDelete)
   918  	}
   919  	if ref.OnUpdate != DefaultAction {
   920  		buf.astPrintf(ref, " on update %v", ref.OnUpdate)
   921  	}
   922  }
   923  
   924  // Format formats the node.
   925  func (c *CheckConstraintDefinition) Format(buf *TrackedBuffer) {
   926  	buf.astPrintf(c, "check (%v)", c.Expr)
   927  	if !c.Enforced {
   928  		buf.astPrintf(c, " not enforced")
   929  	}
   930  }
   931  
   932  // Format formats the node.
   933  func (node *Show) Format(buf *TrackedBuffer) {
   934  	buf.astPrintf(node, "%v", node.Internal)
   935  }
   936  
   937  // Format formats the node.
   938  func (node *ShowFilter) Format(buf *TrackedBuffer) {
   939  	if node == nil {
   940  		return
   941  	}
   942  	if node.Like != "" {
   943  		buf.astPrintf(node, " like ")
   944  		sqltypes.BufEncodeStringSQL(buf.Builder, node.Like)
   945  	} else {
   946  		buf.astPrintf(node, " where %v", node.Filter)
   947  	}
   948  }
   949  
   950  // Format formats the node.
   951  func (node *Use) Format(buf *TrackedBuffer) {
   952  	if node.DBName.v != "" {
   953  		buf.astPrintf(node, "use %v", node.DBName)
   954  	} else {
   955  		buf.astPrintf(node, "use")
   956  	}
   957  }
   958  
   959  // Format formats the node.
   960  func (node *Commit) Format(buf *TrackedBuffer) {
   961  	buf.literal("commit")
   962  }
   963  
   964  // Format formats the node.
   965  func (node *Begin) Format(buf *TrackedBuffer) {
   966  	if node.TxAccessModes == nil {
   967  		buf.literal("begin")
   968  		return
   969  	}
   970  	buf.literal("start transaction")
   971  	for idx, accessMode := range node.TxAccessModes {
   972  		if idx == 0 {
   973  			buf.astPrintf(node, " %s", accessMode.ToString())
   974  			continue
   975  		}
   976  		buf.astPrintf(node, ", %s", accessMode.ToString())
   977  	}
   978  
   979  }
   980  
   981  // Format formats the node.
   982  func (node *Rollback) Format(buf *TrackedBuffer) {
   983  	buf.literal("rollback")
   984  }
   985  
   986  // Format formats the node.
   987  func (node *SRollback) Format(buf *TrackedBuffer) {
   988  	buf.astPrintf(node, "rollback to %v", node.Name)
   989  }
   990  
   991  // Format formats the node.
   992  func (node *Savepoint) Format(buf *TrackedBuffer) {
   993  	buf.astPrintf(node, "savepoint %v", node.Name)
   994  }
   995  
   996  // Format formats the node.
   997  func (node *Release) Format(buf *TrackedBuffer) {
   998  	buf.astPrintf(node, "release savepoint %v", node.Name)
   999  }
  1000  
  1001  // Format formats the node.
  1002  func (node *ExplainStmt) Format(buf *TrackedBuffer) {
  1003  	format := ""
  1004  	switch node.Type {
  1005  	case EmptyType:
  1006  	case AnalyzeType:
  1007  		format = AnalyzeStr + " "
  1008  	default:
  1009  		format = "format = " + node.Type.ToString() + " "
  1010  	}
  1011  	buf.astPrintf(node, "explain %v%s%v", node.Comments, format, node.Statement)
  1012  }
  1013  
  1014  // Format formats the node.
  1015  func (node *VExplainStmt) Format(buf *TrackedBuffer) {
  1016  	buf.astPrintf(node, "vexplain %v%s %v", node.Comments, node.Type.ToString(), node.Statement)
  1017  }
  1018  
  1019  // Format formats the node.
  1020  func (node *ExplainTab) Format(buf *TrackedBuffer) {
  1021  	buf.astPrintf(node, "explain %v", node.Table)
  1022  	if node.Wild != "" {
  1023  		buf.astPrintf(node, " %s", node.Wild)
  1024  	}
  1025  }
  1026  
  1027  // Format formats the node.
  1028  func (node *PrepareStmt) Format(buf *TrackedBuffer) {
  1029  	buf.astPrintf(node, "prepare %v%v from ", node.Comments, node.Name)
  1030  	if node.Statement != nil {
  1031  		buf.astPrintf(node, "%v", node.Statement)
  1032  	}
  1033  }
  1034  
  1035  // Format formats the node.
  1036  func (node *ExecuteStmt) Format(buf *TrackedBuffer) {
  1037  	buf.astPrintf(node, "execute %v%v", node.Comments, node.Name)
  1038  	if len(node.Arguments) > 0 {
  1039  		buf.literal(" using ")
  1040  	}
  1041  	var prefix string
  1042  	for _, n := range node.Arguments {
  1043  		buf.astPrintf(node, "%s%v", prefix, n)
  1044  		prefix = ", "
  1045  	}
  1046  }
  1047  
  1048  // Format formats the node.
  1049  func (node *DeallocateStmt) Format(buf *TrackedBuffer) {
  1050  	buf.astPrintf(node, "%s %vprepare %v", node.Type.ToString(), node.Comments, node.Name)
  1051  }
  1052  
  1053  // Format formats the node.
  1054  func (node *CallProc) Format(buf *TrackedBuffer) {
  1055  	buf.astPrintf(node, "call %v(%v)", node.Name, node.Params)
  1056  }
  1057  
  1058  // Format formats the node.
  1059  func (node *OtherRead) Format(buf *TrackedBuffer) {
  1060  	buf.literal("otherread")
  1061  }
  1062  
  1063  // Format formats the node.
  1064  func (node *OtherAdmin) Format(buf *TrackedBuffer) {
  1065  	buf.literal("otheradmin")
  1066  }
  1067  
  1068  // Format formats the node.
  1069  func (node *ParsedComments) Format(buf *TrackedBuffer) {
  1070  	if node == nil {
  1071  		return
  1072  	}
  1073  	for _, c := range node.comments {
  1074  		buf.astPrintf(node, "%#s ", c)
  1075  	}
  1076  }
  1077  
  1078  // Format formats the node.
  1079  func (node SelectExprs) Format(buf *TrackedBuffer) {
  1080  	var prefix string
  1081  	for _, n := range node {
  1082  		buf.astPrintf(node, "%s%v", prefix, n)
  1083  		prefix = ", "
  1084  	}
  1085  }
  1086  
  1087  // Format formats the node.
  1088  func (node *StarExpr) Format(buf *TrackedBuffer) {
  1089  	if !node.TableName.IsEmpty() {
  1090  		buf.astPrintf(node, "%v.", node.TableName)
  1091  	}
  1092  	buf.astPrintf(node, "*")
  1093  }
  1094  
  1095  // Format formats the node.
  1096  func (node *AliasedExpr) Format(buf *TrackedBuffer) {
  1097  	buf.astPrintf(node, "%v", node.Expr)
  1098  	if !node.As.IsEmpty() {
  1099  		buf.astPrintf(node, " as %v", node.As)
  1100  	}
  1101  }
  1102  
  1103  // Format formats the node.
  1104  func (node *Nextval) Format(buf *TrackedBuffer) {
  1105  	buf.astPrintf(node, "next %v values", node.Expr)
  1106  }
  1107  
  1108  // Format formats the node.
  1109  func (node Columns) Format(buf *TrackedBuffer) {
  1110  	if node == nil {
  1111  		return
  1112  	}
  1113  	buf.WriteByte('(')
  1114  	prefix := ""
  1115  	for _, n := range node {
  1116  		buf.astPrintf(node, "%s%v", prefix, n)
  1117  		prefix = ", "
  1118  	}
  1119  	buf.WriteByte(')')
  1120  }
  1121  
  1122  // Format formats the node
  1123  func (node Partitions) Format(buf *TrackedBuffer) {
  1124  	if node == nil {
  1125  		return
  1126  	}
  1127  	prefix := " partition ("
  1128  	for _, n := range node {
  1129  		buf.astPrintf(node, "%s%v", prefix, n)
  1130  		prefix = ", "
  1131  	}
  1132  	buf.WriteByte(')')
  1133  }
  1134  
  1135  // Format formats the node.
  1136  func (node TableExprs) Format(buf *TrackedBuffer) {
  1137  	var prefix string
  1138  	for _, n := range node {
  1139  		buf.astPrintf(node, "%s%v", prefix, n)
  1140  		prefix = ", "
  1141  	}
  1142  }
  1143  
  1144  // Format formats the node.
  1145  func (node *AliasedTableExpr) Format(buf *TrackedBuffer) {
  1146  	buf.astPrintf(node, "%v%v", node.Expr, node.Partitions)
  1147  	if !node.As.IsEmpty() {
  1148  		buf.astPrintf(node, " as %v", node.As)
  1149  		if len(node.Columns) != 0 {
  1150  			buf.astPrintf(node, "%v", node.Columns)
  1151  		}
  1152  	}
  1153  	if node.Hints != nil {
  1154  		// Hint node provides the space padding.
  1155  		buf.astPrintf(node, "%v", node.Hints)
  1156  	}
  1157  }
  1158  
  1159  // Format formats the node.
  1160  func (node TableNames) Format(buf *TrackedBuffer) {
  1161  	var prefix string
  1162  	for _, n := range node {
  1163  		buf.astPrintf(node, "%s%v", prefix, n)
  1164  		prefix = ", "
  1165  	}
  1166  }
  1167  
  1168  // Format formats the node.
  1169  func (node TableName) Format(buf *TrackedBuffer) {
  1170  	if node.IsEmpty() {
  1171  		return
  1172  	}
  1173  	if !node.Qualifier.IsEmpty() {
  1174  		buf.astPrintf(node, "%v.", node.Qualifier)
  1175  	}
  1176  	buf.astPrintf(node, "%v", node.Name)
  1177  }
  1178  
  1179  // Format formats the node.
  1180  func (node *ParenTableExpr) Format(buf *TrackedBuffer) {
  1181  	buf.astPrintf(node, "(%v)", node.Exprs)
  1182  }
  1183  
  1184  // Format formats the node.
  1185  func (node *JoinCondition) Format(buf *TrackedBuffer) {
  1186  	if node == nil {
  1187  		return
  1188  	}
  1189  	if node.On != nil {
  1190  		buf.astPrintf(node, " on %v", node.On)
  1191  	}
  1192  	if node.Using != nil {
  1193  		buf.astPrintf(node, " using %v", node.Using)
  1194  	}
  1195  }
  1196  
  1197  // Format formats the node.
  1198  func (node *JoinTableExpr) Format(buf *TrackedBuffer) {
  1199  	buf.astPrintf(node, "%v %s %v%v", node.LeftExpr, node.Join.ToString(), node.RightExpr, node.Condition)
  1200  }
  1201  
  1202  // Format formats the node.
  1203  func (node IndexHints) Format(buf *TrackedBuffer) {
  1204  	for _, n := range node {
  1205  		buf.astPrintf(node, "%v", n)
  1206  	}
  1207  }
  1208  
  1209  // Format formats the node.
  1210  func (node *IndexHint) Format(buf *TrackedBuffer) {
  1211  	buf.astPrintf(node, " %sindex ", node.Type.ToString())
  1212  	if node.ForType != NoForType {
  1213  		buf.astPrintf(node, "for %s ", node.ForType.ToString())
  1214  	}
  1215  	if len(node.Indexes) == 0 {
  1216  		buf.astPrintf(node, "()")
  1217  	} else {
  1218  		prefix := "("
  1219  		for _, n := range node.Indexes {
  1220  			buf.astPrintf(node, "%s%v", prefix, n)
  1221  			prefix = ", "
  1222  		}
  1223  		buf.astPrintf(node, ")")
  1224  	}
  1225  }
  1226  
  1227  // Format formats the node.
  1228  func (node *Where) Format(buf *TrackedBuffer) {
  1229  	if node == nil || node.Expr == nil {
  1230  		return
  1231  	}
  1232  	buf.astPrintf(node, " %s %v", node.Type.ToString(), node.Expr)
  1233  }
  1234  
  1235  // Format formats the node.
  1236  func (node Exprs) Format(buf *TrackedBuffer) {
  1237  	var prefix string
  1238  	for _, n := range node {
  1239  		buf.astPrintf(node, "%s%v", prefix, n)
  1240  		prefix = ", "
  1241  	}
  1242  }
  1243  
  1244  // Format formats the node.
  1245  func (node *AndExpr) Format(buf *TrackedBuffer) {
  1246  	buf.astPrintf(node, "%l and %r", node.Left, node.Right)
  1247  }
  1248  
  1249  // Format formats the node.
  1250  func (node *OrExpr) Format(buf *TrackedBuffer) {
  1251  	buf.astPrintf(node, "%l or %r", node.Left, node.Right)
  1252  }
  1253  
  1254  // Format formats the node.
  1255  func (node *XorExpr) Format(buf *TrackedBuffer) {
  1256  	buf.astPrintf(node, "%l xor %r", node.Left, node.Right)
  1257  }
  1258  
  1259  // Format formats the node.
  1260  func (node *NotExpr) Format(buf *TrackedBuffer) {
  1261  	buf.astPrintf(node, "not %v", node.Expr)
  1262  }
  1263  
  1264  // Format formats the node.
  1265  func (node *ComparisonExpr) Format(buf *TrackedBuffer) {
  1266  	buf.astPrintf(node, "%l %s %r", node.Left, node.Operator.ToString(), node.Right)
  1267  	if node.Escape != nil {
  1268  		buf.astPrintf(node, " escape %v", node.Escape)
  1269  	}
  1270  }
  1271  
  1272  // Format formats the node.
  1273  func (node *BetweenExpr) Format(buf *TrackedBuffer) {
  1274  	if node.IsBetween {
  1275  		buf.astPrintf(node, "%v between %l and %r", node.Left, node.From, node.To)
  1276  	} else {
  1277  		buf.astPrintf(node, "%v not between %l and %r", node.Left, node.From, node.To)
  1278  	}
  1279  }
  1280  
  1281  // Format formats the node.
  1282  func (node *IsExpr) Format(buf *TrackedBuffer) {
  1283  	buf.astPrintf(node, "%v %s", node.Left, node.Right.ToString())
  1284  }
  1285  
  1286  // Format formats the node.
  1287  func (node *ExistsExpr) Format(buf *TrackedBuffer) {
  1288  	buf.astPrintf(node, "exists %v", node.Subquery)
  1289  }
  1290  
  1291  // Format formats the node.
  1292  func (node *Literal) Format(buf *TrackedBuffer) {
  1293  	switch node.Type {
  1294  	case StrVal:
  1295  		sqltypes.MakeTrusted(sqltypes.VarBinary, node.Bytes()).EncodeSQL(buf)
  1296  	case IntVal, FloatVal, DecimalVal, HexNum:
  1297  		buf.astPrintf(node, "%s", node.Val)
  1298  	case HexVal:
  1299  		buf.astPrintf(node, "X'%s'", node.Val)
  1300  	case BitVal:
  1301  		buf.astPrintf(node, "B'%s'", node.Val)
  1302  	case DateVal:
  1303  		buf.astPrintf(node, "date'%s'", node.Val)
  1304  	case TimeVal:
  1305  		buf.astPrintf(node, "time'%s'", node.Val)
  1306  	case TimestampVal:
  1307  		buf.astPrintf(node, "timestamp'%s'", node.Val)
  1308  	default:
  1309  		panic("unexpected")
  1310  	}
  1311  }
  1312  
  1313  // Format formats the node.
  1314  func (node Argument) Format(buf *TrackedBuffer) {
  1315  	buf.WriteArg(":", string(node))
  1316  }
  1317  
  1318  // Format formats the node.
  1319  func (node *NullVal) Format(buf *TrackedBuffer) {
  1320  	buf.astPrintf(node, "null")
  1321  }
  1322  
  1323  // Format formats the node.
  1324  func (node BoolVal) Format(buf *TrackedBuffer) {
  1325  	if node {
  1326  		buf.WriteString("true")
  1327  	} else {
  1328  		buf.WriteString("false")
  1329  	}
  1330  }
  1331  
  1332  // Format formats the node.
  1333  func (node *ColName) Format(buf *TrackedBuffer) {
  1334  	if !node.Qualifier.IsEmpty() {
  1335  		buf.astPrintf(node, "%v.", node.Qualifier)
  1336  	}
  1337  	buf.astPrintf(node, "%v", node.Name)
  1338  }
  1339  
  1340  // Format formats the node.
  1341  func (node ValTuple) Format(buf *TrackedBuffer) {
  1342  	buf.astPrintf(node, "(%v)", Exprs(node))
  1343  }
  1344  
  1345  // Format formats the node.
  1346  func (node *Subquery) Format(buf *TrackedBuffer) {
  1347  	buf.astPrintf(node, "(%v)", node.Select)
  1348  }
  1349  
  1350  // Format formats the node.
  1351  func (node *DerivedTable) Format(buf *TrackedBuffer) {
  1352  	if node.Lateral {
  1353  		buf.literal("lateral ")
  1354  	}
  1355  	buf.astPrintf(node, "(%v)", node.Select)
  1356  }
  1357  
  1358  // Format formats the node.
  1359  func (node ListArg) Format(buf *TrackedBuffer) {
  1360  	buf.WriteArg("::", string(node))
  1361  }
  1362  
  1363  // Format formats the node.
  1364  func (node *BinaryExpr) Format(buf *TrackedBuffer) {
  1365  	buf.astPrintf(node, "%l %s %r", node.Left, node.Operator.ToString(), node.Right)
  1366  }
  1367  
  1368  // Format formats the node.
  1369  func (node *UnaryExpr) Format(buf *TrackedBuffer) {
  1370  	if _, unary := node.Expr.(*UnaryExpr); unary {
  1371  		// They have same precedence so parenthesis is not required.
  1372  		buf.astPrintf(node, "%s %v", node.Operator.ToString(), node.Expr)
  1373  		return
  1374  	}
  1375  	buf.astPrintf(node, "%s%v", node.Operator.ToString(), node.Expr)
  1376  }
  1377  
  1378  // Format formats the node.
  1379  func (node *IntroducerExpr) Format(buf *TrackedBuffer) {
  1380  	buf.astPrintf(node, "%#s %v", node.CharacterSet, node.Expr)
  1381  }
  1382  
  1383  // Format formats the node.
  1384  func (node *IntervalExpr) Format(buf *TrackedBuffer) {
  1385  	buf.astPrintf(node, "interval %v %s", node.Expr, node.Unit)
  1386  }
  1387  
  1388  // Format formats the node.
  1389  func (node *TimestampFuncExpr) Format(buf *TrackedBuffer) {
  1390  	buf.astPrintf(node, "%s(%s, %v, %v)", node.Name, node.Unit, node.Expr1, node.Expr2)
  1391  }
  1392  
  1393  // Format formats the node.
  1394  func (node *ExtractFuncExpr) Format(buf *TrackedBuffer) {
  1395  	buf.astPrintf(node, "extract(%s from %v)", node.IntervalTypes.ToString(), node.Expr)
  1396  }
  1397  
  1398  // Format formats the node
  1399  func (node *RegexpInstrExpr) Format(buf *TrackedBuffer) {
  1400  	buf.astPrintf(node, "regexp_instr(%v, %v", node.Expr, node.Pattern)
  1401  	if node.Position != nil {
  1402  		buf.astPrintf(node, ", %v", node.Position)
  1403  	}
  1404  	if node.Occurrence != nil {
  1405  		buf.astPrintf(node, ", %v", node.Occurrence)
  1406  	}
  1407  	if node.ReturnOption != nil {
  1408  		buf.astPrintf(node, ", %v", node.ReturnOption)
  1409  	}
  1410  	if node.MatchType != nil {
  1411  		buf.astPrintf(node, ", %v", node.MatchType)
  1412  	}
  1413  	buf.WriteByte(')')
  1414  }
  1415  
  1416  // Format formats the node
  1417  func (node *RegexpLikeExpr) Format(buf *TrackedBuffer) {
  1418  	buf.astPrintf(node, "regexp_like(%v, %v", node.Expr, node.Pattern)
  1419  	if node.MatchType != nil {
  1420  		buf.astPrintf(node, ", %v", node.MatchType)
  1421  	}
  1422  	buf.WriteByte(')')
  1423  }
  1424  
  1425  // Format formats the node
  1426  func (node *RegexpReplaceExpr) Format(buf *TrackedBuffer) {
  1427  	buf.astPrintf(node, "regexp_replace(%v, %v, %v", node.Expr, node.Pattern, node.Repl)
  1428  	if node.Position != nil {
  1429  		buf.astPrintf(node, ", %v", node.Position)
  1430  	}
  1431  	if node.Occurrence != nil {
  1432  		buf.astPrintf(node, ", %v", node.Occurrence)
  1433  	}
  1434  	if node.MatchType != nil {
  1435  		buf.astPrintf(node, ", %v", node.MatchType)
  1436  	}
  1437  	buf.WriteByte(')')
  1438  }
  1439  
  1440  // Format formats the node
  1441  func (node *RegexpSubstrExpr) Format(buf *TrackedBuffer) {
  1442  	buf.astPrintf(node, "regexp_substr(%v, %v", node.Expr, node.Pattern)
  1443  	if node.Position != nil {
  1444  		buf.astPrintf(node, ", %v", node.Position)
  1445  	}
  1446  	if node.Occurrence != nil {
  1447  		buf.astPrintf(node, ", %v", node.Occurrence)
  1448  	}
  1449  	if node.MatchType != nil {
  1450  		buf.astPrintf(node, ", %v", node.MatchType)
  1451  	}
  1452  	buf.WriteByte(')')
  1453  }
  1454  
  1455  // Format formats the node.
  1456  func (node *TrimFuncExpr) Format(buf *TrackedBuffer) {
  1457  	buf.astPrintf(node, "%s(", node.TrimFuncType.ToString())
  1458  	if node.Type.ToString() != "" {
  1459  		buf.astPrintf(node, "%s ", node.Type.ToString())
  1460  	}
  1461  	if node.TrimArg != nil {
  1462  		buf.astPrintf(node, "%v ", node.TrimArg)
  1463  	}
  1464  
  1465  	if (node.Type.ToString() != "") || (node.TrimArg != nil) {
  1466  		buf.literal("from ")
  1467  	}
  1468  	buf.astPrintf(node, "%v", node.StringArg)
  1469  	buf.WriteByte(')')
  1470  }
  1471  
  1472  // Format formats the node.
  1473  func (node *WeightStringFuncExpr) Format(buf *TrackedBuffer) {
  1474  	if node.As != nil {
  1475  		buf.astPrintf(node, "weight_string(%v as %v)", node.Expr, node.As)
  1476  	} else {
  1477  		buf.astPrintf(node, "weight_string(%v)", node.Expr)
  1478  	}
  1479  }
  1480  
  1481  // Format formats the node.
  1482  func (node *CurTimeFuncExpr) Format(buf *TrackedBuffer) {
  1483  	if node.Fsp != nil {
  1484  		buf.astPrintf(node, "%s(%v)", node.Name.String(), node.Fsp)
  1485  	} else {
  1486  		buf.astPrintf(node, "%s()", node.Name.String())
  1487  	}
  1488  }
  1489  
  1490  // Format formats the node.
  1491  func (node *CollateExpr) Format(buf *TrackedBuffer) {
  1492  	buf.astPrintf(node, "%v collate %#s", node.Expr, node.Collation)
  1493  }
  1494  
  1495  // Format formats the node.
  1496  func (node *FuncExpr) Format(buf *TrackedBuffer) {
  1497  	if !node.Qualifier.IsEmpty() {
  1498  		buf.astPrintf(node, "%v.", node.Qualifier)
  1499  	}
  1500  	// Function names should not be back-quoted even
  1501  	// if they match a reserved word, only if they contain illegal characters
  1502  	funcName := node.Name.String()
  1503  
  1504  	if containEscapableChars(funcName, NoAt) {
  1505  		writeEscapedString(buf, funcName)
  1506  	} else {
  1507  		buf.WriteString(funcName)
  1508  	}
  1509  	buf.astPrintf(node, "(%v)", node.Exprs)
  1510  }
  1511  
  1512  // Format formats the node
  1513  func (node *GroupConcatExpr) Format(buf *TrackedBuffer) {
  1514  	if node.Distinct {
  1515  		buf.astPrintf(node, "group_concat(%s%v%v%s%v)", DistinctStr, node.Exprs, node.OrderBy, node.Separator, node.Limit)
  1516  	} else {
  1517  		buf.astPrintf(node, "group_concat(%v%v%s%v)", node.Exprs, node.OrderBy, node.Separator, node.Limit)
  1518  	}
  1519  }
  1520  
  1521  // Format formats the node.
  1522  func (node *ValuesFuncExpr) Format(buf *TrackedBuffer) {
  1523  	buf.astPrintf(node, "values(%v)", node.Name)
  1524  }
  1525  
  1526  // Format formats the node
  1527  func (node *JSONPrettyExpr) Format(buf *TrackedBuffer) {
  1528  	buf.astPrintf(node, "json_pretty(%v)", node.JSONVal)
  1529  
  1530  }
  1531  
  1532  // Format formats the node
  1533  func (node *JSONStorageFreeExpr) Format(buf *TrackedBuffer) {
  1534  	buf.astPrintf(node, "json_storage_free(%v)", node.JSONVal)
  1535  
  1536  }
  1537  
  1538  // Format formats the node
  1539  func (node *JSONStorageSizeExpr) Format(buf *TrackedBuffer) {
  1540  	buf.astPrintf(node, "json_storage_size(%v)", node.JSONVal)
  1541  
  1542  }
  1543  
  1544  // Format formats the node
  1545  func (node *OverClause) Format(buf *TrackedBuffer) {
  1546  	buf.WriteString("over")
  1547  	if !node.WindowName.IsEmpty() {
  1548  		buf.astPrintf(node, " %v", node.WindowName)
  1549  	}
  1550  	if node.WindowSpec != nil {
  1551  		buf.astPrintf(node, " (%v)", node.WindowSpec)
  1552  	}
  1553  }
  1554  
  1555  // Format formats the node
  1556  func (node *WindowSpecification) Format(buf *TrackedBuffer) {
  1557  	if !node.Name.IsEmpty() {
  1558  		buf.astPrintf(node, " %v", node.Name)
  1559  	}
  1560  	if node.PartitionClause != nil {
  1561  		buf.astPrintf(node, " partition by %v", node.PartitionClause)
  1562  	}
  1563  	if node.OrderClause != nil {
  1564  		buf.astPrintf(node, "%v", node.OrderClause)
  1565  	}
  1566  	if node.FrameClause != nil {
  1567  		buf.astPrintf(node, "%v", node.FrameClause)
  1568  	}
  1569  }
  1570  
  1571  // Format formats the node
  1572  func (node *FrameClause) Format(buf *TrackedBuffer) {
  1573  	buf.astPrintf(node, " %s", node.Unit.ToString())
  1574  	if node.End != nil {
  1575  		buf.astPrintf(node, " between%v and%v", node.Start, node.End)
  1576  	} else {
  1577  		buf.astPrintf(node, "%v", node.Start)
  1578  	}
  1579  }
  1580  
  1581  // Format formats the node
  1582  func (node *NullTreatmentClause) Format(buf *TrackedBuffer) {
  1583  	buf.astPrintf(node, " %s", node.Type.ToString())
  1584  }
  1585  
  1586  // Format formats the node
  1587  func (node *FromFirstLastClause) Format(buf *TrackedBuffer) {
  1588  	buf.astPrintf(node, " %s", node.Type.ToString())
  1589  }
  1590  
  1591  // Format formats the node
  1592  func (node *FramePoint) Format(buf *TrackedBuffer) {
  1593  	if node.Expr != nil {
  1594  		buf.astPrintf(node, " %v", node.Expr)
  1595  	}
  1596  	buf.astPrintf(node, " %s", node.Type.ToString())
  1597  }
  1598  
  1599  // Format formats the node
  1600  func (node *ArgumentLessWindowExpr) Format(buf *TrackedBuffer) {
  1601  	buf.astPrintf(node, "%s()", node.Type.ToString())
  1602  	if node.OverClause != nil {
  1603  		buf.astPrintf(node, " %v", node.OverClause)
  1604  	}
  1605  }
  1606  
  1607  // Format formats the node
  1608  func (node *FirstOrLastValueExpr) Format(buf *TrackedBuffer) {
  1609  	buf.astPrintf(node, "%s(%v)", node.Type.ToString(), node.Expr)
  1610  	if node.NullTreatmentClause != nil {
  1611  		buf.astPrintf(node, "%v", node.NullTreatmentClause)
  1612  	}
  1613  	if node.OverClause != nil {
  1614  		buf.astPrintf(node, " %v", node.OverClause)
  1615  	}
  1616  }
  1617  
  1618  // Format formats the node
  1619  func (node *NtileExpr) Format(buf *TrackedBuffer) {
  1620  	buf.astPrintf(node, "ntile(")
  1621  	buf.astPrintf(node, "%v", node.N)
  1622  	buf.WriteString(")")
  1623  	if node.OverClause != nil {
  1624  		buf.astPrintf(node, " %v", node.OverClause)
  1625  	}
  1626  }
  1627  
  1628  // Format formats the node
  1629  func (node *NTHValueExpr) Format(buf *TrackedBuffer) {
  1630  	buf.astPrintf(node, "nth_value(%v, ", node.Expr)
  1631  	buf.astPrintf(node, "%v", node.N)
  1632  	buf.WriteString(")")
  1633  	if node.FromFirstLastClause != nil {
  1634  		buf.astPrintf(node, "%v", node.FromFirstLastClause)
  1635  	}
  1636  	if node.NullTreatmentClause != nil {
  1637  		buf.astPrintf(node, "%v", node.NullTreatmentClause)
  1638  	}
  1639  	if node.OverClause != nil {
  1640  		buf.astPrintf(node, " %v", node.OverClause)
  1641  	}
  1642  }
  1643  
  1644  // Format formats the node
  1645  func (node *LagLeadExpr) Format(buf *TrackedBuffer) {
  1646  	buf.astPrintf(node, "%s(%v", node.Type.ToString(), node.Expr)
  1647  	if node.N != nil {
  1648  		buf.astPrintf(node, ", %v", node.N)
  1649  	}
  1650  	if node.Default != nil {
  1651  		buf.astPrintf(node, ", %v", node.Default)
  1652  	}
  1653  	buf.WriteString(")")
  1654  	if node.NullTreatmentClause != nil {
  1655  		buf.astPrintf(node, "%v", node.NullTreatmentClause)
  1656  	}
  1657  	if node.OverClause != nil {
  1658  		buf.astPrintf(node, " %v", node.OverClause)
  1659  	}
  1660  }
  1661  
  1662  // Format formats the node
  1663  func (node *ExtractValueExpr) Format(buf *TrackedBuffer) {
  1664  	buf.astPrintf(node, "extractvalue(%v, %v)", node.Fragment, node.XPathExpr)
  1665  }
  1666  
  1667  // Format formats the node
  1668  func (node *UpdateXMLExpr) Format(buf *TrackedBuffer) {
  1669  	buf.astPrintf(node, "updatexml(%v, %v, %v)", node.Target, node.XPathExpr, node.NewXML)
  1670  }
  1671  
  1672  func (node *PerformanceSchemaFuncExpr) Format(buf *TrackedBuffer) {
  1673  	buf.astPrintf(node, "%s(", node.Type.ToString())
  1674  	if node.Argument != nil {
  1675  		buf.astPrintf(node, "%v", node.Argument)
  1676  	}
  1677  	buf.astPrintf(node, ")")
  1678  }
  1679  
  1680  // Format formats the node
  1681  func (node *GTIDFuncExpr) Format(buf *TrackedBuffer) {
  1682  	buf.astPrintf(node, "%s(%v", node.Type.ToString(), node.Set1)
  1683  	if node.Set2 != nil {
  1684  		buf.astPrintf(node, ", %v", node.Set2)
  1685  	}
  1686  	if node.Timeout != nil {
  1687  		buf.astPrintf(node, ", %v", node.Timeout)
  1688  	}
  1689  	if node.Channel != nil {
  1690  		buf.astPrintf(node, ", %v", node.Channel)
  1691  	}
  1692  	buf.astPrintf(node, ")")
  1693  }
  1694  
  1695  // Format formats the node.
  1696  func (node *SubstrExpr) Format(buf *TrackedBuffer) {
  1697  	if node.To == nil {
  1698  		buf.astPrintf(node, "substr(%v, %v)", node.Name, node.From)
  1699  	} else {
  1700  		buf.astPrintf(node, "substr(%v, %v, %v)", node.Name, node.From, node.To)
  1701  	}
  1702  }
  1703  
  1704  // Format formats the node.
  1705  func (node *InsertExpr) Format(buf *TrackedBuffer) {
  1706  	buf.astPrintf(node, "insert(%v, %v, %v, %v)", node.Str, node.Pos, node.Len, node.NewStr)
  1707  }
  1708  
  1709  // Format formats the node.
  1710  func (node *IntervalFuncExpr) Format(buf *TrackedBuffer) {
  1711  	buf.astPrintf(node, "interval(%v, %v)", node.Expr, node.Exprs)
  1712  }
  1713  
  1714  // Format formats the node.
  1715  func (node *LocateExpr) Format(buf *TrackedBuffer) {
  1716  	if node.Pos != nil {
  1717  		buf.astPrintf(node, "locate(%v, %v, %v)", node.SubStr, node.Str, node.Pos)
  1718  	} else {
  1719  		buf.astPrintf(node, "locate(%v, %v)", node.SubStr, node.Str)
  1720  	}
  1721  }
  1722  
  1723  // Format formats the node.
  1724  func (node *CharExpr) Format(buf *TrackedBuffer) {
  1725  	buf.astPrintf(node, "char(%v", node.Exprs)
  1726  	if node.Charset != "" {
  1727  		buf.astPrintf(node, " using %#s", node.Charset)
  1728  	}
  1729  	buf.astPrintf(node, ")")
  1730  }
  1731  
  1732  // Format formats the node.
  1733  func (node *NamedWindow) Format(buf *TrackedBuffer) {
  1734  	buf.astPrintf(node, "window %v", node.Windows)
  1735  }
  1736  
  1737  // Format formats the node.
  1738  func (node NamedWindows) Format(buf *TrackedBuffer) {
  1739  	var prefix string
  1740  	for _, n := range node {
  1741  		buf.astPrintf(node, "%s%v", prefix, n)
  1742  		prefix = ", "
  1743  	}
  1744  }
  1745  
  1746  // Format formats the node.
  1747  func (node *WindowDefinition) Format(buf *TrackedBuffer) {
  1748  	buf.astPrintf(node, "%v AS (%v)", node.Name, node.WindowSpec)
  1749  }
  1750  
  1751  // Format formats the node.
  1752  func (node WindowDefinitions) Format(buf *TrackedBuffer) {
  1753  	var prefix string
  1754  	for _, n := range node {
  1755  		buf.astPrintf(node, "%s%v", prefix, n)
  1756  		prefix = ", "
  1757  	}
  1758  }
  1759  
  1760  // Format formats the node.
  1761  func (node *CastExpr) Format(buf *TrackedBuffer) {
  1762  	buf.astPrintf(node, "cast(%v as %v", node.Expr, node.Type)
  1763  	if node.Array {
  1764  		buf.astPrintf(node, " %s", keywordStrings[ARRAY])
  1765  	}
  1766  	buf.astPrintf(node, ")")
  1767  }
  1768  
  1769  // Format formats the node.
  1770  func (node *ConvertExpr) Format(buf *TrackedBuffer) {
  1771  	buf.astPrintf(node, "convert(%v, %v)", node.Expr, node.Type)
  1772  }
  1773  
  1774  // Format formats the node.
  1775  func (node *ConvertUsingExpr) Format(buf *TrackedBuffer) {
  1776  	buf.astPrintf(node, "convert(%v using %#s)", node.Expr, node.Type)
  1777  }
  1778  
  1779  // Format formats the node.
  1780  func (node *ConvertType) Format(buf *TrackedBuffer) {
  1781  	buf.astPrintf(node, "%s", node.Type)
  1782  	if node.Length != nil {
  1783  		buf.astPrintf(node, "(%v", node.Length)
  1784  		if node.Scale != nil {
  1785  			buf.astPrintf(node, ", %v", node.Scale)
  1786  		}
  1787  		buf.astPrintf(node, ")")
  1788  	}
  1789  	if node.Charset.Name != "" {
  1790  		buf.astPrintf(node, " character set %#s", node.Charset.Name)
  1791  	}
  1792  	if node.Charset.Binary {
  1793  		buf.astPrintf(node, " %#s", keywordStrings[BINARY])
  1794  	}
  1795  }
  1796  
  1797  // Format formats the node
  1798  func (node *MatchExpr) Format(buf *TrackedBuffer) {
  1799  	buf.astPrintf(node, "match(")
  1800  	for i, col := range node.Columns {
  1801  		if i != 0 {
  1802  			buf.astPrintf(node, ", %v", col)
  1803  		} else {
  1804  			buf.astPrintf(node, "%v", col)
  1805  		}
  1806  	}
  1807  	buf.astPrintf(node, ") against (%v%s)", node.Expr, node.Option.ToString())
  1808  }
  1809  
  1810  // Format formats the node.
  1811  func (node *CaseExpr) Format(buf *TrackedBuffer) {
  1812  	buf.astPrintf(node, "case ")
  1813  	if node.Expr != nil {
  1814  		buf.astPrintf(node, "%v ", node.Expr)
  1815  	}
  1816  	for _, when := range node.Whens {
  1817  		buf.astPrintf(node, "%v ", when)
  1818  	}
  1819  	if node.Else != nil {
  1820  		buf.astPrintf(node, "else %v ", node.Else)
  1821  	}
  1822  	buf.astPrintf(node, "end")
  1823  }
  1824  
  1825  // Format formats the node.
  1826  func (node *Default) Format(buf *TrackedBuffer) {
  1827  	buf.astPrintf(node, "default")
  1828  	if node.ColName != "" {
  1829  		buf.WriteByte('(')
  1830  		formatID(buf, node.ColName, NoAt)
  1831  		buf.WriteByte(')')
  1832  	}
  1833  }
  1834  
  1835  // Format formats the node.
  1836  func (node *When) Format(buf *TrackedBuffer) {
  1837  	buf.astPrintf(node, "when %v then %v", node.Cond, node.Val)
  1838  }
  1839  
  1840  // Format formats the node.
  1841  func (node GroupBy) Format(buf *TrackedBuffer) {
  1842  	prefix := " group by "
  1843  	for _, n := range node {
  1844  		buf.astPrintf(node, "%s%v", prefix, n)
  1845  		prefix = ", "
  1846  	}
  1847  }
  1848  
  1849  // Format formats the node.
  1850  func (node OrderBy) Format(buf *TrackedBuffer) {
  1851  	prefix := " order by "
  1852  	for _, n := range node {
  1853  		buf.astPrintf(node, "%s%v", prefix, n)
  1854  		prefix = ", "
  1855  	}
  1856  }
  1857  
  1858  // Format formats the node.
  1859  func (node *Order) Format(buf *TrackedBuffer) {
  1860  	if node, ok := node.Expr.(*NullVal); ok {
  1861  		buf.astPrintf(node, "%v", node)
  1862  		return
  1863  	}
  1864  	if node, ok := node.Expr.(*FuncExpr); ok {
  1865  		if node.Name.Lowered() == "rand" {
  1866  			buf.astPrintf(node, "%v", node)
  1867  			return
  1868  		}
  1869  	}
  1870  
  1871  	buf.astPrintf(node, "%v %s", node.Expr, node.Direction.ToString())
  1872  }
  1873  
  1874  // Format formats the node.
  1875  func (node *Limit) Format(buf *TrackedBuffer) {
  1876  	if node == nil {
  1877  		return
  1878  	}
  1879  	buf.astPrintf(node, " limit ")
  1880  	if node.Offset != nil {
  1881  		buf.astPrintf(node, "%v, ", node.Offset)
  1882  	}
  1883  	buf.astPrintf(node, "%v", node.Rowcount)
  1884  }
  1885  
  1886  // Format formats the node.
  1887  func (node Values) Format(buf *TrackedBuffer) {
  1888  	prefix := "values "
  1889  	for _, n := range node {
  1890  		buf.astPrintf(node, "%s%v", prefix, n)
  1891  		prefix = ", "
  1892  	}
  1893  }
  1894  
  1895  // Format formats the node.
  1896  func (node UpdateExprs) Format(buf *TrackedBuffer) {
  1897  	var prefix string
  1898  	for _, n := range node {
  1899  		buf.astPrintf(node, "%s%v", prefix, n)
  1900  		prefix = ", "
  1901  	}
  1902  }
  1903  
  1904  // Format formats the node.
  1905  func (node *UpdateExpr) Format(buf *TrackedBuffer) {
  1906  	buf.astPrintf(node, "%v = %v", node.Name, node.Expr)
  1907  }
  1908  
  1909  // Format formats the node.
  1910  func (node SetExprs) Format(buf *TrackedBuffer) {
  1911  	var prefix string
  1912  	for _, n := range node {
  1913  		buf.astPrintf(node, "%s%v", prefix, n)
  1914  		prefix = ", "
  1915  	}
  1916  }
  1917  
  1918  // Format formats the node.
  1919  func (node *SetExpr) Format(buf *TrackedBuffer) {
  1920  	// We don't have to backtick set variable names.
  1921  	switch {
  1922  	case node.Var.Name.EqualString("charset") || node.Var.Name.EqualString("names"):
  1923  		buf.astPrintf(node, "%s %v", node.Var.Name.String(), node.Expr)
  1924  	default:
  1925  		buf.astPrintf(node, "%v = %v", node.Var, node.Expr)
  1926  	}
  1927  }
  1928  
  1929  // Format formats the node.
  1930  func (node OnDup) Format(buf *TrackedBuffer) {
  1931  	if node == nil {
  1932  		return
  1933  	}
  1934  	buf.astPrintf(node, " on duplicate key update %v", UpdateExprs(node))
  1935  }
  1936  
  1937  // Format formats the node.
  1938  func (node IdentifierCI) Format(buf *TrackedBuffer) {
  1939  	if node.IsEmpty() {
  1940  		return
  1941  	}
  1942  	formatID(buf, node.val, NoAt)
  1943  }
  1944  
  1945  // Format formats the node.
  1946  func (node IdentifierCS) Format(buf *TrackedBuffer) {
  1947  	formatID(buf, node.v, NoAt)
  1948  }
  1949  
  1950  // Format formats the node.
  1951  func (node *Load) Format(buf *TrackedBuffer) {
  1952  	buf.literal("AST node missing for Load type")
  1953  }
  1954  
  1955  // Format formats the node.
  1956  func (node *ShowBasic) Format(buf *TrackedBuffer) {
  1957  	buf.literal("show")
  1958  	if node.Full {
  1959  		buf.literal(" full")
  1960  	}
  1961  	buf.astPrintf(node, "%s", node.Command.ToString())
  1962  	if !node.Tbl.IsEmpty() {
  1963  		buf.astPrintf(node, " from %v", node.Tbl)
  1964  	}
  1965  	if !node.DbName.IsEmpty() {
  1966  		buf.astPrintf(node, " from %v", node.DbName)
  1967  	}
  1968  	buf.astPrintf(node, "%v", node.Filter)
  1969  }
  1970  
  1971  // Format formats the node.
  1972  func (node *ShowCreate) Format(buf *TrackedBuffer) {
  1973  	buf.astPrintf(node, "show%s %v", node.Command.ToString(), node.Op)
  1974  }
  1975  
  1976  // Format formats the node.
  1977  func (node *ShowOther) Format(buf *TrackedBuffer) {
  1978  	buf.astPrintf(node, "show %s", node.Command)
  1979  }
  1980  
  1981  // Format formats the node.
  1982  func (node *SelectInto) Format(buf *TrackedBuffer) {
  1983  	if node == nil {
  1984  		return
  1985  	}
  1986  	buf.astPrintf(node, "%s%s", node.Type.ToString(), node.FileName)
  1987  	if node.Charset.Name != "" {
  1988  		buf.astPrintf(node, " character set %s", node.Charset.Name)
  1989  	}
  1990  	buf.astPrintf(node, "%s%s%s%s", node.FormatOption, node.ExportOption, node.Manifest, node.Overwrite)
  1991  }
  1992  
  1993  // Format formats the node.
  1994  func (node *CreateDatabase) Format(buf *TrackedBuffer) {
  1995  	buf.astPrintf(node, "create database %v", node.Comments)
  1996  	if node.IfNotExists {
  1997  		buf.literal("if not exists ")
  1998  	}
  1999  	buf.astPrintf(node, "%v", node.DBName)
  2000  	if node.CreateOptions != nil {
  2001  		for _, createOption := range node.CreateOptions {
  2002  			if createOption.IsDefault {
  2003  				buf.literal(" default")
  2004  			}
  2005  			buf.literal(createOption.Type.ToString())
  2006  			buf.WriteByte(' ')
  2007  			buf.literal(createOption.Value)
  2008  		}
  2009  	}
  2010  }
  2011  
  2012  // Format formats the node.
  2013  func (node *AlterDatabase) Format(buf *TrackedBuffer) {
  2014  	buf.literal("alter database")
  2015  	if !node.DBName.IsEmpty() {
  2016  		buf.astPrintf(node, " %v", node.DBName)
  2017  	}
  2018  	if node.UpdateDataDirectory {
  2019  		buf.literal(" upgrade data directory name")
  2020  	}
  2021  	if node.AlterOptions != nil {
  2022  		for _, createOption := range node.AlterOptions {
  2023  			if createOption.IsDefault {
  2024  				buf.literal(" default")
  2025  			}
  2026  			buf.literal(createOption.Type.ToString())
  2027  			buf.WriteByte(' ')
  2028  			buf.literal(createOption.Value)
  2029  		}
  2030  	}
  2031  }
  2032  
  2033  // Format formats the node.
  2034  func (node *CreateTable) Format(buf *TrackedBuffer) {
  2035  	buf.astPrintf(node, "create %v", node.Comments)
  2036  	if node.Temp {
  2037  		buf.literal("temporary ")
  2038  	}
  2039  	buf.literal("table ")
  2040  
  2041  	if node.IfNotExists {
  2042  		buf.literal("if not exists ")
  2043  	}
  2044  	buf.astPrintf(node, "%v", node.Table)
  2045  
  2046  	if node.OptLike != nil {
  2047  		buf.astPrintf(node, " %v", node.OptLike)
  2048  	}
  2049  	if node.TableSpec != nil {
  2050  		buf.astPrintf(node, " %v", node.TableSpec)
  2051  	}
  2052  }
  2053  
  2054  // Format formats the node.
  2055  func (node *CreateView) Format(buf *TrackedBuffer) {
  2056  	buf.astPrintf(node, "create %v", node.Comments)
  2057  	if node.IsReplace {
  2058  		buf.literal("or replace ")
  2059  	}
  2060  	if node.Algorithm != "" {
  2061  		buf.astPrintf(node, "algorithm = %#s ", node.Algorithm)
  2062  	}
  2063  	if node.Definer != nil {
  2064  		buf.astPrintf(node, "definer = %v ", node.Definer)
  2065  	}
  2066  	if node.Security != "" {
  2067  		buf.astPrintf(node, "sql security %s ", node.Security)
  2068  	}
  2069  	buf.astPrintf(node, "view %v", node.ViewName)
  2070  	buf.astPrintf(node, "%v as %v", node.Columns, node.Select)
  2071  	if node.CheckOption != "" {
  2072  		buf.astPrintf(node, " with %s check option", node.CheckOption)
  2073  	}
  2074  }
  2075  
  2076  // Format formats the LockTables node.
  2077  func (node *LockTables) Format(buf *TrackedBuffer) {
  2078  	buf.astPrintf(node, "lock tables %v %s", node.Tables[0].Table, node.Tables[0].Lock.ToString())
  2079  	for i := 1; i < len(node.Tables); i++ {
  2080  		buf.astPrintf(node, ", %v %s", node.Tables[i].Table, node.Tables[i].Lock.ToString())
  2081  	}
  2082  }
  2083  
  2084  // Format formats the UnlockTables node.
  2085  func (node *UnlockTables) Format(buf *TrackedBuffer) {
  2086  	buf.literal("unlock tables")
  2087  }
  2088  
  2089  // Format formats the node.
  2090  func (node *AlterView) Format(buf *TrackedBuffer) {
  2091  	buf.astPrintf(node, "alter %v", node.Comments)
  2092  	if node.Algorithm != "" {
  2093  		buf.astPrintf(node, "algorithm = %s ", node.Algorithm)
  2094  	}
  2095  	if node.Definer != nil {
  2096  		buf.astPrintf(node, "definer = %v ", node.Definer)
  2097  	}
  2098  	if node.Security != "" {
  2099  		buf.astPrintf(node, "sql security %s ", node.Security)
  2100  	}
  2101  	buf.astPrintf(node, "view %v", node.ViewName)
  2102  	buf.astPrintf(node, "%v as %v", node.Columns, node.Select)
  2103  	if node.CheckOption != "" {
  2104  		buf.astPrintf(node, " with %s check option", node.CheckOption)
  2105  	}
  2106  }
  2107  
  2108  func (definer *Definer) Format(buf *TrackedBuffer) {
  2109  	buf.astPrintf(definer, "%#s", definer.Name)
  2110  	if definer.Address != "" {
  2111  		buf.astPrintf(definer, "@%#s", definer.Address)
  2112  	}
  2113  }
  2114  
  2115  // Format formats the node.
  2116  func (node *DropTable) Format(buf *TrackedBuffer) {
  2117  	temp := ""
  2118  	if node.Temp {
  2119  		temp = "temporary "
  2120  	}
  2121  	exists := ""
  2122  	if node.IfExists {
  2123  		exists = " if exists"
  2124  	}
  2125  	buf.astPrintf(node, "drop %v%stable%s %v", node.Comments, temp, exists, node.FromTables)
  2126  }
  2127  
  2128  // Format formats the node.
  2129  func (node *DropView) Format(buf *TrackedBuffer) {
  2130  	buf.astPrintf(node, "drop %v", node.Comments)
  2131  	exists := ""
  2132  	if node.IfExists {
  2133  		exists = " if exists"
  2134  	}
  2135  	buf.astPrintf(node, "view%s %v", exists, node.FromTables)
  2136  }
  2137  
  2138  // Format formats the AlterTable node.
  2139  func (node *AlterTable) Format(buf *TrackedBuffer) {
  2140  	buf.astPrintf(node, "alter %vtable %v", node.Comments, node.Table)
  2141  	prefix := ""
  2142  	for i, option := range node.AlterOptions {
  2143  		if i != 0 {
  2144  			buf.WriteByte(',')
  2145  		}
  2146  		buf.astPrintf(node, " %v", option)
  2147  		if node.PartitionSpec != nil && node.PartitionSpec.Action != RemoveAction {
  2148  			prefix = ","
  2149  		}
  2150  	}
  2151  	if node.PartitionSpec != nil {
  2152  		buf.astPrintf(node, "%s %v", prefix, node.PartitionSpec)
  2153  	}
  2154  	if node.PartitionOption != nil {
  2155  		buf.astPrintf(node, "%s %v", prefix, node.PartitionOption)
  2156  	}
  2157  }
  2158  
  2159  // Format formats the node.
  2160  func (node *AddConstraintDefinition) Format(buf *TrackedBuffer) {
  2161  	buf.astPrintf(node, "add %v", node.ConstraintDefinition)
  2162  }
  2163  
  2164  func (node *AlterCheck) Format(buf *TrackedBuffer) {
  2165  	buf.astPrintf(node, "alter check %v", node.Name)
  2166  	if node.Enforced {
  2167  		buf.astPrintf(node, " %s", keywordStrings[ENFORCED])
  2168  	} else {
  2169  		buf.astPrintf(node, " %s %s", keywordStrings[NOT], keywordStrings[ENFORCED])
  2170  	}
  2171  }
  2172  
  2173  // Format formats the node.
  2174  func (node *AddIndexDefinition) Format(buf *TrackedBuffer) {
  2175  	buf.astPrintf(node, "add %v", node.IndexDefinition)
  2176  }
  2177  
  2178  // Format formats the node.
  2179  func (node *AddColumns) Format(buf *TrackedBuffer) {
  2180  
  2181  	if len(node.Columns) == 1 {
  2182  		buf.astPrintf(node, "add column %v", node.Columns[0])
  2183  		if node.First {
  2184  			buf.astPrintf(node, " first")
  2185  		}
  2186  		if node.After != nil {
  2187  			buf.astPrintf(node, " after %v", node.After)
  2188  		}
  2189  	} else {
  2190  		for i, col := range node.Columns {
  2191  			if i == 0 {
  2192  				buf.astPrintf(node, "add column (%v", col)
  2193  			} else {
  2194  				buf.astPrintf(node, ", %v", col)
  2195  			}
  2196  		}
  2197  		buf.WriteByte(')')
  2198  	}
  2199  }
  2200  
  2201  // Format formats the node.
  2202  func (node AlgorithmValue) Format(buf *TrackedBuffer) {
  2203  	buf.astPrintf(node, "algorithm = %s", string(node))
  2204  }
  2205  
  2206  // Format formats the node
  2207  func (node *AlterColumn) Format(buf *TrackedBuffer) {
  2208  	buf.astPrintf(node, "alter column %v", node.Column)
  2209  	if node.DropDefault {
  2210  		buf.astPrintf(node, " drop default")
  2211  	} else if node.DefaultVal != nil {
  2212  		buf.astPrintf(node, " set default %v", node.DefaultVal)
  2213  	}
  2214  	if node.Invisible != nil {
  2215  		if *node.Invisible {
  2216  			buf.astPrintf(node, " set invisible")
  2217  		} else {
  2218  			buf.astPrintf(node, " set visible")
  2219  		}
  2220  	}
  2221  }
  2222  
  2223  // Format formats the node
  2224  func (node *AlterIndex) Format(buf *TrackedBuffer) {
  2225  	buf.astPrintf(node, "alter index %v", node.Name)
  2226  	if node.Invisible {
  2227  		buf.astPrintf(node, " invisible")
  2228  	} else {
  2229  		buf.astPrintf(node, " visible")
  2230  	}
  2231  }
  2232  
  2233  // Format formats the node
  2234  func (node *ChangeColumn) Format(buf *TrackedBuffer) {
  2235  	buf.astPrintf(node, "change column %v %v", node.OldColumn, node.NewColDefinition)
  2236  	if node.First {
  2237  		buf.astPrintf(node, " first")
  2238  	}
  2239  	if node.After != nil {
  2240  		buf.astPrintf(node, " after %v", node.After)
  2241  	}
  2242  }
  2243  
  2244  // Format formats the node
  2245  func (node *ModifyColumn) Format(buf *TrackedBuffer) {
  2246  	buf.astPrintf(node, "modify column %v", node.NewColDefinition)
  2247  	if node.First {
  2248  		buf.astPrintf(node, " first")
  2249  	}
  2250  	if node.After != nil {
  2251  		buf.astPrintf(node, " after %v", node.After)
  2252  	}
  2253  }
  2254  
  2255  // Format formats the node
  2256  func (node *RenameColumn) Format(buf *TrackedBuffer) {
  2257  	buf.astPrintf(node, "rename column %v to %v", node.OldName, node.NewName)
  2258  }
  2259  
  2260  // Format formats the node
  2261  func (node *AlterCharset) Format(buf *TrackedBuffer) {
  2262  	buf.astPrintf(node, "convert to character set %#s", node.CharacterSet)
  2263  	if node.Collate != "" {
  2264  		buf.astPrintf(node, " collate %#s", node.Collate)
  2265  	}
  2266  }
  2267  
  2268  // Format formats the node
  2269  func (node *KeyState) Format(buf *TrackedBuffer) {
  2270  	if node.Enable {
  2271  		buf.literal("enable keys")
  2272  	} else {
  2273  		buf.literal("disable keys")
  2274  	}
  2275  
  2276  }
  2277  
  2278  // Format formats the node
  2279  func (node *TablespaceOperation) Format(buf *TrackedBuffer) {
  2280  	if node.Import {
  2281  		buf.literal("import tablespace")
  2282  	} else {
  2283  		buf.literal("discard tablespace")
  2284  	}
  2285  }
  2286  
  2287  // Format formats the node
  2288  func (node *DropColumn) Format(buf *TrackedBuffer) {
  2289  	buf.astPrintf(node, "drop column %v", node.Name)
  2290  }
  2291  
  2292  // Format formats the node
  2293  func (node *DropKey) Format(buf *TrackedBuffer) {
  2294  	buf.astPrintf(node, "drop %s", node.Type.ToString())
  2295  	if !node.Name.IsEmpty() {
  2296  		buf.astPrintf(node, " %v", node.Name)
  2297  	}
  2298  }
  2299  
  2300  // Format formats the node
  2301  func (node *Force) Format(buf *TrackedBuffer) {
  2302  	buf.literal("force")
  2303  }
  2304  
  2305  // Format formats the node
  2306  func (node *LockOption) Format(buf *TrackedBuffer) {
  2307  	buf.astPrintf(node, "lock %s", node.Type.ToString())
  2308  }
  2309  
  2310  // Format formats the node
  2311  func (node *OrderByOption) Format(buf *TrackedBuffer) {
  2312  	buf.astPrintf(node, "order by ")
  2313  	prefix := ""
  2314  	for _, n := range node.Cols {
  2315  		buf.astPrintf(node, "%s%v", prefix, n)
  2316  		prefix = ", "
  2317  	}
  2318  }
  2319  
  2320  // Format formats the node
  2321  func (node *RenameTableName) Format(buf *TrackedBuffer) {
  2322  	buf.astPrintf(node, "rename %v", node.Table)
  2323  }
  2324  
  2325  // Format formats the node
  2326  func (node *RenameIndex) Format(buf *TrackedBuffer) {
  2327  	buf.astPrintf(node, "rename index %v to %v", node.OldName, node.NewName)
  2328  }
  2329  
  2330  // Format formats the node
  2331  func (node *Validation) Format(buf *TrackedBuffer) {
  2332  	if node.With {
  2333  		buf.literal("with validation")
  2334  	} else {
  2335  		buf.literal("without validation")
  2336  	}
  2337  }
  2338  
  2339  // Format formats the node
  2340  func (node TableOptions) Format(buf *TrackedBuffer) {
  2341  	for i, option := range node {
  2342  		if i != 0 {
  2343  			buf.WriteByte(' ')
  2344  		}
  2345  		buf.astPrintf(node, "%s", option.Name)
  2346  		switch {
  2347  		case option.String != "":
  2348  			if option.CaseSensitive {
  2349  				buf.astPrintf(node, " %#s", option.String)
  2350  			} else {
  2351  				buf.astPrintf(node, " %s", option.String)
  2352  			}
  2353  		case option.Value != nil:
  2354  			buf.astPrintf(node, " %v", option.Value)
  2355  		default:
  2356  			buf.astPrintf(node, " (%v)", option.Tables)
  2357  		}
  2358  	}
  2359  }
  2360  
  2361  // Format formats the node
  2362  func (node *TruncateTable) Format(buf *TrackedBuffer) {
  2363  	buf.astPrintf(node, "truncate table %v", node.Table)
  2364  }
  2365  
  2366  // Format formats the node.
  2367  func (node *RenameTable) Format(buf *TrackedBuffer) {
  2368  	buf.astPrintf(node, "rename table")
  2369  	prefix := " "
  2370  	for _, pair := range node.TablePairs {
  2371  		buf.astPrintf(node, "%s%v to %v", prefix, pair.FromTable, pair.ToTable)
  2372  		prefix = ", "
  2373  	}
  2374  }
  2375  
  2376  // Format formats the node.
  2377  // If an extracted subquery is still in the AST when we print it,
  2378  // it will be formatted as if the subquery has been extracted, and instead
  2379  // show up like argument comparisons
  2380  func (node *ExtractedSubquery) Format(buf *TrackedBuffer) {
  2381  	node.alternative.Format(buf)
  2382  }
  2383  
  2384  func (node *JSONTableExpr) Format(buf *TrackedBuffer) {
  2385  	buf.astPrintf(node, "json_table(%v, %v columns(\n", node.Expr, node.Filter)
  2386  	sz := len(node.Columns)
  2387  
  2388  	for i := 0; i < sz-1; i++ {
  2389  		buf.astPrintf(node, "\t%v,\n", node.Columns[i])
  2390  	}
  2391  	buf.astPrintf(node, "\t%v\n", node.Columns[sz-1])
  2392  	buf.astPrintf(node, "\t)\n) as %v", node.Alias)
  2393  }
  2394  
  2395  func (node *JtColumnDefinition) Format(buf *TrackedBuffer) {
  2396  	if node.JtOrdinal != nil {
  2397  		buf.astPrintf(node, "%v for ordinality", node.JtOrdinal.Name)
  2398  	} else if node.JtNestedPath != nil {
  2399  		buf.astPrintf(node, "nested path %v columns(\n", node.JtNestedPath.Path)
  2400  		sz := len(node.JtNestedPath.Columns)
  2401  
  2402  		for i := 0; i < sz-1; i++ {
  2403  			buf.astPrintf(node, "\t%v,\n", node.JtNestedPath.Columns[i])
  2404  		}
  2405  		buf.astPrintf(node, "\t%v\n)", node.JtNestedPath.Columns[sz-1])
  2406  	} else if node.JtPath != nil {
  2407  		buf.astPrintf(node, "%v %v ", node.JtPath.Name, node.JtPath.Type)
  2408  		if node.JtPath.JtColExists {
  2409  			buf.astPrintf(node, "exists ")
  2410  		}
  2411  		buf.astPrintf(node, "path %v ", node.JtPath.Path)
  2412  
  2413  		if node.JtPath.EmptyOnResponse != nil {
  2414  			buf.astPrintf(node, "%v on empty ", node.JtPath.EmptyOnResponse)
  2415  		}
  2416  
  2417  		if node.JtPath.ErrorOnResponse != nil {
  2418  			buf.astPrintf(node, "%v on error ", node.JtPath.ErrorOnResponse)
  2419  		}
  2420  	}
  2421  }
  2422  
  2423  func (node *JtOnResponse) Format(buf *TrackedBuffer) {
  2424  	switch node.ResponseType {
  2425  	case ErrorJSONType:
  2426  		buf.astPrintf(node, "error")
  2427  	case NullJSONType:
  2428  		buf.astPrintf(node, "null")
  2429  	case DefaultJSONType:
  2430  		buf.astPrintf(node, "default %v", node.Expr)
  2431  	}
  2432  }
  2433  
  2434  // Format formats the node.
  2435  func (node *Offset) Format(buf *TrackedBuffer) {
  2436  	buf.astPrintf(node, ":%d", node.V)
  2437  }
  2438  
  2439  // Format formats the node.
  2440  func (node *JSONSchemaValidFuncExpr) Format(buf *TrackedBuffer) {
  2441  	buf.astPrintf(node, "json_schema_valid(%v, %v)", node.Schema, node.Document)
  2442  }
  2443  
  2444  // Format formats the node.
  2445  func (node *JSONSchemaValidationReportFuncExpr) Format(buf *TrackedBuffer) {
  2446  	buf.astPrintf(node, "json_schema_validation_report(%v, %v)", node.Schema, node.Document)
  2447  }
  2448  
  2449  // Format formats the node.
  2450  func (node *JSONArrayExpr) Format(buf *TrackedBuffer) {
  2451  	// buf.astPrintf(node,"%s(,"node.Name.Lowered())
  2452  	buf.literal("json_array(")
  2453  	if len(node.Params) > 0 {
  2454  		var prefix string
  2455  		for _, n := range node.Params {
  2456  			buf.astPrintf(node, "%s%v", prefix, n)
  2457  			prefix = ", "
  2458  		}
  2459  	}
  2460  	buf.WriteByte(')')
  2461  }
  2462  
  2463  // Format formats the node.
  2464  func (node *JSONObjectExpr) Format(buf *TrackedBuffer) {
  2465  	// buf.astPrintf(node,"%s(,"node.Name.Lowered())
  2466  	buf.literal("json_object(")
  2467  	if len(node.Params) > 0 {
  2468  		for i, p := range node.Params {
  2469  			if i != 0 {
  2470  				buf.astPrintf(node, ", ")
  2471  
  2472  			}
  2473  			buf.astPrintf(node, "%v", p)
  2474  		}
  2475  	}
  2476  	buf.WriteByte(')')
  2477  }
  2478  
  2479  // Format formats the node.
  2480  func (node *JSONObjectParam) Format(buf *TrackedBuffer) {
  2481  	buf.astPrintf(node, "%v, %v", node.Key, node.Value)
  2482  }
  2483  
  2484  // Format formats the node.
  2485  func (node *JSONQuoteExpr) Format(buf *TrackedBuffer) {
  2486  	buf.astPrintf(node, "json_quote(%v)", node.StringArg)
  2487  }
  2488  
  2489  // Format formats the node
  2490  func (node *JSONContainsExpr) Format(buf *TrackedBuffer) {
  2491  	buf.astPrintf(node, "json_contains(%v, %v", node.Target, node.Candidate)
  2492  	if len(node.PathList) > 0 {
  2493  		buf.literal(", ")
  2494  	}
  2495  	var prefix string
  2496  	for _, n := range node.PathList {
  2497  		buf.astPrintf(node, "%s%v", prefix, n)
  2498  		prefix = ", "
  2499  	}
  2500  	buf.WriteByte(')')
  2501  }
  2502  
  2503  // Format formats the node
  2504  func (node *JSONContainsPathExpr) Format(buf *TrackedBuffer) {
  2505  	buf.astPrintf(node, "json_contains_path(%v, %v, ", node.JSONDoc, node.OneOrAll)
  2506  	var prefix string
  2507  	for _, n := range node.PathList {
  2508  		buf.astPrintf(node, "%s%v", prefix, n)
  2509  		prefix = ", "
  2510  	}
  2511  	buf.WriteByte(')')
  2512  }
  2513  
  2514  // Format formats the node
  2515  func (node *JSONExtractExpr) Format(buf *TrackedBuffer) {
  2516  	buf.astPrintf(node, "json_extract(%v, ", node.JSONDoc)
  2517  	var prefix string
  2518  	for _, n := range node.PathList {
  2519  		buf.astPrintf(node, "%s%v", prefix, n)
  2520  		prefix = ", "
  2521  	}
  2522  	buf.WriteByte(')')
  2523  }
  2524  
  2525  // Format formats the node
  2526  func (node *JSONKeysExpr) Format(buf *TrackedBuffer) {
  2527  	buf.astPrintf(node, "json_keys(%v", node.JSONDoc)
  2528  	if node.Path != nil {
  2529  		buf.astPrintf(node, ", %v)", node.Path)
  2530  		return
  2531  	}
  2532  	buf.WriteByte(')')
  2533  }
  2534  
  2535  // Format formats the node
  2536  func (node *JSONOverlapsExpr) Format(buf *TrackedBuffer) {
  2537  	buf.astPrintf(node, "json_overlaps(%v, %v)", node.JSONDoc1, node.JSONDoc2)
  2538  }
  2539  
  2540  // Format formats the node
  2541  func (node *JSONSearchExpr) Format(buf *TrackedBuffer) {
  2542  	buf.astPrintf(node, "json_search(%v, %v, %v", node.JSONDoc, node.OneOrAll, node.SearchStr)
  2543  	if node.EscapeChar != nil {
  2544  		buf.astPrintf(node, ", %v", node.EscapeChar)
  2545  	}
  2546  	if len(node.PathList) > 0 {
  2547  		buf.literal(", ")
  2548  	}
  2549  	var prefix string
  2550  	for _, n := range node.PathList {
  2551  		buf.astPrintf(node, "%s%v", prefix, n)
  2552  		prefix = ", "
  2553  	}
  2554  	buf.WriteByte(')')
  2555  }
  2556  
  2557  // Format formats the node
  2558  func (node *JSONValueExpr) Format(buf *TrackedBuffer) {
  2559  	buf.astPrintf(node, "json_value(%v, %v", node.JSONDoc, node.Path)
  2560  
  2561  	if node.ReturningType != nil {
  2562  		buf.astPrintf(node, " returning %v", node.ReturningType)
  2563  	}
  2564  
  2565  	if node.EmptyOnResponse != nil {
  2566  		buf.astPrintf(node, " %v on empty", node.EmptyOnResponse)
  2567  	}
  2568  
  2569  	if node.ErrorOnResponse != nil {
  2570  		buf.astPrintf(node, " %v on error", node.ErrorOnResponse)
  2571  	}
  2572  
  2573  	buf.WriteByte(')')
  2574  }
  2575  
  2576  // Format formats the node
  2577  func (node *MemberOfExpr) Format(buf *TrackedBuffer) {
  2578  	buf.astPrintf(node, "%v member of (%v)", node.Value, node.JSONArr)
  2579  }
  2580  
  2581  // Format formats the node
  2582  func (node *JSONAttributesExpr) Format(buf *TrackedBuffer) {
  2583  	buf.astPrintf(node, "%s(", node.Type.ToString())
  2584  	buf.astPrintf(node, "%v", node.JSONDoc)
  2585  	if node.Path != nil {
  2586  		buf.astPrintf(node, ", %v", node.Path)
  2587  	}
  2588  	buf.WriteString(")")
  2589  }
  2590  
  2591  // Format formats the node.
  2592  func (node *JSONValueModifierExpr) Format(buf *TrackedBuffer) {
  2593  	buf.astPrintf(node, "%s(%v, ", node.Type.ToString(), node.JSONDoc)
  2594  	var prefix string
  2595  	for _, n := range node.Params {
  2596  		buf.astPrintf(node, "%s%v", prefix, n)
  2597  		prefix = ", "
  2598  	}
  2599  	buf.WriteString(")")
  2600  }
  2601  
  2602  // Format formats the node.
  2603  func (node *JSONValueMergeExpr) Format(buf *TrackedBuffer) {
  2604  	buf.astPrintf(node, "%s(%v, ", node.Type.ToString(), node.JSONDoc)
  2605  	var prefix string
  2606  	for _, n := range node.JSONDocList {
  2607  		buf.astPrintf(node, "%s%v", prefix, n)
  2608  		prefix = ", "
  2609  	}
  2610  	buf.WriteString(")")
  2611  }
  2612  
  2613  // Format formats the node.
  2614  func (node *JSONRemoveExpr) Format(buf *TrackedBuffer) {
  2615  	buf.astPrintf(node, "json_remove(%v, ", node.JSONDoc)
  2616  	var prefix string
  2617  	for _, n := range node.PathList {
  2618  		buf.astPrintf(node, "%s%v", prefix, n)
  2619  		prefix = ", "
  2620  	}
  2621  	buf.WriteString(")")
  2622  }
  2623  
  2624  // Format formats the node.
  2625  func (node *JSONUnquoteExpr) Format(buf *TrackedBuffer) {
  2626  	buf.astPrintf(node, "json_unquote(%v", node.JSONValue)
  2627  	buf.WriteString(")")
  2628  }
  2629  
  2630  func (node *Count) Format(buf *TrackedBuffer) {
  2631  	buf.astPrintf(node, "%s(", node.AggrName())
  2632  	if node.Distinct {
  2633  		buf.literal(DistinctStr)
  2634  	}
  2635  	buf.astPrintf(node, "%v)", node.Args)
  2636  }
  2637  
  2638  func (node *CountStar) Format(buf *TrackedBuffer) {
  2639  	buf.astPrintf(node, "%s(", node.AggrName())
  2640  	buf.WriteString("*)")
  2641  }
  2642  
  2643  func (node *Avg) Format(buf *TrackedBuffer) {
  2644  	buf.astPrintf(node, "%s(", node.AggrName())
  2645  	if node.Distinct {
  2646  		buf.literal(DistinctStr)
  2647  	}
  2648  	buf.astPrintf(node, "%v)", node.Arg)
  2649  }
  2650  
  2651  func (node *Max) Format(buf *TrackedBuffer) {
  2652  	buf.astPrintf(node, "%s(", node.AggrName())
  2653  	if node.Distinct {
  2654  		buf.literal(DistinctStr)
  2655  	}
  2656  	buf.astPrintf(node, "%v)", node.Arg)
  2657  }
  2658  
  2659  func (node *Min) Format(buf *TrackedBuffer) {
  2660  	buf.astPrintf(node, "%s(", node.AggrName())
  2661  	if node.Distinct {
  2662  		buf.literal(DistinctStr)
  2663  	}
  2664  	buf.astPrintf(node, "%v)", node.Arg)
  2665  }
  2666  
  2667  func (node *Sum) Format(buf *TrackedBuffer) {
  2668  	buf.astPrintf(node, "%s(", node.AggrName())
  2669  	if node.Distinct {
  2670  		buf.literal(DistinctStr)
  2671  	}
  2672  	buf.astPrintf(node, "%v)", node.Arg)
  2673  }
  2674  
  2675  func (node *BitAnd) Format(buf *TrackedBuffer) {
  2676  	buf.astPrintf(node, "%s(", node.AggrName())
  2677  	buf.astPrintf(node, "%v)", node.Arg)
  2678  }
  2679  
  2680  func (node *BitOr) Format(buf *TrackedBuffer) {
  2681  	buf.astPrintf(node, "%s(", node.AggrName())
  2682  	buf.astPrintf(node, "%v)", node.Arg)
  2683  }
  2684  
  2685  func (node *BitXor) Format(buf *TrackedBuffer) {
  2686  	buf.astPrintf(node, "%s(", node.AggrName())
  2687  	buf.astPrintf(node, "%v)", node.Arg)
  2688  }
  2689  
  2690  func (node *Std) Format(buf *TrackedBuffer) {
  2691  	buf.astPrintf(node, "%s(", node.AggrName())
  2692  	buf.astPrintf(node, "%v)", node.Arg)
  2693  }
  2694  
  2695  func (node *StdDev) Format(buf *TrackedBuffer) {
  2696  	buf.astPrintf(node, "%s(", node.AggrName())
  2697  	buf.astPrintf(node, "%v)", node.Arg)
  2698  }
  2699  
  2700  func (node *StdPop) Format(buf *TrackedBuffer) {
  2701  	buf.astPrintf(node, "%s(", node.AggrName())
  2702  	buf.astPrintf(node, "%v)", node.Arg)
  2703  }
  2704  
  2705  func (node *StdSamp) Format(buf *TrackedBuffer) {
  2706  	buf.astPrintf(node, "%s(", node.AggrName())
  2707  	buf.astPrintf(node, "%v)", node.Arg)
  2708  }
  2709  
  2710  func (node *VarPop) Format(buf *TrackedBuffer) {
  2711  	buf.astPrintf(node, "%s(", node.AggrName())
  2712  	buf.astPrintf(node, "%v)", node.Arg)
  2713  }
  2714  
  2715  func (node *VarSamp) Format(buf *TrackedBuffer) {
  2716  	buf.astPrintf(node, "%s(", node.AggrName())
  2717  	buf.astPrintf(node, "%v)", node.Arg)
  2718  }
  2719  
  2720  func (node *Variance) Format(buf *TrackedBuffer) {
  2721  	buf.astPrintf(node, "%s(", node.AggrName())
  2722  	buf.astPrintf(node, "%v)", node.Arg)
  2723  }
  2724  
  2725  // Format formats the node.
  2726  func (node *LockingFunc) Format(buf *TrackedBuffer) {
  2727  	buf.WriteString(node.Type.ToString() + "(")
  2728  	if node.Type != ReleaseAllLocks {
  2729  		buf.astPrintf(node, "%v", node.Name)
  2730  	}
  2731  	if node.Type == GetLock {
  2732  		buf.astPrintf(node, ", %v", node.Timeout)
  2733  	}
  2734  	buf.WriteString(")")
  2735  }
  2736  
  2737  // Format formats the node.
  2738  func (node *Variable) Format(buf *TrackedBuffer) {
  2739  	switch node.Scope {
  2740  	case VariableScope:
  2741  		buf.literal("@")
  2742  	case SessionScope:
  2743  		if node.Name.EqualString(TransactionIsolationStr) || node.Name.EqualString(TransactionReadOnlyStr) {
  2744  			// @@ without session have `next transaction` scope for these system variables.
  2745  			// so if they are in session scope it has to be printed explicitly.
  2746  			buf.astPrintf(node, "@@%s.", node.Scope.ToString())
  2747  			break
  2748  		}
  2749  		buf.literal("@@")
  2750  	case GlobalScope, PersistSysScope, PersistOnlySysScope:
  2751  		buf.astPrintf(node, "@@%s.", node.Scope.ToString())
  2752  	case NextTxScope:
  2753  		buf.literal("@@")
  2754  	}
  2755  	buf.astPrintf(node, "%v", node.Name)
  2756  }
  2757  
  2758  // Format formats the node.
  2759  func (node *PointExpr) Format(buf *TrackedBuffer) {
  2760  	buf.astPrintf(node, "point(%v, %v)", node.XCordinate, node.YCordinate)
  2761  }
  2762  
  2763  // Format formats the node.
  2764  func (node *LineStringExpr) Format(buf *TrackedBuffer) {
  2765  	buf.astPrintf(node, "linestring(%v)", node.PointParams)
  2766  }