github.com/team-ide/go-dialect@v1.9.20/dialect/mapping.index.go (about)

     1  package dialect
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func (this_ *SqlMapping) GetIndexTypeInfos() (indexTypeInfoList []*IndexTypeInfo) {
     8  	list := this_.indexTypeInfoList
     9  	for _, one := range list {
    10  		if one.IsExtend {
    11  			continue
    12  		}
    13  		indexTypeInfoList = append(indexTypeInfoList, one)
    14  	}
    15  	return
    16  }
    17  
    18  func (this_ *SqlMapping) AddIndexTypeInfo(indexTypeInfo *IndexTypeInfo) {
    19  	this_.indexTypeInfoCacheLock.Lock()
    20  	defer this_.indexTypeInfoCacheLock.Unlock()
    21  
    22  	if this_.indexTypeInfoCache == nil {
    23  		this_.indexTypeInfoCache = make(map[string]*IndexTypeInfo)
    24  	}
    25  
    26  	key := strings.ToLower(indexTypeInfo.Name)
    27  	find := this_.indexTypeInfoCache[key]
    28  	this_.indexTypeInfoCache[key] = indexTypeInfo
    29  	if find == nil {
    30  		this_.indexTypeInfoList = append(this_.indexTypeInfoList, indexTypeInfo)
    31  	} else {
    32  		var list = this_.indexTypeInfoList
    33  		var newList []*IndexTypeInfo
    34  		for _, one := range list {
    35  			if one == find {
    36  				newList = append(newList, indexTypeInfo)
    37  			} else {
    38  				newList = append(newList, one)
    39  			}
    40  		}
    41  		this_.indexTypeInfoList = newList
    42  	}
    43  
    44  	return
    45  }
    46  
    47  func (this_ *SqlMapping) GetIndexTypeInfo(typeName string) (indexTypeInfo *IndexTypeInfo, err error) {
    48  	this_.indexTypeInfoCacheLock.Lock()
    49  	defer this_.indexTypeInfoCacheLock.Unlock()
    50  
    51  	if this_.indexTypeInfoCache == nil {
    52  		this_.indexTypeInfoCache = make(map[string]*IndexTypeInfo)
    53  	}
    54  
    55  	key := strings.ToLower(typeName)
    56  	indexTypeInfo = this_.indexTypeInfoCache[key]
    57  	if indexTypeInfo == nil {
    58  
    59  		indexTypeInfo = &IndexTypeInfo{
    60  			Name:     typeName,
    61  			Format:   typeName,
    62  			IsExtend: true,
    63  		}
    64  		this_.indexTypeInfoCache[key] = indexTypeInfo
    65  		//err = errors.New("dialect [" + this_.DialectType().Name + "] GetIndexTypeInfo not support index type name [" + typeName + "]")
    66  		//fmt.Println(err)
    67  		return
    68  	}
    69  	return
    70  }
    71  
    72  func (this_ *SqlMapping) IndexTypeFormat(index *IndexModel) (indexTypeFormat string, err error) {
    73  	indexTypeInfo, err := this_.GetIndexTypeInfo(index.IndexType)
    74  	if err != nil {
    75  		return
    76  	}
    77  	if indexTypeInfo.IndexTypeFormat != nil {
    78  		indexTypeFormat, err = indexTypeInfo.IndexTypeFormat(index)
    79  		return
    80  	}
    81  	indexTypeFormat = indexTypeInfo.Format
    82  	return
    83  }
    84  
    85  func (this_ *SqlMapping) IndexNameFormat(param *ParamModel, ownerName string, tableName string, index *IndexModel) (indexNameFormat string, err error) {
    86  	if index.IndexName != "" {
    87  		indexNameFormat = index.IndexName
    88  		return
    89  	}
    90  	indexTypeInfo, err := this_.GetIndexTypeInfo(index.IndexType)
    91  	if err != nil {
    92  		return
    93  	}
    94  	if indexTypeInfo.IndexNameFormat != nil {
    95  		indexNameFormat, err = indexTypeInfo.IndexNameFormat(param, ownerName, tableName, index)
    96  		return
    97  	}
    98  	if ownerName != "" {
    99  		indexNameFormat += ownerName + "_"
   100  		//indexNameFormat += sortName(ownerName, 4) + "_"
   101  	}
   102  	if tableName != "" {
   103  		indexNameFormat += tableName + "_"
   104  		//indexNameFormat += sortName(tableName, 4) + "_"
   105  	}
   106  	if index.IndexType != "" && !strings.EqualFold(index.IndexType, "index") {
   107  		indexNameFormat += index.IndexType + "_"
   108  		//indexNameFormat += sortName(index.IndexType, 4) + "_"
   109  	}
   110  	indexNameFormat += strings.Join(index.ColumnNames, "_")
   111  	//maxLength := 30 - len(indexNameFormat)
   112  	//columnNamesStr := strings.Join(index.ColumnNames, "_")
   113  	if this_.IndexNameMaxLen > 0 {
   114  		indexNameFormat = sortName(indexNameFormat, this_.IndexNameMaxLen)
   115  	}
   116  	return
   117  }
   118  
   119  func sortName(name string, size int) (res string) {
   120  	name = strings.TrimSpace(name)
   121  	if len(name) <= size {
   122  		res = name
   123  		return
   124  	}
   125  	if strings.Contains(name, "_") {
   126  		ss := strings.Split(name, "_")
   127  		var names []string
   128  
   129  		for _, s := range ss {
   130  			if strings.TrimSpace(s) == "" {
   131  				continue
   132  			}
   133  			names = append(names, strings.TrimSpace(s))
   134  		}
   135  		rSize := size / len(names)
   136  
   137  		for i, s := range ss {
   138  			if len(res) >= size {
   139  				break
   140  			}
   141  			if i < len(ss)-1 {
   142  				if rSize >= len(s)-1 {
   143  					res += s + "_"
   144  				} else {
   145  					res += s[0:rSize-1] + "_"
   146  				}
   147  			} else {
   148  				if rSize >= len(s) {
   149  					res += s
   150  				} else {
   151  					res += s[0:rSize]
   152  				}
   153  			}
   154  		}
   155  	} else {
   156  		res += name[0:size]
   157  	}
   158  	return
   159  }