[feat] 新增配置项
This commit is contained in:
parent
32031a7f53
commit
7f50d9eef1
@ -15,6 +15,11 @@ import (
|
|||||||
|
|
||||||
var conn *db.Connection
|
var conn *db.Connection
|
||||||
|
|
||||||
|
var defaultEngineOptions = engineOptions{
|
||||||
|
debug: false,
|
||||||
|
relations: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
database := db.Open(map[string]db.DBConfig{
|
database := db.Open(map[string]db.DBConfig{
|
||||||
"condition-engine-sqlite3": {
|
"condition-engine-sqlite3": {
|
||||||
@ -27,10 +32,10 @@ func init() {
|
|||||||
|
|
||||||
type Engine[T any] struct {
|
type Engine[T any] struct {
|
||||||
code string
|
code string
|
||||||
|
opts engineOptions
|
||||||
g contracts.GlobalParams
|
g contracts.GlobalParams
|
||||||
def func(data T, g contracts.GlobalParams) error
|
def func(data T, g contracts.GlobalParams) error
|
||||||
predicates []*EngineCase[T]
|
predicates []*EngineCase[T]
|
||||||
relations []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this Engine[T]) GetCode() string {
|
func (this Engine[T]) GetCode() string {
|
||||||
@ -104,7 +109,7 @@ func (this Engine[T]) toTables(rv reflect.Value, table string) (tables map[strin
|
|||||||
if rv.Kind() == reflect.Struct {
|
if rv.Kind() == reflect.Struct {
|
||||||
// 遍历结构体的字段
|
// 遍历结构体的字段
|
||||||
for i := 0; i < rt.NumField(); i++ {
|
for i := 0; i < rt.NumField(); i++ {
|
||||||
if _, ok := tables[rt.Field(i).Name]; !ok && lo.Contains(this.relations, rt.Field(i).Name) {
|
if _, ok := tables[rt.Field(i).Name]; !ok && lo.Contains(this.opts.relations, rt.Field(i).Name) {
|
||||||
if items, err := this.toTables(rv.Field(i).Elem(), rt.Field(i).Name); err == nil {
|
if items, err := this.toTables(rv.Field(i).Elem(), rt.Field(i).Name); err == nil {
|
||||||
tables = lo.Assign(tables, items)
|
tables = lo.Assign(tables, items)
|
||||||
} else {
|
} else {
|
||||||
@ -119,7 +124,7 @@ func (this Engine[T]) toTables(rv reflect.Value, table string) (tables map[strin
|
|||||||
} else if rv.Kind() == reflect.Map {
|
} else if rv.Kind() == reflect.Map {
|
||||||
iter := rv.MapRange()
|
iter := rv.MapRange()
|
||||||
for iter.Next() {
|
for iter.Next() {
|
||||||
if _, ok := tables[iter.Key().String()]; !ok && lo.Contains(this.relations, iter.Key().String()) {
|
if _, ok := tables[iter.Key().String()]; !ok && lo.Contains(this.opts.relations, iter.Key().String()) {
|
||||||
if items, err := this.toTables(iter.Value().Elem(), iter.Key().String()); err == nil {
|
if items, err := this.toTables(iter.Value().Elem(), iter.Key().String()); err == nil {
|
||||||
tables = lo.Assign(tables, items)
|
tables = lo.Assign(tables, items)
|
||||||
} else {
|
} else {
|
||||||
@ -175,7 +180,9 @@ func (this *Engine[T]) Execute(data T) error {
|
|||||||
|
|
||||||
result := map[string]int64{}
|
result := map[string]int64{}
|
||||||
|
|
||||||
// fmt.Println("sql", sql.ToSql())
|
if this.opts.debug {
|
||||||
|
fmt.Println("sql", sql.ToSql())
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := sql.First(&result); err != nil {
|
if _, err := sql.First(&result); err != nil {
|
||||||
return fmt.Errorf("%s => %s", err, sql.ToSql())
|
return fmt.Errorf("%s => %s", err, sql.ToSql())
|
||||||
@ -203,6 +210,16 @@ func (this *Engine[T]) Execute(data T) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func New[T any](table string, relations ...string) *Engine[T] {
|
func New[T any](table string, opt ...EngineOption) *Engine[T] {
|
||||||
return &Engine[T]{code: table, relations: relations, g: support.NewGlobalParam("", nil)}
|
opts := defaultEngineOptions
|
||||||
|
|
||||||
|
for _, o := range opt {
|
||||||
|
o.apply(&opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Engine[T]{
|
||||||
|
code: table,
|
||||||
|
opts: opts,
|
||||||
|
g: support.NewGlobalParam("", nil),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
30
engine/engine_option.go
Normal file
30
engine/engine_option.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
type engineOptions struct {
|
||||||
|
debug bool
|
||||||
|
relations []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type EngineOption interface {
|
||||||
|
apply(*engineOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
type fnOption struct {
|
||||||
|
f func(*engineOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this fnOption) apply(do *engineOptions) {
|
||||||
|
this.f(do)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Debug() EngineOption {
|
||||||
|
return &fnOption{f: func(o *engineOptions) {
|
||||||
|
o.debug = true
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Relation(opts ...string) EngineOption {
|
||||||
|
return &fnOption{f: func(o *engineOptions) {
|
||||||
|
o.relations = append(o.relations, opts...)
|
||||||
|
}}
|
||||||
|
}
|
@ -61,7 +61,10 @@ func TestRelationEngine(t *testing.T) {
|
|||||||
engine := Engine[any]{
|
engine := Engine[any]{
|
||||||
code: "TestTable",
|
code: "TestTable",
|
||||||
g: support.NewGlobalParam(`{"age": 30}`, nil),
|
g: support.NewGlobalParam(`{"age": 30}`, nil),
|
||||||
|
opts: engineOptions{
|
||||||
|
debug: false,
|
||||||
relations: []string{"TestTableA"},
|
relations: []string{"TestTableA"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cond1 := condition.New(contracts.ConditionType_AND, "条件1").
|
cond1 := condition.New(contracts.ConditionType_AND, "条件1").
|
||||||
|
Loading…
Reference in New Issue
Block a user