github.com/vedadiyan/sqlparser@v1.0.0/pkg/sqlparser/bind_var_needs.go (about) 1 /* 2 Copyright 2020 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 // BindVarNeeds represents the bind vars that need to be provided as the result of expression rewriting. 20 type BindVarNeeds struct { 21 NeedFunctionResult, 22 NeedSystemVariable, 23 // NeedUserDefinedVariables keeps track of all user defined variables a query is using 24 NeedUserDefinedVariables []string 25 otherRewrites bool 26 } 27 28 // MergeWith adds bind vars needs coming from sub scopes 29 func (bvn *BindVarNeeds) MergeWith(other *BindVarNeeds) { 30 bvn.NeedFunctionResult = append(bvn.NeedFunctionResult, other.NeedFunctionResult...) 31 bvn.NeedSystemVariable = append(bvn.NeedSystemVariable, other.NeedSystemVariable...) 32 bvn.NeedUserDefinedVariables = append(bvn.NeedUserDefinedVariables, other.NeedUserDefinedVariables...) 33 } 34 35 // AddFuncResult adds a function bindvar need 36 func (bvn *BindVarNeeds) AddFuncResult(name string) { 37 bvn.NeedFunctionResult = append(bvn.NeedFunctionResult, name) 38 } 39 40 // AddSysVar adds a system variable bindvar need 41 func (bvn *BindVarNeeds) AddSysVar(name string) { 42 bvn.NeedSystemVariable = append(bvn.NeedSystemVariable, name) 43 } 44 45 // AddUserDefVar adds a user defined variable bindvar need 46 func (bvn *BindVarNeeds) AddUserDefVar(name string) { 47 bvn.NeedUserDefinedVariables = append(bvn.NeedUserDefinedVariables, name) 48 } 49 50 // NeedsFuncResult says if a function result needs to be provided 51 func (bvn *BindVarNeeds) NeedsFuncResult(name string) bool { 52 return contains(bvn.NeedFunctionResult, name) 53 } 54 55 // NeedsSysVar says if a function result needs to be provided 56 func (bvn *BindVarNeeds) NeedsSysVar(name string) bool { 57 return contains(bvn.NeedSystemVariable, name) 58 } 59 60 func (bvn *BindVarNeeds) NoteRewrite() { 61 bvn.otherRewrites = true 62 } 63 64 func (bvn *BindVarNeeds) HasRewrites() bool { 65 return bvn.otherRewrites || 66 len(bvn.NeedFunctionResult) > 0 || 67 len(bvn.NeedUserDefinedVariables) > 0 || 68 len(bvn.NeedSystemVariable) > 0 69 } 70 71 func contains(strings []string, name string) bool { 72 for _, s := range strings { 73 if name == s { 74 return true 75 } 76 } 77 return false 78 }