src

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 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 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