[add] migrate query & support for binding with connectors

This commit is contained in:
Max 2023-02-02 16:50:01 +08:00
parent 44bbe5b383
commit 1240d83078
3 changed files with 78 additions and 16 deletions

View file

@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}')
NOW := $(shell date +"%FT%T%z") NOW := $(shell date +"%FT%T%z")
# ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector' | grep -vE 'examples|tests*|config|widgets') TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query' | grep -vE 'examples|tests*|config|widgets')
TESTTAGS ?= "" TESTTAGS ?= ""
# TESTWIDGETS := $(shell $(GO) list ./widgets/...) # TESTWIDGETS := $(shell $(GO) list ./widgets/...)

View file

@ -1,6 +1,7 @@
package query package query
import ( import (
"github.com/yaoapp/gou/connector"
"github.com/yaoapp/gou/model" "github.com/yaoapp/gou/model"
"github.com/yaoapp/gou/query" "github.com/yaoapp/gou/query"
dsl "github.com/yaoapp/gou/query/gou" dsl "github.com/yaoapp/gou/query/gou"
@ -11,21 +12,45 @@ import (
// Load 加载查询引擎 // Load 加载查询引擎
func Load(cfg config.Config) { func Load(cfg config.Config) {
DefaultQuery()
if _, has := query.Engines["default"]; !has {
registerDefault()
}
// register connector
for id, conn := range connector.Connectors {
if _, has := query.Engines[id]; has {
continue
}
if conn.Is(connector.DATABASE) {
qb, err := conn.Query()
if err != nil {
log.Error("[Query] load connector error %v", err.Error())
continue
}
query.Register(id, &dsl.Query{
Query: qb,
GetTableName: func(s string) string { return s },
AESKey: config.Conf.DB.AESKey,
})
}
}
} }
// DefaultQuery register the default engine // registerDefaultQuery register the default engine
func DefaultQuery() { func registerDefault() {
query.Register("default", &dsl.Query{ if capsule.Global != nil {
Query: capsule.Query(), query.Register("default", &dsl.Query{
GetTableName: func(s string) string { Query: capsule.Query(),
if mod, has := model.Models[s]; has { GetTableName: func(s string) string {
return mod.MetaData.Table.Name if mod, has := model.Models[s]; has {
} return mod.MetaData.Table.Name
log.Error("%s model does not load", s) }
return s log.Error("%s model does not load", s)
}, return s
AESKey: config.Conf.DB.AESKey, },
}) AESKey: config.Conf.DB.AESKey,
query.Alias("default", "xiang") })
}
} }

37
query/query_test.go Normal file
View file

@ -0,0 +1,37 @@
package query
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/query"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/connector"
"github.com/yaoapp/yao/test"
)
func TestLoad(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
loadConnectors(t)
Load(config.Conf)
check(t)
}
func check(t *testing.T) {
ids := map[string]bool{}
for id := range query.Engines {
ids[id] = true
}
assert.True(t, ids["default"])
assert.True(t, ids["mysql"])
assert.True(t, ids["sqlite"])
}
func loadConnectors(t *testing.T) {
err := connector.Load(config.Conf)
if err != nil {
t.Fatal(err)
}
}