forked from zileyuan/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.go
85 lines (68 loc) · 1.95 KB
/
access.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
package goflow
import (
"fmt"
"reflect"
"time"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
)
var orm *xorm.Engine
//初始化数据库ORM引擎
func InitAccessByXorm(xorm *xorm.Engine) {
orm = xorm
//orm.DumpAllToFile("./db_struct.sql")
err := orm.Sync2(new(HistoryOrder), new(HistoryTask), new(HistoryTaskActor),
new(Order), new(Process), new(Surrogate), new(Task), new(TaskActor), new(CCOrder))
PanicIf(err, "fail to sync tables")
}
//初始化数据库ORM引擎
func InitAccessByConfig(cfg string) {
InitConfig(cfg)
if orm == nil {
flowlog.Info(DbDriverConnstr)
//connString := fmt.Sprintf(DbDriverConnstr, DbUsername, DbPassword,
// DbServer, DbPort, DbDatebase)
connString := DbDriverConnstr
flowlog.Info(connString)
var err error
orm, err = xorm.NewEngine(DbDriver, connString)
fmt.Printf(connString)
PanicIf(err, "fail to init engine")
orm.SetMaxIdleConns(MaxIdleConns)
orm.SetMaxOpenConns(MaxOpenConns)
orm.TZLocation = time.Local
orm.ShowSQL(false)
tbMapper := core.NewPrefixMapper(core.SameMapper{}, "GF_")
orm.SetTableMapper(tbMapper)
orm.SetColumnMapper(core.SameMapper{})
InitAccessByXorm(orm)
}
}
//保存实体对象
func Save(inf interface{}, id interface{}) {
_, err := orm.InsertOne(inf)
t := reflect.TypeOf(inf)
PanicIf(err, "fail to insert %v %v", t, id)
flowlog.Infof("%v %v inserted", t, id)
}
//更新实体对象
func Update(inf interface{}, id interface{}) {
_, err := orm.Id(id).Update(inf)
t := reflect.TypeOf(inf)
PanicIf(err, "fail to update %v %v", t, id)
flowlog.Infof("%v %v updated", t, id)
}
//删除实体对象
func Delete(inf interface{}, id interface{}) {
_, err := orm.Id(id).Delete(inf)
t := reflect.TypeOf(inf)
PanicIf(err, "fail to delete %v %v", t, id)
flowlog.Infof("%v %v deleted", t, id)
}
//删除实体对象
func DeleteObj(inf interface{}) {
_, err := orm.Delete(inf)
t := reflect.TypeOf(inf)
PanicIf(err, "fail to delete %v", t)
flowlog.Infof("%v deleted", t)
}