vitess.io/vitess@v0.16.2/go/vt/external/golib/sqlutils/dialect.go (about)

     1  /*
     2     Copyright 2017 GitHub Inc.
     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  /*
    18  	This file has been copied over from VTOrc package
    19  */
    20  
    21  package sqlutils
    22  
    23  import (
    24  	"regexp"
    25  	"strings"
    26  )
    27  
    28  type regexpMap struct {
    29  	r           *regexp.Regexp
    30  	replacement string
    31  }
    32  
    33  func (this *regexpMap) process(text string) (result string) {
    34  	return this.r.ReplaceAllString(text, this.replacement)
    35  }
    36  
    37  func rmap(regexpExpression string, replacement string) regexpMap {
    38  	return regexpMap{
    39  		r:           regexp.MustCompile(regexpSpaces(regexpExpression)),
    40  		replacement: replacement,
    41  	}
    42  }
    43  
    44  func regexpSpaces(statement string) string {
    45  	return strings.Replace(statement, " ", `[\s]+`, -1)
    46  }
    47  
    48  func applyConversions(statement string, conversions []regexpMap) string {
    49  	for _, rmap := range conversions {
    50  		statement = rmap.process(statement)
    51  	}
    52  	return statement
    53  }