xorm/column.go

167 lines
3.9 KiB
Go
Raw Normal View History

// 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.
2014-04-08 12:57:04 +00:00
package core
import (
"fmt"
"reflect"
"strings"
2016-02-03 03:10:20 +00:00
"time"
2014-04-08 12:57:04 +00:00
)
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
2017-07-20 12:04:00 +00:00
// Column defines database column
2014-04-08 12:57:04 +00:00
type Column struct {
Name string
2016-04-28 13:27:13 +00:00
TableName string
2014-04-08 12:57:04 +00:00
FieldName string
SQLType SQLType
2017-07-20 12:04:00 +00:00
IsJSON bool
2014-04-08 12:57:04 +00:00
Length int
Length2 int
Nullable bool
Default string
2016-06-30 08:33:09 +00:00
Indexes map[string]int
2014-04-08 12:57:04 +00:00
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
IsCreated bool
IsUpdated bool
IsDeleted bool
2014-04-08 12:57:04 +00:00
IsCascade bool
IsVersion bool
2014-04-11 07:38:09 +00:00
DefaultIsEmpty bool
EnumOptions map[string]int
2014-08-07 14:17:14 +00:00
SetOptions map[string]int
2016-02-03 03:10:20 +00:00
DisableTimeZone bool
TimeZone *time.Location // column specified time zone
Comment string
2014-04-08 12:57:04 +00:00
}
// NewColumn creates a new column
2014-04-08 12:57:04 +00:00
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
return &Column{
Name: name,
2016-04-28 13:27:13 +00:00
TableName: "",
FieldName: fieldName,
SQLType: sqlType,
Length: len1,
Length2: len2,
Nullable: nullable,
Default: "",
2016-06-30 08:33:09 +00:00
Indexes: make(map[string]int),
IsPrimaryKey: false,
IsAutoIncrement: false,
MapType: TWOSIDES,
IsCreated: false,
IsUpdated: false,
IsDeleted: false,
IsCascade: false,
IsVersion: false,
DefaultIsEmpty: false,
EnumOptions: make(map[string]int),
Comment: "",
}
2014-04-08 12:57:04 +00:00
}
// String generate column description string according dialect
2014-04-08 12:57:04 +00:00
func (col *Column) String(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.IsPrimaryKey {
sql += "PRIMARY KEY "
if col.IsAutoIncrement {
sql += d.AutoIncrStr() + " "
}
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
2014-04-13 07:37:25 +00:00
if d.ShowCreateNull() {
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
2014-04-08 12:57:04 +00:00
}
return sql
}
// StringNoPk generate column description string according dialect without primary keys
2014-04-08 12:57:04 +00:00
func (col *Column) StringNoPk(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
2014-04-13 07:37:25 +00:00
if d.ShowCreateNull() {
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
2014-04-08 12:57:04 +00:00
}
return sql
}
// ValueOf returns column's filed of struct's value
2014-04-08 12:57:04 +00:00
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
2014-04-08 12:57:04 +00:00
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
var fieldValue reflect.Value
fieldPath := strings.Split(col.FieldName, ".")
2014-04-08 12:57:04 +00:00
if dataStruct.Type().Kind() == reflect.Map {
keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
fieldValue = dataStruct.MapIndex(keyValue)
return &fieldValue, nil
} else if dataStruct.Type().Kind() == reflect.Interface {
structValue := reflect.ValueOf(dataStruct.Interface())
dataStruct = &structValue
}
level := len(fieldPath)
fieldValue = dataStruct.FieldByName(fieldPath[0])
2015-11-25 09:54:33 +00:00
for i := 0; i < level-1; i++ {
if !fieldValue.IsValid() {
break
}
if fieldValue.Kind() == reflect.Struct {
fieldValue = fieldValue.FieldByName(fieldPath[i+1])
2015-11-25 09:54:33 +00:00
} else if fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
2014-05-08 05:24:07 +00:00
}
fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
2014-04-08 12:57:04 +00:00
} else {
2018-05-04 05:52:29 +00:00
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
2014-04-08 12:57:04 +00:00
}
}
if !fieldValue.IsValid() {
2018-05-04 05:52:29 +00:00
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
2014-04-08 12:57:04 +00:00
}
2014-04-08 12:57:04 +00:00
return &fieldValue, nil
}