github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/while.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     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 plan
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  )
    20  
    21  // While represents the WHILE statement, which loops over a set of statements while the condition is true.
    22  type While struct {
    23  	*Loop
    24  }
    25  
    26  var _ sql.Node = (*While)(nil)
    27  var _ sql.DebugStringer = (*While)(nil)
    28  var _ sql.Expressioner = (*While)(nil)
    29  var _ sql.CollationCoercible = (*While)(nil)
    30  var _ RepresentsLabeledBlock = (*While)(nil)
    31  
    32  // NewWhile returns a new *While node.
    33  func NewWhile(label string, condition sql.Expression, block *Block) *While {
    34  	return &While{
    35  		&Loop{
    36  			Label:          label,
    37  			Condition:      condition,
    38  			OnceBeforeEval: false,
    39  			Block:          block,
    40  		},
    41  	}
    42  }
    43  
    44  // String implements the interface sql.Node.
    45  func (w *While) String() string {
    46  	label := ""
    47  	if len(w.Label) > 0 {
    48  		label = w.Label + ": "
    49  	}
    50  	p := sql.NewTreePrinter()
    51  	_ = p.WriteNode("%s: WHILE(%s)", label, w.Condition.String())
    52  	var children []string
    53  	for _, s := range w.statements {
    54  		children = append(children, s.String())
    55  	}
    56  	_ = p.WriteChildren(children...)
    57  	return p.String()
    58  }
    59  
    60  // DebugString implements the interface sql.DebugStringer.
    61  func (w *While) DebugString() string {
    62  	label := ""
    63  	if len(w.Label) > 0 {
    64  		label = w.Label + ": "
    65  	}
    66  	p := sql.NewTreePrinter()
    67  	_ = p.WriteNode("%s: WHILE(%s)", label, sql.DebugString(w.Condition))
    68  	var children []string
    69  	for _, s := range w.statements {
    70  		children = append(children, sql.DebugString(s))
    71  	}
    72  	_ = p.WriteChildren(children...)
    73  	return p.String()
    74  }
    75  
    76  // WithChildren implements the interface sql.Node.
    77  func (w *While) WithChildren(children ...sql.Node) (sql.Node, error) {
    78  	return &While{
    79  		&Loop{
    80  			Label:          w.Loop.Label,
    81  			Condition:      w.Loop.Condition,
    82  			OnceBeforeEval: false,
    83  			Block:          NewBlock(children),
    84  		},
    85  	}, nil
    86  }
    87  
    88  // WithExpressions implements the interface sql.Node.
    89  func (w *While) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
    90  	if len(exprs) != 1 {
    91  		return nil, sql.ErrInvalidChildrenNumber.New(w, len(exprs), 1)
    92  	}
    93  
    94  	return &While{
    95  		&Loop{
    96  			Label:          w.Loop.Label,
    97  			Condition:      exprs[0],
    98  			OnceBeforeEval: false,
    99  			Block:          w.Loop.Block,
   100  		},
   101  	}, nil
   102  }