db

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DriverNameMysql      = "mysql"      //数据库类型标识:mysql
	DriverNamePostgres   = "postgres"   //数据库类型标识:postgres sql
	DriverNameSqlite     = "sqlite"     //数据库类型标识:sqlite
	DriverNameSqlserver  = "sqlserver"  //数据库类型标识:sqlserver
	DriverNameClickhouse = "clickhouse" //数据库类型标识:clickhouse

)

数据库类型标识

Variables

This section is empty.

Functions

func InitDB

func InitDB(conf Conf)

InitDB 初始化数据库连接

func NewFreshDB

func NewFreshDB(conf Conf) (rInstance, wInstance *gorm.DB)

NewFreshDB 实例化全新的数据库链接

直接返回原生的连接实例

rInstance为读实例,wInstance为写实例

Types

type ClickhouseConf

type ClickhouseConf struct {
	Read  ClickhouseConfItem `yaml:"read" toml:"read" json:"read" default:"localhost"`    //读实例
	Write ClickhouseConfItem `yaml:"write" toml:"write" json:"write" default:"localhost"` //写实例
}

type ClickhouseConfItem

type ClickhouseConfItem struct {
	Host         string `yaml:"host" toml:"host" json:"host" default:"localhost"`                     //主机地址
	Port         int    `yaml:"port" toml:"port" json:"port" default:"9000"`                          //端口
	Username     string `yaml:"username" toml:"username" json:"username"`                             //用户名
	Password     string `yaml:"password" toml:"password" json:"password"`                             //密码
	Database     string `yaml:"database" toml:"database" json:"database"`                             //数据库名
	ReadTimeout  int    `yaml:"read_timeout" toml:"read_timeout" json:"read_timeout" default:"20"`    //读取超时时间
	WriteTimeout int    `yaml:"write_timeout" toml:"write_timeout" json:"write_timeout" default:"20"` //写入超时时间
}

ClickhouseConfItem clickhouse配置

type Conf

type Conf struct {
	DriverName     string             `yaml:"driver_name" toml:"driver_name" json:"driver_name" default:"mysql"`    //数据库类型
	AutoMigrate    bool               `yaml:"auto_migrate" toml:"auto_migrate" json:"auto_migrate" default:"false"` //是否自动同步表结构
	LogLevel       string             `yaml:"log_level" toml:"log_level" json:"log_level" default:"info"`           //日志级别
	ConnectionPool ConnectionPoolConf `yaml:"connection_pool" toml:"connection_pool" json:"connection_pool"`        //连接池配置
	Mysql          MysqlConf          `yaml:"mysql" toml:"mysql" json:"mysql"`                                      //mysql配置
	Postgres       PostgresConf       `yaml:"postgres" toml:"postgres" json:"postgres"`                             //postgres配置
	Sqlserver      SqlserverConf      `yaml:"sqlserver" toml:"sqlserver" json:"sqlserver"`                          //sqlserver配置
	Sqlite         SqliteConf         `yaml:"sqlite" toml:"sqlite" json:"sqlite"`                                   //sqlite配置
	Clickhouse     ClickhouseConf     `yaml:"clickhouse" toml:"clickhouse" json:"clickhouse"`                       //clickhouse配置
}

Conf 配置信息

func (Conf) GenDialector

func (conf Conf) GenDialector() (gorm.Dialector, gorm.Dialector)

GenDialector 生成数据库连接方言(驱动)

type ConnectionPoolConf

type ConnectionPoolConf struct {
	MaxOpenConnCount       int `yaml:"max_open_conn_count" toml:"max_open_conn_count" json:"max_open_conn_count" default:"100"`                     //最大开启连接数
	MaxIdleConnCount       int `yaml:"max_idle_conn_count" toml:"max_idle_conn_count" json:"max_idle_conn_count" default:"10"`                      //最大闲置数量
	ConnMaxLifeTimeMinutes int `yaml:"conn_max_life_time_minutes" toml:"conn_max_life_time_minutes" json:"conn_max_life_time_minutes" default:"30"` //连接最大存活时间(分钟)
	ConnMaxIdleTimeMinutes int `yaml:"conn_max_idle_time_minutes" toml:"conn_max_idle_time_minutes" json:"conn_max_idle_time_minutes" default:"10"` //连接最大空闲时间(分钟)
}

ConnectionPoolConf 连接池配置

type Instance

type Instance struct {
	R *gorm.DB //读实例
	W *gorm.DB //写实例
}

func GetInstance

func GetInstance() *Instance

GetInstance 获取数据库实例

type MysqlConf

type MysqlConf struct {
	Read  MysqlConfItem `yaml:"read" toml:"read" json:"read" default:"localhost"`    //读实例
	Write MysqlConfItem `yaml:"write" toml:"write" json:"write" default:"localhost"` //写实例
}

type MysqlConfItem

type MysqlConfItem struct {
	Host      string `yaml:"host" toml:"host" json:"host" default:"localhost"`           //主机地址
	Port      int    `yaml:"port" toml:"port" json:"port" default:"3306"`                //端口
	Username  string `yaml:"username" toml:"username" json:"username"`                   //用户名
	Password  string `yaml:"password" toml:"password" json:"password"`                   //密码
	Database  string `yaml:"database" toml:"database" json:"database"`                   //数据库名
	Charset   string `yaml:"charset" toml:"charset" json:"charset"`                      //字符集
	ParseTime bool   `yaml:"parseTime" toml:"parseTime" json:"parseTime" default:"true"` //是否解析时间
	Loc       string `yaml:"loc" toml:"loc" json:"loc" default:"Local"`                  //位置
}

MysqlConfItem mysql配置

type PostgresConf

type PostgresConf struct {
	Read  PostgresConfItem `yaml:"read" toml:"read" json:"read" default:"localhost"`    //读实例
	Write PostgresConfItem `yaml:"write" toml:"write" json:"write" default:"localhost"` //写实例
}

type PostgresConfItem

type PostgresConfItem struct {
	Host     string `yaml:"host" toml:"host" json:"host" default:"localhost"`                 //主机地址
	Port     int    `yaml:"port" toml:"port" json:"port" default:"9920"`                      //端口
	Username string `yaml:"username" toml:"username" json:"username"`                         //用户名
	Password string `yaml:"password" toml:"password" json:"password"`                         //密码
	Database string `yaml:"database" toml:"database" json:"database"`                         //数据库名
	SSLMode  string `yaml:"ssl_mode" toml:"ssl_mode" json:"ssl_mode"`                         //ssl模式 enable|disable
	TimeZone string `yaml:"timezone" toml:"timezone" json:"timezone" default:"Asia/Shanghai"` //时区
}

PostgresConfItem postgres配置

type SqliteConf

type SqliteConf struct {
	Read  SqliteConfItem `yaml:"read" toml:"read" json:"read" default:"localhost"`    //读实例
	Write SqliteConfItem `yaml:"write" toml:"write" json:"write" default:"localhost"` //写实例
}

type SqliteConfItem

type SqliteConfItem struct {
	File string `yaml:"file" toml:"file" json:"file" default:"sqlite.db"` //数据库文件
}

SqliteConfItem sqlite配置

type SqlserverConf

type SqlserverConf struct {
	Read  SqlserverConfItem `yaml:"read" toml:"read" json:"read" default:"localhost"`    //读实例
	Write SqlserverConfItem `yaml:"write" toml:"write" json:"write" default:"localhost"` //写实例
}

type SqlserverConfItem

type SqlserverConfItem struct {
	Host     string `yaml:"host" toml:"host" json:"host" default:"localhost"` //主机地址
	Port     int    `yaml:"port" toml:"port" json:"port" default:"9930"`      //端口
	Username string `yaml:"username" toml:"username" json:"username"`         //用户名
	Password string `yaml:"password" toml:"password" json:"password"`         //密码
	Database string `yaml:"database" toml:"database" json:"database"`         //数据库名
}

SqlserverConfItem sqlserver配置

type ZapLoggerForGorm

type ZapLoggerForGorm struct {
	ZapLogger                 *zap.Logger
	LogLevel                  gormLogger.LogLevel
	SlowThreshold             time.Duration
	SkipCallerLookup          bool
	IgnoreRecordNotFoundError bool
}

func NewZapLoggerForGorm

func NewZapLoggerForGorm(zapLogger *zap.Logger, conf Conf) ZapLoggerForGorm

func (ZapLoggerForGorm) Error

func (zg ZapLoggerForGorm) Error(_ context.Context, tpl string, v ...interface{})

func (ZapLoggerForGorm) Info

func (zg ZapLoggerForGorm) Info(_ context.Context, tpl string, v ...interface{})

func (ZapLoggerForGorm) LogMode

func (ZapLoggerForGorm) SetAsDefault

func (zg ZapLoggerForGorm) SetAsDefault()

func (ZapLoggerForGorm) Trace

func (zg ZapLoggerForGorm) Trace(_ context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

func (ZapLoggerForGorm) Warn

func (zg ZapLoggerForGorm) Warn(_ context.Context, tpl string, v ...interface{})

Jump to

Keyboard shortcuts

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