db

package
v0.0.0-...-a0fa9d7 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogLevelSilent = "silent"
	LogLevelError  = "error"
	LogLevelWarn   = "warn"
	LogLevelInfo   = "info"
)
View Source
const MysqlDriver = "mysql"
View Source
const PostgresDriver = "postgres"
View Source
const SqliteDriver = "sqlite"

Variables

This section is empty.

Functions

func CreateDB

func CreateDB(logger log.Logger, config Config) error

func DropDB

func DropDB(logger log.Logger, config Config) error

func GetMigrationsDriver

func GetMigrationsDriver(name string) (source.Driver, error)

func RegisterDriver

func RegisterDriver(name string, driver Driver)

func RegisterMigrationsDriver

func RegisterMigrationsDriver(name string, driver source.Driver)

func SourceDesensitization

func SourceDesensitization(source string) string

Types

type Config

type Config struct {
	Driver          string        `yaml:"driver" mapstructure:"driver"`
	Source          string        `yaml:"source" mapstructure:"source"`
	ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" mapstructure:"conn_max_idle_time"`
	ConnMaxLifeTime time.Duration `yaml:"conn_max_life_time" mapstructure:"conn_max_life_time"`
	MaxIdleConn     int           `yaml:"max_idle_conn" mapstructure:"max_idle_conn"`
	MaxOpenConn     int           `yaml:"max_open_conn" mapstructure:"max_open_conn"`
	LogLevel        string        `yaml:"log_level" mapstructure:"log_level"`

	EnableMetric          bool          `yaml:"enable_metric" mapstructure:"enable_metric"`
	RefreshMetricInterval time.Duration `yaml:"refresh_metric_interval" mapstructure:"refresh_metric_interval"`
}

func NewDefConfig

func NewDefConfig() Config

func (Config) GetDatabaseName

func (c Config) GetDatabaseName() string

func (Config) GetLogLevel

func (c Config) GetLogLevel() logger.LogLevel

func (Config) MarshalJSON

func (c Config) MarshalJSON() ([]byte, error)

func (Config) MarshalYAML

func (c Config) MarshalYAML() (any, error)

func (Config) String

func (c Config) String() string

func (Config) Validate

func (c Config) Validate() error

type DB

type DB interface {
	Model(value any) DB
	Where(query any, args ...any) DB
	Limit(limit int) DB
	Scopes(funcs ...func(DB) DB) DB
	Offset(offset int) DB
	Order(value any) DB
	Count(count *int64) DB
	Group(query string) DB
	RowsAffected(number int64) DB

	Select(query any, args ...any) DB
	Distinct(args ...any) DB
	Find(dest any, conds ...any) (err error)
	First(dest any, conds ...any) (found bool, err error)
	MustFirst(dest any, conds ...any) (err error)

	Exec(sql string, values ...any) error

	Create(value any) error
	Update(column string, value any) error
	Updates(values any) error

	Transaction(fn func(tx DB) error) error

	Begin() DB
	Commit() error
	Rollback() error

	AutoMigrate(dst ...any) error

	GetSource() string
	GetDriver() Driver
	Close() error

	WithContext(ctx context.Context) DB
	WithLogger(l log.Logger) DB
}

func NewDB

func NewDB(_ context.Context, l log.Logger, config Config) (DB, error)

func NewMemoryDB

func NewMemoryDB(logLevel, name string) DB

type Driver

type Driver interface {
	Open(source string) gorm.Dialector
	ParseSource(source string) error
	GetDatabaseName(source string) string
	CreateDB(logger log.Logger, config Config) error
	DropDB(logger log.Logger, config Config) error
	MigrateOptions() map[string]string
	GetMigrationsDriver() (source.Driver, error)
	ToMigrateDriver(source string) (string, database.Driver, error)
}

func GetDriver

func GetDriver(name string) (Driver, error)

type Mysql

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

func (*Mysql) CreateDB

func (m *Mysql) CreateDB(logger log.Logger, config Config) error

func (*Mysql) DropDB

func (m *Mysql) DropDB(logger log.Logger, config Config) error

func (*Mysql) GetDatabaseName

func (m *Mysql) GetDatabaseName(source string) string

func (*Mysql) GetMigrationsDriver

func (*Mysql) GetMigrationsDriver() (source.Driver, error)

func (*Mysql) MigrateOptions

func (*Mysql) MigrateOptions() map[string]string

func (*Mysql) Open

func (*Mysql) Open(source string) gorm.Dialector

func (*Mysql) ParseSource

func (m *Mysql) ParseSource(source string) error

func (*Mysql) ToMigrateDriver

func (m *Mysql) ToMigrateDriver(source string) (string, database.Driver, error)

type Sqlite

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

func (*Sqlite) CreateDB

func (*Sqlite) CreateDB(logger log.Logger, config Config) error

func (*Sqlite) DropDB

func (*Sqlite) DropDB(logger log.Logger, config Config) error

func (*Sqlite) GetDatabaseName

func (s *Sqlite) GetDatabaseName(source string) string

func (*Sqlite) GetMigrationsDriver

func (*Sqlite) GetMigrationsDriver() (source.Driver, error)

func (*Sqlite) MigrateOptions

func (*Sqlite) MigrateOptions() map[string]string

func (*Sqlite) Open

func (*Sqlite) Open(source string) gorm.Dialector

func (*Sqlite) ParseSource

func (s *Sqlite) ParseSource(source string) error

func (*Sqlite) ToMigrateDriver

func (*Sqlite) ToMigrateDriver(source string) (string, database.Driver, error)

Jump to

Keyboard shortcuts

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