/
models.go
113 lines (90 loc) · 2.53 KB
/
models.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package models
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
//_ "github.com/lib/pq"
"xorm.io/core"
"xorm.io/xorm"
)
// Engine represents a XORM engine or session.
type Engine interface {
Table(tableNameOrBean interface{}) *xorm.Session
Count(...interface{}) (int64, error)
Decr(column string, arg ...interface{}) *xorm.Session
Delete(interface{}) (int64, error)
Exec(...interface{}) (sql.Result, error)
Find(interface{}, ...interface{}) error
Get(interface{}) (bool, error)
ID(interface{}) *xorm.Session
In(string, ...interface{}) *xorm.Session
Incr(column string, arg ...interface{}) *xorm.Session
Insert(...interface{}) (int64, error)
InsertOne(interface{}) (int64, error)
Iterate(interface{}, xorm.IterFunc) error
Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *xorm.Session
SQL(interface{}, ...interface{}) *xorm.Session
Where(interface{}, ...interface{}) *xorm.Session
Asc(colNames ...string) *xorm.Session
}
var (
x *xorm.Engine
_driverName, _dataSourceName string
tables []interface{}
)
func init() {
tables = append(tables,
new(Admin), new(Api), new(ApiAuditWhitelist), new(ApiCookie),
new(ApiGroup), new(ApiGroupWhitelist), new(ApiVersion), new(ApiVersionAppendixAudit),
new(AppCategory),
new(Developer),
new(GwConf),
new(InnerApp), new(InnerUser),
new(User), new(UserBasicInfo),
)
fmt.Printf("--------------------------------\n")
fmt.Printf("total tables: %d\n", len(tables))
fmt.Printf("--------------------------------\n")
gonicNames := []string{"SSL"}
for _, name := range gonicNames {
core.LintGonicMapper[name] = true
}
}
func checkDriverSupport(d string) bool {
if d == "postgres" || d == "mysql" {
return true
}
return false
}
func InitDb(driverName, dataSourceName string) (err error) {
if !checkDriverSupport(driverName) {
return errors.New("unsupported db driver")
}
_driverName = driverName
_dataSourceName = dataSourceName
x, err = getEngine(driverName, dataSourceName)
if err != nil {
return err
}
err = x.Ping()
if err != nil {
return errors.New("pingerr:" + err.Error())
}
x.SetMapper(core.GonicMapper{})
x.SetLogger(xorm.NewSimpleLogger(os.Stdout))
x.SetLogLevel(core.LOG_DEBUG)
x.ShowSQL(true)
x.SetTZLocation(time.Local)
if err = x.Sync2(tables...); err != nil {
log.Printf("xorm Sync2 err:%s", err.Error())
return err
}
return nil
}
func getEngine(driverName, dataSourceName string) (*xorm.Engine, error) {
return xorm.NewEngine(driverName, dataSourceName)
}