f

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 16 Imported by: 2

README

gofree

符合 golang 风格的 ORM

  1. 消除链式调用,让 SQL 一目了然
  2. QueryBuilder 支持 DBA 审查 SQL,即使是非常复杂的SQL
  3. 接口友好,基于正确的场景设计接
  4. 接口设计松紧灵活,尽可能的消除 interface{} 的同时保持易用性.

Documentation

Index

Constants

View Source
const (
	DESC orderType = iota
	ASC
)

Variables

View Source
var NotesReadQB = NotesReadQBType{
	Message: `
请确保基于 f.QB 的查询结果不可变,或者基于时间倒叙查询。(从最早的数据查询到最新的数据)
目的是为了避免查询数据过程中有新的数据插入,此时 ReadQB 内部通过 offset 查询时会不准确。 
`,
}

Functions

func BetweenTime

func BetweenTime(v nonSupportBetweenTime)

you can use f.TimeRange, not BetweenTime

func GetUUID32

func GetUUID32(uuid string) string

func GetUUID36

func GetUUID36(uuid32 string) string

func ResetAndMock

func ResetAndMock(db Database, mock Mock)

func UUID

func UUID() string

Types

type AND

type AND map[string]OP

QueryBuilder Where

func And

func And(v ...interface{}) []AND
f.And("name","nimo")

接收 ...interface{} 作为参数而不是 map[string]interface{} 是因为会存在这种情况 f.And("age", f.Lt(19), "age", f.Gt(10))

func Or

func Or(find ...[]AND) (andList []AND)

FindOr(Find(...), Find(...))

type Alter

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

func (Alter) Modify

func (al Alter) Modify(migrateField MigrateField) Alter

type CUDIDList

type CUDIDList struct {
	ExistIDList  []string
	UpdateIDList []string
}

type CUDInfo

type CUDInfo struct {
	DeleteIDList []string
}

func CUD

func CUD(data CUDIDList) (output CUDInfo)

type CreateTableInfo

type CreateTableInfo struct {
	TableName      string
	Fields         []MigrateField
	Engine         string
	DefaultCharset string
	Collate        string
}

type DataSourceName

type DataSourceName struct {
	DriverName string
	User       string
	Password   string
	Host       string
	Port       string
	DB         string
	Query      map[string]string
}

DataSourceName.User 是个 map[string]stirng 结构 用于生成 dataSourceName 中的 ?charset=utf8&parseTime=True&loc=Local 部分

func (DataSourceName) GetString

func (config DataSourceName) GetString() (dataSourceName string)

type Database

type Database struct {
	Core *sqlx.DB
	// contains filtered or unexported fields
}

func NewDatabase

func NewDatabase(dataSourceName DataSourceName) (database Database)

func (Database) Close

func (database Database) Close()

func (*Database) CountQB

func (db *Database) CountQB(modelPtr Model, qb QB) (count int)

func (*Database) Create

func (db *Database) Create(modelPtr Model)

func (*Database) Delete

func (db *Database) Delete(modelPtr Model)

func (*Database) DeleteQB

func (db *Database) DeleteQB(modelPtr Model, qb QB)

func (Database) GetDataSourceName

func (database Database) GetDataSourceName() DataSourceName

func (*Database) ListQB

func (db *Database) ListQB(modelListPtr interface{}, qb QB)

func (*Database) OneID

func (db *Database) OneID(modelPtr Model, has *bool, id interface{})

func (*Database) OneQB

func (db *Database) OneQB(modelPtr Model, has *bool, qb QB)

func (Database) ReadQB

func (db Database) ReadQB(conf ReadQB)

must use asc by order created at or query results are immutable

func (Database) Tx

func (db Database) Tx() *Tx

func (*Database) TxCountQB

func (db *Database) TxCountQB(tx *Tx, modelPtr Model, qb QB) (count int)

func (*Database) TxCreate

func (db *Database) TxCreate(tx *Tx, modelPtr Model)

func (*Database) TxDelete

func (db *Database) TxDelete(tx *Tx, modelPtr Model, qb QB)

func (*Database) TxDeleteQB

func (db *Database) TxDeleteQB(tx *Tx, modelPtr Model, qb QB)

func (*Database) TxListQB

func (db *Database) TxListQB(tx *Tx, modelListPtr []Model, qb QB)

func (*Database) TxOneID

func (db *Database) TxOneID(tx *Tx, modelPtr Model, has *bool, id interface{})

func (*Database) TxOneQB

func (db *Database) TxOneQB(tx *Tx, modelPtr Model, has *bool, qb QB)

func (*Database) TxUpdate

func (db *Database) TxUpdate(tx *Tx, modelPtr Model)

func (*Database) Update

func (db *Database) Update(modelPtr Model)

type Filter

type Filter struct {
	FieldWrap    string
	FieldWarpArg string
	Value        interface{}
	Kind         string
	Symbol       string
	Like         string
	Custom       string
	CustomSQL    string
	TimeValue    time.Time
	TimeRange    FilterTimeRange
	BetweenInt   struct {
		Begin int
		End   int
	}
	BetweenFloat struct {
		Begin float64
		End   float64
	}
	BetweenString struct {
		Begin string
		End   string
	}
}

func BetweenFloat

func BetweenFloat(begin float64, end float64) Filter

func BetweenInt

func BetweenInt(begin int, end int) Filter

func BetweenString

func BetweenString(begin string, end string) Filter

func Custom

func Custom(template string, v ...interface{}) Filter

func CustomSQL

func CustomSQL(sql string, values ...interface{}) Filter

func Day

func Day(v time.Time) Filter

func Eql

func Eql(v interface{}) Filter

func Gt

func Gt(v interface{}) Filter

func GtEql

func GtEql(v interface{}) Filter

func Ignore

func Ignore(filter Filter, ignoreCondition bool) Filter

在 IgnoreEmpty 和 IgnorePattern 的场景下WHERE 语句都是 field = ? 有些场景下可能需要 where field in ? 或者没有 field in ? 此时使用 Ignore 完全自定义控制

使用场景:
f.QB{
	Where: f.And(
		"id": f.Ignore(len(query.idList) == 0, f.In(query.idList))
	),
}

func IgnoreEmpty

func IgnoreEmpty(filterFunc FilterFunc, query string) Filter

在查询中有一种常见的场景,当某个请求参数为空时不增加 where。 比如用户搜索姓名, ?name=nimo 时SQL是 WHERE name = ? 。 如果 ?name= (空字符串)则 sql 没有 name = ? gofree 称这种 where 条件为 ignore empty

使用场景:
f.QB{
	Where: f.And(
		"name": f.IgnoreEmpty(f.Eql, query.Name)
	),
}

func IgnoreFilter

func IgnoreFilter() Filter

func IgnorePattern

func IgnorePattern(filterFunc FilterFunc, query string, pattern string) Filter

基于 IgnoreEmpty 的场景下,有些请求并不一定会是空,而是 ?status=all 来表示搜索全部 ?status=done 表示搜索已完成的数据 ,此时使用 IgnorePattern(f.Eql, query.Status, "all")

使用场景:
f.QB{
	Where: f.And(
		"status": f.IgnorePattern( f.Eql, query.Status, "all")
	),
}

func In

func In(v interface{}) Filter

func IsNotNull

func IsNotNull() Filter

func IsNull

func IsNull() Filter

func Like

func Like(v interface{}) Filter

func LikeEnd

func LikeEnd(v interface{}) Filter

func LikeStart

func LikeStart(v interface{}) Filter

func Lt

func Lt(v interface{}) Filter

func LtEql

func LtEql(v interface{}) Filter

func NotEql

func NotEql(v interface{}) Filter

func NotIn

func NotIn(v interface{}) Filter

func TimeRange

func TimeRange(data gtime.Range, sqlValueLayout string) Filter

func (Filter) Dict

func (self Filter) Dict() (dict struct {
	Kind struct {
		Day           string
		Not           string
		IsNotNull     string
		IsNull        string
		Custom        string
		CustomSQL     string
		In            string
		NotIn         string
		Like          string
		GofreeIgnore  string
		TimeRange     string
		BetweenInt    string
		BetweenFloat  string
		BetweenString string
	}
})

type FilterFunc

type FilterFunc func(v interface{}) Filter

type FilterTimeRange

type FilterTimeRange struct {
	Range          gtime.Range
	SQLValueLayout string `note:"value use gtime.Day gtime.Second"`
}

type Group

type Group struct {
	Field string
}

type Map

type Map map[string]interface{}

type Migrate

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

func NewMigrate

func NewMigrate(db Database) Migrate

func (Migrate) AlterTable

func (Migrate) AlterTable(tableName string) Alter

func (Migrate) CUDTimestamp

func (mi Migrate) CUDTimestamp() []MigrateField

func (Migrate) CreateTable

func (Migrate) CreateTable(info CreateTableInfo)

func (Migrate) CreatedAtTimestamp

func (mi Migrate) CreatedAtTimestamp() MigrateField

func (Migrate) CurrentTimeStamp

func (mi Migrate) CurrentTimeStamp() migrateDefaultValue

func (Migrate) DefaultString

func (mi Migrate) DefaultString(s string) migrateDefaultValue

func (Migrate) DeletedAtTimestamp

func (mi Migrate) DeletedAtTimestamp() MigrateField

func (Migrate) Field

func (Migrate) Field(name string) MigrateField

func (Migrate) Init

func (mi Migrate) Init(db Database)

func (Migrate) InnoDB

func (mi Migrate) InnoDB() string

func (Migrate) MigrateName

func (Migrate) MigrateName(name string)

func (Migrate) UpdatedAtTimestamp

func (mi Migrate) UpdatedAtTimestamp() MigrateField

func (Migrate) Utf8mb4

func (mi Migrate) Utf8mb4() string

func (Migrate) Utf8mb4_unicode_ci

func (mi Migrate) Utf8mb4_unicode_ci() string

type MigrateField

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

func (MigrateField) AutoIncrement

func (mi MigrateField) AutoIncrement() MigrateField

func (MigrateField) Collate

func (mi MigrateField) Collate(kind string) MigrateField

func (MigrateField) Commit

func (mi MigrateField) Commit(commit string) MigrateField

func (MigrateField) Default

func (mi MigrateField) Default(value migrateDefaultValue) MigrateField

func (MigrateField) Extra

func (mi MigrateField) Extra(extra string) MigrateField

func (MigrateField) Int

func (mi MigrateField) Int(size int) MigrateField

func (MigrateField) Null

func (mi MigrateField) Null() MigrateField

func (MigrateField) OnUpdateCurrentTimeStamp

func (mi MigrateField) OnUpdateCurrentTimeStamp() MigrateField

func (MigrateField) PrimaryKey

func (mi MigrateField) PrimaryKey(field string) MigrateField

func (MigrateField) Text

func (mi MigrateField) Text() MigrateField

func (MigrateField) Timestamp

func (mi MigrateField) Timestamp() MigrateField

func (MigrateField) Tinyint

func (mi MigrateField) Tinyint(size int) MigrateField

func (MigrateField) Unsigned

func (mi MigrateField) Unsigned() MigrateField

func (MigrateField) Varchar

func (mi MigrateField) Varchar(size int) MigrateField

type MigrateModel

type MigrateModel struct {
	ID    int    `db:"id"`
	Name  string `db:"name"`
	Batch int    `db:"batch"`
	Data  string `db:"data"`
}

type Mock

type Mock struct {
	Tables []interface{}
}

type Model

type Model interface {
	TableName() string
	BeforeCreate()
}

type NotesReadQBType

type NotesReadQBType struct {
	Message string
}

type OP

type OP []Filter

type Order

type Order struct {
	// Field Type 的顺序永远不能换
	Field string
	Type  orderType
}

type QB

type QB struct {
	Table      string
	UseIndex   string
	Select     []string
	Where      []AND
	Offset     int
	Limit      int
	Order      []Order
	Group      []string
	SoftDelete string
	Insert     Map
	Update     Map
	Count      bool
	Debug      bool
	Check      []string
}

func Where

func Where(v ...interface{}) QB

func (QB) BindModel

func (qb QB) BindModel(model interface{}) QB

func (QB) GetInsert

func (qb QB) GetInsert() (sql string, sqlValues []interface{})

func (QB) GetSelect

func (qb QB) GetSelect() (sql string, sqlValues []interface{})

filter list interface maybe Filter

func (QB) GetUpdate

func (qb QB) GetUpdate() (sql string, sqlValues []interface{})

func (QB) Paging

func (qb QB) Paging(page int, perPage int) QB

func (QB) SQL

func (qb QB) SQL(props SQLProps) (sql string, sqlValues []interface{})

type ReadQB

type ReadQB struct {
	Limit   int
	ListPtr interface{}
	QB      QB
	Read    func()
	Notes   NotesReadQBType
}

配置项

type SQLProps

type SQLProps struct {
	Statement string `eg:"[]string{\"SELECT\", \"UPDATE\", \"DELETE\", \"INSERT\"}"`
}

type Tx

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

func (*Tx) Commit

func (tx *Tx) Commit()

func (*Tx) Core

func (tx *Tx) Core() *sqlx.Tx

func (*Tx) End

func (tx *Tx) End(recoverValue interface{})

使用此函数千万注意不要出现多个 defer 都运行 recover() ,并且 recover 不是 nil 时会向上传递 建议阅读此函数源码了解运行机制(代码很简单的哦)

func (*Tx) Rollback

func (tx *Tx) Rollback()

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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