提交 581e4867 编写于 作者: M Ming Deng

Merge pull request #4173 from AllenX2018/fix-bug-queryRow

Fix issue 3866
...@@ -23,6 +23,26 @@ cp ./githook/pre-commit ./.git/hooks/pre-commit ...@@ -23,6 +23,26 @@ cp ./githook/pre-commit ./.git/hooks/pre-commit
``` ```
This will add git hooks into .git/hooks. Or you can add it manually. This will add git hooks into .git/hooks. Or you can add it manually.
## Prepare middleware
Beego uses many middlewares, including MySQL, Redis, SSDB and so on.
We provide docker compose file to start all middlewares.
You can run:
```shell script
docker-compose -f scripts/test_docker_compose.yml up -d
```
Unit tests read addressed from environment, here is an example:
```shell script
export ORM_DRIVER=mysql
export ORM_SOURCE="beego:test@tcp(192.168.0.105:13306)/orm_test?charset=utf8"
export MEMCACHE_ADDR="192.168.0.105:11211"
export REDIS_ADDR="192.168.0.105:6379"
export SSDB_ADDR="192.168.0.105:8888"
```
## Contribution guidelines ## Contribution guidelines
### Pull requests ### Pull requests
......
// Copyright 2020 beego // Copyright 2020
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// we will move all web related codes here // used to keep compatible with v1.x
package web package adapter
...@@ -37,7 +37,7 @@ import ( ...@@ -37,7 +37,7 @@ import (
"github.com/bradfitz/gomemcache/memcache" "github.com/bradfitz/gomemcache/memcache"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
// Cache Memcache adapter. // Cache Memcache adapter.
......
...@@ -15,17 +15,26 @@ ...@@ -15,17 +15,26 @@
package memcache package memcache
import ( import (
"fmt"
"os"
_ "github.com/bradfitz/gomemcache/memcache" _ "github.com/bradfitz/gomemcache/memcache"
"strconv" "strconv"
"testing" "testing"
"time" "time"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
func TestMemcacheCache(t *testing.T) { func TestMemcacheCache(t *testing.T) {
bm, err := cache.NewCache("memcache", `{"conn": "127.0.0.1:11211"}`)
addr := os.Getenv("MEMCACHE_ADDR")
if addr == "" {
addr = "127.0.0.1:11211"
}
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
......
...@@ -39,7 +39,7 @@ import ( ...@@ -39,7 +39,7 @@ import (
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
var ( var (
......
...@@ -16,17 +16,24 @@ package redis ...@@ -16,17 +16,24 @@ package redis
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
func TestRedisCache(t *testing.T) { func TestRedisCache(t *testing.T) {
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {
redisAddr = "127.0.0.1:6379"
}
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
...@@ -110,8 +117,14 @@ func TestRedisCache(t *testing.T) { ...@@ -110,8 +117,14 @@ func TestRedisCache(t *testing.T) {
func TestCache_Scan(t *testing.T) { func TestCache_Scan(t *testing.T) {
timeoutDuration := 10 * time.Second timeoutDuration := 10 * time.Second
addr := os.Getenv("REDIS_ADDR")
if addr == "" {
addr = "127.0.0.1:6379"
}
// init // init
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
...@@ -121,6 +134,7 @@ func TestCache_Scan(t *testing.T) { ...@@ -121,6 +134,7 @@ func TestCache_Scan(t *testing.T) {
t.Error("set Error", err) t.Error("set Error", err)
} }
} }
time.Sleep(time.Second)
// scan all for the first time // scan all for the first time
keys, err := bm.(*Cache).Scan(DefaultKey + ":*") keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
if err != nil { if err != nil {
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"github.com/ssdb/gossdb/ssdb" "github.com/ssdb/gossdb/ssdb"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
// Cache SSDB adapter // Cache SSDB adapter
......
package ssdb package ssdb
import ( import (
"fmt"
"os"
"strconv" "strconv"
"testing" "testing"
"time" "time"
"github.com/astaxie/beego/pkg/cache" "github.com/astaxie/beego/pkg/client/cache"
) )
func TestSsdbcacheCache(t *testing.T) { func TestSsdbcacheCache(t *testing.T) {
ssdb, err := cache.NewCache("ssdb", `{"conn": "127.0.0.1:8888"}`)
ssdbAddr := os.Getenv("SSDB_ADDR")
if ssdbAddr == "" {
ssdbAddr = "127.0.0.1:8888"
}
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
"context" "context"
"net/http" "net/http"
"github.com/astaxie/beego/pkg/httplib" "github.com/astaxie/beego/pkg/client/httplib"
logKit "github.com/go-kit/kit/log" logKit "github.com/go-kit/kit/log"
opentracingKit "github.com/go-kit/kit/tracing/opentracing" opentracingKit "github.com/go-kit/kit/tracing/opentracing"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
......
...@@ -23,7 +23,7 @@ import ( ...@@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/httplib" "github.com/astaxie/beego/pkg/client/httplib"
) )
func TestFilterChainBuilder_FilterChain(t *testing.T) { func TestFilterChainBuilder_FilterChain(t *testing.T) {
......
...@@ -22,8 +22,8 @@ import ( ...@@ -22,8 +22,8 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
beego "github.com/astaxie/beego/pkg" "github.com/astaxie/beego/pkg/client/httplib"
"github.com/astaxie/beego/pkg/httplib" "github.com/astaxie/beego/pkg/server/web"
) )
type FilterChainBuilder struct { type FilterChainBuilder struct {
...@@ -36,9 +36,9 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt ...@@ -36,9 +36,9 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt
Name: "beego", Name: "beego",
Subsystem: "remote_http_request", Subsystem: "remote_http_request",
ConstLabels: map[string]string{ ConstLabels: map[string]string{
"server": beego.BConfig.ServerName, "server": web.BConfig.ServerName,
"env": beego.BConfig.RunMode, "env": web.BConfig.RunMode,
"appname": beego.BConfig.AppName, "appname": web.BConfig.AppName,
}, },
Help: "The statics info for remote http requests", Help: "The statics info for remote http requests",
}, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"}) }, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"})
......
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/httplib" "github.com/astaxie/beego/pkg/client/httplib"
) )
func TestFilterChainBuilder_FilterChain(t *testing.T) { func TestFilterChainBuilder_FilterChain(t *testing.T) {
......
...@@ -15,8 +15,9 @@ ...@@ -15,8 +15,9 @@
package testing package testing
import ( import (
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/client/httplib"
"github.com/astaxie/beego/pkg/httplib"
"github.com/astaxie/beego/pkg/infrastructure/config"
) )
var port = "" var port = ""
......
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
) )
const ( const (
......
...@@ -21,11 +21,10 @@ import ( ...@@ -21,11 +21,10 @@ import (
"sync" "sync"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
"github.com/astaxie/beego/pkg/infrastructure/utils"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"github.com/astaxie/beego/pkg/common"
) )
// DriverType database driver constant int. // DriverType database driver constant int.
...@@ -341,7 +340,7 @@ func detectTZ(al *alias) { ...@@ -341,7 +340,7 @@ func detectTZ(al *alias) {
} }
} }
func addAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV) (*alias, error) { func addAliasWthDB(aliasName, driverName string, db *sql.DB, params ...utils.KV) (*alias, error) {
existErr := fmt.Errorf("DataBase alias name `%s` already registered, cannot reuse", aliasName) existErr := fmt.Errorf("DataBase alias name `%s` already registered, cannot reuse", aliasName)
if _, ok := dataBaseCache.get(aliasName); ok { if _, ok := dataBaseCache.get(aliasName); ok {
return nil, existErr return nil, existErr
...@@ -359,8 +358,8 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV ...@@ -359,8 +358,8 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV
return al, nil return al, nil
} }
func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...common.KV) (*alias, error) { func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...utils.KV) (*alias, error) {
kvs := common.NewKVs(params...) kvs := utils.NewKVs(params...)
var stmtCache *lru.Cache var stmtCache *lru.Cache
var stmtCacheSize int var stmtCacheSize int
...@@ -418,13 +417,13 @@ func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...common.K ...@@ -418,13 +417,13 @@ func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...common.K
} }
// AddAliasWthDB add a aliasName for the drivename // AddAliasWthDB add a aliasName for the drivename
func AddAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV) error { func AddAliasWthDB(aliasName, driverName string, db *sql.DB, params ...utils.KV) error {
_, err := addAliasWthDB(aliasName, driverName, db, params...) _, err := addAliasWthDB(aliasName, driverName, db, params...)
return err return err
} }
// RegisterDataBase Setting the database connect params. Use the database driver self dataSource args. // RegisterDataBase Setting the database connect params. Use the database driver self dataSource args.
func RegisterDataBase(aliasName, driverName, dataSource string, params ...common.KV) error { func RegisterDataBase(aliasName, driverName, dataSource string, params ...utils.KV) error {
var ( var (
err error err error
db *sql.DB db *sql.DB
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
) )
// oracle operators. // oracle operators.
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
) )
// sqlite operators. // sqlite operators.
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/infrastructure/utils"
) )
// DoNothingOrm won't do anything, usually you use this to custom your mock Ormer implementation // DoNothingOrm won't do anything, usually you use this to custom your mock Ormer implementation
...@@ -54,11 +54,11 @@ func (d *DoNothingOrm) ReadOrCreateWithCtx(ctx context.Context, md interface{}, ...@@ -54,11 +54,11 @@ func (d *DoNothingOrm) ReadOrCreateWithCtx(ctx context.Context, md interface{},
return false, 0, nil return false, 0, nil
} }
func (d *DoNothingOrm) LoadRelated(md interface{}, name string, args ...common.KV) (int64, error) { func (d *DoNothingOrm) LoadRelated(md interface{}, name string, args ...utils.KV) (int64, error) {
return 0, nil return 0, nil
} }
func (d *DoNothingOrm) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...common.KV) (int64, error) { func (d *DoNothingOrm) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...utils.KV) (int64, error) {
return 0, nil return 0, nil
} }
......
...@@ -19,9 +19,10 @@ import ( ...@@ -19,9 +19,10 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/astaxie/beego/pkg/bean" "github.com/astaxie/beego/pkg/infrastructure/logs"
"github.com/astaxie/beego/pkg/logs"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/client/orm"
"github.com/astaxie/beego/pkg/infrastructure/bean"
) )
// DefaultValueFilterChainBuilder only works for InsertXXX method, // DefaultValueFilterChainBuilder only works for InsertXXX method,
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/client/orm"
) )
func TestDefaultValueFilterChainBuilder_FilterChain(t *testing.T) { func TestDefaultValueFilterChainBuilder_FilterChain(t *testing.T) {
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/client/orm"
) )
// FilterChainBuilder provides an extension point // FilterChainBuilder provides an extension point
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/client/orm"
) )
func TestFilterChainBuilder_FilterChain(t *testing.T) { func TestFilterChainBuilder_FilterChain(t *testing.T) {
......
...@@ -22,8 +22,8 @@ import ( ...@@ -22,8 +22,8 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
beego "github.com/astaxie/beego/pkg" "github.com/astaxie/beego/pkg/client/orm"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/server/web"
) )
// FilterChainBuilder is an extension point, // FilterChainBuilder is an extension point,
...@@ -42,9 +42,9 @@ func NewFilterChainBuilder() *FilterChainBuilder { ...@@ -42,9 +42,9 @@ func NewFilterChainBuilder() *FilterChainBuilder {
Name: "beego", Name: "beego",
Subsystem: "orm_operation", Subsystem: "orm_operation",
ConstLabels: map[string]string{ ConstLabels: map[string]string{
"server": beego.BConfig.ServerName, "server": web.BConfig.ServerName,
"env": beego.BConfig.RunMode, "env": web.BConfig.RunMode,
"appname": beego.BConfig.AppName, "appname": web.BConfig.AppName,
}, },
Help: "The statics info for orm operation", Help: "The statics info for orm operation",
}, []string{"method", "name", "duration", "insideTx", "txName"}) }, []string{"method", "name", "duration", "insideTx", "txName"})
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/client/orm"
) )
func TestFilterChainBuilder_FilterChain(t *testing.T) { func TestFilterChainBuilder_FilterChain(t *testing.T) {
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"reflect" "reflect"
"time" "time"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/infrastructure/utils"
) )
const ( const (
...@@ -137,11 +137,11 @@ func (f *filterOrmDecorator) ReadOrCreateWithCtx(ctx context.Context, md interfa ...@@ -137,11 +137,11 @@ func (f *filterOrmDecorator) ReadOrCreateWithCtx(ctx context.Context, md interfa
return res[0].(bool), res[1].(int64), f.convertError(res[2]) return res[0].(bool), res[1].(int64), f.convertError(res[2])
} }
func (f *filterOrmDecorator) LoadRelated(md interface{}, name string, args ...common.KV) (int64, error) { func (f *filterOrmDecorator) LoadRelated(md interface{}, name string, args ...utils.KV) (int64, error) {
return f.LoadRelatedWithCtx(context.Background(), md, name, args...) return f.LoadRelatedWithCtx(context.Background(), md, name, args...)
} }
func (f *filterOrmDecorator) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...common.KV) (int64, error) { func (f *filterOrmDecorator) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...utils.KV) (int64, error) {
mi, _ := modelCache.getByMd(md) mi, _ := modelCache.getByMd(md)
inv := &Invocation{ inv := &Invocation{
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/infrastructure/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -362,7 +362,7 @@ func (f *filterMockOrm) ReadForUpdateWithCtx(ctx context.Context, md interface{} ...@@ -362,7 +362,7 @@ func (f *filterMockOrm) ReadForUpdateWithCtx(ctx context.Context, md interface{}
return errors.New("read for update error") return errors.New("read for update error")
} }
func (f *filterMockOrm) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...common.KV) (int64, error) { func (f *filterMockOrm) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...utils.KV) (int64, error) {
return 99, errors.New("load related error") return 99, errors.New("load related error")
} }
......
...@@ -17,7 +17,7 @@ package hints ...@@ -17,7 +17,7 @@ package hints
import ( import (
"time" "time"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/infrastructure/utils"
) )
const ( const (
...@@ -43,7 +43,7 @@ type Hint struct { ...@@ -43,7 +43,7 @@ type Hint struct {
value interface{} value interface{}
} }
var _ common.KV = new(Hint) var _ utils.KV = new(Hint)
// GetKey return key // GetKey return key
func (s *Hint) GetKey() interface{} { func (s *Hint) GetKey() interface{} {
...@@ -55,7 +55,7 @@ func (s *Hint) GetValue() interface{} { ...@@ -55,7 +55,7 @@ func (s *Hint) GetValue() interface{} {
return s.value return s.value
} }
var _ common.KV = new(Hint) var _ utils.KV = new(Hint)
// MaxIdleConnections return a hint about MaxIdleConnections // MaxIdleConnections return a hint about MaxIdleConnections
func MaxIdleConnections(v int) *Hint { func MaxIdleConnections(v int) *Hint {
......
...@@ -17,7 +17,7 @@ package migration ...@@ -17,7 +17,7 @@ package migration
import ( import (
"fmt" "fmt"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/infrastructure/logs"
) )
// Index struct defines the structure of Index Columns // Index struct defines the structure of Index Columns
......
...@@ -33,8 +33,8 @@ import ( ...@@ -33,8 +33,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/client/orm"
"github.com/astaxie/beego/pkg/orm" "github.com/astaxie/beego/pkg/infrastructure/logs"
) )
// const the data format for the bee generate migration datatype // const the data format for the bee generate migration datatype
......
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
...@@ -303,7 +303,7 @@ type Post struct { ...@@ -303,7 +303,7 @@ type Post struct {
Content string `orm:"type(text)"` Content string `orm:"type(text)"`
Created time.Time `orm:"auto_now_add"` Created time.Time `orm:"auto_now_add"`
Updated time.Time `orm:"auto_now"` Updated time.Time `orm:"auto_now"`
Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/orm.PostTags)"` Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/client/orm.PostTags)"`
} }
func (u *Post) TableIndex() [][]string { func (u *Post) TableIndex() [][]string {
...@@ -361,7 +361,7 @@ type Group struct { ...@@ -361,7 +361,7 @@ type Group struct {
type Permission struct { type Permission struct {
ID int `orm:"column(id)"` ID int `orm:"column(id)"`
Name string Name string
Groups []*Group `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/orm.GroupPermissions)"` Groups []*Group `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/client/orm.GroupPermissions)"`
} }
type GroupPermissions struct { type GroupPermissions struct {
...@@ -470,7 +470,7 @@ var ( ...@@ -470,7 +470,7 @@ var (
usage: usage:
go get -u github.com/astaxie/beego/pkg/orm go get -u github.com/astaxie/beego/pkg/client/orm
go get -u github.com/go-sql-driver/mysql go get -u github.com/go-sql-driver/mysql
go get -u github.com/mattn/go-sqlite3 go get -u github.com/mattn/go-sqlite3
go get -u github.com/lib/pq go get -u github.com/lib/pq
...@@ -480,20 +480,20 @@ var ( ...@@ -480,20 +480,20 @@ var (
mysql -u root -e 'create database orm_test;' mysql -u root -e 'create database orm_test;'
export ORM_DRIVER=mysql export ORM_DRIVER=mysql
export ORM_SOURCE="root:@/orm_test?charset=utf8" export ORM_SOURCE="root:@/orm_test?charset=utf8"
go test -v github.com/astaxie/beego/pkg/orm go test -v github.com/astaxie/beego/pkg/client/orm
#### Sqlite3 #### Sqlite3
export ORM_DRIVER=sqlite3 export ORM_DRIVER=sqlite3
export ORM_SOURCE='file:memory_test?mode=memory' export ORM_SOURCE='file:memory_test?mode=memory'
go test -v github.com/astaxie/beego/pkg/orm go test -v github.com/astaxie/beego/pkg/client/orm
#### PostgreSQL #### PostgreSQL
psql -c 'create database orm_test;' -U postgres psql -c 'create database orm_test;' -U postgres
export ORM_DRIVER=postgres export ORM_DRIVER=postgres
export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable" export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable"
go test -v github.com/astaxie/beego/pkg/orm go test -v github.com/astaxie/beego/pkg/client/orm
#### TiDB #### TiDB
export ORM_DRIVER=tidb export ORM_DRIVER=tidb
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
// //
// import ( // import (
// "fmt" // "fmt"
// "github.com/astaxie/beego/pkg/orm" // "github.com/astaxie/beego/pkg/client/orm"
// _ "github.com/go-sql-driver/mysql" // import your used driver // _ "github.com/go-sql-driver/mysql" // import your used driver
// ) // )
// //
...@@ -62,10 +62,10 @@ import ( ...@@ -62,10 +62,10 @@ import (
"reflect" "reflect"
"time" "time"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/client/orm/hints"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/infrastructure/utils"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/infrastructure/logs"
) )
// DebugQueries define the debug // DebugQueries define the debug
...@@ -307,10 +307,10 @@ func (o *ormBase) QueryM2MWithCtx(ctx context.Context, md interface{}, name stri ...@@ -307,10 +307,10 @@ func (o *ormBase) QueryM2MWithCtx(ctx context.Context, md interface{}, name stri
// for _,tag := range post.Tags{...} // for _,tag := range post.Tags{...}
// //
// make sure the relation is defined in model struct tags. // make sure the relation is defined in model struct tags.
func (o *ormBase) LoadRelated(md interface{}, name string, args ...common.KV) (int64, error) { func (o *ormBase) LoadRelated(md interface{}, name string, args ...utils.KV) (int64, error) {
return o.LoadRelatedWithCtx(context.Background(), md, name, args...) return o.LoadRelatedWithCtx(context.Background(), md, name, args...)
} }
func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...common.KV) (int64, error) { func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...utils.KV) (int64, error) {
_, fi, ind, qseter := o.queryRelated(md, name) _, fi, ind, qseter := o.queryRelated(md, name)
qs := qseter.(*querySet) qs := qseter.(*querySet)
...@@ -319,7 +319,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s ...@@ -319,7 +319,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s
var limit, offset int64 var limit, offset int64
var order string var order string
kvs := common.NewKVs(args...) kvs := utils.NewKVs(args...)
kvs.IfContains(hints.KeyRelDepth, func(value interface{}) { kvs.IfContains(hints.KeyRelDepth, func(value interface{}) {
if v, ok := value.(bool); ok { if v, ok := value.(bool); ok {
if v { if v {
...@@ -603,7 +603,7 @@ func NewOrmUsingDB(aliasName string) Ormer { ...@@ -603,7 +603,7 @@ func NewOrmUsingDB(aliasName string) Ormer {
} }
// NewOrmWithDB create a new ormer object with specify *sql.DB for query // NewOrmWithDB create a new ormer object with specify *sql.DB for query
func NewOrmWithDB(driverName, aliasName string, db *sql.DB, params ...common.KV) (Ormer, error) { func NewOrmWithDB(driverName, aliasName string, db *sql.DB, params ...utils.KV) (Ormer, error) {
al, err := newAliasWithDb(aliasName, driverName, db, params...) al, err := newAliasWithDb(aliasName, driverName, db, params...)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
) )
type colValue struct { type colValue struct {
......
...@@ -383,19 +383,33 @@ func (o *rawSet) QueryRow(containers ...interface{}) error { ...@@ -383,19 +383,33 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
} }
} }
} else { } else {
for i := 0; i < ind.NumField(); i++ { // define recursive function
f := ind.Field(i) var recursiveSetField func(rv reflect.Value)
fe := ind.Type().Field(i) recursiveSetField = func(rv reflect.Value) {
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName)) for i := 0; i < rv.NumField(); i++ {
var col string f := rv.Field(i)
if col = tags["column"]; col == "" { fe := rv.Type().Field(i)
col = nameStrategyMap[nameStrategy](fe.Name)
} // check if the field is a Struct
if v, ok := columnsMp[col]; ok { // recursive the Struct type
value := reflect.ValueOf(v).Elem().Interface() if fe.Type.Kind() == reflect.Struct {
o.setFieldValue(f, value) recursiveSetField(f)
}
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
var col string
if col = tags["column"]; col == "" {
col = nameStrategyMap[nameStrategy](fe.Name)
}
if v, ok := columnsMp[col]; ok {
value := reflect.ValueOf(v).Elem().Interface()
o.setFieldValue(f, value)
}
} }
} }
// init call the recursive function
recursiveSetField(ind)
} }
} else { } else {
......
...@@ -31,7 +31,7 @@ import ( ...@@ -31,7 +31,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/astaxie/beego/pkg/orm/hints" "github.com/astaxie/beego/pkg/client/orm/hints"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -1742,6 +1742,24 @@ func TestRawQueryRow(t *testing.T) { ...@@ -1742,6 +1742,24 @@ func TestRawQueryRow(t *testing.T) {
throwFail(t, AssertIs(*status, 3)) throwFail(t, AssertIs(*status, 3))
throwFail(t, AssertIs(pid, nil)) throwFail(t, AssertIs(pid, nil))
type Embeded struct {
Email string
}
type queryRowNoModelTest struct {
Id int
EmbedField Embeded
}
cols = []string{
"id", "email",
}
var row queryRowNoModelTest
query = fmt.Sprintf("SELECT %s%s%s FROM %suser%s WHERE id = ?", Q, strings.Join(cols, sep), Q, Q, Q)
err = dORM.Raw(query, 4).QueryRow(&row)
throwFail(t, err)
throwFail(t, AssertIs(row.Id, 4))
throwFail(t, AssertIs(row.EmbedField.Email, "nobody@gmail.com"))
// test for sql.Null* fields // test for sql.Null* fields
nData := &DataNull{ nData := &DataNull{
NullString: sql.NullString{String: "test sql.null", Valid: true}, NullString: sql.NullString{String: "test sql.null", Valid: true},
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"reflect" "reflect"
"time" "time"
"github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/infrastructure/utils"
) )
// TableNaming is usually used by model // TableNaming is usually used by model
...@@ -183,8 +183,8 @@ type DQL interface { ...@@ -183,8 +183,8 @@ type DQL interface {
// hints.Offset int offset default offset 0 // hints.Offset int offset default offset 0
// hints.OrderBy string order for example : "-Id" // hints.OrderBy string order for example : "-Id"
// make sure the relation is defined in model struct tags. // make sure the relation is defined in model struct tags.
LoadRelated(md interface{}, name string, args ...common.KV) (int64, error) LoadRelated(md interface{}, name string, args ...utils.KV) (int64, error)
LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...common.KV) (int64, error) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...utils.KV) (int64, error)
// create a models to models queryer // create a models to models queryer
// for example: // for example:
......
/* // Copyright 2020
Package beego provide a MVC framework //
beego: an open-source, high-performance, modular, full-stack web framework // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
It is used for rapid development of RESTful APIs, web apps and backend services in Go. // You may obtain a copy of the License at
beego is inspired by Tornado, Sinatra and Flask with the added benefit of some Go-specific features such as interfaces and struct embedding. //
// http://www.apache.org/licenses/LICENSE-2.0
package main //
import "github.com/astaxie/beego/pkg" // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
func main() { // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
beego.Run() // See the License for the specific language governing permissions and
} // limitations under the License.
more information: http://beego.me package pkg
*/
package beego
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/infrastructure/logs"
) )
const DefaultValueTagKey = "default" const DefaultValueTagKey = "default"
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/astaxie/beego/pkg/utils" "github.com/astaxie/beego/pkg/infrastructure/utils"
) )
var env *utils.BeeMap var env *utils.BeeMap
......
...@@ -24,7 +24,7 @@ import ( ...@@ -24,7 +24,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/infrastructure/config"
) )
// JSONConfig is a json config parser and implements Config interface. // JSONConfig is a json config parser and implements Config interface.
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/infrastructure/config"
) )
func TestJsonStartsWithArray(t *testing.T) { func TestJsonStartsWithArray(t *testing.T) {
......
...@@ -39,7 +39,7 @@ import ( ...@@ -39,7 +39,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/infrastructure/config"
"github.com/beego/x2j" "github.com/beego/x2j"
) )
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/infrastructure/config"
) )
func TestXML(t *testing.T) { func TestXML(t *testing.T) {
......
...@@ -40,7 +40,7 @@ import ( ...@@ -40,7 +40,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/pkg/config" "github.com/astaxie/beego/pkg/infrastructure/config"
"github.com/beego/goyaml2" "github.com/beego/goyaml2"
) )
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册