代码拉取完成,页面将自动刷新
package option
const (
TypeMaxColumn = iota
TypeFullSql
TypeSqlColumnConvert
TypeSubQuery
TypeQuery
TypeNameKey //子查询
TypeTimeOut
TypeAlarm
TypeSqlDns = 1000
TypeSqlTls = 1001
TypeRedisTls = 1002
TypeSqlServerPubKey = 1003
)
type Option interface {
String() string
Type() int
Value() interface{}
}
func Filter(filter func(opt Option) bool, opts ...Option) {
for i := range opts {
if filter(opts[i]) {
return
}
}
}
func Exist(ty int, opts ...Option) (Option, bool) {
for i := range opts {
if opts[i].Type() == ty {
return opts[i], true
}
}
return nil, false
}
type Opt[T any] struct {
v T
t int
d string
}
func WithOpt[T any](v T, desc string, t int) Option {
return &Opt[T]{v: v, d: desc, t: t}
}
func (o Opt[T]) String() string {
return o.d
}
func (o Opt[T]) Type() int { return o.t }
func (o Opt[T]) Value() interface{} { return o.v }
func OptExist[T any](t int, defValue T, opts ...Option) (T, bool) {
if r, ok := Exist(t, opts...); ok {
return r.Value().(T), true
}
return defValue, false
}
type Query struct {
Q map[string]interface{}
}
func WithQuery() Option {
return &Query{Q: map[string]interface{}{}}
}
func (t Query) String() string {
return "query"
}
func (t Query) Type() int { return TypeQuery }
func (t Query) Value() interface{} { return t.Q }
func QueryExist(opts ...Option) *Query {
if r, ok := Exist(TypeQuery, opts...); ok {
return r.(*Query)
}
return nil
}
type QueryFunc func(q *Query)
func (f QueryFunc) Apply(q *Query) {
f(q)
}
func WithMaxColumn(c int) Option {
return WithOpt(c, "max:column", TypeMaxColumn)
}
func MaxColumnExist(opts ...Option) int {
r, _ := OptExist[int](TypeMaxColumn, 6, opts...)
return r
}
func WithFullSQL() Option {
return WithOpt(true, "full:sql", TypeFullSql)
}
func FullSqlExist(opts ...Option) bool {
r, _ := OptExist[bool](TypeFullSql, false, opts...)
return r
}
func SqlColumnConvert(f func(col string) string) Option {
return ColumnConvertFunc(f)
}
type ColumnConvertFunc func(col string) string
func (t ColumnConvertFunc) String() string {
return "sql:column:convert"
}
func (t ColumnConvertFunc) Type() int { return TypeSqlColumnConvert }
func (t ColumnConvertFunc) Value() interface{} { return t }
func SqlColumnConvertExist(opts ...Option) ColumnConvertFunc {
if r, ok := Exist(TypeSqlColumnConvert, opts...); ok {
return r.Value().(ColumnConvertFunc)
}
return nil
}
func WithSubQuerySQL(as string) Option {
return WithOpt(as, "subquery:sql", TypeSubQuery)
}
func SubQueryExist(opts ...Option) (string, bool) {
return OptExist[string](TypeSubQuery, "", opts...)
}
func WithNameKey(name string) Option {
return WithOpt(name, "db.name.key", TypeNameKey)
}
func NameKeyExist(opts ...Option) (string, bool) {
return OptExist[string](TypeNameKey, "", opts...)
}
func WithTimeOutKey(timeout int64) Option {
return WithOpt(timeout, "db.timeout.key", TypeTimeOut)
}
func TimeOutExist(opts ...Option) (int64, bool) {
return OptExist[int64](TypeTimeOut, 3000, opts...)
}
func WithAlarmKey() Option {
return WithOpt(true, "db.alarm.key", TypeAlarm)
}
func AlarmExist(opts ...Option) bool {
r, _ := OptExist(TypeAlarm, false, opts...)
return r
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。