github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/iteration.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  type RepeatStmt struct {
    18  	statementImpl
    19  	Name Identifier
    20  	Body []Statement
    21  	Cond Expr
    22  }
    23  
    24  func (node *RepeatStmt) Format(ctx *FmtCtx) {
    25  	if node.Name != "" {
    26  		ctx.WriteString(string(node.Name))
    27  		ctx.WriteString(": ")
    28  	}
    29  	ctx.WriteString("repeat ")
    30  	for _, s := range node.Body {
    31  		if s != nil {
    32  			s.Format(ctx)
    33  			ctx.WriteString("; ")
    34  		}
    35  	}
    36  	ctx.WriteString("until ")
    37  	node.Cond.Format(ctx)
    38  	ctx.WriteString(" end repeat")
    39  	if node.Name != "" {
    40  		ctx.WriteString(string(node.Name))
    41  	}
    42  }
    43  
    44  func (node *RepeatStmt) GetStatementType() string { return "Repeat Statement" }
    45  func (node *RepeatStmt) GetQueryType() string     { return QueryTypeTCL }
    46  
    47  type WhileStmt struct {
    48  	statementImpl
    49  	Name Identifier
    50  	Cond Expr
    51  	Body []Statement
    52  }
    53  
    54  func (node *WhileStmt) Format(ctx *FmtCtx) {
    55  	if node.Name != "" {
    56  		ctx.WriteString(string(node.Name))
    57  		ctx.WriteString(": ")
    58  	}
    59  	ctx.WriteString("while ")
    60  	node.Cond.Format(ctx)
    61  	ctx.WriteString(" do ")
    62  	for _, s := range node.Body {
    63  		if s != nil {
    64  			s.Format(ctx)
    65  			ctx.WriteString("; ")
    66  		}
    67  	}
    68  	ctx.WriteString("end while")
    69  	if node.Name != "" {
    70  		ctx.WriteByte(' ')
    71  		ctx.WriteString(string(node.Name))
    72  	}
    73  }
    74  
    75  func (node *WhileStmt) GetStatementType() string { return "While Statement" }
    76  func (node *WhileStmt) GetQueryType() string     { return QueryTypeTCL }
    77  
    78  type LoopStmt struct {
    79  	statementImpl
    80  	Name Identifier
    81  	Body []Statement
    82  }
    83  
    84  func (node *LoopStmt) Format(ctx *FmtCtx) {
    85  	if node.Name != "" {
    86  		ctx.WriteString(string(node.Name))
    87  		ctx.WriteString(": ")
    88  	}
    89  	ctx.WriteString("loop ")
    90  	for _, s := range node.Body {
    91  		if s != nil {
    92  			s.Format(ctx)
    93  			ctx.WriteString("; ")
    94  		}
    95  	}
    96  	ctx.WriteString("end loop")
    97  	if node.Name != "" {
    98  		ctx.WriteByte(' ')
    99  		ctx.WriteString(string(node.Name))
   100  	}
   101  }
   102  
   103  func (node *LoopStmt) GetStatementType() string { return "Loop Statement" }
   104  func (node *LoopStmt) GetQueryType() string     { return QueryTypeTCL }
   105  
   106  type IterateStmt struct {
   107  	statementImpl
   108  	Name Identifier
   109  }
   110  
   111  func (node *IterateStmt) Format(ctx *FmtCtx) {
   112  	ctx.WriteString("iterate ")
   113  	ctx.WriteString(string(node.Name))
   114  }
   115  
   116  func (node *IterateStmt) GetStatementType() string { return "Iterate Statement" }
   117  func (node *IterateStmt) GetQueryType() string     { return QueryTypeTCL }
   118  
   119  type LeaveStmt struct {
   120  	statementImpl
   121  	Name Identifier
   122  }
   123  
   124  func (node *LeaveStmt) Format(ctx *FmtCtx) {
   125  	ctx.WriteString("leave ")
   126  	ctx.WriteString(string(node.Name))
   127  }
   128  
   129  func (node *LeaveStmt) GetStatementType() string { return "Leave Statement" }
   130  func (node *LeaveStmt) GetQueryType() string     { return QueryTypeTCL }