1
- func (m *custom{ {.upperStartCamelObject} }Model) BulkInsert(ctx context.Context, datas []*{ {.upperStartCamelObject} }) error {
1
+ func (m *custom{ {.upperStartCamelObject} }Model) BulkInsert(ctx context.Context, session sqlx.Session, datas []*{ {.upperStartCamelObject} }) error {
2
2
sb := sqlbuilder.InsertInto(m.table)
3
3
sb.Cols({{.lowerStartCamelObject} }RowsExpectAutoSet)
4
4
for _, data := range datas {
5
5
sb.Values({{.expressionValues} })
6
6
}
7
- sql, args := sb.Build()
8
- _, err:= m.conn.ExecCtx(ctx, sql, args...)
7
+ statement, args := sb.Build()
8
+
9
+ var err error
10
+ if session != nil {
11
+ _, err = session.ExecCtx(ctx, statement, args...)
12
+ } else {
13
+ {{if .withCache} }_, err = m.ExecNoCacheCtx(ctx, statement, args...){ {else } }_, err = m.conn.ExecCtx(ctx, statement, args...){ {end} }
14
+ }
9
15
return err
10
16
}
11
17
12
- func (m *custom{ {.upperStartCamelObject} }Model) FindByCondition(ctx context.Context, conds ...condition.Condition) ([]*{ {.upperStartCamelObject} }, error) {
13
- sb := sqlbuilder.Select({{.lowerStartCamelObject} }FieldNames...).From(m.table)
18
+ func (m *custom{ {.upperStartCamelObject} }Model) FindByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{ {.upperStartCamelObject} }, error) {
19
+ sb := sqlbuilder.Select({{.lowerStartCamelObject} }FieldNames...).From(m.table)
14
20
condition.ApplySelect(sb, conds...)
15
- sql , args := sb.Build()
21
+ statement , args := sb.Build()
16
22
17
23
var resp []*{ {.upperStartCamelObject} }
18
- err := m.conn.QueryRowsCtx(ctx, &resp, sql, args...)
24
+ var err error
25
+
26
+ if session != nil {
27
+ err = session.QueryRowsCtx(ctx, &resp, statement, args...)
28
+ } else {
29
+ {{if .withCache} }err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){ {else } }err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){ {end} }
30
+ }
19
31
if err != nil {
20
32
return nil, err
21
33
}
22
34
return resp, nil
23
35
}
24
36
25
- func (m *custom{ {.upperStartCamelObject} }Model) FindOneByCondition(ctx context.Context, conds ...condition.Condition) (*{ {.upperStartCamelObject} }, error) {
37
+ func (m *custom{ {.upperStartCamelObject} }Model) FindOneByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) (*{ {.upperStartCamelObject} }, error) {
26
38
sb := sqlbuilder.Select({{.lowerStartCamelObject} }FieldNames...).From(m.table)
39
+
27
40
condition.ApplySelect(sb, conds...)
28
41
sb.Limit(1)
29
- sql , args := sb.Build()
42
+ statement , args := sb.Build()
30
43
31
44
var resp { {.upperStartCamelObject} }
32
- err := m.conn.QueryRowCtx(ctx, &resp, sql, args...)
45
+ var err error
46
+
47
+ if session != nil {
48
+ err = session.QueryRowCtx(ctx, &resp, statement, args...)
49
+ } else {
50
+ {{if .withCache} }err = m.QueryRowNoCacheCtx(ctx, &resp, statement, args...){ {else } }err = m.conn.QueryRowCtx(ctx, &resp, statement, args...){ {end} }
51
+ }
33
52
if err != nil {
34
53
return nil, err
35
54
}
36
55
return &resp, nil
37
56
}
38
57
39
- func (m *custom{ {.upperStartCamelObject} }Model) PageByCondition(ctx context.Context, conds ...condition.Condition) ([]*{ {.upperStartCamelObject} }, int64 ,error) {
58
+ func (m *custom{ {.upperStartCamelObject} }Model) PageByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{ {.upperStartCamelObject} }, int64 ,error) {
40
59
sb := sqlbuilder.Select({{.lowerStartCamelObject} }FieldNames...).From(m.table)
41
60
countsb := sqlbuilder.Select("count(*)").From(m.table)
42
61
@@ -51,24 +70,34 @@ func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Cont
51
70
condition.ApplySelect(countsb, countConds...)
52
71
53
72
var resp []*{ {.upperStartCamelObject} }
73
+ var err error
54
74
55
- sql, args := sb.Build()
56
- err := m.conn.QueryRowsCtx(ctx, &resp, sql, args...)
75
+ statement, args := sb.Build()
76
+
77
+ if session != nil {
78
+ err = session.QueryRowsCtx(ctx, &resp, statement, args...)
79
+ } else {
80
+ {{if .withCache} }err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){ {else } }err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){ {end} }
81
+ }
57
82
if err != nil {
58
83
return nil, 0, err
59
84
}
60
85
61
86
var total int64
62
- sql, args = countsb.Build()
63
- err = m.conn.QueryRowCtx(ctx, &total, sql, args...)
87
+ statement, args = countsb.Build()
88
+ if session != nil {
89
+ err = session.QueryRowCtx(ctx, &total, statement, args...)
90
+ } else {
91
+ {{if .withCache} }err = m.QueryRowNoCacheCtx(ctx, &total, statement, args...){ {else } }err = m.conn.QueryRowCtx(ctx, &total, statement, args...){ {end} }
92
+ }
64
93
if err != nil {
65
94
return nil, 0, err
66
95
}
67
96
68
97
return resp, total, nil
69
98
}
70
99
71
- func (m *custom{ {.upperStartCamelObject} }Model) UpdateFieldsByCondition(ctx context.Context, field map[string]any, conds ...condition.Condition) error {
100
+ func (m *custom{ {.upperStartCamelObject} }Model) UpdateFieldsByCondition(ctx context.Context, session sqlx.Session, field map[string]any, conds ...condition.Condition) error {
72
101
if field == nil {
73
102
return nil
74
103
}
@@ -82,21 +111,33 @@ func (m *custom{{.upperStartCamelObject}}Model) UpdateFieldsByCondition(ctx cont
82
111
}
83
112
sb.Set(assigns...)
84
113
85
- sql, args := sb.Build()
86
- _, err := m.conn.ExecCtx(ctx, sql, args...)
114
+ statement, args := sb.Build()
115
+
116
+ var err error
117
+ if session != nil {
118
+ _, err = session.ExecCtx(ctx, statement, args...)
119
+ } else {
120
+ {{if .withCache} }_, err = m.ExecNoCacheCtx(ctx, statement, args...){ {else } }_, err = m.conn.ExecCtx(ctx, statement, args...){ {end} }
121
+ }
87
122
if err != nil {
88
123
return err
89
124
}
90
125
return nil
91
126
}
92
127
93
- func (m *custom{ {.upperStartCamelObject} }Model) DeleteByCondition(ctx context.Context, conds ...condition.Condition) error {
128
+ func (m *custom{ {.upperStartCamelObject} }Model) DeleteByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) error {
94
129
if len(conds) == 0 {
95
130
return nil
96
131
}
97
132
sb := sqlbuilder.DeleteFrom(m.table)
98
133
condition.ApplyDelete(sb, conds...)
99
- sql, args := sb.Build()
100
- _, err := m.conn.ExecCtx(ctx, sql, args...)
134
+ statement, args := sb.Build()
135
+
136
+ var err error
137
+ if session != nil {
138
+ _, err = session.ExecCtx(ctx, statement, args...)
139
+ } else {
140
+ {{if .withCache} }_, err = m.ExecNoCacheCtx(ctx, statement, args...){ {else } }_, err = m.conn.ExecCtx(ctx, statement, args...){ {end} }
141
+ }
101
142
return err
102
143
}
0 commit comments