hey

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 7 Imported by: 1

README

What is HEY?

hey is an assistant tool similar to ORM to operate the database; its purpose is to allow you to perform addition, deletion, modification and query efficiently and quickly.

Advantages of HEY

hey does not limit you to use the database driver, you only need to provide the database connection pool instance *sql.DB

hey only supports the execution of a single SQL statement, if you need to execute SQL scripts in batches, please use (*sql.DB).Exec()

hey optional logging, you can use the CallTakeWarn() method to set the SQL execution time-consuming threshold, unit: milliseconds

hey uses ? as the placeholder of the SQL statement by default, if your database instance does not support it, please use the Prepare() method to preprocess the SQL statement

hey uses the tag-db value of the structure to match the query result field by default, if the tag does not match your business, you can use the Scanner() method to customize the matching rules

hey supports transactions, and allows nested calls to facilitate you to care more about business implementation

hey cleverly implements the conditional filtering of SQL statements, so you can easily use Filter to filter delete, update, query

hey When querying SQL, you can choose to use Scanner[reflection implementation] and rows.Scan() to customize the scan query results, performance and speed are up to you

Supports raw SQL execution by default, and raw SQL statements are valid in logs, transactions, Scanner, and SQL preprocessing

INSTALL

go get github.com/cd365/hey@latest

FOR EXAMPLE

package main

import (
	"database/sql"

	"github.com/cd365/hey"
)

func way() {
	var db *sql.DB

	way := hey.NewWay(db)
	way.Fix(hey.FixPgsql) // PostgreSQL
	way.Log(nil).Slow(10) // logger

	table := "account"

	add := way.Add(table)

	/* insert one */
	add.Column("name", "email").Values([]interface{}{
		"Jack",
		"jack@gmail.com",
	}).Add()

	/* insert batch */
	add.Column("name", "email").Values(
		[]interface{}{
			"Alice",
			"alice@gmail.com",
		},
		[]interface{}{
			"Tom",
			"tom@gmail.com",
		},
		// ...
	)

	/* delete */
	del := way.Del(table)
	del.WhereFunc(func(f hey.Filter) {
		f.In("id", 1, 2, 3)
	}).Del()

	/* update */
	mod := way.Mod(table)
	mod.WhereFunc(func(f hey.Filter) {
		f.In("id", 1, 2, 3)
	}).Incr("times", 1).Set("name", "Jerry").Mod()

	/* select count */
	get := way.Get()
	get.WhereFunc(func(f hey.Filter) {
		f.IsNotNull("id")
	}).Count()

	type ScanStruct struct {
		Name  string `db:"name"`
		Email string `db:"email"`
	}
	result := make([]*ScanStruct, 0)
	query := get.Column("name", "email").
		WhereFunc(func(f hey.Filter) {
			f.MoreThan("id", 0)
		}).
		OrderDesc("id").
		Limit(10).
		Offset(0)
	query.Get(&result)

	query.Query(func(rows *sql.Rows) (err error) {
		name := new(string)
		email := new(string)
		if err = rows.Scan(&name, &email); err != nil {
			return
		}
		// todo ...
		return nil
	})

	/* transaction */
	way.Trans(func(tx *hey.Way) (err error) {
		tx.Add(table).Add()   // todo...
		tx.Del(table).Del()   // todo...
		tx.Mod(table).Mod()   // todo...
		tx.Get().Table(table) // todo...
		return
	})
}

Documentation

Index

Constants

View Source
const (
	SqlAs   = "AS"
	SqlAsc  = "ASC"
	SqlDesc = "DESC"
)
View Source
const (
	DefaultTag = "db"
)
View Source
const (
	Placeholder = "?"
)

Variables

This section is empty.

Functions

func FixPgsql added in v1.0.1

func FixPgsql(str string) string

func RemoveDuplicate

func RemoveDuplicate(dynamic ...interface{}) (result []interface{})

RemoveDuplicate remove duplicate element

func ScanSliceStruct

func ScanSliceStruct(rows *sql.Rows, result interface{}, tag string) error

ScanSliceStruct Scan the query result set into the receiving object, the receiving object type is *[]AnyStruct or *[]*AnyStruct.

func StructAssign

func StructAssign(origin interface{}, latest interface{})

StructAssign struct attribute assignment, both the origin and latest parameters must be struct pointers.

func StructAttributeIndex

func StructAttributeIndex(rt reflect.Type) map[string]int

StructAttributeIndex struct attribute name => index

func StructInsert added in v1.0.1

func StructInsert(insert interface{}, tag string, except ...string) (column []string, values []interface{})

StructInsert create one by AnyStruct or *AnyStruct Obtain a list of all fields to be inserted and corresponding values through the tag attribute of the structure, and support the exclusion of fixed fields.

func StructModify

func StructModify(origin interface{}, latest interface{})

StructModify struct attribute is not nil assignment, both the origin and latest parameters must be struct pointers.

func StructUpdate added in v1.0.1

func StructUpdate(origin interface{}, latest interface{}, tag string) (modify map[string]interface{})

StructUpdate compare origin and latest for update

Types

type Add added in v1.0.1

type Add struct {
	*Tab
	// contains filtered or unexported fields
}

func NewAdd added in v1.0.1

func NewAdd(way *Way) *Add

func (*Add) Add added in v1.0.1

func (s *Add) Add() (int64, error)

func (*Add) AppendValues added in v1.0.1

func (s *Add) AppendValues(values ...[]interface{}) *Add

func (*Add) Column added in v1.0.1

func (s *Add) Column(column ...string) *Add

func (*Add) ColumnValues added in v1.0.1

func (s *Add) ColumnValues(column []string, values ...[]interface{}) *Add

func (*Add) Comment added in v1.0.1

func (s *Add) Comment(comment string) *Add

func (*Add) Context added in v1.0.1

func (s *Add) Context(ctx context.Context) *Add

func (*Add) Create added in v1.0.1

func (s *Add) Create(creates ...interface{}) *Add

func (*Add) Except added in v1.0.1

func (s *Add) Except(except ...string) *Add

Except only valid for Create and Map

func (*Add) Map added in v1.0.1

func (s *Add) Map(columnValue map[string]interface{}) *Add

func (*Add) SQL added in v1.0.1

func (s *Add) SQL() (prepare string, args []interface{})

func (*Add) Table added in v1.0.1

func (s *Add) Table(table string) *Add

func (*Add) Values added in v1.0.1

func (s *Add) Values(values ...[]interface{}) *Add

func (*Add) ValuesSubQuery added in v1.0.1

func (s *Add) ValuesSubQuery(prepare string, args ...interface{}) *Add

func (*Add) ValuesSubQueryGet added in v1.0.1

func (s *Add) ValuesSubQueryGet(get *Get) *Add

type Del added in v1.0.1

type Del struct {
	*Tab
	// contains filtered or unexported fields
}

func NewDel added in v1.0.1

func NewDel(way *Way) *Del

func (*Del) Comment added in v1.0.1

func (s *Del) Comment(comment string) *Del

func (*Del) Context added in v1.0.1

func (s *Del) Context(ctx context.Context) *Del

func (*Del) Del added in v1.0.1

func (s *Del) Del() (int64, error)

func (*Del) SQL added in v1.0.1

func (s *Del) SQL() (prepare string, args []interface{})

func (*Del) Table added in v1.0.1

func (s *Del) Table(table string) *Del

func (*Del) Where added in v1.0.1

func (s *Del) Where(where Filter) *Del

func (*Del) WhereEqual added in v1.0.1

func (s *Del) WhereEqual(column string, values interface{}) *Del

func (*Del) WhereFunc added in v1.0.1

func (s *Del) WhereFunc(where func(f Filter)) *Del

func (*Del) WhereIn added in v1.0.1

func (s *Del) WhereIn(column string, values ...interface{}) *Del

type Filter

type Filter interface {
	And(prepare string, args ...interface{}) Filter
	Or(prepare string, args ...interface{}) Filter
	Filter(filters ...Filter) Filter
	OrFilter(filters ...Filter) Filter
	Group(group func(filter Filter) Filter) Filter
	OrGroup(group func(filter Filter) Filter) Filter
	Equal(column string, value interface{}) Filter
	NotEqual(column string, value interface{}) Filter
	MoreThan(column string, value interface{}) Filter
	MoreThanEqual(column string, value interface{}) Filter
	LessThan(column string, value interface{}) Filter
	LessThanEqual(column string, value interface{}) Filter
	In(column string, values ...interface{}) Filter
	NotIn(column string, values ...interface{}) Filter
	Between(column string, start interface{}, end interface{}) Filter
	NotBetween(column string, start interface{}, end interface{}) Filter
	Like(column string, value interface{}) Filter
	NotLike(column string, value interface{}) Filter
	IsNull(column string) Filter
	IsNotNull(column string) Filter
	OrEqual(column string, value interface{}) Filter
	OrNotEqual(column string, value interface{}) Filter
	OrMoreThan(column string, value interface{}) Filter
	OrMoreThanEqual(column string, value interface{}) Filter
	OrLessThan(column string, value interface{}) Filter
	OrLessThanEqual(column string, value interface{}) Filter
	OrIn(column string, values ...interface{}) Filter
	OrNotIn(column string, values ...interface{}) Filter
	OrBetween(column string, start interface{}, end interface{}) Filter
	OrNotBetween(column string, start interface{}, end interface{}) Filter
	OrLike(column string, value interface{}) Filter
	OrNotLike(column string, value interface{}) Filter
	OrIsNull(column string) Filter
	OrIsNotNull(column string) Filter
	SQL() (prepare string, args []interface{})
}

func NewFilter

func NewFilter() Filter

type Get added in v1.0.1

type Get struct {
	*Tab // query table
	// contains filtered or unexported fields
}

func NewGet added in v1.0.1

func NewGet(way *Way) *Get

func (*Get) AddCol added in v1.0.1

func (s *Get) AddCol(column ...string) *Get

func (*Get) Alias added in v1.0.1

func (s *Get) Alias(name string, alias string) string

func (*Get) Column added in v1.0.1

func (s *Get) Column(column ...string) *Get

func (*Get) Comment added in v1.0.1

func (s *Get) Comment(comment string) *Get

func (*Get) Context added in v1.0.1

func (s *Get) Context(ctx context.Context) *Get

func (*Get) Count added in v1.0.1

func (s *Get) Count(column ...string) (count int64, err error)

func (*Get) FullJoin added in v1.0.1

func (s *Get) FullJoin() *GetJoin

func (*Get) Get added in v1.0.1

func (s *Get) Get(result interface{}) error

func (*Get) Group added in v1.0.1

func (s *Get) Group(group ...string) *Get

func (*Get) Having added in v1.0.1

func (s *Get) Having(having Filter) *Get

func (*Get) HavingFunc added in v1.0.1

func (s *Get) HavingFunc(having func(f Filter)) *Get

func (*Get) InnerJoin added in v1.0.1

func (s *Get) InnerJoin() *GetJoin

func (*Get) Join added in v1.0.1

func (s *Get) Join(joins ...*GetJoin) *Get

func (*Get) LeftJoin added in v1.0.1

func (s *Get) LeftJoin() *GetJoin

func (*Get) Limit added in v1.0.1

func (s *Get) Limit(limit int64) *Get

func (*Get) Offset added in v1.0.1

func (s *Get) Offset(offset int64) *Get

func (*Get) Order added in v1.0.1

func (s *Get) Order(column string, order string) *Get

func (*Get) OrderAsc added in v1.0.1

func (s *Get) OrderAsc(column string) *Get

func (*Get) OrderDesc added in v1.0.1

func (s *Get) OrderDesc(column string) *Get

func (*Get) Prefix added in v1.0.1

func (s *Get) Prefix(prefix string, name string) string

func (*Get) Query added in v1.0.1

func (s *Get) Query(query func(rows *sql.Rows) (err error)) error

func (*Get) RightJoin added in v1.0.1

func (s *Get) RightJoin() *GetJoin

func (*Get) SQL added in v1.0.1

func (s *Get) SQL() (prepare string, args []interface{})

func (*Get) SQLCount added in v1.0.1

func (s *Get) SQLCount(column ...string) (prepare string, args []interface{})

func (*Get) Table added in v1.0.1

func (s *Get) Table(table string) *Get

func (*Get) TableAlias added in v1.0.1

func (s *Get) TableAlias(alias string) *Get

func (*Get) TableSubQuery added in v1.0.1

func (s *Get) TableSubQuery(prepare string, args ...interface{}) *Get

func (*Get) TableSubQueryGet added in v1.0.1

func (s *Get) TableSubQueryGet(get *Get) *Get

func (*Get) Where added in v1.0.1

func (s *Get) Where(where Filter) *Get

func (*Get) WhereFunc added in v1.0.1

func (s *Get) WhereFunc(where func(f Filter)) *Get

type GetJoin added in v1.0.1

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

func NewGetJoin added in v1.0.1

func NewGetJoin(joinType JoinType) *GetJoin

func (*GetJoin) Alias added in v1.0.1

func (s *GetJoin) Alias(alias string) *GetJoin

func (*GetJoin) On added in v1.0.1

func (s *GetJoin) On(on string) *GetJoin

func (*GetJoin) OnEqual added in v1.0.1

func (s *GetJoin) OnEqual(left string, right string) *GetJoin

func (*GetJoin) SQL added in v1.0.1

func (s *GetJoin) SQL() (prepare string, args []interface{})

func (*GetJoin) Table added in v1.0.1

func (s *GetJoin) Table(table string) *GetJoin

func (*GetJoin) TableSubQuery added in v1.0.1

func (s *GetJoin) TableSubQuery(prepare string, args ...interface{}) *GetJoin

func (*GetJoin) TableSubQueryGet added in v1.0.1

func (s *GetJoin) TableSubQueryGet(get *Get) *GetJoin

type JoinType

type JoinType string
const (
	SqlJoinInner JoinType = "INNER JOIN"
	SqlJoinLeft  JoinType = "LEFT JOIN"
	SqlJoinRight JoinType = "RIGHT JOIN"
	SqlJoinFull  JoinType = "FULL JOIN"
)

type Logger

type Logger interface {
	Info(v ...interface{})
	Infof(format string, v ...interface{})
	Warn(v ...interface{})
	Warnf(format string, v ...interface{})
	Error(v ...interface{})
	Errorf(format string, v ...interface{})
}

type Mod added in v1.0.1

type Mod struct {
	*Tab
	// contains filtered or unexported fields
}

func NewMod added in v1.0.1

func NewMod(way *Way) *Mod

func (*Mod) Comment added in v1.0.1

func (s *Mod) Comment(comment string) *Mod

func (*Mod) Compare added in v1.0.1

func (s *Mod) Compare(origin interface{}, latest interface{}) *Mod

func (*Mod) Context added in v1.0.1

func (s *Mod) Context(ctx context.Context) *Mod

func (*Mod) Decr added in v1.0.1

func (s *Mod) Decr(column string, value interface{}) *Mod

func (*Mod) Except added in v1.0.1

func (s *Mod) Except(except ...string) *Mod

func (*Mod) Incr added in v1.0.1

func (s *Mod) Incr(column string, value interface{}) *Mod

func (*Mod) Map added in v1.0.1

func (s *Mod) Map(columnValue map[string]interface{}) *Mod

func (*Mod) Mod added in v1.0.1

func (s *Mod) Mod() (int64, error)

func (*Mod) SQL added in v1.0.1

func (s *Mod) SQL() (prepare string, args []interface{})

func (*Mod) Set added in v1.0.1

func (s *Mod) Set(column string, value interface{}) *Mod

func (*Mod) Slice added in v1.0.1

func (s *Mod) Slice(column []string, value []interface{}) *Mod

func (*Mod) Table added in v1.0.1

func (s *Mod) Table(table string) *Mod

func (*Mod) Where added in v1.0.1

func (s *Mod) Where(where Filter) *Mod

func (*Mod) WhereEqual added in v1.0.1

func (s *Mod) WhereEqual(column string, value interface{}) *Mod

func (*Mod) WhereFunc added in v1.0.1

func (s *Mod) WhereFunc(where func(f Filter)) *Mod

func (*Mod) WhereIn added in v1.0.1

func (s *Mod) WhereIn(column string, values ...interface{}) *Mod

type SubQuery

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

func NewSubQuery added in v1.0.1

func NewSubQuery(prepare string, args ...interface{}) *SubQuery

func (*SubQuery) SQL added in v1.0.1

func (s *SubQuery) SQL() (prepare string, args []interface{})

type Tab added in v1.0.1

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

type Way

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

func Choose added in v1.0.1

func Choose(way *Way, items ...*Way) *Way

func NewWay

func NewWay(db *sql.DB) *Way

func (*Way) Add added in v1.0.1

func (s *Way) Add(table string) *Add

func (*Way) Clone added in v1.0.1

func (s *Way) Clone() *Way

func (*Way) DB

func (s *Way) DB() *sql.DB

func (*Way) Del added in v1.0.1

func (s *Way) Del(table string) *Del

func (*Way) Exec

func (s *Way) Exec(ctx context.Context, prepare string, args ...interface{}) (rowsAffected int64, err error)

func (*Way) ExecAll added in v1.0.1

func (s *Way) ExecAll(ctx context.Context, script string, args ...interface{}) (int64, error)

func (*Way) Fix added in v1.0.1

func (s *Way) Fix(fix func(string) string) *Way

func (*Way) Get added in v1.0.1

func (s *Way) Get() *Get

func (*Way) Log added in v1.0.1

func (s *Way) Log(log Logger) *Way

func (*Way) Mod added in v1.0.1

func (s *Way) Mod(table string) *Mod

func (*Way) Query

func (s *Way) Query(ctx context.Context, query func(rows *sql.Rows) (err error), prepare string, args ...interface{}) (err error)

func (*Way) ScanAll added in v1.0.1

func (s *Way) ScanAll(ctx context.Context, result interface{}, prepare string, args ...interface{}) error

func (*Way) Slow added in v1.0.1

func (s *Way) Slow(slow time.Duration) *Way

func (*Way) Tag added in v1.0.1

func (s *Way) Tag(tag string) *Way

func (*Way) Trans added in v1.0.1

func (s *Way) Trans(fn func(tx *Way) (err error)) error

func (*Way) Transaction

func (s *Way) Transaction(ctx context.Context, opts *sql.TxOptions, fn func(tx *Way) (err error)) (err error)

func (*Way) TxNil

func (s *Way) TxNil() bool

Jump to

Keyboard shortcuts

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