xbuild

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 7 Imported by: 0

README

XormBuilder

Go Reference

SQL builder base on xorm. Using a struct to build query sql.

Config tags

Using tag 'sql'. Split by ','. Key-value like 'k=v'

Option Meaning
zero Allowed zero value
no-null Concat 'AND xx IS NOT NULL' when building
or Concat previous condition by 'OR'
opt=? eq/in/gt... default is eq
col=? define column name, default is field name

FieldName Operation judging has been deprecated

Supported opt
Opt Meaning
eq =
neq !=
gt >
ge >=
lt <
le <=
in IN
not-in/nin NOT IN
like LIKE %value%
like-l LIKE %value
like-r LIKE value%
btw BETWEEN v1 AND v2

btw panic if value is not array or slice or containing nil element

Group opt

Using '&' or '|' to group conditions with array parameter. Ex:

type Cond struct {
  Age  *[2]int 				`sql:"opt=gt&le"`       //zero value element will be ignored
  Date *[2]time.Time  `sql:"opt=gt|lt,zero"`  //allow zero value element
}
// age >= ? AND age < ? AND (date >= ? OR date < ?) 

using others splitter will cause panic

Example

See test file

Note: Anonymous field thinks as a part of main table and named field is a joined table. All un-exported or nil field will be ignored.

Benchmark

Simple benchmark:

goos: darwin
goarch: amd64
pkg: github.com/838239178/xbuild
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkDeepCondAlias
BenchmarkDeepCondAlias-4   	  106696	     11351 ns/op	    5889 B/op	     161 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilValue  = errors.New("nil value")
	ErrNotStruct = errors.New("not struct")
)

Functions

func DeepCond

func DeepCond(bean interface{}) (builder.Cond, error)

func DeepCondAlias

func DeepCondAlias(bean interface{}, tableAlias string) (builder.Cond, error)

func SetXormNames

func SetXormNames(mapper names.Mapper)

Types

This section is empty.

Jump to

Keyboard shortcuts

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