| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package sql_tpl
- import (
- "errors"
- "reflect"
- "strconv"
- "time"
- )
- const (
- timeWriteFormat = time.DateTime + ".000000 +08:00"
- )
- func parseValue(value any) (string, error) {
- valueValue := reflect.ValueOf(value)
- if !valueValue.IsValid() {
- return "", errors.New("无效值")
- }
- if valueValue.Kind() == reflect.Ptr && valueValue.IsNil() {
- return "", errors.New("空值")
- }
- if valueValue.Kind() == reflect.Ptr {
- valueValue = valueValue.Elem()
- }
- switch v := valueValue.Interface().(type) {
- case string:
- return "'" + v + "'", nil
- case bool:
- return strconv.FormatBool(v), nil
- case time.Time:
- return "'" + v.Format(timeWriteFormat) + "'", nil
- case int:
- return strconv.Itoa(v), nil
- case int8:
- return strconv.FormatInt(int64(v), 10), nil
- case int16:
- return strconv.FormatInt(int64(v), 10), nil
- case int32:
- return strconv.FormatInt(int64(v), 10), nil
- case int64:
- return strconv.FormatInt(v, 10), nil
- case uint:
- return strconv.FormatUint(uint64(v), 10), nil
- case uint8:
- return strconv.FormatUint(uint64(v), 10), nil
- case uint16:
- return strconv.FormatUint(uint64(v), 10), nil
- case uint32:
- return strconv.FormatUint(uint64(v), 10), nil
- case uint64:
- return strconv.FormatUint(v, 10), nil
- default:
- return "", errors.New("不支持的类型")
- }
- }
|