github.com/webx-top/com@v1.2.12/sql.go (about)

     1  package com
     2  
     3  import "regexp"
     4  
     5  //HasTableAlias 检查sql语句中是否包含指定表别名
     6  //sqlStr 可以由"<where子句>,<select子句>,<orderBy子句>,<groupBy子句>"组成
     7  func HasTableAlias(alias string, sqlStr string, quotes ...string) (bool, error) {
     8  	var left, right string
     9  	switch len(quotes) {
    10  	case 2:
    11  		right = quotes[1]
    12  		left = quotes[0]
    13  	case 1:
    14  		left = quotes[0]
    15  		right = left
    16  	default:
    17  		left = "`"
    18  		right = "`"
    19  	}
    20  	re, err := regexp.Compile("[ ,][" + left + "]?" + alias + "[" + right + "]?\\.")
    21  	if err != nil {
    22  		return false, err
    23  	}
    24  	return re.MatchString(` ` + sqlStr), err
    25  }