mysql

package module
v0.0.0-...-0033d94 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 15 Imported by: 0

README

1. 项目简介

1.基于gorm库封装,增加tracing 2.接入统一日志输出

2. 详细文档地址

https://jasperxu.github.io/gorm-zh

3. 配置文件参数

mysqlDB:
  alias: admin-server
  type: mysql  #当前代码里面预制的是mysql
  server: 127.0.0.1
  port: 3306
  user: root
  password: my-secret-pw
  charset: utf8mb4
  database: ets_db
  maxIdleConns: 3
  maxOpenConns: 10
  maxLeftTime: 10s
  timezone: Local

4. 快速开始

db := gorm.New(&config.Config{
    Type:     "mysql",  
    Server:   "127.0.0.1",
    Port:     3306,
    Database: "db",
    User:     "db",
    Password: "123456",
})

//应用中使用,sess的具体方法查看 https://jasperxu.github.io/gorm-zh
sess = db.Context(context.Background())

5. 使用示例

参见gorm_test.go

Documentation

Index

Constants

View Source
const (
	LogLevelSilent = logger.Silent
	LogLevelError  = logger.Error
	LogLevelWarn   = logger.Warn
	LogLevelInfo   = logger.Info
)

Variables

View Source
var (
	// ErrRecordNotFound returns a "record not found error". Occurs only when attempting to query the database with a struct; querying with a slice won't return this error
	ErrRecordNotFound = gorm.ErrRecordNotFound
	// ErrInvalidTransaction occurs when you are trying to `Commit` or `Rollback`
	ErrInvalidTransaction = gorm.ErrInvalidTransaction
)

Functions

func Context

func Context(ctx context.Context) *gorm.DB

func Expr

func Expr(expr string, args ...interface{}) clause.Expr

Types

type Association

type Association = gorm.Association

type Config

type Config struct {
	Alias        string        `yaml:"alias" json:"alias"`
	Type         string        `yaml:"type" json:"type"`
	Server       string        `yaml:"server" json:"server"`
	Port         int           `yaml:"port" json:"port"`
	Database     string        `yaml:"database" json:"database"`
	User         string        `yaml:"user" json:"user"`
	Password     string        `yaml:"password" json:"password"`
	MaxIdleConns int           `yaml:"maxIdleConns" json:"maxIdleConns"`
	MaxOpenConns int           `yaml:"maxOpenConns" json:"maxOpenConns"`
	Charset      string        `yaml:"charset" json:"charset"`
	TimeZone     string        `yaml:"timezone" json:"timezone"`
	MaxLeftTime  time.Duration `yaml:"maxLeftTime" json:"maxLeftTime"`
}

type DB

type DB = gorm.DB

type DBConditions

type DBConditions struct {
	And       map[string]interface{}
	Or        map[string]interface{}
	Not       map[string]interface{}
	Limit     int
	Offset    int
	Order     interface{}
	Select    interface{}
	Group     string
	Having    interface{}
	NeedCount bool
	Count     int64
	Distinct  interface{} // grom v1 暂不支持
}

DBConditions DB常用的查询条件封装

func (*DBConditions) Fill

func (d *DBConditions) Fill(db *gorm.DB) *gorm.DB

Fill 填充查询条件

type DeletedAt

type DeletedAt = gorm.DeletedAt

type Engine

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

func GetEngine

func GetEngine() *Engine

func New

func New(conf *Config) *Engine

New 实例化新的Gorm实例

func (*Engine) Context

func (db *Engine) Context(ctx context.Context) *gorm.DB

func (*Engine) GetDB

func (db *Engine) GetDB() *gorm.DB

func (*Engine) SetLogLevel

func (db *Engine) SetLogLevel(level LogLevel)

func (*Engine) SetLogMode

func (db *Engine) SetLogMode(mode bool)

type LogLevel

type LogLevel = logger.LogLevel

LogLevel 日志等级

type LoggerFunc

type LoggerFunc func(...interface{})

func (LoggerFunc) Print

func (f LoggerFunc) Print(args ...interface{})

type Model

type Model = gorm.Model

type Writer

type Writer struct {
}

func (Writer) Printf

func (w Writer) Printf(format string, args ...interface{})

Jump to

Keyboard shortcuts

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