github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/schedule.go (about) 1 // Copyright 2020 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 // FullBackupClause describes the frequency of full backups. 14 type FullBackupClause struct { 15 AlwaysFull bool 16 Recurrence Expr 17 } 18 19 // LabelSpec describes the labeling specification for an object. 20 type LabelSpec struct { 21 IfNotExists bool 22 Label Expr 23 } 24 25 // Format implements the NodeFormatter interface. 26 func (l *LabelSpec) Format(ctx *FmtCtx) { 27 if l.IfNotExists { 28 ctx.WriteString(" IF NOT EXISTS") 29 } 30 if l.Label != nil { 31 ctx.WriteString(" ") 32 ctx.FormatNode(l.Label) 33 } 34 } 35 36 var _ NodeFormatter = &LabelSpec{} 37 38 // ScheduledBackup represents scheduled backup job. 39 type ScheduledBackup struct { 40 ScheduleLabelSpec LabelSpec 41 Recurrence Expr 42 FullBackup *FullBackupClause /* nil implies choose default */ 43 Targets *BackupTargetList /* nil implies tree.AllDescriptors coverage */ 44 To StringOrPlaceholderOptList 45 BackupOptions BackupOptions 46 ScheduleOptions KVOptions 47 } 48 49 var _ Statement = &ScheduledBackup{} 50 51 // Format implements the NodeFormatter interface. 52 func (node *ScheduledBackup) Format(ctx *FmtCtx) { 53 ctx.WriteString("CREATE SCHEDULE") 54 55 ctx.FormatNode(&node.ScheduleLabelSpec) 56 ctx.WriteString(" FOR BACKUP") 57 if node.Targets != nil { 58 ctx.WriteString(" ") 59 ctx.FormatNode(node.Targets) 60 } 61 62 ctx.WriteString(" INTO ") 63 ctx.FormatNode(&node.To) 64 65 if !node.BackupOptions.IsDefault() { 66 ctx.WriteString(" WITH ") 67 ctx.FormatNode(&node.BackupOptions) 68 } 69 70 ctx.WriteString(" RECURRING ") 71 if node.Recurrence == nil { 72 ctx.WriteString("NEVER") 73 } else { 74 ctx.FormatNode(node.Recurrence) 75 } 76 77 if node.FullBackup != nil { 78 79 if node.FullBackup.Recurrence != nil { 80 ctx.WriteString(" FULL BACKUP ") 81 ctx.FormatNode(node.FullBackup.Recurrence) 82 } else if node.FullBackup.AlwaysFull { 83 ctx.WriteString(" FULL BACKUP ALWAYS") 84 } 85 } 86 87 if node.ScheduleOptions != nil { 88 ctx.WriteString(" WITH SCHEDULE OPTIONS ") 89 ctx.FormatNode(&node.ScheduleOptions) 90 } 91 } 92 93 // Coverage return the coverage (all vs requested). 94 func (node ScheduledBackup) Coverage() DescriptorCoverage { 95 if node.Targets == nil { 96 return AllDescriptors 97 } 98 return RequestedDescriptors 99 } 100 101 // ScheduledChangefeed represents scheduled changefeed job. 102 type ScheduledChangefeed struct { 103 *CreateChangefeed 104 ScheduleLabelSpec LabelSpec 105 Recurrence Expr 106 ScheduleOptions KVOptions 107 } 108 109 // Format implements the NodeFormatter interface. 110 func (node *ScheduledChangefeed) Format(ctx *FmtCtx) { 111 ctx.WriteString("CREATE SCHEDULE") 112 113 if node.ScheduleLabelSpec.IfNotExists { 114 ctx.WriteString(" IF NOT EXISTS") 115 } 116 117 if node.ScheduleLabelSpec.Label != nil { 118 ctx.WriteString(" ") 119 ctx.FormatNode(node.ScheduleLabelSpec.Label) 120 } 121 122 ctx.WriteString(" FOR CHANGEFEED") 123 124 if node.Select == nil { 125 ctx.WriteString(" ") 126 ctx.FormatNode(&node.Targets) 127 } 128 129 ctx.WriteString(" INTO ") 130 ctx.FormatNode(node.SinkURI) 131 132 if node.Options != nil { 133 ctx.WriteString(" WITH OPTIONS (") 134 ctx.FormatNode(&node.Options) 135 ctx.WriteString(" )") 136 } 137 138 if node.Select != nil { 139 ctx.WriteString(" AS ") 140 ctx.FormatNode(node.Select) 141 } 142 143 ctx.WriteString(" RECURRING ") 144 ctx.FormatNode(node.Recurrence) 145 146 if node.ScheduleOptions != nil { 147 ctx.WriteString(" WITH SCHEDULE OPTIONS ") 148 ctx.FormatNode(&node.ScheduleOptions) 149 } 150 }