github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/sequence.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tree 16 17 import ( 18 "fmt" 19 20 "github.com/matrixorigin/matrixone/pkg/common/reuse" 21 ) 22 23 func init() { 24 reuse.CreatePool[AlterSequence]( 25 func() *AlterSequence { return &AlterSequence{} }, 26 func(a *AlterSequence) { a.reset() }, 27 reuse.DefaultOptions[AlterSequence](), //. 28 ) //WithEnableChecker() 29 30 reuse.CreatePool[CreateSequence]( 31 func() *CreateSequence { return &CreateSequence{} }, 32 func(c *CreateSequence) { c.reset() }, 33 reuse.DefaultOptions[CreateSequence](), //. 34 ) //WithEnableChecker() 35 36 reuse.CreatePool[DropSequence]( 37 func() *DropSequence { return &DropSequence{} }, 38 func(a *DropSequence) { a.reset() }, 39 reuse.DefaultOptions[DropSequence](), //. 40 ) //WithEnableChecker() 41 } 42 43 type CreateSequence struct { 44 statementImpl 45 46 Name *TableName 47 Type ResolvableTypeReference 48 IfNotExists bool 49 IncrementBy *IncrementByOption 50 MinValue *MinValueOption 51 MaxValue *MaxValueOption 52 StartWith *StartWithOption 53 Cycle bool 54 } 55 56 func NewCreateSequence(name *TableName, typ ResolvableTypeReference, ifnotexists bool, incrementby *IncrementByOption, minvalue *MinValueOption, maxvalue *MaxValueOption, startwith *StartWithOption, cycle bool) *CreateSequence { 57 create := reuse.Alloc[CreateSequence](nil) 58 create.Name = name 59 create.Type = typ 60 create.IfNotExists = ifnotexists 61 create.IncrementBy = incrementby 62 create.MinValue = minvalue 63 create.MaxValue = maxvalue 64 create.StartWith = startwith 65 create.Cycle = cycle 66 return create 67 } 68 69 func (node *CreateSequence) Free() { 70 reuse.Free[CreateSequence](node, nil) 71 } 72 73 func (node *CreateSequence) Format(ctx *FmtCtx) { 74 ctx.WriteString("create sequence ") 75 76 if node.IfNotExists { 77 ctx.WriteString("if not exists ") 78 } 79 80 node.Name.Format(ctx) 81 82 ctx.WriteString(" as ") 83 node.Type.(*T).InternalType.Format(ctx) 84 ctx.WriteString(" ") 85 if node.IncrementBy != nil { 86 node.IncrementBy.Format(ctx) 87 } 88 if node.MinValue != nil { 89 node.MinValue.Format(ctx) 90 } 91 if node.MaxValue != nil { 92 node.MaxValue.Format(ctx) 93 } 94 if node.StartWith != nil { 95 node.StartWith.Format(ctx) 96 } 97 if node.Cycle { 98 ctx.WriteString("cycle") 99 } else { 100 ctx.WriteString("no cycle") 101 } 102 } 103 104 func (node *CreateSequence) reset() { 105 // if node.Name != nil { 106 // node.Name.Free() 107 // } 108 // if node.IncrementBy != nil { 109 // node.IncrementBy.Free() 110 // } 111 // if node.MinValue != nil { 112 // node.MinValue.Free() 113 // } 114 // if node.MaxValue != nil { 115 // node.MaxValue.Free() 116 // } 117 // if node.StartWith != nil { 118 // node.StartWith.Free() 119 // } 120 *node = CreateSequence{} 121 } 122 123 func (node CreateSequence) TypeName() string { return "tree.CreateSequence" } 124 125 func (node *CreateSequence) GetStatementType() string { return "Create Sequence" } 126 func (node *CreateSequence) GetQueryType() string { return QueryTypeDDL } 127 128 type IncrementByOption struct { 129 Minus bool 130 Num any 131 } 132 133 func (node *IncrementByOption) Format(ctx *FmtCtx) { 134 ctx.WriteString("increment by ") 135 formatAny(node.Minus, node.Num, ctx) 136 } 137 138 type MinValueOption struct { 139 Minus bool 140 Num any 141 } 142 143 func (node *MinValueOption) Format(ctx *FmtCtx) { 144 ctx.WriteString("minvalue ") 145 formatAny(node.Minus, node.Num, ctx) 146 } 147 148 type MaxValueOption struct { 149 Minus bool 150 Num any 151 } 152 153 func (node *MaxValueOption) Format(ctx *FmtCtx) { 154 ctx.WriteString("maxvalue ") 155 formatAny(node.Minus, node.Num, ctx) 156 } 157 158 type StartWithOption struct { 159 Minus bool 160 Num any 161 } 162 163 func (node *StartWithOption) Format(ctx *FmtCtx) { 164 ctx.WriteString("start with ") 165 formatAny(node.Minus, node.Num, ctx) 166 } 167 168 type CycleOption struct { 169 Cycle bool 170 } 171 172 func (node *CycleOption) Format(ctx *FmtCtx) { 173 if node.Cycle { 174 ctx.WriteString("cycle") 175 } else { 176 ctx.WriteString("no cycle") 177 } 178 } 179 180 type TypeOption struct { 181 Type ResolvableTypeReference 182 } 183 184 func (node *TypeOption) Format(ctx *FmtCtx) { 185 ctx.WriteString(" as ") 186 node.Type.(*T).InternalType.Format(ctx) 187 } 188 189 func formatAny(minus bool, num any, ctx *FmtCtx) { 190 switch num := num.(type) { 191 case uint64: 192 ctx.WriteString(fmt.Sprintf("%v ", num)) 193 case int64: 194 var v int64 195 if minus { 196 v = -num 197 } else { 198 v = num 199 } 200 ctx.WriteString(fmt.Sprintf("%v ", v)) 201 } 202 } 203 204 type DropSequence struct { 205 statementImpl 206 IfExists bool 207 Names TableNames 208 } 209 210 func (node *DropSequence) Free() { reuse.Free[DropSequence](node, nil) } 211 212 func (node DropSequence) TypeName() string { return "tree.DropSequence" } 213 214 func (node *DropSequence) reset() { 215 *node = DropSequence{} 216 } 217 218 func NewDropSequence(ifexists bool, names TableNames) *DropSequence { 219 drop := reuse.Alloc[DropSequence](nil) 220 drop.IfExists = ifexists 221 drop.Names = names 222 return drop 223 } 224 225 func (node *DropSequence) Format(ctx *FmtCtx) { 226 ctx.WriteString("drop sequence") 227 if node.IfExists { 228 ctx.WriteString(" if exists") 229 } 230 ctx.WriteByte(' ') 231 node.Names.Format(ctx) 232 } 233 234 func (node *DropSequence) GetStatementType() string { return "Drop Sequence" } 235 func (node *DropSequence) GetQueryType() string { return QueryTypeDDL } 236 237 type AlterSequence struct { 238 statementImpl 239 240 Name *TableName 241 Type *TypeOption 242 IfExists bool 243 IncrementBy *IncrementByOption 244 MinValue *MinValueOption 245 MaxValue *MaxValueOption 246 StartWith *StartWithOption 247 Cycle *CycleOption 248 } 249 250 func NewAlterSequence(ifexists bool, name *TableName, typ *TypeOption, incrementby *IncrementByOption, minvalue *MinValueOption, maxvalue *MaxValueOption, startwith *StartWithOption, cycle *CycleOption) *AlterSequence { 251 alter := reuse.Alloc[AlterSequence](nil) 252 alter.IfExists = ifexists 253 alter.Name = name 254 alter.Type = typ 255 alter.IncrementBy = incrementby 256 alter.MinValue = minvalue 257 alter.MaxValue = maxvalue 258 alter.StartWith = startwith 259 alter.Cycle = cycle 260 return alter 261 } 262 263 func (node *AlterSequence) Free() { reuse.Free[AlterSequence](node, nil) } 264 265 func (node *AlterSequence) Format(ctx *FmtCtx) { 266 ctx.WriteString("alter sequence ") 267 268 if node.IfExists { 269 ctx.WriteString("if exists ") 270 } 271 272 node.Name.Format(ctx) 273 274 if node.Type != nil { 275 node.Type.Format(ctx) 276 } 277 ctx.WriteString(" ") 278 if node.IncrementBy != nil { 279 node.IncrementBy.Format(ctx) 280 } 281 if node.MinValue != nil { 282 node.MinValue.Format(ctx) 283 } 284 if node.MaxValue != nil { 285 node.MaxValue.Format(ctx) 286 } 287 if node.StartWith != nil { 288 node.StartWith.Format(ctx) 289 } 290 if node.Cycle != nil { 291 node.Cycle.Format(ctx) 292 } 293 } 294 295 func (node AlterSequence) TypeName() string { return "tree.AlterSequence" } 296 func (node *AlterSequence) GetStatementType() string { return "Alter Sequence" } 297 func (node *AlterSequence) GetQueryType() string { return QueryTypeDDL } 298 func (node *AlterSequence) reset() { 299 *node = AlterSequence{} 300 }