123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package mysql
- import (
- _ "github.com/go-sql-driver/mysql"
- "github.com/jinzhu/gorm"
- )
- type GormClient struct {
- master *gorm.DB
- slave *gorm.DB
- }
- func NewClient() *GormClient {
- return &GormClient{}
- }
- var Client *GormClient
- func Dial(dialect string, args ...interface{}) (*GormClient, error) {
- Client = NewClient()
- return Client.Dial(dialect, args...)
- }
- func (c *GormClient) Dial(dialect string, args ...interface{}) (*GormClient, error) {
- var err error
- // master dial
- c.master, err = gorm.Open(dialect, args...)
- if err != nil {
- return c, err
- }
- c.master.LogMode(true)
- c.master.Debug()
- // slave dial
- c.slave, err = gorm.Open(dialect, args...)
- if err != nil {
- return c, err
- }
- c.slave.LogMode(true)
- return c, nil
- }
- func (c *GormClient) Master() *gorm.DB {
- return c.master
- }
- func (c *GormClient) Slave() *gorm.DB {
- return c.slave
- }
|