db

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MYSQL_DSN  = "{user}:{password}@tcp({server}:{port})/{name}?{args}"                           //?charset={charset}&parseTime=True&loc=Local&timeout=10s
	PQ_DSN     = "user={user} password={password} host={server} port={port} dbname={name} {args}" // sslmode=disable TimeZone=Asia/Shanghai
	SQLITE_DSN = "{path}?{args}"                                                                  //?_pragma=busy_timeout(3000)&_pragma=journal_mode(WAL)
)

Variables

View Source
var (
	DoNothing = Clauses(clause.OnConflict{DoNothing: true})
	UpdateAll = Clauses(clause.OnConflict{UpdateAll: true})
	Conflict  = func(onConflict clause.OnConflict) Scope { return Clauses(onConflict) }
	DoUpdates = func(columns ...string) Scope {
		return Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns(columns)})
	}
)

OnConflict

View Source
var DefaultConfig = &gorm.Config{
	NamingStrategy:                           SingularTable,
	SkipDefaultTransaction:                   true,
	PrepareStmt:                              true,
	DisableForeignKeyConstraintWhenMigrating: true,
}
View Source
var SingularTable = schema.NamingStrategy{SingularTable: true}

Functions

func GormOpen

func GormOpen() (db *gorm.DB, err error)

func HumanDate

func HumanDate(date time.Time) int

func ParsePaging

func ParsePaging(index, size, defaultSize int) (skip, take int)

func ParseTimeRange

func ParseTimeRange(humanDate ...string) (s int, e int)

func RegisterDialector

func RegisterDialector(name string, dialector func(dsn string) gorm.Dialector)

func Sort

func Sort[K strs.Int | ~string](db *gorm.DB, keyCol, sortCol string, keys []K, reverse ...bool) *gorm.DB

func SortExpr

func SortExpr[K strs.Int | ~string](keys []K, reverses ...bool) clause.Expression

func SortMap

func SortMap[K strs.Int | ~string, S strs.Int](db *gorm.DB, keyCol, sortCol string, sorts map[K]S) *gorm.DB

func SortMapExpr

func SortMapExpr[K strs.Int | ~string, S strs.Int](sorts map[K]S) clause.Expr

func SortMapSimple

func SortMapSimple[K strs.Int | ~string, S strs.Int](db *gorm.DB, sorts map[K]S) *gorm.DB

满足排序字段为sort且为数值型, 单主键且为数值型

func SortSimple

func SortSimple[K strs.Int | ~string](db *gorm.DB, keys []K, reverse ...bool) *gorm.DB

满足排序字段为sort且为数值型, 单主键且为数值型

Types

type Config

type Config struct {
	Type     string `json:"type,omitempty"`     // 数据库类型
	User     string `json:"user,omitempty"`     // 用户
	Password string `json:"password,omitempty"` // 密码
	Host     string `json:"host,omitempty"`     // 服务器
	Port     int    `json:"port,omitempty"`     // 端口
	Name     string `json:"name,omitempty"`     // 数据库名
	Path     string `json:"path,omitempty"`     // 文件路径
	Args     string `json:"args,omitempty"`     // 扩展参数
	Debug    bool   `json:"debug,omitempty"`    // 调试模式
}

func DefineConfig

func DefineConfig(c Config) Config

func (Config) DSN

func (c Config) DSN() string

type LoggerWriter

type LoggerWriter bool

func (LoggerWriter) Printf

func (w LoggerWriter) Printf(format string, v ...any)

type Scope

type Scope = func(tx *gorm.DB) *gorm.DB

func Clauses

func Clauses(conds ...clause.Expression) Scope

Clauses

func CreatedDateRange

func CreatedDateRange(date ...string) Scope

func Paging

func Paging(index, size, defSize int) Scope
func Search(keywords string, columns ...string) Scope

type Table

type Table struct {
	ID        int `json:"id"`
	CreatedAt int `json:"created_at"`
}

func ID

func ID(id int) Table

func (*Table) BeforeCreate

func (m *Table) BeforeCreate(tx *gorm.DB) (err error)

func (Table) GetCreatedAt

func (m Table) GetCreatedAt() int

func (Table) GetPK

func (m Table) GetPK() int

type Tabled

type Tabled interface {
	GetPK() int
	GetCreatedAt() int
}

type Transaction

type Transaction = func(tx *gorm.DB) error

Jump to

Keyboard shortcuts

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