mirror of
https://github.com/astaxie/beego.git
synced 2024-12-24 05:00:50 +00:00
Merge pull request #4173 from AllenX2018/fix-bug-queryRow
Fix issue 3866
This commit is contained in:
commit
581e48679e
@ -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.
|
||||
|
||||
## 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
|
||||
|
||||
### Pull requests
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 beego
|
||||
// Copyright 2020
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,5 +12,5 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// we will move all web related codes here
|
||||
package web
|
||||
// used to keep compatible with v1.x
|
||||
package adapter
|
@ -37,7 +37,7 @@ import (
|
||||
|
||||
"github.com/bradfitz/gomemcache/memcache"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
// Cache Memcache adapter.
|
@ -15,17 +15,26 @@
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
_ "github.com/bradfitz/gomemcache/memcache"
|
||||
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
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 {
|
||||
t.Error("init err")
|
||||
}
|
@ -39,7 +39,7 @@ import (
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
var (
|
@ -16,17 +16,24 @@ package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
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 {
|
||||
t.Error("init err")
|
||||
}
|
||||
@ -110,8 +117,14 @@ func TestRedisCache(t *testing.T) {
|
||||
|
||||
func TestCache_Scan(t *testing.T) {
|
||||
timeoutDuration := 10 * time.Second
|
||||
|
||||
addr := os.Getenv("REDIS_ADDR")
|
||||
if addr == "" {
|
||||
addr = "127.0.0.1:6379"
|
||||
}
|
||||
|
||||
// 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 {
|
||||
t.Error("init err")
|
||||
}
|
||||
@ -121,6 +134,7 @@ func TestCache_Scan(t *testing.T) {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
// scan all for the first time
|
||||
keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
|
||||
if err != nil {
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
"github.com/ssdb/gossdb/ssdb"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
// Cache SSDB adapter
|
@ -1,15 +1,23 @@
|
||||
package ssdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/cache"
|
||||
"github.com/astaxie/beego/pkg/client/cache"
|
||||
)
|
||||
|
||||
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 {
|
||||
t.Error("init err")
|
||||
}
|
@ -18,7 +18,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/astaxie/beego/pkg/httplib"
|
||||
"github.com/astaxie/beego/pkg/client/httplib"
|
||||
logKit "github.com/go-kit/kit/log"
|
||||
opentracingKit "github.com/go-kit/kit/tracing/opentracing"
|
||||
"github.com/opentracing/opentracing-go"
|
@ -23,7 +23,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/pkg/httplib"
|
||||
"github.com/astaxie/beego/pkg/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
@ -22,8 +22,8 @@ import (
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
beego "github.com/astaxie/beego/pkg"
|
||||
"github.com/astaxie/beego/pkg/httplib"
|
||||
"github.com/astaxie/beego/pkg/client/httplib"
|
||||
"github.com/astaxie/beego/pkg/server/web"
|
||||
)
|
||||
|
||||
type FilterChainBuilder struct {
|
||||
@ -36,9 +36,9 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt
|
||||
Name: "beego",
|
||||
Subsystem: "remote_http_request",
|
||||
ConstLabels: map[string]string{
|
||||
"server": beego.BConfig.ServerName,
|
||||
"env": beego.BConfig.RunMode,
|
||||
"appname": beego.BConfig.AppName,
|
||||
"server": web.BConfig.ServerName,
|
||||
"env": web.BConfig.RunMode,
|
||||
"appname": web.BConfig.AppName,
|
||||
},
|
||||
Help: "The statics info for remote http requests",
|
||||
}, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"})
|
@ -22,7 +22,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/pkg/httplib"
|
||||
"github.com/astaxie/beego/pkg/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
@ -15,8 +15,9 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/httplib"
|
||||
"github.com/astaxie/beego/pkg/client/httplib"
|
||||
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
)
|
||||
|
||||
var port = ""
|
@ -22,7 +22,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
)
|
||||
|
||||
const (
|
@ -21,11 +21,10 @@ import (
|
||||
"sync"
|
||||
"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"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// DriverType database driver constant int.
|
||||
@ -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)
|
||||
if _, ok := dataBaseCache.get(aliasName); ok {
|
||||
return nil, existErr
|
||||
@ -359,8 +358,8 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV
|
||||
return al, nil
|
||||
}
|
||||
|
||||
func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...common.KV) (*alias, error) {
|
||||
kvs := common.NewKVs(params...)
|
||||
func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...utils.KV) (*alias, error) {
|
||||
kvs := utils.NewKVs(params...)
|
||||
|
||||
var stmtCache *lru.Cache
|
||||
var stmtCacheSize int
|
||||
@ -418,13 +417,13 @@ func newAliasWithDb(aliasName, driverName string, db *sql.DB, params ...common.K
|
||||
}
|
||||
|
||||
// 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...)
|
||||
return err
|
||||
}
|
||||
|
||||
// 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 (
|
||||
err error
|
||||
db *sql.DB
|
@ -18,7 +18,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
@ -18,7 +18,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
)
|
||||
|
||||
// oracle operators.
|
@ -21,7 +21,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
)
|
||||
|
||||
// sqlite operators.
|
@ -18,7 +18,7 @@ import (
|
||||
"context"
|
||||
"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
|
||||
@ -54,11 +54,11 @@ func (d *DoNothingOrm) ReadOrCreateWithCtx(ctx context.Context, md interface{},
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -19,9 +19,10 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/pkg/bean"
|
||||
"github.com/astaxie/beego/pkg/logs"
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/logs"
|
||||
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/bean"
|
||||
)
|
||||
|
||||
// DefaultValueFilterChainBuilder only works for InsertXXX method,
|
@ -19,7 +19,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
)
|
||||
|
||||
func TestDefaultValueFilterChainBuilder_FilterChain(t *testing.T) {
|
@ -20,7 +20,7 @@ import (
|
||||
|
||||
"github.com/opentracing/opentracing-go"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
)
|
||||
|
||||
// FilterChainBuilder provides an extension point
|
@ -21,7 +21,7 @@ import (
|
||||
|
||||
"github.com/opentracing/opentracing-go"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
@ -22,8 +22,8 @@ import (
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
beego "github.com/astaxie/beego/pkg"
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
"github.com/astaxie/beego/pkg/server/web"
|
||||
)
|
||||
|
||||
// FilterChainBuilder is an extension point,
|
||||
@ -42,9 +42,9 @@ func NewFilterChainBuilder() *FilterChainBuilder {
|
||||
Name: "beego",
|
||||
Subsystem: "orm_operation",
|
||||
ConstLabels: map[string]string{
|
||||
"server": beego.BConfig.ServerName,
|
||||
"env": beego.BConfig.RunMode,
|
||||
"appname": beego.BConfig.AppName,
|
||||
"server": web.BConfig.ServerName,
|
||||
"env": web.BConfig.RunMode,
|
||||
"appname": web.BConfig.AppName,
|
||||
},
|
||||
Help: "The statics info for orm operation",
|
||||
}, []string{"method", "name", "duration", "insideTx", "txName"})
|
@ -21,7 +21,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm"
|
||||
"github.com/astaxie/beego/pkg/client/orm"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
@ -20,7 +20,7 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -137,11 +137,11 @@ func (f *filterOrmDecorator) ReadOrCreateWithCtx(ctx context.Context, md interfa
|
||||
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...)
|
||||
}
|
||||
|
||||
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)
|
||||
inv := &Invocation{
|
@ -21,7 +21,7 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/utils"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -362,7 +362,7 @@ func (f *filterMockOrm) ReadForUpdateWithCtx(ctx context.Context, md interface{}
|
||||
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")
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ package hints
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -43,7 +43,7 @@ type Hint struct {
|
||||
value interface{}
|
||||
}
|
||||
|
||||
var _ common.KV = new(Hint)
|
||||
var _ utils.KV = new(Hint)
|
||||
|
||||
// GetKey return key
|
||||
func (s *Hint) GetKey() interface{} {
|
||||
@ -55,7 +55,7 @@ func (s *Hint) GetValue() interface{} {
|
||||
return s.value
|
||||
}
|
||||
|
||||
var _ common.KV = new(Hint)
|
||||
var _ utils.KV = new(Hint)
|
||||
|
||||
// MaxIdleConnections return a hint about MaxIdleConnections
|
||||
func MaxIdleConnections(v int) *Hint {
|
@ -17,7 +17,7 @@ package migration
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/astaxie/beego/pkg/logs"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/logs"
|
||||
)
|
||||
|
||||
// Index struct defines the structure of Index Columns
|
@ -33,8 +33,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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/logs"
|
||||
)
|
||||
|
||||
// const the data format for the bee generate migration datatype
|
@ -22,7 +22,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
@ -303,7 +303,7 @@ type Post struct {
|
||||
Content string `orm:"type(text)"`
|
||||
Created time.Time `orm:"auto_now_add"`
|
||||
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 {
|
||||
@ -361,7 +361,7 @@ type Group struct {
|
||||
type Permission struct {
|
||||
ID int `orm:"column(id)"`
|
||||
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 {
|
||||
@ -470,7 +470,7 @@ var (
|
||||
|
||||
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/mattn/go-sqlite3
|
||||
go get -u github.com/lib/pq
|
||||
@ -480,20 +480,20 @@ var (
|
||||
mysql -u root -e 'create database orm_test;'
|
||||
export ORM_DRIVER=mysql
|
||||
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
|
||||
export ORM_DRIVER=sqlite3
|
||||
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
|
||||
psql -c 'create database orm_test;' -U postgres
|
||||
export ORM_DRIVER=postgres
|
||||
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
|
||||
export ORM_DRIVER=tidb
|
@ -21,7 +21,7 @@
|
||||
//
|
||||
// import (
|
||||
// "fmt"
|
||||
// "github.com/astaxie/beego/pkg/orm"
|
||||
// "github.com/astaxie/beego/pkg/client/orm"
|
||||
// _ "github.com/go-sql-driver/mysql" // import your used driver
|
||||
// )
|
||||
//
|
||||
@ -62,10 +62,10 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/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
|
||||
@ -307,10 +307,10 @@ func (o *ormBase) QueryM2MWithCtx(ctx context.Context, md interface{}, name stri
|
||||
// for _,tag := range post.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...)
|
||||
}
|
||||
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)
|
||||
|
||||
qs := qseter.(*querySet)
|
||||
@ -319,7 +319,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s
|
||||
var limit, offset int64
|
||||
var order string
|
||||
|
||||
kvs := common.NewKVs(args...)
|
||||
kvs := utils.NewKVs(args...)
|
||||
kvs.IfContains(hints.KeyRelDepth, func(value interface{}) {
|
||||
if v, ok := value.(bool); ok {
|
||||
if v {
|
||||
@ -603,7 +603,7 @@ func NewOrmUsingDB(aliasName string) Ormer {
|
||||
}
|
||||
|
||||
// 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...)
|
||||
if err != nil {
|
||||
return nil, err
|
@ -18,7 +18,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
)
|
||||
|
||||
type colValue struct {
|
@ -383,19 +383,33 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < ind.NumField(); i++ {
|
||||
f := ind.Field(i)
|
||||
fe := ind.Type().Field(i)
|
||||
_, 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)
|
||||
// define recursive function
|
||||
var recursiveSetField func(rv reflect.Value)
|
||||
recursiveSetField = func(rv reflect.Value) {
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
f := rv.Field(i)
|
||||
fe := rv.Type().Field(i)
|
||||
|
||||
// check if the field is a Struct
|
||||
// recursive the Struct type
|
||||
if fe.Type.Kind() == reflect.Struct {
|
||||
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 {
|
@ -31,7 +31,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/orm/hints"
|
||||
"github.com/astaxie/beego/pkg/client/orm/hints"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -1742,6 +1742,24 @@ func TestRawQueryRow(t *testing.T) {
|
||||
throwFail(t, AssertIs(*status, 3))
|
||||
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
|
||||
nData := &DataNull{
|
||||
NullString: sql.NullString{String: "test sql.null", Valid: true},
|
@ -20,7 +20,7 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/utils"
|
||||
)
|
||||
|
||||
// TableNaming is usually used by model
|
||||
@ -183,8 +183,8 @@ type DQL interface {
|
||||
// hints.Offset int offset default offset 0
|
||||
// hints.OrderBy string order for example : "-Id"
|
||||
// make sure the relation is defined in model struct tags.
|
||||
LoadRelated(md interface{}, name string, args ...common.KV) (int64, error)
|
||||
LoadRelatedWithCtx(ctx context.Context, 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 ...utils.KV) (int64, error)
|
||||
|
||||
// create a models to models queryer
|
||||
// for example:
|
30
pkg/doc.go
30
pkg/doc.go
@ -1,17 +1,15 @@
|
||||
/*
|
||||
Package beego provide a MVC framework
|
||||
beego: an open-source, high-performance, modular, full-stack web framework
|
||||
// Copyright 2020
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
It is used for rapid development of RESTful APIs, web apps and backend services in Go.
|
||||
beego is inspired by Tornado, Sinatra and Flask with the added benefit of some Go-specific features such as interfaces and struct embedding.
|
||||
|
||||
package main
|
||||
import "github.com/astaxie/beego/pkg"
|
||||
|
||||
func main() {
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
more information: http://beego.me
|
||||
*/
|
||||
package beego
|
||||
package pkg
|
||||
|
@ -22,7 +22,7 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/astaxie/beego/pkg/logs"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/logs"
|
||||
)
|
||||
|
||||
const DefaultValueTagKey = "default"
|
@ -21,7 +21,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/pkg/utils"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/utils"
|
||||
)
|
||||
|
||||
var env *utils.BeeMap
|
@ -24,7 +24,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
)
|
||||
|
||||
// JSONConfig is a json config parser and implements Config interface.
|
@ -19,7 +19,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
)
|
||||
|
||||
func TestJsonStartsWithArray(t *testing.T) {
|
@ -39,7 +39,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
"github.com/beego/x2j"
|
||||
)
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
)
|
||||
|
||||
func TestXML(t *testing.T) {
|
@ -40,7 +40,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
"github.com/astaxie/beego/pkg/infrastructure/config"
|
||||
"github.com/beego/goyaml2"
|
||||
)
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user