diff --git a/model.go b/model.go index 3f550c8..6aee33f 100644 --- a/model.go +++ b/model.go @@ -42,7 +42,7 @@ func (m *Model) Delete() *Query { // Select func (m *Model) Select(f ...string) *Query { if len(f) == 0 { - f = m.Table.AllFields + f = m.Table.SelectFields } q := NewQuery(Servers[m.Module]) diff --git a/table.go b/table.go index 60dbcce..98a77e3 100644 --- a/table.go +++ b/table.go @@ -19,25 +19,64 @@ type Table struct { // All fields, except primary Fields []string - // All fields - AllFields []string + // Fields for select, include primary + SelectFields []string + + // Fields for add + AddFields []string + + // Fields for update + UpdateFields []string // json and field property map FiledsMap map[string]string - // Entity + // Entity type EntityType reflect.Type } // New Table func NewTable(tableName string, entity interface{}) *Table { - p, f, af, jf := tableFields(entity) + primary := "" + fields := make([]string, 0) + selectFields := make([]string, 0) + addFields := make([]string, 0) + updateFields := make([]string, 0) + filedsMap := make(map[string]string) + typ := reflect.Indirect(reflect.ValueOf(entity)).Type() + + for i := 0; i < typ.NumField(); i++ { + field := typ.Field(i) + + fd := field.Name + if field.Tag.Get("field") != "" { + fd = field.Tag.Get("field") + } + + jn := field.Name + if field.Tag.Get("json") != "" { + jn = field.Tag.Get("json") + } + + //!field.Anonymous + if field.Tag.Get("pk") == "true" { + primary = fd + } else { + fields = append(fields, fd) + } + + selectFields = append(selectFields, fd) + filedsMap[jn] = fd + } + return &Table{ - Name: tableName, - Primary: p, - Fields: f, - AllFields: af, - FiledsMap: jf, - EntityType: reflect.ValueOf(entity).Elem().Type(), + Name: tableName, + Primary: primary, + Fields: fields, + SelectFields: selectFields, + AddFields: addFields, + UpdateFields: updateFields, + FiledsMap: filedsMap, + EntityType: typ, } } diff --git a/util.go b/util.go index 3ce51b7..4f00db4 100644 --- a/util.go +++ b/util.go @@ -10,6 +10,29 @@ import ( "reflect" ) +// Type assertions +func typeAssertion(v interface{}) interface{} { + switch v.(type) { + case []byte: + return v.([]byte) + case []rune: + return v.([]rune) + case bool: + return v.(bool) + case float64: + return v.(float64) + case int64: + return v.(int64) + case nil: + return nil + case string: + return v.(string) + default: + log.Printf("Unexpected type %#v\n", v) + return "" + } +} + // Get scan variables func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, interface{}, []interface{}, error) { typ := reflect.TypeOf(ptr) @@ -59,73 +82,3 @@ func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, return 0, nil, nil, errors.New("ptr is not a point struct, map or slice") } - -// Type assertions -func typeAssertion(v interface{}) interface{} { - switch v.(type) { - case []byte: - return v.([]byte) - case []rune: - return v.([]rune) - case bool: - return v.(bool) - case float64: - return v.(float64) - case int64: - return v.(int64) - case nil: - return nil - case string: - return v.(string) - default: - log.Printf("Unexpected type %#v\n", v) - return "" - } -} - -// Table alias -func tableAlias(alias []string) string { - if len(alias) > 0 { - return alias[0] - } - return "" -} - -// Reflect struct, construct Field slice -func tableFields(entity interface{}) (string, []string, []string, map[string]string) { - typ := reflect.Indirect(reflect.ValueOf(entity)).Type() - primary := "" - fields := make([]string, 0) - allFields := make([]string, 0) - jsonMap := make(map[string]string) - - for i := 0; i < typ.NumField(); i++ { - field := typ.Field(i) - - var fd string - if field.Tag.Get("field") != "" { - fd = field.Tag.Get("field") - } else { - fd = field.Name - } - - var jn string - if field.Tag.Get("json") != "" { - jn = field.Tag.Get("json") - } else { - jn = field.Name - } - - //!field.Anonymous - if field.Tag.Get("pk") == "true" { - primary = fd - } else { - fields = append(fields, fd) - } - - allFields = append(allFields, fd) - jsonMap[jn] = fd - } - - return primary, fields, allFields, jsonMap -}