orm

package module
v0.0.0-...-17d9e79 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 12 Imported by: 0

README

orm

TODO

  • sql builder
    • builder 和 action 分离
    • 支持 join
    • 如果没有 join, 不需要使用 表名.字段名
    • 支持 subquery
    • 无需指定表名的场景, 不用加表名称
  • 完善 scan
    • 完善 支持 json
    • 支持同一个表的字段被多次bind的场景
  • 完善 payload
    • payload 自动更新
  • 补充测试用例

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Log   = log.New(os.Stdout, "[SQL] ", log.LstdFlags)
	Debug = log.New(os.Stdout, "[DEBUG] ", log.Lshortfile)
)

logger

View Source
var (
	BaseDir = filepath.Clean(fmt.Sprintf("%s", filepath.Dir(b))) // nolint
)
View Source
var (
	ErrNotFund = errors.New("资源未找到")
)

errors

Functions

func BindField

func BindField[T any](ref *T, f Field[T], base *PayloadBase)

func BindFieldIfc

func BindFieldIfc(ref any, f FieldIfc, base *PayloadBase)

func FieldWrapper

func FieldWrapper(field string) string

Types

type Action

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

func (*Action) Delete

func (o *Action) Delete() *Stmt

func (*Action) InsertPayload

func (o *Action) InsertPayload(rows ...PayloadIfc) *Stmt

func (*Action) Select

func (o *Action) Select(field ...FieldIfc) *Stmt

func (*Action) Update

func (o *Action) Update(cond ...Cond) *Stmt

func (*Action) UpdatePayload

func (o *Action) UpdatePayload(payload PayloadIfc) *Stmt

type Client

type Client struct {
	DB *DefaultExecutor
	// contains filtered or unexported fields
}

func NewClient

func NewClient(driverName, dataSourceName string) (*Client, error)

func NewFromDB

func NewFromDB(db *sql.DB) (*Client, error)

func (*Client) Table

func (c *Client) Table(schema Schema) *Action

func (*Client) Transaction

func (c *Client) Transaction(ctx context.Context, fn func(s *Session) error) (err error)

type Cond

type Cond struct {
	Op string
	// contains filtered or unexported fields
}

func And

func And(cond ...Cond) Cond

func Or

func Or(cond ...Cond) Cond

func (*Cond) Expr

func (c *Cond) Expr() (expr string, args []any)

type DefaultExecutor

type DefaultExecutor struct {
	*sql.DB
}

func NewDefaultExecutor

func NewDefaultExecutor(db *sql.DB) *DefaultExecutor

func (*DefaultExecutor) ExecContext

func (e *DefaultExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DefaultExecutor) QueryContext

func (e *DefaultExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type ExecutorIfc

type ExecutorIfc interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

type ExprIfc

type ExprIfc interface {
	Expr() (string, []any)
}

func OrderBy

func OrderBy(order ...Order) ExprIfc

func Select

func Select(table Schema, fields ...FieldIfc) ExprIfc

func Where

func Where(cond ...Cond) ExprIfc

type ExprSlice

type ExprSlice []ExprIfc

func (ExprSlice) Expr

func (a ExprSlice) Expr() (expr string, args []any)

type Field

type Field[T any] struct {
	Name          string
	Schema        Schema
	AutoIncrement bool
}

func NewField

func NewField[T any](name string, schema Schema) *Field[T]

func (Field[T]) Asc

func (f Field[T]) Asc() Order

func (Field[T]) ColName

func (f Field[T]) ColName(withEscape bool) string

func (Field[T]) DBColName

func (f Field[T]) DBColName(withEscape bool) string

func (Field[T]) Desc

func (f Field[T]) Desc(desc bool) Order

func (Field[T]) Eq

func (f Field[T]) Eq(val T) Cond

func (Field[T]) EqCol

func (f Field[T]) EqCol(col Field[T]) Cond

func (Field[T]) EqQuery

func (f Field[T]) EqQuery(q ExprIfc) Cond

func (Field[T]) Expr

func (f Field[T]) Expr() (string, []any)

func (Field[T]) Gt

func (f Field[T]) Gt(val T) Cond

func (Field[T]) GtCol

func (f Field[T]) GtCol(col FieldIfc) Cond

func (Field[T]) Gte

func (f Field[T]) Gte(val T) Cond

func (Field[T]) GteCol

func (f Field[T]) GteCol(col FieldIfc) Cond

func (Field[T]) In

func (f Field[T]) In(val ...T) Cond

func (Field[T]) InQuery

func (f Field[T]) InQuery(q ExprIfc) Cond

func (Field[T]) IsAutoIncrement

func (f Field[T]) IsAutoIncrement() bool

func (Field[T]) IsNull

func (f Field[T]) IsNull(isNull bool) Cond

func (Field[T]) Lt

func (f Field[T]) Lt(val T) Cond

func (Field[T]) LtCol

func (f Field[T]) LtCol(col FieldIfc) Cond

func (Field[T]) Lte

func (f Field[T]) Lte(val T) Cond

func (Field[T]) LteCol

func (f Field[T]) LteCol(col FieldIfc) Cond

func (Field[T]) NotEq

func (f Field[T]) NotEq(val T) Cond

func (Field[T]) NotEqCol

func (f Field[T]) NotEqCol(col Field[T]) Cond

func (Field[T]) NotIn

func (f Field[T]) NotIn(val ...T) Cond

func (Field[T]) NotInQuery

func (f Field[T]) NotInQuery(q ExprIfc) Cond

func (*Field[T]) SetAutoIncrement

func (f *Field[T]) SetAutoIncrement(b bool)

type FieldGroup

type FieldGroup []FieldIfc

func Group

func Group(field ...FieldIfc) FieldGroup

func (FieldGroup) InQuery

func (fg FieldGroup) InQuery(q ExprIfc) Cond

func (FieldGroup) NotInQuery

func (fg FieldGroup) NotInQuery(q ExprIfc) Cond

type FieldIfc

type FieldIfc interface {
	ColName(withEscape bool) string
	DBColName(withEscape bool) string
	IsAutoIncrement() bool
	ExprIfc
	// contains filtered or unexported methods
}

type Order

type Order struct {
	Field FieldIfc
	Desc  bool
}

func (*Order) Expr

func (a *Order) Expr() (expr string, args []any)

type PayloadBase

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

func (*PayloadBase) BindField

func (p *PayloadBase) BindField(ref any, f FieldIfc)

func (*PayloadBase) BoundFields

func (p *PayloadBase) BoundFields() []*fieldBind

func (*PayloadBase) Fields

func (p *PayloadBase) Fields() []FieldIfc

type PayloadIfc

type PayloadIfc interface {
	Bind()
	BoundFields() []*fieldBind
	Fields() []FieldIfc
}

type Schema

type Schema interface {
	TableName() string
	IDField() FieldIfc
}

type Session

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

func (*Session) Table

func (s *Session) Table(schema Schema) *Action

type Stmt

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

func (*Stmt) Do

func (a *Stmt) Do(ctx context.Context) (rowCnt int64, err error)

func (*Stmt) FindPayload

func (a *Stmt) FindPayload(ctx context.Context, payloadsRef any) error

func (*Stmt) GroupBy

func (a *Stmt) GroupBy(field ...FieldIfc) *Stmt

GroupBy set sql group by

func (*Stmt) Join

func (a *Stmt) Join(s Schema, on ...Cond) *Stmt

func (*Stmt) LeftJoin

func (a *Stmt) LeftJoin(s Schema, on ...Cond) *Stmt

func (*Stmt) Limit

func (a *Stmt) Limit(l int64) *Stmt

Limit set sql limit

func (*Stmt) Offset

func (a *Stmt) Offset(o int64) *Stmt

Offset set sql offset

func (*Stmt) OrderBy

func (a *Stmt) OrderBy(order ...Order) *Stmt

OrderBy set sql order by

func (*Stmt) OuterJoin

func (a *Stmt) OuterJoin(s Schema, on ...Cond) *Stmt

func (*Stmt) Page

func (a *Stmt) Page(page, size int64) *Stmt

Page set sql limit and offset

func (*Stmt) RightJoin

func (a *Stmt) RightJoin(s Schema, on ...Cond) *Stmt

func (*Stmt) Select

func (a *Stmt) Select(field ...FieldIfc) *Stmt

func (*Stmt) Set

func (a *Stmt) Set(cond ...Cond) *Stmt

func (*Stmt) SubQuery

func (a *Stmt) SubQuery() ExprIfc

func (*Stmt) TakePayload

func (a *Stmt) TakePayload(ctx context.Context, payload PayloadIfc, nestedPayload ...any) error

func (*Stmt) Where

func (a *Stmt) Where(cond ...Cond) *Stmt

Where generate where condition

type TransactionIfc

type TransactionIfc interface {
	Rollback() error
	Commit() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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