github.com/team-ide/go-dialect@v1.9.20/dialect/mapping.column.go (about) 1 package dialect 2 3 import ( 4 "encoding/json" 5 "errors" 6 "github.com/dop251/goja" 7 "regexp" 8 "strconv" 9 "strings" 10 ) 11 12 func (this_ *SqlMapping) GetColumnTypeInfos() (columnTypeInfoList []*ColumnTypeInfo) { 13 list := this_.columnTypeInfoList 14 for _, one := range list { 15 if one.IsExtend { 16 continue 17 } 18 columnTypeInfoList = append(columnTypeInfoList, one) 19 } 20 return 21 } 22 23 func (this_ *SqlMapping) AddColumnTypeInfo(columnTypeInfo *ColumnTypeInfo) { 24 this_.columnTypeInfoCacheLock.Lock() 25 defer this_.columnTypeInfoCacheLock.Unlock() 26 27 if this_.columnTypeInfoCache == nil { 28 this_.columnTypeInfoCache = make(map[string]*ColumnTypeInfo) 29 } 30 31 key := strings.ToLower(columnTypeInfo.Name) 32 find := this_.columnTypeInfoCache[key] 33 this_.columnTypeInfoCache[key] = columnTypeInfo 34 if find == nil { 35 this_.columnTypeInfoList = append(this_.columnTypeInfoList, columnTypeInfo) 36 } else { 37 var list = this_.columnTypeInfoList 38 var newList []*ColumnTypeInfo 39 for _, one := range list { 40 if one == find { 41 newList = append(newList, columnTypeInfo) 42 } else { 43 newList = append(newList, one) 44 } 45 } 46 this_.columnTypeInfoList = newList 47 } 48 49 return 50 } 51 52 func (this_ *SqlMapping) GetColumnTypeInfo(column *ColumnModel) (columnTypeInfo *ColumnTypeInfo, err error) { 53 if column == nil || column.ColumnDataType == "" { 54 err = errors.New("dialect [" + this_.DialectType().Name + "] GetColumnTypeInfo column data type is null") 55 return 56 } 57 this_.columnTypeInfoCacheLock.Lock() 58 defer this_.columnTypeInfoCacheLock.Unlock() 59 60 if this_.columnTypeInfoCache == nil { 61 this_.columnTypeInfoCache = make(map[string]*ColumnTypeInfo) 62 } 63 columnDataType := column.ColumnDataType 64 65 key := strings.ToLower(columnDataType) 66 columnTypeInfo = this_.columnTypeInfoCache[key] 67 68 if columnTypeInfo == nil { 69 70 var list = this_.columnTypeInfoList 71 for _, one := range list { 72 if len(one.Matches) == 0 { 73 continue 74 } 75 var matched = false 76 //fmt.Println("typeName:", typeName, ",MatchName:", one.Name, ",matches:", one.Matches) 77 for _, match := range one.Matches { 78 if match == strings.ToUpper(columnDataType) { 79 matched = true 80 break 81 } 82 //fmt.Println("typeName:", typeName, ",match:", match) 83 if strings.Contains(match, "&&") { 84 matchRule := match[0:strings.Index(match, "&&")] 85 scriptRule := match[strings.Index(match, "&&")+2:] 86 matchRule = strings.TrimSpace(matchRule) 87 scriptRule = strings.TrimSpace(scriptRule) 88 if matchRule == strings.ToUpper(columnDataType) || regexp.MustCompile(matchRule).MatchString(strings.ToUpper(columnDataType)) { 89 if scriptRule != "" { 90 setValueRule := "" 91 if strings.Contains(scriptRule, ";") { 92 setValueRule = scriptRule[strings.Index(scriptRule, ";")+1:] 93 setValueRule = strings.TrimSpace(setValueRule) 94 scriptRule = scriptRule[0:strings.Index(scriptRule, ";")] 95 scriptRule = strings.TrimSpace(scriptRule) 96 } 97 // 执行函数 98 // 99 vm := goja.New() 100 _ = vm.Set("columnLength", column.ColumnLength) 101 _ = vm.Set("columnPrecision", column.ColumnPrecision) 102 _ = vm.Set("columnScale", column.ColumnScale) 103 _ = vm.Set("columnDataType", column.ColumnDataType) 104 _ = vm.Set("columnDefault", column.ColumnDefault) 105 106 var res goja.Value 107 res, err = vm.RunString(scriptRule) 108 if err != nil { 109 err = errors.New("run script [" + scriptRule + "] error:" + err.Error()) 110 return 111 } 112 //fmt.Println("scriptRule:", scriptRule, ",scriptValue:", res.String(), ",ToBoolean:", res.ToBoolean()) 113 if !res.ToBoolean() { 114 matched = false 115 continue 116 } 117 if setValueRule != "" { 118 setValues := strings.Split(setValueRule, ",") 119 for _, setValueStr := range setValues { 120 if !strings.Contains(setValueStr, "=") { 121 continue 122 } 123 setName := setValueStr[0:strings.Index(setValueStr, "=")] 124 setValue := setValueStr[strings.Index(setValueStr, "=")+1:] 125 setName = strings.TrimSpace(setName) 126 setValue = strings.TrimSpace(setValue) 127 if strings.EqualFold(setName, "columnLength") { 128 column.ColumnLength, err = StringToInt(setValue) 129 if err != nil { 130 err = errors.New("set value [" + setValue + "] error:" + err.Error()) 131 return 132 } 133 } else if strings.EqualFold(setName, "columnPrecision") { 134 column.ColumnPrecision, err = StringToInt(setValue) 135 if err != nil { 136 err = errors.New("set value [" + setValue + "] error:" + err.Error()) 137 return 138 } 139 } else if strings.EqualFold(setName, "columnScale") { 140 column.ColumnScale, err = StringToInt(setValue) 141 if err != nil { 142 err = errors.New("set value [" + setValue + "] error:" + err.Error()) 143 return 144 } 145 } 146 } 147 } 148 } 149 matched = true 150 break 151 } 152 153 } else { 154 if regexp.MustCompile(match).MatchString(strings.ToUpper(columnDataType)) { 155 matched = true 156 break 157 } 158 } 159 160 } 161 if matched { 162 columnTypeInfo = one 163 break 164 } 165 } 166 } 167 168 if columnTypeInfo == nil { 169 columnTypeInfo = &ColumnTypeInfo{ 170 Name: column.ColumnDataType, 171 Format: column.ColumnDataType, 172 IsExtend: true, 173 } 174 this_.columnTypeInfoCache[key] = columnTypeInfo 175 //err = errors.New("dialect [" + this_.DialectType().Name + "] GetColumnTypeInfo not support column type name [" + column.ColumnDataType + "]") 176 //return 177 } 178 return 179 } 180 181 func (this_ *SqlMapping) ColumnTypePack(column *ColumnModel) (columnTypePack string, err error) { 182 columnTypeInfo, err := this_.GetColumnTypeInfo(column) 183 if err != nil { 184 bs, _ := json.Marshal(column) 185 err = errors.New("ColumnTypePack error column:" + string(bs) + ",error:" + err.Error()) 186 return 187 } 188 if columnTypeInfo.ColumnTypePack != nil { 189 columnTypePack, err = columnTypeInfo.ColumnTypePack(column) 190 if err != nil { 191 return 192 } 193 } else { 194 if columnTypeInfo.IsEnum { 195 enums := column.ColumnEnums 196 if len(enums) == 0 { 197 enums = []string{""} 198 } 199 columnTypePack = columnTypeInfo.Name + "(" + packingValues("'", enums) + ")" 200 } else { 201 columnTypePack = columnTypeInfo.Format 202 if strings.Contains(columnTypePack, "(") { 203 beforeStr := columnTypePack[0:strings.Index(columnTypePack, "(")] 204 endStr := columnTypePack[strings.Index(columnTypePack, "("):] 205 lStr := "" 206 pStr := "" 207 sStr := "" 208 if column.ColumnLength >= 0 { 209 lStr = strconv.Itoa(column.ColumnLength) 210 } 211 if column.ColumnPrecision >= 0 { 212 pStr = strconv.Itoa(column.ColumnPrecision) 213 } 214 if column.ColumnScale >= 0 { 215 sStr = strconv.Itoa(column.ColumnScale) 216 } 217 if pStr == "0" && sStr == "0" { 218 pStr = "" 219 sStr = "" 220 } 221 endStr = strings.ReplaceAll(endStr, "$l", lStr) 222 endStr = strings.ReplaceAll(endStr, "$p", pStr) 223 endStr = strings.ReplaceAll(endStr, "$s", sStr) 224 columnTypePack = beforeStr + endStr 225 } 226 } 227 } 228 columnTypePack = strings.ReplaceAll(columnTypePack, " )", ")") 229 columnTypePack = strings.ReplaceAll(columnTypePack, " ,", ",") 230 columnTypePack = strings.ReplaceAll(columnTypePack, ",)", ")") 231 columnTypePack = strings.ReplaceAll(columnTypePack, "(,)", "") 232 columnTypePack = strings.ReplaceAll(columnTypePack, "()", "") 233 234 return 235 }