sql

package
v3.8.18 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: Apache-2.0 Imports: 24 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLastInsertIdIsNotSupported = errors.New("LastInsertId is not supported by this driver")
	ErrRowsAffectedIsNotSupported = errors.New("RowsAffected is not supported by this driver")
	ErrNoLastInsertIdAvailable    = errors.New("no LastInsertId available after DDL statement")
	ErrNoRowsAffectedAvailable    = errors.New("no RowsAffected available after DDL statement")
)

Functions

func Acceptable added in v3.8.18

func Acceptable(err error) error

func Caller

func Caller(skip int) map[string]string

func ErrorIn added in v3.8.18

func ErrorIn(err error, errs ...error) bool

In checks if the given err is one of errs.

func LoggerCaller added in v3.8.17

func LoggerCaller(skip int) map[string]string

func RegisterInterceptor added in v3.8.17

func RegisterInterceptor(db *gorm.DB, options *Client, interceptors ...Interceptor)

Types

type Client

type Client struct {
	Host          string        // 数据库 IP
	Port          string        // 数据库端口
	Username      string        // 数据库用户名
	Password      string        // 数据库密码
	Charset       string        // 数据库字符集
	Database      string        // 数据库名称
	ParseTime     bool          // 是否解析时间
	TimeZone      string        // 数据库时区  mysql & postgresql 专用
	SslMode       bool          // 数据库SSL模式  postgresql 专用
	DialTimeOut   string        // 连接超时时间 clickhouse 专用
	ReadTimeOut   string        // 读取超时时间 clickhouse 专用
	MaxIdleConns  int           // 连接池里最大空闲连接数。必须要比maxOpenConns小
	MaxOpenConns  int           // 连接池最多同时打开的连接数
	MaxLifetime   time.Duration // 连接池里面的连接最大存活时长
	MaxIdleTime   time.Duration // 连接池里面的连接最大空闲时长
	DisableMetric bool          // 关闭指标采集
	DisableTrace  bool          // 关闭链路追踪
	DisableDebug  bool          // 关闭调试模式
	DriverName    string        // 数据库驱动名称
}

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient 创建一个新的客户端

func (*Client) ClickHouseDialector added in v3.8.17

func (c *Client) ClickHouseDialector() (gorm.Dialector, error)

clickhouse open

func (*Client) ClickHouseDsn added in v3.8.17

func (c *Client) ClickHouseDsn() string

clickhouse Dsn

func (*Client) Dialector added in v3.8.17

func (c *Client) Dialector() (gorm.Dialector, error)

Dsn 数据库连接

func (*Client) Dsn added in v3.8.17

func (c *Client) Dsn() (string, error)

func (*Client) MySqlDialector added in v3.8.17

func (c *Client) MySqlDialector() (gorm.Dialector, error)

mysql open

func (*Client) MySqlDsn added in v3.8.17

func (c *Client) MySqlDsn() string

MySql Dsn

func (*Client) Open added in v3.8.17

func (c *Client) Open() (*ClientManager, error)

Open 打开数据库连接

func (*Client) PostgreSQLDialector added in v3.8.17

func (c *Client) PostgreSQLDialector() (gorm.Dialector, error)

postgres open

func (*Client) PostgreSQLDsn added in v3.8.17

func (c *Client) PostgreSQLDsn() string

postgres Dsn

func (*Client) SqlConn added in v3.8.17

func (c *Client) SqlConn() (*gorm.DB, error)

GetConn 获取数据库连接

type ClientManager added in v3.8.17

type ClientManager struct {
	*gorm.DB
}

func (*ClientManager) Close added in v3.8.17

func (c *ClientManager) Close() error

func (*ClientManager) Gorm added in v3.8.17

func (c *ClientManager) Gorm() *gorm.DB

type Handler added in v3.8.17

type Handler func(*gorm.DB)

type Interceptor added in v3.8.17

type Interceptor func(op string, client *Client, next Handler) Handler

func MetricInterceptor added in v3.8.17

func MetricInterceptor() Interceptor

func TraceInterceptor added in v3.8.17

func TraceInterceptor() Interceptor

type Option

type Option func(r *Client)

Option 选项

func WithCharset

func WithCharset(charset string) Option

WithCharset 设置数据库字符集

func WithDatabase

func WithDatabase(database string) Option

WithDatabase 设置数据库名称

func WithDialTimeOut added in v3.8.17

func WithDialTimeOut(dialTimeOut string) Option

WithDialTimeOut 设置连接超时时间

func WithDisableDebug

func WithDisableDebug(disableDebug bool) Option

WithDisableDebug 关闭调试模式

func WithDisableMetric

func WithDisableMetric(disableMetric bool) Option

WithDisableMetric 关闭指标采集

func WithDisableTrace

func WithDisableTrace(disableTrace bool) Option

WithDisableTrace 关闭链路追踪

func WithDriverName

func WithDriverName(driverName string) Option

WithDriverName 设置数据库驱动名称

func WithHost

func WithHost(host string) Option

WithHost 设置数据库IP

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

WithMaxIdleConns 设置连接池里最大空闲连接数

func WithMaxIdleTime

func WithMaxIdleTime(maxIdleTime time.Duration) Option

WithMaxIdleTime 设置连接池里面的连接最大空闲时长

func WithMaxLifetime

func WithMaxLifetime(maxLifetime time.Duration) Option

WithMaxLifetime 设置连接池里面的连接最大存活时长

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) Option

WithMaxOpenConns 设置连接池最多同时打开的连接数

func WithParseTime

func WithParseTime(parseTime bool) Option

WithParseTime 是否解析时间

func WithPassword

func WithPassword(password string) Option

WithPassword 设置数据库密码

func WithPort

func WithPort(port string) Option

WithPort 设置数据库端口

func WithReadTimeOut added in v3.8.17

func WithReadTimeOut(readTimeOut string) Option

WithReadTimeOut 设置读取超时时间

func WithSslMode added in v3.8.17

func WithSslMode(sslMode bool) Option

WithSslMode 设置数据库SSL模式

func WithTimeZone

func WithTimeZone(timeZone string) Option

WithTimeZone 设置数据库时区

func WithUsername

func WithUsername(username string) Option

WithUsername 设置数据库用户名

type Pagination added in v3.7.25

type Pagination struct {
	Offset *int64
	Limit  *int64
}

func (*Pagination) GetLimit added in v3.7.25

func (obj *Pagination) GetLimit() int64

func (*Pagination) GetOffset added in v3.7.25

func (obj *Pagination) GetOffset() int64

type WrapEntry added in v3.8.17

type WrapEntry struct {
	Logrus                *logrus.Entry
	SlowThreshold         time.Duration
	SourceField           string
	SkipErrRecordNotFound bool
	Debug                 bool
}

func NewEntry added in v3.8.17

func NewEntry(entry *logrus.Entry) *WrapEntry

func (*WrapEntry) Error added in v3.8.17

func (l *WrapEntry) Error(ctx context.Context, s string, args ...interface{})

func (*WrapEntry) Info added in v3.8.17

func (l *WrapEntry) Info(ctx context.Context, s string, args ...interface{})

func (*WrapEntry) LogMode added in v3.8.17

func (l *WrapEntry) LogMode(level logger.LogLevel) logger.Interface

func (*WrapEntry) SetDebug added in v3.8.17

func (l *WrapEntry) SetDebug(debug bool)

func (*WrapEntry) Trace added in v3.8.17

func (l *WrapEntry) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

func (*WrapEntry) Warn added in v3.8.17

func (l *WrapEntry) Warn(ctx context.Context, s string, args ...interface{})

type WrapLogger added in v3.8.17

type WrapLogger struct {
	Logrus                *logrus.Logger
	SlowThreshold         time.Duration
	SourceField           string
	SkipErrRecordNotFound bool
	Debug                 bool
}

func NewLogger added in v3.8.17

func NewLogger(logger *logrus.Logger) *WrapLogger

func (*WrapLogger) Error added in v3.8.17

func (l *WrapLogger) Error(ctx context.Context, s string, args ...interface{})

func (*WrapLogger) Info added in v3.8.17

func (l *WrapLogger) Info(ctx context.Context, s string, args ...interface{})

func (*WrapLogger) LogMode added in v3.8.17

func (l *WrapLogger) LogMode(level logger.LogLevel) logger.Interface

func (*WrapLogger) SetDebug added in v3.8.17

func (l *WrapLogger) SetDebug(debug bool)

func (*WrapLogger) SetSkipErrRecordNotFound added in v3.8.17

func (l *WrapLogger) SetSkipErrRecordNotFound(skip bool)

func (*WrapLogger) SetSlowThreshold added in v3.8.17

func (l *WrapLogger) SetSlowThreshold(threshold time.Duration)

func (*WrapLogger) SetSourceField added in v3.8.17

func (l *WrapLogger) SetSourceField(field string)

func (*WrapLogger) Trace added in v3.8.17

func (l *WrapLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

func (*WrapLogger) Warn added in v3.8.17

func (l *WrapLogger) Warn(ctx context.Context, s string, args ...interface{})

Jump to

Keyboard shortcuts

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