Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Aug 18, 2015
1 parent 5ad35bc commit bd430dc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 81 deletions.
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
59 changes: 49 additions & 10 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
93 changes: 23 additions & 70 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit bd430dc

Please sign in to comment.