Skip to content

Commit

Permalink
cache snake, upper values
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 17, 2013
1 parent 8fd8604 commit 9a1c0d9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,8 @@ db.Where("email = ?", "[email protected]").Attrs(User{RegisteredIp: "111.111.111.111
```

## TODO
* Rows, Row
* Cache Stmt for performance
* Join, Having, Group, Includes
* Scopes, Valiations
* AlertColumn, DropColumn, AddIndex, RemoveIndex
Expand Down
2 changes: 1 addition & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func parseSqlTag(str string) (typ string, addational_typ string, size int) {
m := make(map[string]string)
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.Trim(strings.ToUpper(v[0]), " ")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) == 2 {
m[k] = v[1]
} else {
Expand Down
4 changes: 2 additions & 2 deletions gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ func TestQueryChain(t *testing.T) {
}

func BenchmarkGorm(b *testing.B) {
b.N = 2000
b.N = 5000
for x := 0; x < b.N; x++ {
e := strconv.Itoa(x) + "[email protected]"
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: time.Now()}
Expand All @@ -1388,7 +1388,7 @@ func BenchmarkRawSql(b *testing.B) {
update_sql := "UPDATE emails SET email = $1, updated_at = $2 WHERE id = $3"
delete_sql := "DELETE FROM orders WHERE id = $1"

b.N = 2000
b.N = 5000
for x := 0; x < b.N; x++ {
var id int64
e := strconv.Itoa(x) + "[email protected]"
Expand Down
11 changes: 1 addition & 10 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func (m *Model) primaryKeyDb() string {
}

func (m *Model) fields(operation string) (fields []*Field) {
if len(m._cache_fields[operation]) > 0 {
return m._cache_fields[operation]
}

indirect_value := m.reflectData()
if !indirect_value.IsValid() {
return
Expand Down Expand Up @@ -79,14 +75,10 @@ func (m *Model) fields(operation string) (fields []*Field) {
field.structField = p
field.reflectValue = value
field.Value = value.Interface()
field.parseAssociation()
fields = append(fields, &field)
}
}

if len(m._cache_fields) == 0 {
m._cache_fields = map[string][]*Field{}
}
m._cache_fields[operation] = fields
return
}

Expand Down Expand Up @@ -225,7 +217,6 @@ func (m *Model) setValueByColumn(name string, value interface{}, out interface{}

func (m *Model) beforeAssociations() (fields []*Field) {
for _, field := range m.fields("null") {
field.parseAssociation()
if field.beforeAssociation && !field.isBlank() {
fields = append(fields, field)
}
Expand Down
30 changes: 26 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,48 @@ import (
"time"
)

func toSnake(s string) string {
var toSnakeMap map[string]string
var toUpperMap map[string]string

func init() {
toSnakeMap = map[string]string{}
toUpperMap = map[string]string{}
}

func toSnake(u string) string {
if v := toSnakeMap[u]; v != "" {
return v
}

buf := bytes.NewBufferString("")
for i, v := range s {
for i, v := range u {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
return strings.ToLower(buf.String())

s := strings.ToLower(buf.String())
toSnakeMap[u] = s
return s
}

func snakeToUpperCamel(s string) string {
if v := toUpperMap[s]; v != "" {
return v
}

buf := bytes.NewBufferString("")
for _, v := range strings.Split(s, "_") {
if len(v) > 0 {
buf.WriteString(strings.ToUpper(v[:1]))
buf.WriteString(v[1:])
}
}
return buf.String()

u := buf.String()
toUpperMap[s] = u
return u
}

func toSearchableMap(attrs ...interface{}) (result interface{}) {
Expand Down

0 comments on commit 9a1c0d9

Please sign in to comment.