Compare commits

..

No commits in common. "dbd51ff4759e398c6facd2af6540562e4469424b" and "fa5def962c0bcadbdd7bdc43131f8fcfa54d30bc" have entirely different histories.

View File

@ -425,9 +425,9 @@ func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
}
if reflect.TypeOf(fieldValue).Kind() == reflect.Slice {
// 传入数组, in语句
placeholder, dataList := parseInSql(fieldValue)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` IN ("+placeholder+")")
bindValue = append(bindValue, dataList...)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)")
byteData, _ := json.Marshal(fieldValue)
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
} else {
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` = ?")
bindValue = append(bindValue, fieldValue)
@ -442,16 +442,16 @@ func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
// in 语句
// 传入数组, in语句
for fieldName, fieldValue := range o.In {
placeholder, dataList := parseInSql(fieldValue)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` IN ("+placeholder+")")
bindValue = append(bindValue, dataList...)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)")
byteData, _ := json.Marshal(fieldValue)
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
}
// not in 语句
for fieldName, fieldValue := range o.NotIn {
placeholder, dataList := parseInSql(fieldValue)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT IN ("+placeholder+")")
bindValue = append(bindValue, dataList...)
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT IN (?)")
byteData, _ := json.Marshal(fieldValue)
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
}
// like 语句
@ -489,22 +489,3 @@ func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
}
return
}
// parseInSql 解析in语句需要绑定占位符以及数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:07 2024/8/9
func parseInSql(fieldValue any) (string, []any) {
byteData, _ := json.Marshal(fieldValue)
var dataList []any
decoder := json.NewDecoder(strings.NewReader(string(byteData)))
decoder.UseNumber()
_ = decoder.Decode(&dataList)
// 生成占位符
placeholderList := make([]string, 0)
for i := 0; i < len(dataList); i++ {
placeholderList = append(placeholderList, "?")
}
return strings.Join(placeholderList, ","), dataList
}