mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 12:50:55 +00:00
Merge branch 'frt/wrap_kv' into frt/stmt_config
# Conflicts: # pkg/orm/constant.go # pkg/orm/db_alias.go
This commit is contained in:
commit
4304b40a82
@ -327,6 +327,26 @@ func (input *BeegoInput) ResetParams() {
|
|||||||
input.pvalues = input.pvalues[:0]
|
input.pvalues = input.pvalues[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResetData: reset data
|
||||||
|
func (input *BeegoInput) ResetData() {
|
||||||
|
input.dataLock.Lock()
|
||||||
|
input.data = nil
|
||||||
|
input.dataLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetBody: reset body
|
||||||
|
func (input *BeegoInput) ResetBody() {
|
||||||
|
input.RequestBody = []byte{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear: clear all data in input
|
||||||
|
func (input *BeegoInput) Clear() {
|
||||||
|
input.ResetParams()
|
||||||
|
input.ResetData()
|
||||||
|
input.ResetBody()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Query returns input data item string by a given string.
|
// Query returns input data item string by a given string.
|
||||||
func (input *BeegoInput) Query(key string) string {
|
func (input *BeegoInput) Query(key string) string {
|
||||||
if val := input.Param(key); val != "" {
|
if val := input.Param(key); val != "" {
|
||||||
|
@ -50,9 +50,15 @@ func NewOutput() *BeegoOutput {
|
|||||||
// Reset init BeegoOutput
|
// Reset init BeegoOutput
|
||||||
func (output *BeegoOutput) Reset(ctx *Context) {
|
func (output *BeegoOutput) Reset(ctx *Context) {
|
||||||
output.Context = ctx
|
output.Context = ctx
|
||||||
|
output.Clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear: clear all data in output
|
||||||
|
func (output *BeegoOutput) Clear() {
|
||||||
output.Status = 0
|
output.Status = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Header sets response header item string via given key.
|
// Header sets response header item string via given key.
|
||||||
func (output *BeegoOutput) Header(key, val string) {
|
func (output *BeegoOutput) Header(key, val string) {
|
||||||
output.Context.ResponseWriter.Header().Set(key, val)
|
output.Context.ResponseWriter.Header().Set(key, val)
|
||||||
|
@ -14,14 +14,29 @@
|
|||||||
|
|
||||||
package common
|
package common
|
||||||
|
|
||||||
// KV is common structure to store key-value data.
|
type KV interface {
|
||||||
|
GetKey() interface{}
|
||||||
|
GetValue() interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SimpleKV is common structure to store key-value data.
|
||||||
// when you need something like Pair, you can use this
|
// when you need something like Pair, you can use this
|
||||||
type KV struct {
|
type SimpleKV struct {
|
||||||
Key interface{}
|
Key interface{}
|
||||||
Value interface{}
|
Value interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// KVs will store KV collection as map
|
var _ KV = new(SimpleKV)
|
||||||
|
|
||||||
|
func (s *SimpleKV) GetKey() interface{} {
|
||||||
|
return s.Key
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SimpleKV) GetValue() interface{} {
|
||||||
|
return s.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// KVs will store SimpleKV collection as map
|
||||||
type KVs struct {
|
type KVs struct {
|
||||||
kvs map[interface{}]interface{}
|
kvs map[interface{}]interface{}
|
||||||
}
|
}
|
||||||
@ -63,7 +78,7 @@ func NewKVs(kvs ...KV) *KVs {
|
|||||||
kvs: make(map[interface{}]interface{}, len(kvs)),
|
kvs: make(map[interface{}]interface{}, len(kvs)),
|
||||||
}
|
}
|
||||||
for _, kv := range kvs {
|
for _, kv := range kvs {
|
||||||
res.kvs[kv.Key] = kv.Value
|
res.kvs[kv.GetKey()] = kv.GetValue()
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
func TestKVs(t *testing.T) {
|
func TestKVs(t *testing.T) {
|
||||||
key := "my-key"
|
key := "my-key"
|
||||||
kvs := NewKVs(KV{
|
kvs := NewKVs(&SimpleKV{
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: 12,
|
Value: 12,
|
||||||
})
|
})
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
// Copyright 2020 beego-dev
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
|
|
||||||
package orm
|
|
||||||
|
|
||||||
const (
|
|
||||||
MaxIdleConnsKey = "MaxIdleConns"
|
|
||||||
MaxOpenConnsKey = "MaxOpenConns"
|
|
||||||
ConnMaxLifetimeKey = "ConnMaxLifetime"
|
|
||||||
MaxStmtCacheSize = "MaxStmtCacheSize"
|
|
||||||
)
|
|
@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/astaxie/beego/pkg/orm/hints"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -402,7 +403,7 @@ func AddAliasWthDB(aliasName, driverName string, db *sql.DB, params ...common.KV
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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, hints ...common.KV) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
@ -416,7 +417,7 @@ func RegisterDataBase(aliasName, driverName, dataSource string, params ...common
|
|||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
al, err = addAliasWthDB(aliasName, driverName, db, params...)
|
al, err = addAliasWthDB(aliasName, driverName, db, hints...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
@ -19,21 +19,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/astaxie/beego/pkg/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegisterDataBase(t *testing.T) {
|
func TestRegisterDataBase(t *testing.T) {
|
||||||
err := RegisterDataBase("test-params", DBARGS.Driver, DBARGS.Source, common.KV{
|
err := RegisterDataBase("test-params", DBARGS.Driver, DBARGS.Source,
|
||||||
Key: MaxIdleConnsKey,
|
MaxIdleConnections(20),
|
||||||
Value: 20,
|
MaxOpenConnections(300),
|
||||||
}, common.KV{
|
ConnMaxLifetime(time.Minute))
|
||||||
Key: MaxOpenConnsKey,
|
|
||||||
Value: 300,
|
|
||||||
}, common.KV{
|
|
||||||
Key: ConnMaxLifetimeKey,
|
|
||||||
Value: time.Minute,
|
|
||||||
})
|
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
al := getDbAlias("test-params")
|
al := getDbAlias("test-params")
|
||||||
|
68
pkg/orm/db_hints.go
Normal file
68
pkg/orm/db_hints.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// Copyright 2020 beego-dev
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package orm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego/pkg/common"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hint struct {
|
||||||
|
key interface{}
|
||||||
|
value interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ common.KV = new(Hint)
|
||||||
|
|
||||||
|
// GetKey return key
|
||||||
|
func (s *Hint) GetKey() interface{} {
|
||||||
|
return s.key
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetValue return value
|
||||||
|
func (s *Hint) GetValue() interface{} {
|
||||||
|
return s.value
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxIdleConnectionsKey = "MaxIdleConnections"
|
||||||
|
maxOpenConnectionsKey = "MaxOpenConnections"
|
||||||
|
connMaxLifetimeKey = "ConnMaxLifetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ common.KV = new(Hint)
|
||||||
|
|
||||||
|
// MaxIdleConnections return a hint about MaxIdleConnections
|
||||||
|
func MaxIdleConnections(v int) *Hint {
|
||||||
|
return NewHint(maxIdleConnectionsKey, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxOpenConnections return a hint about MaxOpenConnections
|
||||||
|
func MaxOpenConnections(v int) *Hint {
|
||||||
|
return NewHint(maxOpenConnectionsKey, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConnMaxLifetime return a hint about ConnMaxLifetime
|
||||||
|
func ConnMaxLifetime(v time.Duration) *Hint {
|
||||||
|
return NewHint(connMaxLifetimeKey, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHint return a hint
|
||||||
|
func NewHint(key interface{}, value interface{}) *Hint {
|
||||||
|
return &Hint{
|
||||||
|
key: key,
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
69
pkg/orm/db_hints_test.go
Normal file
69
pkg/orm/db_hints_test.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// Copyright 2020 beego-dev
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package orm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewHint_time(t *testing.T) {
|
||||||
|
key := "qweqwe"
|
||||||
|
value := time.Second
|
||||||
|
hint := NewHint(key, value)
|
||||||
|
|
||||||
|
assert.Equal(t, hint.GetKey(), key)
|
||||||
|
assert.Equal(t, hint.GetValue(), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewHint_int(t *testing.T) {
|
||||||
|
key := "qweqwe"
|
||||||
|
value := 281230
|
||||||
|
hint := NewHint(key, value)
|
||||||
|
|
||||||
|
assert.Equal(t, hint.GetKey(), key)
|
||||||
|
assert.Equal(t, hint.GetValue(), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewHint_float(t *testing.T) {
|
||||||
|
key := "qweqwe"
|
||||||
|
value := 21.2459753
|
||||||
|
hint := NewHint(key, value)
|
||||||
|
|
||||||
|
assert.Equal(t, hint.GetKey(), key)
|
||||||
|
assert.Equal(t, hint.GetValue(), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaxOpenConnections(t *testing.T) {
|
||||||
|
i := 887423
|
||||||
|
hint := MaxOpenConnections(i)
|
||||||
|
assert.Equal(t, hint.GetValue(), i)
|
||||||
|
assert.Equal(t, hint.GetKey(), maxOpenConnectionsKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConnMaxLifetime(t *testing.T) {
|
||||||
|
i := time.Hour
|
||||||
|
hint := ConnMaxLifetime(i)
|
||||||
|
assert.Equal(t, hint.GetValue(), i)
|
||||||
|
assert.Equal(t, hint.GetKey(), connMaxLifetimeKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaxIdleConnections(t *testing.T) {
|
||||||
|
i := 42316
|
||||||
|
hint := MaxIdleConnections(i)
|
||||||
|
assert.Equal(t, hint.GetValue(), i)
|
||||||
|
assert.Equal(t, hint.GetKey(), maxIdleConnectionsKey)
|
||||||
|
}
|
@ -28,7 +28,6 @@ import (
|
|||||||
// As tidb can't use go get, so disable the tidb testing now
|
// As tidb can't use go get, so disable the tidb testing now
|
||||||
// _ "github.com/pingcap/tidb"
|
// _ "github.com/pingcap/tidb"
|
||||||
|
|
||||||
"github.com/astaxie/beego/pkg/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A slice string field.
|
// A slice string field.
|
||||||
@ -489,10 +488,7 @@ func init() {
|
|||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, common.KV{
|
err := RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, MaxIdleConnections(20))
|
||||||
Key: MaxIdleConnsKey,
|
|
||||||
Value: 20,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("can not register database: %v", err))
|
panic(fmt.Sprintf("can not register database: %v", err))
|
||||||
|
@ -32,7 +32,8 @@ func (o *rawPrepare) Exec(args ...interface{}) (sql.Result, error) {
|
|||||||
if o.closed {
|
if o.closed {
|
||||||
return nil, ErrStmtClosed
|
return nil, ErrStmtClosed
|
||||||
}
|
}
|
||||||
return o.stmt.Exec(args...)
|
flatParams := getFlatParams(nil, args, o.rs.orm.alias.TZ)
|
||||||
|
return o.stmt.Exec(flatParams...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *rawPrepare) Close() error {
|
func (o *rawPrepare) Close() error {
|
||||||
|
@ -319,6 +319,10 @@ func (p *ControllerRegister) GetContext() *beecontext.Context {
|
|||||||
|
|
||||||
// GiveBackContext put the ctx into pool so that it could be reuse
|
// GiveBackContext put the ctx into pool so that it could be reuse
|
||||||
func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) {
|
func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) {
|
||||||
|
// clear input cached data
|
||||||
|
ctx.Input.Clear()
|
||||||
|
// clear output cached data
|
||||||
|
ctx.Output.Clear()
|
||||||
p.pool.Put(ctx)
|
p.pool.Put(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user