mysql

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

Mysql

package main

import (
    "context"

    "github.com/joshqu1985/fireman/configor"
    "github.com/joshqu1985/fireman/store/mysql"
)

type Config struct {
    Mysql    []mysql.Config
}

var (
    Conf Config
)

func init() {
    if err := configor.Load("./configs/conf.toml", &Conf); err != nil {
        panic(err)
    }
}

type Account struct {
    Uid        string `json:"uid"`
    Phone      string `json:"phone"`
}

func main() {
    pool := mysql.NewPool(Conf.Mysql)

    item := &Account{}
    handle := func(orm *mysql.DB) error {
        return orm.Where("uid=?", uid).Find(item).Error
    }
    err := pool.Slave("accounts").Doit(context.Background(), handle)

    fmt.Prinln(item, err)
}
#conf.toml
[[mysql]]
    name = "uauth"
    opts = "charset=utf8mb4"
    [mysql.master]
        host     = "127.0.0.1:3306"
        auth     = "root:"
        max_idle = 5
        max_open = 30
        max_life = 600
    [[mysql.slave]]
        host     = "127.0.0.1:3306"
        auth     = "root:"
        max_idle = 5
        max_open = 30
        max_life = 600

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRecordNotFound = errors.New("record not found")

Functions

This section is empty.

Types

type Client

type Client struct {
	*gorm.DB
}

func (*Client) Doit

func (c *Client) Doit(ctx context.Context, h func(*gorm.DB) error) error

type Config

type Config struct {
	Name   string
	Opts   string
	Master ConfigNode
	Slaves []ConfigNode
}

type ConfigNode

type ConfigNode struct {
	Host    string
	Auth    string
	MaxIdle int `toml:"max_idle"`
	MaxOpen int `toml:"max_open"`
	MaxLife int `toml:"max_life"`
}

type DB

type DB = gorm.DB

type Pool

type Pool struct {
	Instances map[string]dbInstance
}

func NewPool

func NewPool(confs []Config) *Pool

func (*Pool) Master

func (p *Pool) Master(dbname string) *Client

func (*Pool) Slave

func (p *Pool) Slave(dbname string) *Client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL