db

package
v0.0.0-...-8845ca5 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBatchesStop = errors.New("batches stop")

Functions

func Camel2UnderScore

func Camel2UnderScore(name string) string

func EnsureIsSliceOrArray

func EnsureIsSliceOrArray(obj interface{}) (res reflect.Value)

func EscapeMysqlString

func EscapeMysqlString(sql string) string

func Expr

func Expr(expression string, args ...interface{}) map[string]any

func FormatSql

func FormatSql(sql string, values ...interface{}) string

func IsUniqueIndexConflictErr

func IsUniqueIndexConflictErr(err error) bool

func UniqueSlice

func UniqueSlice(s interface{}) interface{}

func UpdateCase

func UpdateCase[M string | *Cond](caseMap map[M]any, def ...interface{}) clause.Expr

CASE WHEN (`a` = 1) THEN "1" WHEN (`b` = 2) THEN 2 ELSE 10 END

func UpdateCaseOneField

func UpdateCaseOneField(field string, caseMap map[any]any, def ...interface{}) clause.Expr

CASE `a` WHEN 1 THEN "1" WHEN 2 THEN 2 ELSE 10 END

Types

type ChunkResult

type ChunkResult struct {
	Error error
}

type Client

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

func New

func New(c *Config, tables ...Tabler) (*Client, error)

func (*Client) AutoMigrate

func (p *Client) AutoMigrate(dst ...Tabler) error

func (*Client) NewScoop

func (p *Client) NewScoop() *Scoop

type Cond

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

func OrWhere

func OrWhere(args ...interface{}) *Cond

func Where

func Where(args ...interface{}) *Cond

func (*Cond) Clean

func (p *Cond) Clean() *Cond

func (*Cond) GoString

func (p *Cond) GoString() string

func (*Cond) OrWhere

func (p *Cond) OrWhere(args ...interface{}) *Cond

func (*Cond) String

func (p *Cond) String() string

func (*Cond) ToString

func (p *Cond) ToString() string

func (*Cond) Where

func (p *Cond) Where(args ...interface{}) *Cond

type Config

type Config struct {
	// Database type, support sqlite, mysql, postgres, sqlserver, default sqlite
	// sqlite: sqlite|sqlite3
	// mysql: mysql
	// postgres: postgres|pg|postgresql|pgsql
	// sqlserver: sqlserver|mssql
	Type string `yaml:"type" json:"type"`

	// Database debug, default false
	Debug bool `yaml:"debug" json:"debug,omitempty"`

	// Database address
	// sqlite: full filepath, default exec path
	// mysql: database address, default 127.0.0.1
	// postgres: database address, default 127.0.0.1
	// sqlserver: database address, default 127.0.0.1
	Address string `yaml:"address" json:"address,omitempty"`

	// Database port
	// sqlite: empty
	// mysql: database port, default 3306
	// postgres: database port, default 5432
	// sqlserver: database port, default 1433
	Port int `yaml:"port" json:"port,omitempty"`

	// Database name
	// sqlite: database file name, default ice.db
	// mysql: database name, default ice
	// postgres: database name, default ice
	// sqlserver: database name, default ice
	Name string `yaml:"name" json:"name,omitempty"`

	// Database username
	// sqlite: empty
	// mysql: database username
	// postgres: database username
	// sqlserver: database username
	Username string `yaml:"username" json:"username,omitempty"`

	// Database password
	// sqlite: empty
	// mysql: database password
	// postgres: database password
	// sqlserver: database password
	Password string `yaml:"password" json:"password,omitempty"`

	Logger logger.Interface `json:"-" yaml:"-"`
}

type CreateInBatchesResult

type CreateInBatchesResult struct {
	RowsAffected int64
	Error        error
}

type CreateNotExistResult

type CreateNotExistResult[M any] struct {
	IsCreated bool
	Error     error

	Object *M
}

type CreateOrUpdateResult

type CreateOrUpdateResult[M any] struct {
	Error  error
	Object *M

	Created bool
	Updated bool
}

type CreateResult

type CreateResult struct {
	RowsAffected int64
	Error        error
}

type DeleteResult

type DeleteResult struct {
	RowsAffected int64
	Error        error
}

type Field

type Field struct {
	Name      string
	Type      reflect.Kind
	GormField string
}

type FindResult

type FindResult struct {
	RowsAffected int64
	Error        error
}

type FirstOrCreateResult

type FirstOrCreateResult[M any] struct {
	IsCreated bool
	Error     error

	Object *M
}

type FirstResult

type FirstResult struct {
	Error error
}

type ListOption

type ListOption struct {
	Offset uint64 `json:"offset,omitempty"`
	Limit  uint64 `json:"limit,omitempty"`

	Options []*Options `json:"options,omitempty"`

	ShowTotal bool `json:"show_total,omitempty"`
}

func NewListOption

func NewListOption() *ListOption

func (*ListOption) AddOption

func (p *ListOption) AddOption(key uint8, value string) *ListOption

func (*ListOption) AddOptions

func (p *ListOption) AddOptions(options ...*Options) *ListOption

func (*ListOption) Clone

func (p *ListOption) Clone() *ListOption

func (*ListOption) Page

func (p *ListOption) Page() *Page

func (*ListOption) Processor

func (p *ListOption) Processor() *ListOptionProcessor

func (*ListOption) SetLimit

func (p *ListOption) SetLimit(limit uint64) *ListOption

func (*ListOption) SetOffset

func (p *ListOption) SetOffset(offset uint64) *ListOption

func (*ListOption) SetOptions

func (p *ListOption) SetOptions(options ...*Options) *ListOption

func (*ListOption) SetShowTotal

func (p *ListOption) SetShowTotal(showTotal ...bool) *ListOption

type ListOptionProcessor

type ListOptionProcessor struct {
	ListOption *ListOption

	Handler map[uint8]func(string) error
}

func NewListOptionProcessor

func NewListOptionProcessor(option *ListOption) *ListOptionProcessor

func (*ListOptionProcessor) BetweenTimestamp

func (p *ListOptionProcessor) BetweenTimestamp(key uint8, logic func(start, end int64) error) *ListOptionProcessor

func (*ListOptionProcessor) Bool

func (p *ListOptionProcessor) Bool(key uint8, logic func(value bool) error) *ListOptionProcessor

func (*ListOptionProcessor) BoolSlice

func (p *ListOptionProcessor) BoolSlice(key uint8, logic func(value []bool) error) *ListOptionProcessor

func (*ListOptionProcessor) Float32

func (p *ListOptionProcessor) Float32(key uint8, logic func(value float32) error) *ListOptionProcessor

func (*ListOptionProcessor) Float32Slice

func (p *ListOptionProcessor) Float32Slice(key uint8, logic func(value []float32) error) *ListOptionProcessor

func (*ListOptionProcessor) Float64

func (p *ListOptionProcessor) Float64(key uint8, logic func(value float64) error) *ListOptionProcessor

func (*ListOptionProcessor) Float64Slice

func (p *ListOptionProcessor) Float64Slice(key uint8, logic func(value []float64) error) *ListOptionProcessor

func (*ListOptionProcessor) Has

func (p *ListOptionProcessor) Has(key uint8, logic func() error) *ListOptionProcessor

func (*ListOptionProcessor) Int

func (p *ListOptionProcessor) Int(key uint8, logic func(value int) error) *ListOptionProcessor

func (*ListOptionProcessor) Int16

func (p *ListOptionProcessor) Int16(key uint8, logic func(value int16) error) *ListOptionProcessor

func (*ListOptionProcessor) Int16Slice

func (p *ListOptionProcessor) Int16Slice(key uint8, logic func(value []int16) error) *ListOptionProcessor

func (*ListOptionProcessor) Int32

func (p *ListOptionProcessor) Int32(key uint8, logic func(value int32) error) *ListOptionProcessor

func (*ListOptionProcessor) Int32Slice

func (p *ListOptionProcessor) Int32Slice(key uint8, logic func(value []int32) error) *ListOptionProcessor

func (*ListOptionProcessor) Int64

func (p *ListOptionProcessor) Int64(key uint8, logic func(value int64) error) *ListOptionProcessor

func (*ListOptionProcessor) Int64Slice

func (p *ListOptionProcessor) Int64Slice(key uint8, logic func(value []int64) error) *ListOptionProcessor

func (*ListOptionProcessor) Int8

func (p *ListOptionProcessor) Int8(key uint8, logic func(value int8) error) *ListOptionProcessor

func (*ListOptionProcessor) Int8Slice

func (p *ListOptionProcessor) Int8Slice(key uint8, logic func(value []int8) error) *ListOptionProcessor

func (*ListOptionProcessor) IntSlice

func (p *ListOptionProcessor) IntSlice(key uint8, logic func(value []int) error) *ListOptionProcessor

func (*ListOptionProcessor) Order

func (p *ListOptionProcessor) Order(key uint8, logic func(value string) error) *ListOptionProcessor

func (*ListOptionProcessor) Process

func (p *ListOptionProcessor) Process(tx *Scoop, values any) (*Page, error)

func (*ListOptionProcessor) String

func (p *ListOptionProcessor) String(key uint8, logic func(value string) error) *ListOptionProcessor

func (*ListOptionProcessor) StringSlice

func (p *ListOptionProcessor) StringSlice(key uint8, logic func(value []string) error) *ListOptionProcessor

func (*ListOptionProcessor) Timestamp

func (p *ListOptionProcessor) Timestamp(key uint8, logic func(value int64) error) *ListOptionProcessor

func (*ListOptionProcessor) TimestampRange

func (p *ListOptionProcessor) TimestampRange(key uint8, logic func(start, end int64) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint

func (p *ListOptionProcessor) Uint(key uint8, logic func(value uint) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint16

func (p *ListOptionProcessor) Uint16(key uint8, logic func(value uint16) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint16Slice

func (p *ListOptionProcessor) Uint16Slice(key uint8, logic func(value []uint16) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint32

func (p *ListOptionProcessor) Uint32(key uint8, logic func(value uint32) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint32Slice

func (p *ListOptionProcessor) Uint32Slice(key uint8, logic func(value []uint32) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint64

func (p *ListOptionProcessor) Uint64(key uint8, logic func(value uint64) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint64Slice

func (p *ListOptionProcessor) Uint64Slice(key uint8, logic func(value []uint64) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint8

func (p *ListOptionProcessor) Uint8(key uint8, logic func(value uint8) error) *ListOptionProcessor

func (*ListOptionProcessor) Uint8Slice

func (p *ListOptionProcessor) Uint8Slice(key uint8, logic func(value []uint8) error) *ListOptionProcessor

func (*ListOptionProcessor) UintSlice

func (p *ListOptionProcessor) UintSlice(key uint8, logic func(value []uint) error) *ListOptionProcessor

type Logger

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

func NewLogger

func NewLogger() *Logger

func (*Logger) Error

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

func (*Logger) Info

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

func (*Logger) Log

func (l *Logger) Log(skip int, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

func (*Logger) LogMode

func (l *Logger) LogMode(logLevel logger.LogLevel) logger.Interface

func (*Logger) Trace

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

func (*Logger) Warn

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

type Model

type Model[M any] struct {
	// contains filtered or unexported fields
}

func NewModel

func NewModel[M any](db *Client) *Model[M]

func (*Model[M]) IsNotFound

func (p *Model[M]) IsNotFound(err error) bool

func (*Model[M]) NewScoop

func (p *Model[M]) NewScoop(tx ...*Scoop) *ModelScoop[M]

func (*Model[M]) SetNotFound

func (p *Model[M]) SetNotFound(err error) *Model[M]

func (*Model[M]) TableName

func (p *Model[M]) TableName() string

type ModelScoop

type ModelScoop[M any] struct {
	Scoop
	// contains filtered or unexported fields
}

func NewModelScoop

func NewModelScoop[M any](db *gorm.DB) *ModelScoop[M]

func (*ModelScoop[M]) Between

func (p *ModelScoop[M]) Between(column string, min, max interface{}) *ModelScoop[M]

func (*ModelScoop[M]) Chunk

func (p *ModelScoop[M]) Chunk(size uint64, fc func(tx *Scoop, out []*M, offset uint64) error) *ChunkResult

func (*ModelScoop[M]) Create

func (p *ModelScoop[M]) Create(m *M) error

func (*ModelScoop[M]) CreateInBatches

func (p *ModelScoop[M]) CreateInBatches(value []*M, batchSize int) *CreateInBatchesResult

func (*ModelScoop[M]) CreateNotExist

func (p *ModelScoop[M]) CreateNotExist(m *M) *CreateNotExistResult[M]

func (*ModelScoop[M]) CreateOrUpdate

func (p *ModelScoop[M]) CreateOrUpdate(values map[string]interface{}, m *M) *CreateOrUpdateResult[M]

func (*ModelScoop[M]) Equal

func (p *ModelScoop[M]) Equal(column string, value interface{}) *ModelScoop[M]

func (*ModelScoop[M]) Find

func (p *ModelScoop[M]) Find() ([]*M, error)

func (*ModelScoop[M]) First

func (p *ModelScoop[M]) First() (*M, error)

func (*ModelScoop[M]) FirstOrCreate

func (p *ModelScoop[M]) FirstOrCreate(m *M) *FirstOrCreateResult[M]

func (*ModelScoop[M]) Group

func (p *ModelScoop[M]) Group(fields ...string) *ModelScoop[M]

func (*ModelScoop[M]) Ignore

func (p *ModelScoop[M]) Ignore(b ...bool) *ModelScoop[M]

func (*ModelScoop[M]) In

func (p *ModelScoop[M]) In(column string, values interface{}) *ModelScoop[M]

func (*ModelScoop[M]) LeftLike

func (p *ModelScoop[M]) LeftLike(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) Like

func (p *ModelScoop[M]) Like(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) Limit

func (p *ModelScoop[M]) Limit(limit uint64) *ModelScoop[M]

func (*ModelScoop[M]) NotBetween

func (p *ModelScoop[M]) NotBetween(column string, min, max interface{}) *ModelScoop[M]

func (*ModelScoop[M]) NotEqual

func (p *ModelScoop[M]) NotEqual(column string, value interface{}) *ModelScoop[M]

func (*ModelScoop[M]) NotIn

func (p *ModelScoop[M]) NotIn(column string, values interface{}) *ModelScoop[M]

func (*ModelScoop[M]) NotLeftLike

func (p *ModelScoop[M]) NotLeftLike(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) NotLike

func (p *ModelScoop[M]) NotLike(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) NotRightLike

func (p *ModelScoop[M]) NotRightLike(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) Offset

func (p *ModelScoop[M]) Offset(offset uint64) *ModelScoop[M]

func (*ModelScoop[M]) Order

func (p *ModelScoop[M]) Order(fields ...string) *ModelScoop[M]

func (*ModelScoop[M]) Replace

func (p *ModelScoop[M]) Replace(b ...bool) *ModelScoop[M]

func (*ModelScoop[M]) RightLike

func (p *ModelScoop[M]) RightLike(column string, value string) *ModelScoop[M]

func (*ModelScoop[M]) Select

func (p *ModelScoop[M]) Select(fields ...string) *ModelScoop[M]

func (*ModelScoop[M]) Unscoped

func (p *ModelScoop[M]) Unscoped(b ...bool) *ModelScoop[M]

func (*ModelScoop[M]) UpdateOrCreate

func (p *ModelScoop[M]) UpdateOrCreate(values map[string]interface{}, m *M) *UpdateOrCreateResult[M]

func (*ModelScoop[M]) Where

func (p *ModelScoop[M]) Where(args ...interface{}) *ModelScoop[M]

type Options

type Options struct {
	Key   uint8  `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

type Page

type Page struct {
	Offset uint64 `json:"offset"`
	Limit  uint64 `json:"limit"`

	Total uint64 `json:"total,omitempty"`
}

type Scoop

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

func NewScoop

func NewScoop(db *gorm.DB) *Scoop

func (*Scoop) AutoMigrate

func (p *Scoop) AutoMigrate(dst ...interface{}) error

func (*Scoop) Begin

func (p *Scoop) Begin() *Scoop

func (*Scoop) Between

func (p *Scoop) Between(column string, min, max interface{}) *Scoop

func (*Scoop) Chunk

func (p *Scoop) Chunk(dest interface{}, size uint64, fc func(tx *Scoop, offset uint64) error) *ChunkResult

func (*Scoop) Commit

func (p *Scoop) Commit() *Scoop

func (*Scoop) CommitOrRollback

func (p *Scoop) CommitOrRollback(tx *Scoop, logic func(tx *Scoop) error) error

func (*Scoop) Count

func (p *Scoop) Count() (uint64, error)

func (*Scoop) Create

func (p *Scoop) Create(value interface{}) *CreateResult

func (*Scoop) CreateInBatches

func (p *Scoop) CreateInBatches(value interface{}, batchSize int) *CreateInBatchesResult

func (*Scoop) Delete

func (p *Scoop) Delete() *DeleteResult

func (*Scoop) Equal

func (p *Scoop) Equal(column string, value interface{}) *Scoop

func (*Scoop) Exist

func (p *Scoop) Exist() (bool, error)

func (*Scoop) Find

func (p *Scoop) Find(out interface{}) *FindResult

func (*Scoop) First

func (p *Scoop) First(out interface{}) *FirstResult

func (*Scoop) Group

func (p *Scoop) Group(fields ...string) *Scoop

func (*Scoop) Ignore

func (p *Scoop) Ignore(b ...bool) *Scoop

func (*Scoop) In

func (p *Scoop) In(column string, values interface{}) *Scoop

func (*Scoop) IsNotFound

func (p *Scoop) IsNotFound(err error) bool

func (*Scoop) LeftLike

func (p *Scoop) LeftLike(column string, value string) *Scoop

func (*Scoop) Like

func (p *Scoop) Like(column string, value string) *Scoop

func (*Scoop) Limit

func (p *Scoop) Limit(limit uint64) *Scoop

func (*Scoop) Model

func (p *Scoop) Model(m any) *Scoop

func (*Scoop) NotBetween

func (p *Scoop) NotBetween(column string, min, max interface{}) *Scoop

func (*Scoop) NotEqual

func (p *Scoop) NotEqual(column string, value interface{}) *Scoop

func (*Scoop) NotIn

func (p *Scoop) NotIn(column string, values interface{}) *Scoop

func (*Scoop) NotLeftLike

func (p *Scoop) NotLeftLike(column string, value string) *Scoop

func (*Scoop) NotLike

func (p *Scoop) NotLike(column string, value string) *Scoop

func (*Scoop) NotRightLike

func (p *Scoop) NotRightLike(column string, value string) *Scoop

func (*Scoop) Offset

func (p *Scoop) Offset(offset uint64) *Scoop

func (*Scoop) Order

func (p *Scoop) Order(fields ...string) *Scoop

func (*Scoop) Replace

func (p *Scoop) Replace(b ...bool) *Scoop

func (*Scoop) RightLike

func (p *Scoop) RightLike(column string, value string) *Scoop

func (*Scoop) Rollback

func (p *Scoop) Rollback() *Scoop

func (*Scoop) Select

func (p *Scoop) Select(fields ...string) *Scoop

func (*Scoop) Unscoped

func (p *Scoop) Unscoped(b ...bool) *Scoop

func (*Scoop) Updates

func (p *Scoop) Updates(m interface{}) *UpdateResult

func (*Scoop) Where

func (p *Scoop) Where(args ...interface{}) *Scoop

type Tabler

type Tabler interface {
	TableName() string
}

type UpdateOrCreateResult

type UpdateOrCreateResult[M any] struct {
	IsCreated bool
	Error     error

	Object *M
}

type UpdateResult

type UpdateResult struct {
	RowsAffected int64
	Error        error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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