orm

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 14 Imported by: 0

README

orm

orm

Documentation

Index

Constants

View Source
const (
	Or  = `OR`
	And = `AND`
)

Variables

View Source
var (
	ErrInvalidRowsTypes     = errors.New(`only *struct or []*struct types are supported`)
	ErrNotFoundField        = errors.New(`failed to match the field from the struct to the database. Please configure the borm tag correctly`)
	ErrNotFoundPrimaryField = errors.New(`failed to found primary field. Please configure the primary on borm tag correctly`)
	ErrInvalidTypes         = errors.New(`only *struct types are supported`)
	ErrInvalidFieldTypes    = errors.New(`only bool(1 is true, other is false),string、float64、float32、int、uint、int8、uint8、int16、uint16、int32、uint32、int64 and uint64 types are supported`)
)
View Source
var (
	ErrNoMasterConn = errors.New("mysql group: no master connection available")
	ErrNoSlaveConn  = errors.New("mysql group: no slave connection available")
)

Functions

func FormatRows

func FormatRows(rows *sql.Rows, handler RowFormat)

func SqlDelete

func SqlDelete(args *[]interface{}, table string, condition Condition) (sql string)

func SqlDeleteByObj

func SqlDeleteByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)

func SqlDeleteByWhere

func SqlDeleteByWhere(args *[]interface{}, table string, where Where) (sql string)

func SqlFindOneObj

func SqlFindOneObj(args *[]interface{}, condition Condition, obj interface{}) (sql string, err error)

func SqlInsert

func SqlInsert(args *[]interface{}, table string, rows ...map[string]interface{}) (sql string)

func SqlInsertObjs

func SqlInsertObjs(args *[]interface{}, rows interface{}) (sql string, err error)

func SqlUpdate

func SqlUpdate(args *[]interface{}, table string, set map[string]interface{}, condition Condition) (sql string)

func SqlUpdateByObj

func SqlUpdateByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)

func SqlUpdateByWhere

func SqlUpdateByWhere(args *[]interface{}, table string, set map[string]interface{}, where Where) (sql string)

func ToMap

func ToMap(rows *sql.Rows) ([]map[string]string, error)

func ToObj

func ToObj(rows *sql.Rows, obj interface{}) error

func ToObjList

func ToObjList(rows *sql.Rows, obj interface{}) (interface{}, error)

Types

type Condition

type Condition interface {
	Opt() (opt string)
	Sql(args *[]interface{}) (sql string)
}

func AndCondition

func AndCondition(fields map[string][]interface{}) Condition

func OrCondition

func OrCondition(fields map[string][]interface{}) Condition

type Group

type Group interface {
	BadPool(isMaster bool) (list []int)
	Query(useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	QueryContext(ctx context.Context, useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
	InsertObj(obj interface{}) (result sql.Result, err error)
	InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	DeleteObj(obj interface{}) (result sql.Result, err error)
	DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	UpdateObj(obj interface{}) (result sql.Result, err error)
	UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	Find(query Query, useMaster bool) (rows []map[string]string, err error)
	FindContext(ctx context.Context, query Query, useMaster bool) (rows []map[string]string, err error)
	FindAll(query Query, obj interface{}, useMaster bool) (objList interface{}, err error)
	FindAllContext(ctx context.Context, query Query, obj interface{}, useMaster bool) (objList interface{}, err error)
	FindOne(table string, condition Condition, useMaster bool) (row map[string]string, err error)
	FindOneContext(ctx context.Context, table string, condition Condition, useMaster bool) (row map[string]string, err error)
	FindOneObj(condition Condition, useMaster bool, obj interface{}) (err error)
	FindOneObjContext(ctx context.Context, condition Condition, useMaster bool, obj interface{}) (err error)
	Insert(table string, rows ...map[string]interface{}) (result sql.Result, err error)
	InsertContext(ctx context.Context, table string, rows ...map[string]interface{}) (result sql.Result, err error)
	DeleteAll(table string, condition Condition) (result sql.Result, err error)
	DeleteAllContext(ctx context.Context, table string, condition Condition) (result sql.Result, err error)
	UpdateAll(table string, set map[string]interface{}, condition Condition) (result sql.Result, err error)
	UpdateAllContext(ctx context.Context, table string, set map[string]interface{}, condition Condition) (result sql.Result, err error)
	Begin() (Transaction, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}

func NewMysqlGroup

func NewMysqlGroup(groupOption *GroupOption) (Group, error)

type GroupOption

type GroupOption struct {
	Masters []PoolOption `yaml:"masters" json:"masters"`
	Slaves  []PoolOption `yaml:"slaves" json:"slaves"`
	//单位s
	RetryInterval int64 `yaml:"retryInterval" json:"retryInterval"`
}

type Pool

type Pool interface {
	Query(sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
	QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
	Begin() (Transaction, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}

type PoolOption

type PoolOption struct {
	//格式:"userName:password@schema(host:port)/dbName",如:root:123456@tcp(127.0.0.1:3306)/test
	Dsn string `yaml:"dsn" json:"dsn"`
	//单位s
	MaxConnLifetime int `yaml:"maxConnLifetime" json:"maxConnLifetime"`
	MaxOpenConns    int `yaml:"maxOpenConns" json:"maxOpenConns"`
	MaxIdleConns    int `yaml:"maxIdleConns" json:"maxIdleConns"`
}

type Query

type Query interface {
	Select(columns ...string) Query
	From(table string) Query
	Where(condition Condition) Query
	AndWhere(condition Condition) Query
	OrWhere(condition Condition) Query
	Group(fields ...string) Query
	Having(having string) Query
	Order(orders ...string) Query
	Offset(offset int64) Query
	Limit(limit int64) Query
	Sql(arguments *[]interface{}) (sql string)
	Close()
}

func NewMysqlQuery

func NewMysqlQuery() Query

type RowFormat

type RowFormat func(fieldValue map[string][]byte)

type Transaction added in v1.0.1

type Transaction interface {
	Commit() (err error)
	Rollback() (err error)
	Query(sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
	InsertObj(obj interface{}) (result sql.Result, err error)
	InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	DeleteObj(obj interface{}) (result sql.Result, err error)
	DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	UpdateObj(obj interface{}) (result sql.Result, err error)
	UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	Find(query Query) (rows []map[string]string, err error)
	FindContext(ctx context.Context, query Query) (rows []map[string]string, err error)
	FindAll(query Query, obj interface{}) (objList interface{}, err error)
	FindAllContext(ctx context.Context, query Query, obj interface{}) (objList interface{}, err error)
	FindOne(table string, condition Condition) (row map[string]string, err error)
	FindOneContext(ctx context.Context, table string, condition Condition) (row map[string]string, err error)
	FindOneObj(condition Condition, obj interface{}) (err error)
	FindOneObjContext(ctx context.Context, condition Condition, obj interface{}) (err error)
	Insert(table string, rows ...map[string]interface{}) (result sql.Result, err error)
	InsertContext(ctx context.Context, table string, rows ...map[string]interface{}) (result sql.Result, err error)
	DeleteAll(table string, condition Condition) (result sql.Result, err error)
	DeleteAllContext(ctx context.Context, table string, condition Condition) (result sql.Result, err error)
	UpdateAll(table string, set map[string]interface{}, condition Condition) (result sql.Result, err error)
	UpdateAllContext(ctx context.Context, table string, set map[string]interface{}, condition Condition) (result sql.Result, err error)
}

type Where

type Where []WhereCondition

func (Where) Sql

func (w Where) Sql(args *[]interface{}) (sql string)

type WhereCondition

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

func AndWhere

func AndWhere(condition Condition) WhereCondition

func OrWhere

func OrWhere(condition Condition) WhereCondition

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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