src

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultKey         = "db"
	DefaultDriver      = "mysql"
	DefaultDns         = "root:pass@tcp(127.0.0.1)/test"
	DefaultMapper      = "snake"
	DefaultMaxIdle     = 2
	DefaultMaxOpen     = 30
	DefaultMaxLifetime = 60
)

Variables

View Source
var (
	DefaultEnableSession = false
	DefaultEnableLogger  = true
)

Functions

func Transaction

func Transaction(ctx context.Context, handlers ...TransactionHandler) (err error)

func TransactionWithSession

func TransactionWithSession(ctx context.Context, session *Session, handlers ...TransactionHandler) (err error)

Types

type Configuration

type Configuration interface {
	// Add
	// adds a database mapping to configuration.
	Add(key string, database *Database) (err error)

	// Del
	// deletes database from mapping and activated engine group.
	Del(key string)

	// Get
	// returns a shared database configuration.
	Get(keys ...string) (database *Database, exists bool)

	// GetEngine
	// returns a shared xorm engine group, creates a new when called
	// at first time.
	GetEngine(keys ...string) (*EngineGroup, error)

	// GetMaster
	// returns a shared xorm master session of a connection.
	GetMaster(ctx context.Context, keys ...string) (session *Session, err error)

	// GetSlave
	// returns a shared xorm slave session of a connection.
	GetSlave(ctx context.Context, keys ...string) (session *Session, err error)
}

Configuration is an interface for connections.

var Config Configuration

Config is a singleton instance that constructed when package init.

type Dao added in v1.1.5

type Dao[T any] struct {
	// contains filtered or unexported fields
}

Dao is an alias of Database Access Object, It's read and write data on database server such as mysql, postgres.

func NewDao added in v1.1.5

func NewDao[T any](t T) *Dao[T]

NewDao creates a dao instance with given model type and model struct. Data in database operated depends on model type.

type ModelService struct {
    Dao *db.Dao[models.Model]
}

func NewModelService(dbs ...*framework.DB) *ModelService {
    o := &ModelService{}
    o.Dao = db.NewDao[models.ErpSales](models.ErpSales{})
    o.Dao.WithConn(dbs...)
    o.Dao.WithKey("db")
    return o
}

func (*Dao[T]) AddByStruct added in v1.1.5

func (o *Dao[T]) AddByStruct(ctx context.Context, t *T) (model *T, err error)

AddByStruct adds a model by given model struct.

svc.Dao.AddByModel(ctx, &models.Model{
    UserId: 1,
    ...
})

func (*Dao[T]) DeleteBy added in v1.1.5

func (o *Dao[T]) DeleteBy(ctx context.Context, k string, v any) (affects int64, err error)

DeleteBy delete records by given condition with key value pairs. Param k is column name and v is column value.

// sql: DELETE FORM `table` WHERE `id` = 1
svc.Dao.DeleteBy(ctx, "id", 1)

func (*Dao[T]) DeleteById added in v1.1.5

func (o *Dao[T]) DeleteById(ctx context.Context, v any) (affects int64, err error)

DeleteById delete records by given condition with primary key value. Param v is an integer value of primary key.

// sql: DELETE FORM `table` WHERE `id` = 1
svc.Dao.DeleteById(ctx, 1)

func (*Dao[T]) DeleteByMap added in v1.1.5

func (o *Dao[T]) DeleteByMap(ctx context.Context, m map[string]any) (affects int64, err error)

DeleteByMap delete records by given condition with multiple key value pairs.

// sql: DELETE FORM `table` WHERE `status` = 0 AND `status_type` = "error"
svc.Dao.DeleteByMap(ctx, map[string]any{
    "status": 0,
    "status_type": "error",
})

func (*Dao[T]) DeleteByStruct added in v1.1.5

func (o *Dao[T]) DeleteByStruct(ctx context.Context, t *T) (affects int64, err error)

DeleteByStruct delete records by given condition with struct definition.

// sql: DELETE FORM `table` WHERE `status` = 1 AND `status_type` = "error"
svc.Dao.DeleteByMap(ctx, models.Model{
    "status": 1,
    "status_type": "error",
})

func (*Dao[T]) GetBy added in v1.1.5

func (o *Dao[T]) GetBy(ctx context.Context, column string, value any) (model *T, has bool, err error)

GetBy returns a model specified column and value.

// sql: SELECT * FROM `table` WHERE `id` = 10 LIMIT 1
svc.Dao.GetBy(ctx, "id", 10)

func (*Dao[T]) GetById added in v1.1.5

func (o *Dao[T]) GetById(ctx context.Context, value any) (model *T, has bool, err error)

GetById returns a model by given primary key value.

// sql: SELECT * FROM `table` WHERE `id` = 10 LIMIT 1
svc.Dao.GetById(ctx, 10)

func (*Dao[T]) GetByMap added in v1.1.5

func (o *Dao[T]) GetByMap(ctx context.Context, condition map[string]any) (model *T, has bool, err error)

GetByMap returns a model by given condition with mapper param.

// sql: SELECT * FROM `table` WHERE `id` = 10 LIMIT 1
svc.Dao.GetByMap(ctx, map[string]any{
    "id": 10
})

func (*Dao[T]) GetByStruct added in v1.1.5

func (o *Dao[T]) GetByStruct(ctx context.Context, t T) (model *T, has bool, err error)

GetByStruct returns a model with model condition. It's ignore default value condition such as zero or empty string.

// sql: SELECT * FROM `table` WHERE `id` = 10 LIMIT 1
svc.Dao.GetByModel(ctx, &models.Model{
    Id: 10,
})

func (*Dao[T]) ListByMap added in v1.1.5

func (o *Dao[T]) ListByMap(ctx context.Context, m map[string]any, sorts ...string) (list []*T, err error)

ListByMap returns a list of models by given condition with map param.

sql: SELECT * FROM `table` WHERE `user_id` = 1
svc.Dao.GetByModel(ctx, map[string]any{
    "user_id": 1
})

func (*Dao[T]) ListByStruct added in v1.1.5

func (o *Dao[T]) ListByStruct(ctx context.Context, t *T, sorts ...string) (list []*T, err error)

ListByStruct returns a list of models by given condition with model struct.

// sql: SELECT * FROM `table` WHERE `user_id` = 10
svc.Dao.GetByModel(ctx, &models.Model{
    UserId: 1
})

func (*Dao[T]) Master added in v1.1.5

func (o *Dao[T]) Master(ctx context.Context) (*xorm.Session, error)

Master returns a session from master connection.

func (*Dao[T]) PagingByMap added in v1.1.5

func (o *Dao[T]) PagingByMap(ctx context.Context, m map[string]any, page, size int, sorts ...string) (list []*T, total int64, err error)

PagingByMap returns a list of models by given condition with map and calculate total items in table.

func (*Dao[T]) PagingByStruct added in v1.1.5

func (o *Dao[T]) PagingByStruct(ctx context.Context, t *T, page, size int, sorts ...string) (list []*T, total int64, err error)

PagingByStruct returns a list of models by given condition with model struct and calculate total items in table.

func (*Dao[T]) Slaver added in v1.1.5

func (o *Dao[T]) Slaver(ctx context.Context) (*xorm.Session, error)

Slaver returns a session from slaver connection.

func (*Dao[T]) UpdateFieldsById added in v1.1.5

func (o *Dao[T]) UpdateFieldsById(ctx context.Context, fields map[string]any, v any) (affects int64, err error)

UpdateFieldsById updates a record with key value pairs by primary key.

// sql: UPDATE `table` SET `name` = "test", `age` = 18 WHERE `id` = 10
svc.Dao.UpdateMapById(ctx, map[string]any{
    "name": "test",
    "age": 18,
}, 10)

func (*Dao[T]) UpdateFieldsByMap added in v1.1.5

func (o *Dao[T]) UpdateFieldsByMap(ctx context.Context, fields, condition map[string]any) (affects int64, err error)

UpdateFieldsByMap updates a record with map condition.

// sql: UPDATE `table` SET `name` = "test", `age` = 18 WHERE `id` = 10 AND `status` = 0
svc.Dao.UpdateFieldsByMap(ctx, map[string]any{
    "name": "test",
    "age": 18,
}, map[string]any{
    "id": 10,
    "status": 0,
})

func (*Dao[T]) UpdateModel added in v1.2.0

func (o *Dao[T]) UpdateModel(ctx context.Context, model *T) (affects int64, err error)

func (*Dao[T]) WithKey added in v1.1.5

func (o *Dao[T]) WithKey(k string) *Dao[T]

WithKey bind a connection key for connector.

func (*Dao[T]) WithSess added in v1.1.5

func (o *Dao[T]) WithSess(ss ...*xorm.Session) *Dao[T]

WithSess bind a session of database connection.

type DaoFieldNameForPrimaryKey added in v1.1.5

type DaoFieldNameForPrimaryKey interface {
	PrimaryKeyName() string
}

type DaoFieldNameForPrimaryKeyValue added in v1.2.0

type DaoFieldNameForPrimaryKeyValue interface {
	PrimaryKeyValue() any
}

type Database

type Database struct {
	// Driver
	// database type. Accept mysql, mssql etc.
	//
	// Default: mysql
	Driver string `yaml:"driver"`

	// Dsn
	// is a list for database connection. First for them as primary and
	// others as slaves.
	Dsn []string `yaml:"dsn"`

	// Mapper
	// define.
	Mapper string `yaml:"mapper"`

	// MaxIdle
	// maximum number of idle connections maintained. The manager will close
	// unnecessary idle connections if there are too many idle connections.
	//
	// Default: 2
	MaxIdle int `yaml:"max-idle"`

	// MaxOpen
	// maximum number of connections established. The manager will block and
	// wait for an idle connection to use.
	//
	// Default: 30
	MaxOpen int `yaml:"max-open"`

	// MaxLifetime
	// a connection lifecycle.
	//
	// Default: 60
	// Unit: second
	MaxLifetime int `yaml:"max-lifetime"`

	// EnableLogger
	// is a flag for enable logger. The manager will send sql to logging
	// system if enabled.
	EnableLogger *bool `yaml:"enable-logger"`

	// EnableSession
	// is a flag for enable session. The manager will create a session
	// id for each connection.
	EnableSession *bool `yaml:"enable-session"`
	// contains filtered or unexported fields
}

Database is a component for db connection configurations.

func (*Database) After

func (o *Database) After()

type EngineGroup

type EngineGroup = xorm.EngineGroup

type GonicMapper

type GonicMapper = names.GonicMapper

type Mapper

type Mapper = names.Mapper

type PrefixMapper

type PrefixMapper = names.PrefixMapper

type SameMapper

type SameMapper = names.SameMapper

type Service

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

Service is a top level component used to operate database.

func (*Service) Master

func (o *Service) Master(ctx context.Context) (*Session, error)

Master returns a shared master session of a connection.

func (*Service) Slave

func (o *Service) Slave(ctx context.Context) (*Session, error)

Slave returns a shared slave session of a connection.

func (*Service) With

func (o *Service) With(sessions ...*Session)

With set a shared session of a connection on a service.

func (*Service) WithKey

func (o *Service) WithKey(key string)

WithKey specify a connection key in your mapping.

type Session

type Session = xorm.Session

type SnakeMapper

type SnakeMapper = names.SnakeMapper

type SuffixMapper

type SuffixMapper = names.SuffixMapper

type TransactionHandler

type TransactionHandler func(ctx context.Context, session *Session) error

Jump to

Keyboard shortcuts

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