db

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

go-lib-db

基础开发框架数据库组件

依赖

  • github.com/go-sql-driver/mysql
  • github.com/jinzhu/gorm
  • github.com/go-xorm/xorm
  • github.com/BurntSushi/toml
  • git.qutoutiao.net/framework/autumn-lib-configuration

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGormCallbacks

func AddGormCallbacks(db *gorm.DB)

AddGormCallbacks adds callbacks for tracing, you should call SetSpanToGorm to make them work

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

----------------------------------------

数据库客户端管理器

----------------------------------------

func CreateDatabase

func CreateDatabase() *Database

创建数据库客户端管理器实例

func (*Database) GORM

func (database *Database) GORM(name string) *GORMClient

获取一个已经注册的 GORM 客户端(如果未注册将返回 nil)

func (*Database) RegisterGORMClient

func (database *Database) RegisterGORMClient(name string, config *ORMConfig) error

注册一个 GORM 客户端实例

func (*Database) RegisterGORMClients

func (database *Database) RegisterGORMClients(configs map[string]*ORMConfig) error

注册多个 GORM 客户端实例

func (*Database) RegisterORMClient

func (database *Database) RegisterORMClient(name string, config *ORMConfig) error

注册一个 ORM 客户端实例(支持 XORM 和 GORM)

func (*Database) RegisterORMClients

func (database *Database) RegisterORMClients(configs map[string]*ORMConfig) error

注册多个 ORM 客户端实例

type GORMClient

type GORMClient struct {
	// contains filtered or unexported fields
}

----------------------------------------

GORM 客户端

----------------------------------------

func CreateGORMClient

func CreateGORMClient(name string, config *ORMConfig) (*GORMClient, error)

创建 GORM 客户端实例

func (*GORMClient) Client

func (client *GORMClient) Client() *gorm.DB

获取当前 GORM 客户端的第三方客户端实例

func (*GORMClient) Close

func (client *GORMClient) Close() error

关闭远程服务连接

func (*GORMClient) Connect

func (client *GORMClient) Connect() error

连接远程服务

func (*GORMClient) Name

func (client *GORMClient) Name() string

获取当前 GORM 客户端实例名称

func (*GORMClient) Ping

func (client *GORMClient) Ping() error

检查远程服务的可用性

func (*GORMClient) Reconnect

func (client *GORMClient) Reconnect() error

重连远程服务

func (*GORMClient) SetLogger

func (client *GORMClient) SetLogger(log logger.Log)

设置日志

func (*GORMClient) WithCtx

func (client *GORMClient) WithCtx(ctx context.Context) *gorm.DB

设置上下文

type GORMClientManager

type GORMClientManager struct {
	// contains filtered or unexported fields
}

----------------------------------------

GORM 客户端管理器

----------------------------------------

func CreateGORMClientManager

func CreateGORMClientManager() *GORMClientManager

创建 GORM 客户端管理器实例

func (*GORMClientManager) Clear

func (manager *GORMClientManager) Clear()

清空 GORM 客户端实例

func (*GORMClientManager) Get

func (manager *GORMClientManager) Get(name string) *GORMClient

获取给定名称的 GORM 客户端实例(如果客户端不存在则返回 nil)

func (*GORMClientManager) Set

func (manager *GORMClientManager) Set(name string, client *GORMClient)

添加或更新 GORM 客户端实例

type ORMConfig

type ORMConfig struct {
	Engine      string `toml:"engine" mapstructure:"engine"`
	Driver      string `toml:"driver" mapstructure:"driver"`
	Host        string `toml:"host" mapstructure:"host"`
	Port        int    `toml:"port" mapstructure:"port"`
	Username    string `toml:"username" mapstructure:"username"`
	Password    string `toml:"password" mapstructure:"password"`
	Database    string `toml:"database" mapstructure:"database"`
	Charset     string `toml:"charset" mapstructure:"charset"`
	MaxConn     int    `toml:"max_conn" mapstructure:"max_conn"`
	MaxIdleConn int    `toml:"max_idle_conn" mapstructure:"max_idle_conn"`
	Ping        bool   `toml:"ping" mapstructure:"ping"`
}

----------------------------------------

ORM 配置项(TOML 支持, Viper 支持)

----------------------------------------

func (*ORMConfig) Bind

func (config *ORMConfig) Bind(bs []byte) error

绑定数据到当前 ORM 配置清单

func (*ORMConfig) Copy

func (config *ORMConfig) Copy() *ORMConfig

获取当前 ORM 配置项的副本

type ORMConfigs

type ORMConfigs struct {
	Items map[string]*ORMConfig `toml:"orm"`
}

----------------------------------------

ORM 配置集合(TOML 支持)

----------------------------------------

func (*ORMConfigs) Bind

func (configs *ORMConfigs) Bind(bs []byte) error

绑定数据到当前 ORM 配置清单

Jump to

Keyboard shortcuts

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