db

package
v0.0.0-...-7edcfce Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SORT_ASC  string = "ASC"
	SORT_DESC string = "DESC"
)

Variables

This section is empty.

Functions

func IsFieldSet

func IsFieldSet(f Fields, key string) bool

func SetGlobalModelStore

func SetGlobalModelStore(m ModelStore)

func Update

func Update(db DBHandlers, ctx logger.WithLogger, obj common.Object, fields Fields) error

func UpdateAll

func UpdateAll(db DBHandlers, ctx logger.WithLogger, obj interface{}, fields Fields) error

func UpdateMulti

func UpdateMulti(db DBHandlers, ctx logger.WithLogger, obj interface{}, filter Fields, fields Fields) error

func UpdateWithFilter

func UpdateWithFilter(db DBHandlers, ctx logger.WithLogger, obj interface{}, filter *Filter, fields Fields) error

Types

type AllDatabases

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

func Databases

func Databases() *AllDatabases

func (*AllDatabases) CloseAll

func (a *AllDatabases) CloseAll()

func (*AllDatabases) Register

func (a *AllDatabases) Register(db DB)

func (*AllDatabases) Unregister

func (a *AllDatabases) Unregister(db DB)

type BetweenFields

type BetweenFields struct {
	FromField string
	ToField   string
	Value     interface{}
	FromOpen  bool
	ToOpen    bool
}

type Cursor

type Cursor interface {
	Next(ctx logger.WithLogger) (bool, error)
	Close(ctx logger.WithLogger) error
	Scan(ctx logger.WithLogger, obj interface{}) error
}

type DB

type DB interface {
	WithFilterParser

	ID() string
	Clone() DB

	InitWithConfig(ctx logger.WithLogger, vld validator.Validator, cfg *DBConfig) error

	DBHandlers

	EnableVerboseErrors(bool)

	AutoMigrate(ctx logger.WithLogger, models []interface{}) error
	MigrateDropIndex(ctx logger.WithLogger, model interface{}, indexName string) error

	PartitionedMonthAutoMigrate(ctx logger.WithLogger, models []interface{}) error
	PartitionedMonthsDetach(ctx logger.WithLogger, table string, months []utils.Month) error
	PartitionedMonthsDelete(ctx logger.WithLogger, table string, months []utils.Month) error

	NativeHandler() interface{}

	Close()
}

type DBConfig

type DBConfig struct {
	DB_PROVIDER     string `gorm:"index"`
	DB_HOST         string `gorm:"index"`
	DB_PORT         uint16 `gorm:"index"`
	DB_NAME         string `gorm:"index"`
	DB_USER         string `gorm:"index"`
	DB_PASSWORD     string `mask:"true"`
	DB_EXTRA_CONFIG string
	DB_DSN          string
}

type DBHandlers

type DBHandlers interface {
	FindByField(ctx logger.WithLogger, field string, value interface{}, obj interface{}, dest ...interface{}) (found bool, err error)
	FindByFields(ctx logger.WithLogger, fields Fields, obj interface{}, dest ...interface{}) (found bool, err error)
	FindWithFilter(ctx logger.WithLogger, filter *Filter, docs interface{}, dest ...interface{}) (int64, error)
	FindForUpdate(ctx logger.WithLogger, fields Fields, obj interface{}) (bool, error)
	FindForShare(ctx logger.WithLogger, fields Fields, obj interface{}) (bool, error)
	Exists(ctx logger.WithLogger, filter *Filter, doc interface{}) (bool, error)

	Create(ctx logger.WithLogger, obj interface{}) error
	CreateDup(ctx logger.WithLogger, obj interface{}, ignoreConflict ...bool) (bool, error)

	Delete(ctx logger.WithLogger, obj common.Object) error
	DeleteByField(ctx logger.WithLogger, field string, value interface{}, model interface{}) error
	DeleteByFields(ctx logger.WithLogger, fields Fields, obj interface{}) error

	RowsWithFilter(ctx logger.WithLogger, filter *Filter, obj interface{}) (Cursor, error)
	AllRows(ctx logger.WithLogger, obj interface{}) (Cursor, error)

	Update(ctx logger.WithLogger, obj interface{}, filter Fields, fields Fields) error
	UpdateAll(ctx logger.WithLogger, obj interface{}, newFields Fields) error
	UpdateWithFilter(ctx logger.WithLogger, obj interface{}, filter *Filter, newFields Fields) error

	Join(ctx logger.WithLogger, joinConfig *JoinQueryConfig, filter *Filter, dest interface{}) (int64, error)

	Joiner() Joiner

	CreateDatabase(ctx logger.WithLogger, dbName string) error
	MakeExpression(expr string, args ...interface{}) interface{}

	Sum(ctx logger.WithLogger, groupFields []string, sumFields []string, filter *Filter, model interface{}, dest ...interface{}) (int64, error)

	Transaction(handler TransactionHandler) error
	EnableDebug(bool)
}

type Fields

type Fields = map[string]interface{}

type Filter

type Filter struct {
	FilterConfig
	Fields        Fields
	FieldsIn      map[string][]interface{}
	FieldsNotIn   map[string][]interface{}
	Intervals     map[string]*Interval
	BetweenFields []*BetweenFields
	OrFields      []*OrFields

	PresetFields []Fields

	NoLimit bool
}

func NewFilter

func NewFilter() *Filter

func ParseQuery

func ParseQuery(db DB, query string, model interface{}, parserName string, validator ...*FilterValidator) (*Filter, error)

func (*Filter) AddBetweenField

func (f *Filter) AddBetweenField(fromField string, toField string, value interface{})

func (*Filter) AddField

func (f *Filter) AddField(name string, value interface{})

func (*Filter) AddFieldIn

func (f *Filter) AddFieldIn(name string, values ...interface{})

func (*Filter) AddFieldNotIn

func (f *Filter) AddFieldNotIn(name string, values ...interface{})

func (*Filter) AddFields

func (f *Filter) AddFields(fields Fields)

func (*Filter) AddInterval

func (f *Filter) AddInterval(name string, from interface{}, to interface{})

func (*Filter) AddOrFields

func (f *Filter) AddOrFields(value interface{}, names ...string)

func (*Filter) PopPresetFields

func (f *Filter) PopPresetFields()

func (*Filter) PushPresetFields

func (f *Filter) PushPresetFields(fields Fields)

func (*Filter) SetSorting

func (f *Filter) SetSorting(field string, direction ...string)

func (*Filter) ToQuery

func (f *Filter) ToQuery() *Query

func (*Filter) ToQueryString

func (f *Filter) ToQueryString() string

type FilterConfig

type FilterConfig struct {
	SortField     string `json:"sort_field,omitempty"`
	SortDirection string `json:"sort_direction,omitempty" validate:"omitempty,oneof=ASC DESC" vmessage:"Sort direction can be either ASC or DESC"`
	Offset        int    `json:"offset,omitempty" validate:"gte=0" vmessage:"Offset can not be negative"`
	Limit         int    `json:"limit,omitempty" validate:"gte=0" vmessage:"Limit can not be negative"`
	Count         bool   `json:"count,omitempty"`
}

type FilterParser

type FilterParser interface {
	Parse(query *Query) (*Filter, error)
}

type FilterValidator

type FilterValidator struct {
	Validator       validator.Validator
	Rules           map[string]string
	PlainFieldNames bool
}

func EmptyFilterValidator

func EmptyFilterValidator(vld validator.Validator) *FilterValidator

type Interval

type Interval struct {
	From     interface{}
	To       interface{}
	FromOpen bool
	ToOpen   bool
}

func (*Interval) IsNull

func (i *Interval) IsNull() bool

type JoinBegin

type JoinBegin interface {
	On(model interface{}, field string) JoinEnd
}

type JoinEnd

type JoinEnd interface {
	Joiner
	Sum(groupFields []string, sumFields []string) JoinEnd
	Destination(dst interface{}) (JoinQuery, error)
}

type JoinPairBase

type JoinPairBase struct {
	JoinPairData
}

func (*JoinPairBase) LeftField

func (j *JoinPairBase) LeftField() string

func (*JoinPairBase) RightField

func (j *JoinPairBase) RightField() string

type JoinPairData

type JoinPairData struct {
	LeftField  string
	RightField string
}

type JoinQueries

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

func NewJoinQueries

func NewJoinQueries() *JoinQueries

func (*JoinQueries) FindOrCreate

func (j *JoinQueries) FindOrCreate(config *JoinQueryConfig) (JoinQuery, error)

type JoinQuery

type JoinQuery interface {
	Join(ctx logger.WithLogger, filter *Filter, dest interface{}) (int64, error)
}

type JoinQueryBase

type JoinQueryBase struct {
	JoinQueryData
}

func (*JoinQueryBase) Destination

func (j *JoinQueryBase) Destination() interface{}

func (*JoinQueryBase) SetDestination

func (j *JoinQueryBase) SetDestination(destination interface{})

type JoinQueryBuilder

type JoinQueryBuilder = func() (JoinQuery, error)

type JoinQueryConfig

type JoinQueryConfig struct {
	Builder JoinQueryBuilder
	Name    string
	Nocache bool
}

func NewJoin

func NewJoin(builder JoinQueryBuilder, name string, nocache ...bool) *JoinQueryConfig

type JoinQueryData

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

type JoinTableBase

type JoinTableBase struct {
	JoinTableData
}

func (*JoinTableBase) Model

func (j *JoinTableBase) Model() interface{}

type JoinTableData

type JoinTableData struct {
	Model interface{}
}

type Joiner

type Joiner interface {
	Join(model interface{}, field string) JoinBegin
}

type ModelStore

type ModelStore interface {
	RegisterModel(model interface{})
	FindModel(name string) interface{}
	AllModels() []interface{}
}

func GlobalModelStore

func GlobalModelStore() ModelStore

type MonthPartition

type MonthPartition struct {
	common.ObjectBase
	Table string      `gorm:"index;uniqueIndex:u_month_partition"`
	Month utils.Month `gorm:"index;uniqueIndex:u_month_partition"`
}

type OrFields

type OrFields struct {
	Value  interface{}
	Fields []string
}

type Query

type Query struct {
	FilterConfig

	Fields        map[string]string        `json:"fields,omitempty"`
	FieldsIn      map[string][]string      `json:"fields_in,omitempty"`
	FieldsNotIn   map[string][]string      `json:"fields_not_in,omitempty"`
	Intervals     map[string]QueryInterval `json:"intervals,omitempty"`
	BetweenFields []QueryBetweenFields     `json:"betwees_fields,omitempty"`
	OrFields      []QueryOrFields          `json:"or_fields,omitempty"`
}

type QueryBetweenFields

type QueryBetweenFields struct {
	FromField string `json:"from_field,omitempty"`
	ToField   string `json:"to_field,omitempty"`
	Value     string `json:"value"`
	FromOpen  bool   `json:"from_open,omitempty"`
	ToOpen    bool   `json:"to_open,omitempty"`
}

type QueryInterval

type QueryInterval struct {
	From     optional.String `json:"from,omitempty"`
	To       optional.String `json:"to,omitempty"`
	FromOpen bool            `json:"from_open,omitempty"`
	ToOpen   bool            `json:"to_open,omitempty"`
}

type QueryOrFields

type QueryOrFields struct {
	Value  string   `json:"value"`
	Fields []string `json:"fields"`
}

type Transaction

type Transaction interface {
	DBHandlers
}

type TransactionHandler

type TransactionHandler = func(tx Transaction) error

type WithDB

type WithDB interface {
	Db() DB
}

type WithDBBase

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

func (*WithDBBase) Db

func (w *WithDBBase) Db() DB

func (*WithDBBase) Init

func (w *WithDBBase) Init(db DB)

type WithFilterParser

type WithFilterParser interface {
	PrepareFilterParser(model interface{}, name string, validator ...*FilterValidator) (FilterParser, error)
	ParseFilter(query *Query, parserName string) (*Filter, error)
	ParseFilterDirect(query *Query, model interface{}, name string, validator ...*FilterValidator) (*Filter, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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