pki/vendor/xorm.io/xorm/schemas/column.go

114 lines
2.9 KiB
Go
Raw Normal View History

2020-11-25 20:36:07 +01:00
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package schemas
import (
"errors"
"reflect"
"strconv"
"time"
)
2021-08-30 20:04:15 +02:00
// enumerates all database mapping way
2020-11-25 20:36:07 +01:00
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
// Column defines database column
type Column struct {
Name string
TableName string
2021-08-30 20:04:15 +02:00
FieldName string // Available only when parsed from a struct
FieldIndex []int // Available only when parsed from a struct
2020-11-25 20:36:07 +01:00
SQLType SQLType
IsJSON bool
2022-11-02 17:49:03 +01:00
Length int64
Length2 int64
2020-11-25 20:36:07 +01:00
Nullable bool
Default string
Indexes map[string]int
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
IsCreated bool
IsUpdated bool
IsDeleted bool
IsCascade bool
IsVersion bool
DefaultIsEmpty bool // false means column has no default set, but not default value is empty
EnumOptions map[string]int
SetOptions map[string]int
DisableTimeZone bool
TimeZone *time.Location // column specified time zone
Comment string
2023-10-01 12:07:22 +02:00
Collation string
2020-11-25 20:36:07 +01:00
}
// NewColumn creates a new column
2022-11-02 17:49:03 +01:00
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int64, nullable bool) *Column {
2020-11-25 20:36:07 +01:00
return &Column{
Name: name,
2021-04-07 10:58:21 +02:00
IsJSON: sqlType.IsJson(),
2020-11-25 20:36:07 +01:00
TableName: "",
FieldName: fieldName,
SQLType: sqlType,
Length: len1,
Length2: len2,
Nullable: nullable,
Default: "",
Indexes: make(map[string]int),
IsPrimaryKey: false,
IsAutoIncrement: false,
MapType: TWOSIDES,
IsCreated: false,
IsUpdated: false,
IsDeleted: false,
IsCascade: false,
IsVersion: false,
DefaultIsEmpty: true, // default should be no default
EnumOptions: make(map[string]int),
Comment: "",
}
}
// ValueOf returns column's filed of struct's value
func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
dataStruct := reflect.Indirect(reflect.ValueOf(bean))
return col.ValueOfV(&dataStruct)
}
// ValueOfV returns column's filed of struct's value accept reflevt value
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
2022-11-02 17:49:03 +01:00
v := *dataStruct
2021-08-30 20:04:15 +02:00
for _, i := range col.FieldIndex {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
2020-11-25 20:36:07 +01:00
}
2021-08-30 20:04:15 +02:00
v = v.Elem()
2023-10-01 12:07:22 +02:00
} else if v.Kind() == reflect.Interface {
v = reflect.Indirect(v.Elem())
2020-11-25 20:36:07 +01:00
}
2021-08-30 20:04:15 +02:00
v = v.FieldByIndex([]int{i})
2020-11-25 20:36:07 +01:00
}
2021-08-30 20:04:15 +02:00
return &v, nil
2020-11-25 20:36:07 +01:00
}
// ConvertID converts id content to suitable type according column type
func (col *Column) ConvertID(sid string) (interface{}, error) {
if col.SQLType.IsNumeric() {
n, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
return nil, err
}
return n, nil
} else if col.SQLType.IsText() {
return sid, nil
}
return nil, errors.New("not supported")
}