fork github.com/doug-martin

This commit is contained in:
2025-03-22 23:02:05 +08:00
commit f14642a736
131 changed files with 34555 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
package exp
import (
"reflect"
"sort"
"git.fsdpf.net/go/db/v2/internal/errors"
"git.fsdpf.net/go/db/v2/internal/util"
)
type (
update struct {
col IdentifierExpression
val interface{}
}
)
func set(col IdentifierExpression, val interface{}) UpdateExpression {
return update{col: col, val: val}
}
func NewUpdateExpressions(update interface{}) (updates []UpdateExpression, err error) {
if us, ok := update.([]UpdateExpression); ok {
updates = append(updates, us...)
return updates, nil
}
if u, ok := update.(UpdateExpression); ok {
updates = append(updates, u)
return updates, nil
}
updateValue := reflect.Indirect(reflect.ValueOf(update))
switch updateValue.Kind() {
case reflect.Map:
keys := util.ValueSlice(updateValue.MapKeys())
sort.Sort(keys)
for _, key := range keys {
updates = append(updates, ParseIdentifier(key.String()).Set(updateValue.MapIndex(key).Interface()))
}
case reflect.Struct:
return getUpdateExpressionsStruct(updateValue)
default:
return nil, errors.New("unsupported update interface type %+v", updateValue.Type())
}
return updates, nil
}
func getUpdateExpressionsStruct(value reflect.Value) (updates []UpdateExpression, err error) {
r, err := NewRecordFromStruct(value.Interface(), false, true)
if err != nil {
return updates, err
}
cols := r.Cols()
for _, col := range cols {
updates = append(updates, ParseIdentifier(col).Set(r[col]))
}
return updates, nil
}
func (u update) Expression() Expression {
return u
}
func (u update) Clone() Expression {
return update{col: u.col.Clone().(IdentifierExpression), val: u.val}
}
func (u update) Col() IdentifierExpression {
return u.col
}
func (u update) Val() interface{} {
return u.val
}