db

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	MYSQL_DSN  = "{user}:{password}@tcp({server}:{port})/{name}?charset={charset}&parseTime=True&loc=Local&timeout=10s"
	PQ_DSN     = "host={server} port={port} user={user} password={password} dbname={name} sslmode=disable TimeZone=Asia/Shanghai"
	SQLITE_DSN = "file:{path}?_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 DatabaseOptions added in v1.0.0

type DatabaseOptions struct {
	Type     string `json:"type,omitempty"`     // 数据库类型
	User     string `json:"user,omitempty"`     // 用户
	Password string `json:"password,omitempty"` // 密码
	Server   string `json:"server,omitempty"`   // 服务器
	Port     string `json:"port,omitempty"`     // 端口
	Name     string `json:"name,omitempty"`     // 数据库名
	Charset  string `json:"charset,omitempty"`  // 字符集
	Path     string `json:"path,omitempty"`     // 文件路径
}

func (DatabaseOptions) DSN added in v1.0.0

func (o DatabaseOptions) 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