github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/run_control.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  // ControlJobs represents a PAUSE/RESUME/CANCEL JOBS statement.
    14  type ControlJobs struct {
    15  	Jobs    *Select
    16  	Command JobCommand
    17  	Reason  Expr
    18  }
    19  
    20  // JobCommand determines which type of action to effect on the selected job(s).
    21  type JobCommand int
    22  
    23  // JobCommand values
    24  const (
    25  	PauseJob JobCommand = iota
    26  	CancelJob
    27  	ResumeJob
    28  )
    29  
    30  // JobCommandToStatement translates a job command integer to a statement prefix.
    31  var JobCommandToStatement = map[JobCommand]string{
    32  	PauseJob:  "PAUSE",
    33  	CancelJob: "CANCEL",
    34  	ResumeJob: "RESUME",
    35  }
    36  
    37  // Format implements the NodeFormatter interface.
    38  func (n *ControlJobs) Format(ctx *FmtCtx) {
    39  	ctx.WriteString(JobCommandToStatement[n.Command])
    40  	ctx.WriteString(" JOBS ")
    41  	ctx.FormatNode(n.Jobs)
    42  	if n.Reason != nil {
    43  		ctx.WriteString(" WITH REASON = ")
    44  		ctx.FormatNode(n.Reason)
    45  	}
    46  }
    47  
    48  // CancelQueries represents a CANCEL QUERIES statement.
    49  type CancelQueries struct {
    50  	Queries  *Select
    51  	IfExists bool
    52  }
    53  
    54  // Format implements the NodeFormatter interface.
    55  func (node *CancelQueries) Format(ctx *FmtCtx) {
    56  	ctx.WriteString("CANCEL QUERIES ")
    57  	if node.IfExists {
    58  		ctx.WriteString("IF EXISTS ")
    59  	}
    60  	ctx.FormatNode(node.Queries)
    61  }
    62  
    63  // CancelSessions represents a CANCEL SESSIONS statement.
    64  type CancelSessions struct {
    65  	Sessions *Select
    66  	IfExists bool
    67  }
    68  
    69  // Format implements the NodeFormatter interface.
    70  func (node *CancelSessions) Format(ctx *FmtCtx) {
    71  	ctx.WriteString("CANCEL SESSIONS ")
    72  	if node.IfExists {
    73  		ctx.WriteString("IF EXISTS ")
    74  	}
    75  	ctx.FormatNode(node.Sessions)
    76  }
    77  
    78  // ScheduleCommand determines which type of action to effect on the selected job(s).
    79  type ScheduleCommand int
    80  
    81  // ScheduleCommand values
    82  const (
    83  	PauseSchedule ScheduleCommand = iota
    84  	ResumeSchedule
    85  	DropSchedule
    86  )
    87  
    88  func (c ScheduleCommand) String() string {
    89  	switch c {
    90  	case PauseSchedule:
    91  		return "PAUSE"
    92  	case ResumeSchedule:
    93  		return "RESUME"
    94  	case DropSchedule:
    95  		return "DROP"
    96  	default:
    97  		panic("unhandled schedule command")
    98  	}
    99  }
   100  
   101  // ControlSchedules represents PAUSE/RESUME SCHEDULE statement.
   102  type ControlSchedules struct {
   103  	Schedules *Select
   104  	Command   ScheduleCommand
   105  }
   106  
   107  var _ Statement = &ControlSchedules{}
   108  
   109  // Format implements the NodeFormatter interface.
   110  func (n *ControlSchedules) Format(ctx *FmtCtx) {
   111  	ctx.WriteString(n.Command.String())
   112  	ctx.WriteString(" SCHEDULES ")
   113  	ctx.FormatNode(n.Schedules)
   114  }
   115  
   116  // ControlJobsForSchedules represents PAUSE/RESUME/CANCEL clause
   117  // which applies job command to the jobs matching specified schedule(s).
   118  type ControlJobsForSchedules struct {
   119  	Schedules *Select
   120  	Command   JobCommand
   121  }
   122  
   123  // ControlJobsOfType represents PAUSE/RESUME/CANCEL clause which
   124  // applies the job command to the job matching a specified type
   125  type ControlJobsOfType struct {
   126  	Type    string
   127  	Command JobCommand
   128  }
   129  
   130  // Format implements the NodeFormatter interface.
   131  func (n *ControlJobsOfType) Format(ctx *FmtCtx) {
   132  	ctx.WriteString(JobCommandToStatement[n.Command])
   133  	ctx.WriteString(" ALL ")
   134  	ctx.WriteString(n.Type)
   135  	ctx.WriteString(" JOBS")
   136  }
   137  
   138  // Format implements NodeFormatter interface.
   139  func (n *ControlJobsForSchedules) Format(ctx *FmtCtx) {
   140  	ctx.WriteString(JobCommandToStatement[n.Command])
   141  	ctx.WriteString(" JOBS FOR SCHEDULES ")
   142  	ctx.FormatNode(n.Schedules)
   143  }
   144  
   145  var _ Statement = &ControlJobsForSchedules{}
   146  var _ Statement = &ControlJobsOfType{}