github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/set_normalizer.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/team-ide/go-dialect/vitess/vterrors"
    23  	"github.com/team-ide/go-dialect/vitess/vtrpc"
    24  )
    25  
    26  type setNormalizer struct {
    27  	err error
    28  }
    29  
    30  func (n *setNormalizer) rewriteSetComingUp(cursor *Cursor) bool {
    31  	set, ok := cursor.node.(*Set)
    32  	if ok {
    33  		for i, expr := range set.Exprs {
    34  			exp, err := n.normalizeSetExpr(expr)
    35  			if err != nil {
    36  				n.err = err
    37  				return false
    38  			}
    39  			set.Exprs[i] = exp
    40  		}
    41  	}
    42  	return true
    43  }
    44  
    45  func (n *setNormalizer) normalizeSetExpr(in *SetExpr) (*SetExpr, error) {
    46  	switch in.Name.at { // using switch so we can use break
    47  	case DoubleAt:
    48  		if in.Scope != ImplicitScope {
    49  			return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cannot use scope and @@")
    50  		}
    51  		switch {
    52  		case strings.HasPrefix(in.Name.Lowered(), "session."):
    53  			in.Name = createColumn(in.Name.Lowered()[8:])
    54  			in.Scope = SessionScope
    55  		case strings.HasPrefix(in.Name.Lowered(), "global."):
    56  			in.Name = createColumn(in.Name.Lowered()[7:])
    57  			in.Scope = GlobalScope
    58  		case strings.HasPrefix(in.Name.Lowered(), "vitess_metadata."):
    59  			in.Name = createColumn(in.Name.Lowered()[16:])
    60  			in.Scope = VitessMetadataScope
    61  		default:
    62  			in.Name.at = NoAt
    63  			in.Scope = SessionScope
    64  		}
    65  		return in, nil
    66  	case SingleAt:
    67  		if in.Scope != ImplicitScope {
    68  			return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cannot mix scope and user defined variables")
    69  		}
    70  		return in, nil
    71  	case NoAt:
    72  		switch in.Scope {
    73  		case ImplicitScope:
    74  			in.Scope = SessionScope
    75  		case LocalScope:
    76  			in.Scope = SessionScope
    77  		}
    78  		return in, nil
    79  	}
    80  	panic("this should never happen")
    81  }
    82  
    83  func createColumn(str string) ColIdent {
    84  	size := len(str)
    85  	if str[0] == '`' && str[size-1] == '`' {
    86  		str = str[1 : size-1]
    87  	}
    88  	return NewColIdent(str)
    89  }