diff --git a/model.go b/model.go index 62dee27..3f550c8 100644 --- a/model.go +++ b/model.go @@ -12,12 +12,13 @@ type Model struct { Module string // table instance - Table Table + Table *Table } // Insert func (m *Model) Insert() *Query { q := NewQuery(Servers[m.Module]) + q.Table = m.Table q.InsertInto(m.Table.Name) return q } @@ -25,6 +26,7 @@ func (m *Model) Insert() *Query { // Update func (m *Model) Update() *Query { q := NewQuery(Servers[m.Module]) + q.Table = m.Table q.Update(m.Table.Name) return q } @@ -32,6 +34,7 @@ func (m *Model) Update() *Query { // Delete func (m *Model) Delete() *Query { q := NewQuery(Servers[m.Module]) + q.Table = m.Table q.DeleteFrom(m.Table.Name) return q } @@ -43,12 +46,13 @@ func (m *Model) Select(f ...string) *Query { } q := NewQuery(Servers[m.Module]) + q.Table = m.Table q.Select(f...) q.From(m.Table.Name) return q } // New Model -func NewModel(module string, table Table) Model { +func NewModel(module string, table *Table) Model { return Model{Module: module, Table: table} } diff --git a/query.go b/query.go index 09fc55a..ab219d1 100644 --- a/query.go +++ b/query.go @@ -49,6 +49,9 @@ type Query struct { //args index ArgIndex int + // If the query object is created by Model, this is will be assigned. optional. + Table *Table + // Current Sql node current string } @@ -260,6 +263,13 @@ func (q *Query) quoteField(f string) string { if fs == "*" || fs == "1" { return f } + + if q.Table != nil { + if fm, ok := q.Table.FiledsMap[fs]; ok { + f = fm + } + } + return fmt.Sprintf("\"%s\"", strings.Replace(f, ".", "\".\"", -1)) } diff --git a/table.go b/table.go index d528ffb..60dbcce 100644 --- a/table.go +++ b/table.go @@ -22,18 +22,22 @@ type Table struct { // All fields AllFields []string + // json and field property map + FiledsMap map[string]string + // Entity EntityType reflect.Type } // New Table -func NewTable(tableName string, entity interface{}) Table { - p, f, af := tableFields(entity) - t := Table{ +func NewTable(tableName string, entity interface{}) *Table { + p, f, af, jf := tableFields(entity) + return &Table{ Name: tableName, Primary: p, Fields: f, AllFields: af, - EntityType: reflect.ValueOf(entity).Elem().Type()} - return t + FiledsMap: jf, + EntityType: reflect.ValueOf(entity).Elem().Type(), + } } diff --git a/util.go b/util.go index 35512a4..3ce51b7 100644 --- a/util.go +++ b/util.go @@ -12,13 +12,11 @@ import ( // Get scan variables func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, interface{}, []interface{}, error) { - typ := reflect.ValueOf(ptr).Type() - + typ := reflect.TypeOf(ptr) if typ.Kind() != reflect.Ptr { return 0, nil, nil, errors.New("ptr is not a pointer") } - //log.Printf("%s\n", dataType.Elem().Kind()) elemTyp := typ.Elem() if isRows { // Rows @@ -34,8 +32,6 @@ func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, // element(value) is point to row scan := make([]interface{}, columnsLen) - //log.Printf("%s\n", elemKind) - if elemKind == reflect.Struct { if columnsLen != elemTyp.NumField() { return 0, nil, nil, errors.New("columnsLen is not equal elemTyp.NumField()") @@ -44,7 +40,7 @@ func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, row := reflect.New(elemTyp) // Data for i := 0; i < columnsLen; i++ { f := elemTyp.Field(i) - if !f.Anonymous { // && f.Tag.Get("json") != "" + if !f.Anonymous { // && f.Tag.Get("field") != "" scan[i] = row.Elem().FieldByIndex([]int{i}).Addr().Interface() } } @@ -96,30 +92,40 @@ func tableAlias(alias []string) string { } // Reflect struct, construct Field slice -func tableFields(entity interface{}) (string, []string, []string) { +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 name string + + 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") != "" { - name = field.Tag.Get("json") + jn = field.Tag.Get("json") } else { - name = field.Name + jn = field.Name } //!field.Anonymous if field.Tag.Get("pk") == "true" { - primary = name + primary = fd } else { - fields = append(fields, name) + fields = append(fields, fd) } - allFields = append(allFields, name) + allFields = append(allFields, fd) + jsonMap[jn] = fd } - return primary, fields, allFields + return primary, fields, allFields, jsonMap }