db

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Database typeName = "database"
	Gorm     typeName = "gorm"
)

Variables

View Source
var (
	// DBs map[DB_NAME]*gorm.DB
	DbWrapper map[string]*standard.Wrapper
)
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
	if len(values) > 1 {
		var (
			sql             string
			formattedValues []string
			level           = values[0]
			currentTime     = "[" + time.Now().Format("2006-01-02 15:04:05") + "]"
			source          = fmt.Sprintf("(%v)", values[1])
		)

		messages = []interface{}{source, currentTime}

		if len(values) == 2 {

			currentTime = currentTime[1:]

			source = fmt.Sprintf("%v", values[1])

			messages = []interface{}{currentTime, source}
		}

		if level == "sql" {

			messages = append(messages, fmt.Sprintf(" [%.2fms]", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))

			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						if t.IsZero() {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00"))
						} else {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
						}
					} else if b, ok := value.([]byte); ok {
						if str := string(b); isPrintable(str) {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
						} else {
							formattedValues = append(formattedValues, "'<binary>'")
						}
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						} else {
							formattedValues = append(formattedValues, "NULL")
						}
					} else {
						switch value.(type) {
						case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
							formattedValues = append(formattedValues, fmt.Sprintf("%v", value))
						default:
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						}
					}
				} else {
					formattedValues = append(formattedValues, "NULL")
				}
			}

			if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
				sql = values[3].(string)
				for index, value := range formattedValues {
					placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
					sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
				}
			} else {
				formattedValuesLength := len(formattedValues)
				for index, value := range sqlRegexp.Split(values[3].(string), -1) {
					sql += value
					if index < formattedValuesLength {
						sql += formattedValues[index]
					}
				}
			}

			messages = append(messages, sql)
			messages = append(messages, fmt.Sprintf("[%v]", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned "))
		} else {
			messages = append(messages, "\033")
			messages = append(messages, values[2:]...)
			messages = append(messages, "\033")
		}
	}

	return
}

Functions

func ExecTrans

func ExecTrans(ctx context.Context, builder standard.IBuilder, trans ...Task) error

func GetConnect

func GetConnect(typeName typeName) standard.Connecter

数据库连接适配

func SetLogger

func SetLogger(log Logger)

Types

type CommonReq

type CommonReq struct {
	Start int32 `json:"start,omitempty"`
	Limit int32 `json:"limit,omitempty"`
	// 排序:sort=otc_type,-created_at,*custom
	// 以符号开头,可选符号:(+或空 正序)(- 倒序)(* 自定义复杂排序标识关键词)
	Sorts []string `json:"sorts,omitempty"`
}

type Connecter

type Connecter interface {
	Connection() string
}

数据库连接配置常量

type Dao

type Dao struct {
	*CommonReq
	// contains filtered or unexported fields
}

func NewDao

func NewDao(model IDao) *Dao

NewDao new

func RegisterDao

func RegisterDao(model IDao) *Dao

register dao

func (*Dao) Add

func (d *Dao) Add(ctx context.Context, value Tabler) error

func (*Dao) Delete

func (d *Dao) Delete(ctx context.Context, param IQuery) error

func (*Dao) Filter

func (d *Dao) Filter(build standard.IBuilder, condition *CommonReq) standard.IBuilder

filter

func (*Dao) Find

func (d *Dao) Find(ctx context.Context, param IQuery, result Tabler) error

func (*Dao) Get

func (d *Dao) Get(ctx context.Context, param IQuery, result interface{}) error

func (*Dao) GetCommonReq

func (d *Dao) GetCommonReq() *CommonReq

func (*Dao) Update

func (d *Dao) Update(ctx context.Context, param IQuery, update map[string]interface{}) error

type IDao

type IDao interface {
	// Tabler
	// Connecter
	IQuery
	// IFilter
	// 添加
	Add(ctx context.Context, value Tabler) error
	// 更新
	Update(ctx context.Context, param IQuery, update map[string]interface{}) error
	// 删除
	Delete(ctx context.Context, param IQuery) error
	// 查询单条
	Find(ctx context.Context, param IQuery, result Tabler) error
	// 查询多条
	Get(ctx context.Context, param IQuery, result interface{}) error
	// 查询
	Where(query interface{}, args ...interface{}) standard.IBuilder
	// get builder
	GetBuilder() standard.IBuilder
	// set builder
	SetBuilder(build standard.IBuilder) IDao
}

数据dao标准

type IFilter

type IFilter interface {
	BuildFilterQuery(filter IQuery) standard.IBuilder
	Filter(build standard.IBuilder, condition *CommonReq) standard.IBuilder
}

数据库过滤标准

type IQuery

type IQuery interface {
	GetCommonReq() *CommonReq
}

公共查询

type Logger

type Logger interface {
	Print(v ...interface{})
	WithCtx(ctx context.Context) Logger
}

type Tabler

type Tabler interface {
	TableName() string
}

数据库表标准

type Task

type Task func(builder standard.IBuilder) error

type TransError

type TransError struct {
	Code int
	Msg  string
}

func (TransError) Error

func (t TransError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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