db_hints.go 2.8 KB
Newer Older
J
wrap kv  
jianzhiyao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

J
jianzhiyao 已提交
15
package hints
J
wrap kv  
jianzhiyao 已提交
16 17 18

import (
	"time"
M
Ming Deng 已提交
19

20
	"github.com/astaxie/beego/pkg/infrastructure/utils"
J
wrap kv  
jianzhiyao 已提交
21 22
)

J
jianzhiyao 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
const (
	//db level
	KeyMaxIdleConnections = iota
	KeyMaxOpenConnections
	KeyConnMaxLifetime
	KeyMaxStmtCacheSize

	//query level
	KeyForceIndex
	KeyUseIndex
	KeyIgnoreIndex
	KeyForUpdate
	KeyLimit
	KeyOffset
	KeyOrderBy
	KeyRelDepth
)

J
wrap kv  
jianzhiyao 已提交
41 42 43 44 45
type Hint struct {
	key   interface{}
	value interface{}
}

46
var _ utils.KV = new(Hint)
J
wrap kv  
jianzhiyao 已提交
47 48 49 50 51 52 53 54 55 56 57

// GetKey return key
func (s *Hint) GetKey() interface{} {
	return s.key
}

// GetValue return value
func (s *Hint) GetValue() interface{} {
	return s.value
}

58
var _ utils.KV = new(Hint)
J
wrap kv  
jianzhiyao 已提交
59 60 61

// MaxIdleConnections return a hint about MaxIdleConnections
func MaxIdleConnections(v int) *Hint {
J
jianzhiyao 已提交
62
	return NewHint(KeyMaxIdleConnections, v)
J
wrap kv  
jianzhiyao 已提交
63 64 65 66
}

// MaxOpenConnections return a hint about MaxOpenConnections
func MaxOpenConnections(v int) *Hint {
J
jianzhiyao 已提交
67
	return NewHint(KeyMaxOpenConnections, v)
J
wrap kv  
jianzhiyao 已提交
68 69 70 71
}

// ConnMaxLifetime return a hint about ConnMaxLifetime
func ConnMaxLifetime(v time.Duration) *Hint {
J
jianzhiyao 已提交
72
	return NewHint(KeyConnMaxLifetime, v)
J
wrap kv  
jianzhiyao 已提交
73 74
}

J
jianzhiyao 已提交
75 76
// MaxStmtCacheSize return a hint about MaxStmtCacheSize
func MaxStmtCacheSize(v int) *Hint {
J
jianzhiyao 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	return NewHint(KeyMaxStmtCacheSize, v)
}

// ForceIndex return a hint about ForceIndex
func ForceIndex(indexes ...string) *Hint {
	return NewHint(KeyForceIndex, indexes)
}

// UseIndex return a hint about UseIndex
func UseIndex(indexes ...string) *Hint {
	return NewHint(KeyUseIndex, indexes)
}

// IgnoreIndex return a hint about IgnoreIndex
func IgnoreIndex(indexes ...string) *Hint {
	return NewHint(KeyIgnoreIndex, indexes)
}

// ForUpdate return a hint about ForUpdate
func ForUpdate() *Hint {
	return NewHint(KeyForUpdate, true)
}

// DefaultRelDepth return a hint about DefaultRelDepth
func DefaultRelDepth() *Hint {
	return NewHint(KeyRelDepth, true)
}

// RelDepth return a hint about RelDepth
func RelDepth(d int) *Hint {
	return NewHint(KeyRelDepth, d)
}

// Limit return a hint about Limit
func Limit(d int64) *Hint {
	return NewHint(KeyLimit, d)
}

// Offset return a hint about Offset
func Offset(d int64) *Hint {
	return NewHint(KeyOffset, d)
}

// OrderBy return a hint about OrderBy
func OrderBy(s string) *Hint {
	return NewHint(KeyOrderBy, s)
J
jianzhiyao 已提交
123 124
}

J
wrap kv  
jianzhiyao 已提交
125 126 127 128 129 130 131
// NewHint return a hint
func NewHint(key interface{}, value interface{}) *Hint {
	return &Hint{
		key:   key,
		value: value,
	}
}