-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.go
99 lines (78 loc) · 1.86 KB
/
orm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package orm
import (
"database/sql"
// "fmt"
// "github.com/elago/webapp/model"
_ "github.com/go-sql-driver/mysql"
"github.com/gogather/com/log"
"reflect"
)
var typeMap map[string]interface{}
func init() {
var err error
db, err = sql.Open("mysql", "root:lijun@tcp(127.0.0.1:3306)/blog")
if err != nil {
log.Warnln(err)
}
db.SetMaxOpenConns(2000)
db.SetMaxIdleConns(1000)
db.Ping()
models = make(map[string]*Model)
// data := query("users", 8)
// log.Pinkln(data)
}
// query the bean
func Get(object interface{}) error {
tableName := camel2Snake(getTypeName(object))
val := reflect.ValueOf(object).Elem()
fid := val.FieldByName("Id")
id := fid.Int()
rows, err := db.Query("SELECT * FROM `"+tableName+"` where id=? limit 1", id)
defer rows.Close()
if err != nil {
return err
}
columns, _ := rows.Columns()
scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns))
for j := range values {
scanArgs[j] = &values[j]
}
for rows.Next() {
err = rows.Scan(scanArgs...)
for i, col := range values {
if col != nil {
fieldName := snake2Camel(columns[i])
f := val.FieldByName(fieldName)
if f.IsValid() {
assignField(&f, assertType(col))
}
}
}
}
object = val.Interface()
return nil
}
func RegisterModel(model interface{}) {
value := reflect.ValueOf(model)
indir := reflect.Indirect(value)
typ := indir.Type()
m := &Model{}
m.typ = typ
m.fields = make(map[string]Field)
for i := 0; i < indir.NumField(); i++ {
var f Field
f.sqlType = typ.Field(i).Type
sqlField := camel2Snake(typ.Field(i).Name)
m.fields[sqlField] = f
}
// log.Pinkln(m)
// log.Pinkf("put [%s] into [%s] \n", m.name, m)
models[camel2Snake(typ.Name())] = m
}
func getTypeName(model interface{}) string {
value := reflect.ValueOf(model)
indir := reflect.Indirect(value)
typ := indir.Type()
return typ.Name()
}