db

package
v0.0.0-...-e5f04d6 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 8 Imported by: 0

README

db

1. 使用

package main

import (
	"github.com/forestyc/playground/pkg/db"
)

func main() {
	mdb := db.NewMysql(db.Config{
		Dsn:              "baal:Baal@123@tcp(140.143.163.171:3306)/baal?charset=utf8mb4&parseTime=true&loc=Local",
		MaxOpen:          10,
		IdleConns:        5,
		MaxIdleTime:      300,
		OperationTimeout: 10,
	})
	defer mdb.Close()
	var user User
	session := mdb.Session()
	result := session.Model(&user).
		//Where("date=? and exchange=? and contract_id=?", "20231212", "DCE", "a2312").
		Take(&user)
	if code, msg := mdb.DBError(result.Error); code > 0 {
		fmt.Println(msg)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Dsn              string `mapstructure:"dsn"`
	MaxOpen          int    `mapstructure:"max-open"`
	IdleConns        int    `mapstructure:"idle-conns"`
	MaxIdleTime      int    `mapstructure:"idle-timeout"`
	OperationTimeout int    `mapstructure:"operation-timeout"`
}

type Mysql

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

func NewMysql

func NewMysql(config Config) *Mysql

func (*Mysql) Close

func (mdb *Mysql) Close() error

func (*Mysql) DBError

func (mdb *Mysql) DBError(err error) (int, string)

func (*Mysql) Session

func (mdb *Mysql) Session(opts ...SessionOption) *gorm.DB

Session new a session with prepared statement and manually controlled transactions. To switch auto-commit call mdb.Session(db.WithSessionSkipDefaultTransaction(true))

type SessionOption

type SessionOption func(opt *gorm.Session)

func WithSessionAllowGlobalUpdate

func WithSessionAllowGlobalUpdate(AllowGlobalUpdate bool) SessionOption

func WithSessionContext

func WithSessionContext(Context context.Context) SessionOption

func WithSessionCreateBatchSize

func WithSessionCreateBatchSize(CreateBatchSize int) SessionOption

func WithSessionDisableNestedTransaction

func WithSessionDisableNestedTransaction(DisableNestedTransaction bool) SessionOption

func WithSessionDryRun

func WithSessionDryRun(DryRun bool) SessionOption

func WithSessionFullSaveAssociations

func WithSessionFullSaveAssociations(FullSaveAssociations bool) SessionOption

func WithSessionInitialized

func WithSessionInitialized(Initialized bool) SessionOption

func WithSessionLogger

func WithSessionLogger(Logger logger.Interface) SessionOption

func WithSessionNewDB

func WithSessionNewDB(NewDB bool) SessionOption

func WithSessionNowFunc

func WithSessionNowFunc(NowFunc func() time.Time) SessionOption

func WithSessionPrepareStmt

func WithSessionPrepareStmt(PrepareStmt bool) SessionOption

func WithSessionQueryFields

func WithSessionQueryFields(QueryFields bool) SessionOption

func WithSessionSkipDefaultTransaction

func WithSessionSkipDefaultTransaction(SkipDefaultTransaction bool) SessionOption

func WithSessionSkipHooks

func WithSessionSkipHooks(SkipHooks bool) SessionOption

Jump to

Keyboard shortcuts

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