diff --git a/engine/engine.go b/engine/engine.go index 8aa33f1..f083bfd 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -15,6 +15,11 @@ import ( var conn *db.Connection +var defaultEngineOptions = engineOptions{ + debug: false, + relations: []string{}, +} + func init() { database := db.Open(map[string]db.DBConfig{ "condition-engine-sqlite3": { @@ -27,10 +32,10 @@ func init() { type Engine[T any] struct { code string + opts engineOptions g contracts.GlobalParams def func(data T, g contracts.GlobalParams) error predicates []*EngineCase[T] - relations []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 { // 遍历结构体的字段 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 { tables = lo.Assign(tables, items) } else { @@ -119,7 +124,7 @@ func (this Engine[T]) toTables(rv reflect.Value, table string) (tables map[strin } else if rv.Kind() == reflect.Map { iter := rv.MapRange() 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 { tables = lo.Assign(tables, items) } else { @@ -175,7 +180,9 @@ func (this *Engine[T]) Execute(data T) error { 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 { return fmt.Errorf("%s => %s", err, sql.ToSql()) @@ -203,6 +210,16 @@ func (this *Engine[T]) Execute(data T) error { return nil } -func New[T any](table string, relations ...string) *Engine[T] { - return &Engine[T]{code: table, relations: relations, g: support.NewGlobalParam("", nil)} +func New[T any](table string, opt ...EngineOption) *Engine[T] { + opts := defaultEngineOptions + + for _, o := range opt { + o.apply(&opts) + } + + return &Engine[T]{ + code: table, + opts: opts, + g: support.NewGlobalParam("", nil), + } } diff --git a/engine/engine_option.go b/engine/engine_option.go new file mode 100644 index 0000000..9da088d --- /dev/null +++ b/engine/engine_option.go @@ -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...) + }} +} diff --git a/engine/engine_test.go b/engine/engine_test.go index 349103f..063a318 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -59,9 +59,12 @@ func TestEngine(t *testing.T) { func TestRelationEngine(t *testing.T) { engine := Engine[any]{ - code: "TestTable", - g: support.NewGlobalParam(`{"age": 30}`, nil), - relations: []string{"TestTableA"}, + code: "TestTable", + g: support.NewGlobalParam(`{"age": 30}`, nil), + opts: engineOptions{ + debug: false, + relations: []string{"TestTableA"}, + }, } cond1 := condition.New(contracts.ConditionType_AND, "条件1").