Coder Social home page Coder Social logo

xbuild's Introduction

English doc

XormBuilder

Go Reference

利用结构体TAG轻松构造XORM复杂的条件查询

可配置的TAG

TAG名为‘sql’,使用‘,’分割

Option Meaning
zero 允许零值,默认当字段为零值时忽略条件
- 跳过这个字段
no-null 额外拼接 'AND xx IS NOT NULL' 到条件中
or 使用 'OR' 与上一个字段拼接
opt=? 比较条件 eq/in/gt... 默认为eq
col=? 定义列名,默认为结构体字段名
func=? 设置一个可以应用到列名的SQL函数 Ex. TIMESTAMP

支持的查询条件

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

组合条件

使用分隔符 '&' or '|' 和数组或切片字段形成一组会被括号起来的条件. 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 < ?) 

使用嵌套结构体也可以实现条件分组,具体看测试文件

使用其他分隔符会造成PANIC

例子

具体查看 测试文件

Note: 未导出字段和Nil指针将被忽略,匿名结构体(组合)被视为一张表的组合条件,但是具名结构体会被视为另一张表,表明与结构体字段的名称一致

基准测试

简易基准测试:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.