/usr/share/gocode/src/github.com/go-xorm/core/column.go is in golang-github-go-xorm-core-dev 0.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | package core
import (
"errors"
"fmt"
"reflect"
"strings"
)
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
// database column
type Column struct {
Name string
FieldName string
SQLType SQLType
Length int
Length2 int
Nullable bool
Default string
Indexes map[string]bool
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
IsCreated bool
IsUpdated bool
IsDeleted bool
IsCascade bool
IsVersion bool
fieldPath []string
DefaultIsEmpty bool
EnumOptions map[string]int
SetOptions map[string]int
}
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
return &Column{
Name: name,
FieldName: fieldName,
SQLType: sqlType,
Length: len1,
Length2: len2,
Nullable: nullable,
Default: "",
Indexes: make(map[string]bool),
IsPrimaryKey: false,
IsAutoIncrement: false,
MapType: TWOSIDES,
IsCreated: false,
IsUpdated: false,
IsDeleted: false,
IsCascade: false,
IsVersion: false,
fieldPath: nil,
DefaultIsEmpty: false,
EnumOptions: make(map[string]int),
}
}
// generate column description string according dialect
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 d.ShowCreateNull() {
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
func (col *Column) StringNoPk(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if d.ShowCreateNull() {
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
// return col'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)
}
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
var fieldValue reflect.Value
if col.fieldPath == nil {
col.fieldPath = strings.Split(col.FieldName, ".")
}
if dataStruct.Type().Kind() == reflect.Map {
var keyValue reflect.Value
if len(col.fieldPath) == 1 {
keyValue = reflect.ValueOf(col.FieldName)
} else if len(col.fieldPath) == 2 {
keyValue = reflect.ValueOf(col.fieldPath[1])
} else {
return nil, fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
}
fieldValue = dataStruct.MapIndex(keyValue)
return &fieldValue, nil
}
if len(col.fieldPath) == 1 {
fieldValue = dataStruct.FieldByName(col.FieldName)
} else if len(col.fieldPath) == 2 {
parentField := dataStruct.FieldByName(col.fieldPath[0])
if parentField.IsValid() {
if parentField.Kind() == reflect.Struct {
fieldValue = parentField.FieldByName(col.fieldPath[1])
} else if parentField.Kind() == reflect.Ptr {
if parentField.IsNil() {
parentField.Set(reflect.New(parentField.Type().Elem()))
fieldValue = parentField.Elem().FieldByName(col.fieldPath[1])
} else {
parentField = parentField.Elem()
if parentField.IsValid() {
fieldValue = parentField.FieldByName(col.fieldPath[1])
} else {
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
}
}
}
} else {
// so we can use a different struct as conditions
fieldValue = dataStruct.FieldByName(col.fieldPath[1])
}
} else {
return nil, fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
}
if !fieldValue.IsValid() {
return nil, errors.New("no find field matched")
}
return &fieldValue, nil
}
|