正在显示
1 个修改的文件
包含
0 行增加
和
94 行删除
1 | -package repository | ||
2 | - | ||
3 | -import ( | ||
4 | - "bytes" | ||
5 | - "encoding/gob" | ||
6 | - "fmt" | ||
7 | - "github.com/go-pg/pg/v10/orm" | ||
8 | - "github.com/linmadan/egglib-go/utils/snowflake" | ||
9 | -) | ||
10 | - | ||
11 | -var ( | ||
12 | - ERR_EMPTY_TC = fmt.Errorf("transactionContext参数不能为nil") | ||
13 | -) | ||
14 | - | ||
15 | -type Query struct { | ||
16 | - *orm.Query | ||
17 | - queryOptions map[string]interface{} | ||
18 | - AffectRow int | ||
19 | -} | ||
20 | - | ||
21 | -func NewQuery(query *orm.Query, queryOptions map[string]interface{}) *Query { | ||
22 | - return &Query{ | ||
23 | - query, | ||
24 | - queryOptions, | ||
25 | - 0, | ||
26 | - } | ||
27 | -} | ||
28 | - | ||
29 | -func (query *Query) SetWhere(condition, key string) *Query { | ||
30 | - if v, ok := query.queryOptions[key]; ok { | ||
31 | - query.Where(condition, v) | ||
32 | - } | ||
33 | - return query | ||
34 | -} | ||
35 | - | ||
36 | -func (query *Query) SetLimit() *Query { | ||
37 | - if offset, ok := query.queryOptions["offset"]; ok { | ||
38 | - offset := offset.(int) | ||
39 | - if offset > -1 { | ||
40 | - query.Offset(offset) | ||
41 | - } | ||
42 | - } else { | ||
43 | - query.Offset(0) | ||
44 | - } | ||
45 | - if limit, ok := query.queryOptions["limit"]; ok { | ||
46 | - limit := limit.(int) | ||
47 | - if limit > -1 { | ||
48 | - query.Limit(limit) | ||
49 | - } else { | ||
50 | - query.Limit(20) | ||
51 | - } | ||
52 | - } | ||
53 | - return query | ||
54 | -} | ||
55 | - | ||
56 | -func (query *Query) SetOrder(orderColumn string, key string) *Query { | ||
57 | - //query.Order(condition...) | ||
58 | - //return query | ||
59 | - if v, ok := query.queryOptions[key]; ok { | ||
60 | - query.Order(fmt.Sprintf("%v %v", orderColumn, v)) | ||
61 | - } | ||
62 | - return query | ||
63 | -} | ||
64 | - | ||
65 | -func (query *Query) HandleError(err error, errMsg string) error { | ||
66 | - if err.Error() == "pg: no rows in result set" { | ||
67 | - return fmt.Errorf(errMsg) | ||
68 | - } else { | ||
69 | - return err | ||
70 | - } | ||
71 | -} | ||
72 | - | ||
73 | -func NewSnowflakeId() (int64, error) { | ||
74 | - IdWorker, err := snowflake.NewIdWorker(2) | ||
75 | - if err != nil { | ||
76 | - return 0, err | ||
77 | - } | ||
78 | - id, err := IdWorker.NextId() | ||
79 | - return id, err | ||
80 | -} | ||
81 | - | ||
82 | -//GobModelTransform 模型转换 | ||
83 | -func GobModelTransform(dst interface{}, src interface{}) error { | ||
84 | - var data bytes.Buffer | ||
85 | - enc := gob.NewEncoder(&data) | ||
86 | - if err := enc.Encode(src); err != nil { | ||
87 | - return err | ||
88 | - } | ||
89 | - dec := gob.NewDecoder(&data) | ||
90 | - if err := dec.Decode(dst); err != nil { | ||
91 | - return err | ||
92 | - } | ||
93 | - return nil | ||
94 | -} |
-
请 注册 或 登录 后发表评论