Skip to content

Commit

Permalink
Improve orm feture.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Aug 18, 2015
1 parent 5d32e88 commit 5ad35bc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
8 changes: 6 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@ 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
}

// Update
func (m *Model) Update() *Query {
q := NewQuery(Servers[m.Module])
q.Table = m.Table
q.Update(m.Table.Name)
return q
}

// Delete
func (m *Model) Delete() *Query {
q := NewQuery(Servers[m.Module])
q.Table = m.Table
q.DeleteFrom(m.Table.Name)
return q
}
Expand All @@ -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}
}
10 changes: 10 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}

Expand Down
14 changes: 9 additions & 5 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
34 changes: 20 additions & 14 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()")
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit 5ad35bc

Please sign in to comment.