[add] migrate query & support for binding with connectors
This commit is contained in:
parent
44bbe5b383
commit
1240d83078
3 changed files with 78 additions and 16 deletions
2
Makefile
2
Makefile
|
|
@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}')
|
|||
NOW := $(shell date +"%FT%T%z")
|
||||
|
||||
# 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 ?= ""
|
||||
|
||||
# TESTWIDGETS := $(shell $(GO) list ./widgets/...)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"github.com/yaoapp/gou/connector"
|
||||
"github.com/yaoapp/gou/model"
|
||||
"github.com/yaoapp/gou/query"
|
||||
dsl "github.com/yaoapp/gou/query/gou"
|
||||
|
|
@ -11,21 +12,45 @@ import (
|
|||
|
||||
// Load 加载查询引擎
|
||||
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
|
||||
func DefaultQuery() {
|
||||
query.Register("default", &dsl.Query{
|
||||
Query: capsule.Query(),
|
||||
GetTableName: func(s string) string {
|
||||
if mod, has := model.Models[s]; has {
|
||||
return mod.MetaData.Table.Name
|
||||
}
|
||||
log.Error("%s model does not load", s)
|
||||
return s
|
||||
},
|
||||
AESKey: config.Conf.DB.AESKey,
|
||||
})
|
||||
query.Alias("default", "xiang")
|
||||
// registerDefaultQuery register the default engine
|
||||
func registerDefault() {
|
||||
if capsule.Global != nil {
|
||||
query.Register("default", &dsl.Query{
|
||||
Query: capsule.Query(),
|
||||
GetTableName: func(s string) string {
|
||||
if mod, has := model.Models[s]; has {
|
||||
return mod.MetaData.Table.Name
|
||||
}
|
||||
log.Error("%s model does not load", s)
|
||||
return s
|
||||
},
|
||||
AESKey: config.Conf.DB.AESKey,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
37
query/query_test.go
Normal file
37
query/query_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue