Skip to content

yumubi/dynamic-SQL-constructor

 
 

Repository files navigation

fi

动态 SQL 查询条件构造器

Build Status MIT licensed Go Version codecov Go Report Card

快速开始

  • 反射生成过滤器
package main

import (
	"fmt"
	"github.com/lxzan/fi"
)

type Template struct {
	Name   string `filter:"cmp=like"`
	Age    int    `filter:"cmp=lte"`
	Height int
}

func main() {
	var v = &Template{
		Name: "caster",
		Age:  18,
	}
	var filter = fi.GetFilter(v)
	fmt.Printf("%s %v\n", filter.GetExpression(), filter.Args)
}

// `name` LIKE ? AND `age` <= ? [%caster% 18]
  • 手动构造过滤器
package main

import (
	"fmt"
	"github.com/lxzan/fi"
)

type Template struct {
	Name   string `filter:"cmp=like"`
	Age    int    `filter:"cmp=lte"`
	Height int
}

func main() {
	var v = &Template{
		Name: "caster",
		Age:  18,
	}
	var filter = fi.
		NewFilter().
		Like("name", v.Name).
		Lte("age", v.Age).
		Eq("height", v.Height)
	fmt.Printf("%s %v\n", filter.GetExpression(), filter.Args)
}

// `name` LIKE ? AND `age` <= ? [%caster% 18]

标签

字段 描述
column 自定义字段名; 默认值是下划线风格
cmp 比较操作符; 默认值是eq
- 忽略

操作符

操作符 描述
eq =
not_eq !=
gt >
lt <
gte >=
lte <=
like LIKE
not_like NOT LIKE
in IN
not_in NOT IN

性能测试

go test -benchmem -run=^$ -bench ^Benchmark github.com/lxzan/fi
goos: darwin
goarch: arm64
pkg: github.com/lxzan/fi
BenchmarkGetFilterReflect-8               501673              2374 ns/op            1376 B/op         37 allocs/op
BenchmarkGetFilterNoReflect-8            1650524               721.8 ns/op          1104 B/op         18 allocs/op
PASS
ok      github.com/lxzan/fi     4.035s

About

动态 SQL 查询条件构造器

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.1%
  • Makefile 0.9%