sql

package
v3.8.37 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: Apache-2.0 Imports: 28 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MySQLDriverName      = "mysql"
	ClickHouseDriverName = "clickhouse"
	PostgreSQLDriverName = "postgres"
	TDengineDriverName   = "tdengine"
)

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 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 // 连接池里面的连接最大空闲时长
	EnableMetric   bool          // 开启指标采集
	EnableTrace    bool          // 开启链路追踪
	EnableDebug    bool          // 关闭调试模式
	ConnectionMode string        // tdengine 连接模式(native/rest/websocket)
	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 获取数据库连接

func (*Client) TDengineDialector added in v3.8.37

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

mysql open

func (*Client) TDengineDsn added in v3.8.37

func (c *Client) TDengineDsn() string

MySql Dsn

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 WithDriverName

func WithDriverName(driverName string) Option

WithDriverName 设置数据库驱动名称

func WithEnableDebug added in v3.8.21

func WithEnableDebug(disableDebug bool) Option

WithEnableDebug 关闭调试模式

func WithEnableMetric added in v3.8.21

func WithEnableMetric(disableMetric bool) Option

WithEnableMetric 开启指标采集

func WithEnableTrace added in v3.8.21

func WithEnableTrace(disableTrace bool) Option

WithEnableTrace 开启链路追踪

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, error)

func (*Pagination) GetOffset added in v3.7.25

func (obj *Pagination) GetOffset() (int64, error)

type Scope added in v3.8.21

type Scope struct {
	Start *string
	End   *string
}

func (*Scope) GetEndFloat64 added in v3.8.21

func (obj *Scope) GetEndFloat64() (float64, error)

func (*Scope) GetEndInt64 added in v3.8.21

func (obj *Scope) GetEndInt64() (int64, error)

func (*Scope) GetEndString added in v3.8.21

func (obj *Scope) GetEndString() (string, error)

func (*Scope) GetStartFloat64 added in v3.8.21

func (obj *Scope) GetStartFloat64() (float64, error)

func (*Scope) GetStartInt64 added in v3.8.21

func (obj *Scope) GetStartInt64() (int64, error)

func (*Scope) GetStartString added in v3.8.21

func (obj *Scope) GetStartString() (string, error)

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 ...any)

func (*WrapEntry) Info added in v3.8.17

func (l *WrapEntry) Info(ctx context.Context, s string, args ...any)

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 ...any)

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 ...any)

func (*WrapLogger) Info added in v3.8.17

func (l *WrapLogger) Info(ctx context.Context, s string, args ...any)

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 ...any)

Jump to

Keyboard shortcuts

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