-
Notifications
You must be signed in to change notification settings - Fork 1
/
orm.go
44 lines (38 loc) · 844 Bytes
/
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
package orm
import (
"fmt"
"log"
"github.com/codebdy/entify-core/db"
"github.com/codebdy/entify-core/model"
)
func DbString(cfg db.DbConfig) string {
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
cfg.User,
cfg.Password,
cfg.Host,
cfg.Port,
cfg.Database,
)
}
func Open(dbConfig db.DbConfig, m *model.Model) (*Session, error) {
dbx, err := db.Open(dbConfig.Driver, DbString(dbConfig))
if err != nil {
return nil, err
}
session := Session{
idSeed: 1,
Dbx: dbx,
model: m,
}
return &session, nil
}
func OpenWithoutRepository(dbConfig db.DbConfig) (*Session, error) {
return Open(dbConfig, nil)
}
func IsEntityExists(name string, dbConfig db.DbConfig) bool {
session, err := Open(dbConfig, nil)
if err != nil {
log.Panic(err.Error())
}
return session.doCheckEntity(name, dbConfig.Database)
}