gitee.com/h79/goutils@v1.22.10/dao/option/option.go (about) 1 package option 2 3 import "gitee.com/h79/goutils/common/option" 4 5 const ( 6 TypeMaxColumn = iota 7 TypeFullSql 8 TypeSqlColumnConvert 9 TypeSubQuery 10 TypeQuery 11 TypeNameKey //子查询 12 TypeTimeOut 13 TypeAlarm 14 TypeSqlDns = 1000 15 TypeSqlTls = 1001 16 TypeRedisTls = 1002 17 TypeSqlServerPubKey = 1003 18 ) 19 20 type Query struct { 21 Q map[string]interface{} 22 } 23 24 func WithQuery() option.Option { 25 return &Query{Q: map[string]interface{}{}} 26 } 27 28 func (t Query) String() string { 29 return "query" 30 } 31 func (t Query) Type() int { return TypeQuery } 32 func (t Query) Value() interface{} { return t.Q } 33 34 func QueryExist(opts ...option.Option) *Query { 35 if r, ok := option.Exist(TypeQuery, opts...); ok { 36 return r.(*Query) 37 } 38 return nil 39 } 40 41 type QueryFunc func(q *Query) 42 43 func (f QueryFunc) Apply(q *Query) { 44 f(q) 45 } 46 47 func WithMaxColumn(c int) option.Option { 48 return option.WithOpt(c, "max:column", TypeMaxColumn) 49 } 50 51 func MaxColumnExist(opts ...option.Option) int { 52 return option.Select[int](TypeMaxColumn, 6, opts...) 53 } 54 55 func WithFullSQL() option.Option { 56 return option.WithOpt(true, "full:sql", TypeFullSql) 57 } 58 59 func FullSqlExist(opts ...option.Option) bool { 60 return option.Select[bool](TypeFullSql, false, opts...) 61 } 62 63 func SqlColumnConvert(f func(col string) string) option.Option { 64 return ColumnConvertFunc(f) 65 } 66 67 type ColumnConvertFunc func(col string) string 68 69 func (t ColumnConvertFunc) String() string { 70 return "sql:column:convert" 71 } 72 func (t ColumnConvertFunc) Type() int { return TypeSqlColumnConvert } 73 func (t ColumnConvertFunc) Value() interface{} { return t } 74 75 func SqlColumnConvertExist(opts ...option.Option) ColumnConvertFunc { 76 if r, ok := option.Exist(TypeSqlColumnConvert, opts...); ok { 77 return r.Value().(ColumnConvertFunc) 78 } 79 return nil 80 } 81 82 func WithSubQuerySQL(as string) option.Option { 83 return option.WithOpt(as, "subquery:sql", TypeSubQuery) 84 } 85 86 func SubQueryExist(opts ...option.Option) (string, bool) { 87 return option.SelectV2[string](TypeSubQuery, "", opts...) 88 } 89 90 func WithNameKey(name string) option.Option { 91 return option.WithOpt(name, "db.name.key", TypeNameKey) 92 } 93 94 func NameKeyExist(opts ...option.Option) (string, bool) { 95 return option.SelectV2[string](TypeNameKey, "", opts...) 96 } 97 98 func WithTimeOutKey(timeout int64) option.Option { 99 return option.WithOpt(timeout, "db.timeout.key", TypeTimeOut) 100 } 101 102 func TimeOutExist(opts ...option.Option) (int64, bool) { 103 return option.SelectV2[int64](TypeTimeOut, 3000, opts...) 104 } 105 106 func WithAlarmKey() option.Option { 107 return option.WithOpt(true, "db.alarm.key", TypeAlarm) 108 } 109 110 func AlarmExist(opts ...option.Option) bool { 111 return option.Select(TypeAlarm, false, opts...) 112 }