Skip to content

Commit 59c2bb1

Browse files
committed
push code client grpc-client sql_util
1 parent 9e7da98 commit 59c2bb1

File tree

3 files changed

+335
-0
lines changed

3 files changed

+335
-0
lines changed

client/instance.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,164 @@ func (c *ProxyClient) QueryWithTx(ctx context.Context, tx string, commit bool, r
101101
err := PostWithClientAndDecode(ctx, c.Client, c.Url+"/query?tx="+tx+sc, stm, &result, c.Log)
102102
return err
103103
}
104+
105+
func (c *ProxyClient) Insert(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error) {
106+
var buildParam func(int) string
107+
if len(options) > 0 {
108+
buildParam = options[0]
109+
} else {
110+
buildParam = sql.BuildDollarParam
111+
}
112+
s, values := sql.BuildToInsert(table, model, buildParam)
113+
return c.Exec(ctx, s, values...)
114+
}
115+
func (c *ProxyClient) Update(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error) {
116+
var buildParam func(int) string
117+
if len(options) > 0 {
118+
buildParam = options[0]
119+
} else {
120+
buildParam = sql.BuildDollarParam
121+
}
122+
s, values := sql.BuildToUpdate(table, model, buildParam)
123+
return c.Exec(ctx, s, values...)
124+
}
125+
func (c *ProxyClient) Save(ctx context.Context, table string, model interface{}, options...string) (int64, error) {
126+
driver := sql.DriverPostgres
127+
var buildParam func(int) string
128+
if len(options) > 0 {
129+
driver = options[0]
130+
}
131+
buildParam = sql.GetBuildByDriver(driver)
132+
s, values, err := sql.BuildToSave(table, model, driver, buildParam)
133+
if err != nil {
134+
return -1, err
135+
}
136+
return c.Exec(ctx, s, values...)
137+
}
138+
func (c *ProxyClient) InsertBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
139+
driver := sql.DriverPostgres
140+
var buildParam func(int) string
141+
if len(options) > 0 {
142+
driver = options[0]
143+
}
144+
buildParam = sql.GetBuildByDriver(driver)
145+
s, values, err := sql.BuildToInsertBatch(table, models, driver, buildParam)
146+
if err != nil {
147+
return -1, err
148+
}
149+
return c.Exec(ctx, s, values...)
150+
}
151+
func (c *ProxyClient) UpdateBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
152+
driver := sql.DriverPostgres
153+
var buildParam func(int) string
154+
if len(options) > 0 {
155+
driver = options[0]
156+
}
157+
buildParam = sql.GetBuildByDriver(driver)
158+
s, err := sql.BuildToUpdateBatch(table, models, buildParam, driver)
159+
if err != nil {
160+
return -1, err
161+
}
162+
if len(s) > 0 {
163+
return c.ExecBatch(ctx, false, s...)
164+
} else {
165+
return 0, nil
166+
}
167+
}
168+
func (c *ProxyClient) SaveBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
169+
driver := sql.DriverPostgres
170+
var buildParam func(int) string
171+
if len(options) > 0 {
172+
driver = options[0]
173+
}
174+
buildParam = sql.GetBuildByDriver(driver)
175+
s, err := sql.BuildToSaveBatch(table, models, driver, buildParam)
176+
if err != nil {
177+
return -1, err
178+
}
179+
if len(s) > 0 {
180+
return c.ExecBatch(ctx, false, s...)
181+
} else {
182+
return 0, nil
183+
}
184+
}
185+
func (c *ProxyClient) InsertWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error) {
186+
var buildParam func(int) string
187+
if len(options) > 0 {
188+
buildParam = options[0]
189+
} else {
190+
buildParam = sql.BuildDollarParam
191+
}
192+
s, values := sql.BuildToInsert(table, model, buildParam)
193+
return c.ExecWithTx(ctx, tx, commit, s, values...)
194+
}
195+
func (c *ProxyClient) UpdateWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error) {
196+
var buildParam func(int) string
197+
if len(options) > 0 {
198+
buildParam = options[0]
199+
} else {
200+
buildParam = sql.BuildDollarParam
201+
}
202+
s, values := sql.BuildToUpdate(table, model, buildParam)
203+
return c.ExecWithTx(ctx, tx, commit, s, values...)
204+
}
205+
func (c *ProxyClient) SaveWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...string) (int64, error) {
206+
driver := sql.DriverPostgres
207+
var buildParam func(int) string
208+
if len(options) > 0 {
209+
driver = options[0]
210+
}
211+
buildParam = sql.GetBuildByDriver(driver)
212+
s, values, err := sql.BuildToSave(table, model, driver, buildParam)
213+
if err != nil {
214+
return -1, err
215+
}
216+
return c.ExecWithTx(ctx, tx, commit, s, values...)
217+
}
218+
func (c *ProxyClient) InsertBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
219+
driver := sql.DriverPostgres
220+
var buildParam func(int) string
221+
if len(options) > 0 {
222+
driver = options[0]
223+
}
224+
buildParam = sql.GetBuildByDriver(driver)
225+
s, values, err := sql.BuildToInsertBatch(table, models, driver, buildParam)
226+
if err != nil {
227+
return -1, err
228+
}
229+
return c.ExecWithTx(ctx, tx, commit, s, values...)
230+
}
231+
func (c *ProxyClient) UpdateBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
232+
driver := sql.DriverPostgres
233+
var buildParam func(int) string
234+
if len(options) > 0 {
235+
driver = options[0]
236+
}
237+
buildParam = sql.GetBuildByDriver(driver)
238+
s, err := sql.BuildToUpdateBatch(table, models, buildParam, driver)
239+
if err != nil {
240+
return -1, err
241+
}
242+
if len(s) > 0 {
243+
return c.ExecBatchWithTx(ctx, tx, commit, false, s...)
244+
} else {
245+
return 0, nil
246+
}
247+
}
248+
func (c *ProxyClient) SaveBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
249+
driver := sql.DriverPostgres
250+
var buildParam func(int) string
251+
if len(options) > 0 {
252+
driver = options[0]
253+
}
254+
buildParam = sql.GetBuildByDriver(driver)
255+
s, err := sql.BuildToSaveBatch(table, models, driver, buildParam)
256+
if err != nil {
257+
return -1, err
258+
}
259+
if len(s) > 0 {
260+
return c.ExecBatchWithTx(ctx, tx, commit, false, s...)
261+
} else {
262+
return 0, nil
263+
}
264+
}

grpc-client/grpc_client.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,164 @@ func (c *GRPCClient) QueryWithTx(ctx context.Context, tx string, commit bool, re
205205
er3 := x.Decode(result)
206206
return er3
207207
}
208+
209+
func (c *GRPCClient) Insert(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error) {
210+
var buildParam func(int) string
211+
if len(options) > 0 {
212+
buildParam = options[0]
213+
} else {
214+
buildParam = sql.BuildDollarParam
215+
}
216+
s, values := sql.BuildToInsert(table, model, buildParam)
217+
return c.Exec(ctx, s, values...)
218+
}
219+
func (c *GRPCClient) Update(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error) {
220+
var buildParam func(int) string
221+
if len(options) > 0 {
222+
buildParam = options[0]
223+
} else {
224+
buildParam = sql.BuildDollarParam
225+
}
226+
s, values := sql.BuildToUpdate(table, model, buildParam)
227+
return c.Exec(ctx, s, values...)
228+
}
229+
func (c *GRPCClient) Save(ctx context.Context, table string, model interface{}, options...string) (int64, error) {
230+
driver := sql.DriverPostgres
231+
var buildParam func(int) string
232+
if len(options) > 0 {
233+
driver = options[0]
234+
}
235+
buildParam = sql.GetBuildByDriver(driver)
236+
s, values, err := sql.BuildToSave(table, model, driver, buildParam)
237+
if err != nil {
238+
return -1, err
239+
}
240+
return c.Exec(ctx, s, values...)
241+
}
242+
func (c *GRPCClient) InsertBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
243+
driver := sql.DriverPostgres
244+
var buildParam func(int) string
245+
if len(options) > 0 {
246+
driver = options[0]
247+
}
248+
buildParam = sql.GetBuildByDriver(driver)
249+
s, values, err := sql.BuildToInsertBatch(table, models, driver, buildParam)
250+
if err != nil {
251+
return -1, err
252+
}
253+
return c.Exec(ctx, s, values...)
254+
}
255+
func (c *GRPCClient) UpdateBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
256+
driver := sql.DriverPostgres
257+
var buildParam func(int) string
258+
if len(options) > 0 {
259+
driver = options[0]
260+
}
261+
buildParam = sql.GetBuildByDriver(driver)
262+
s, err := sql.BuildToUpdateBatch(table, models, buildParam, driver)
263+
if err != nil {
264+
return -1, err
265+
}
266+
if len(s) > 0 {
267+
return c.ExecBatch(ctx, false, s...)
268+
} else {
269+
return 0, nil
270+
}
271+
}
272+
func (c *GRPCClient) SaveBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error) {
273+
driver := sql.DriverPostgres
274+
var buildParam func(int) string
275+
if len(options) > 0 {
276+
driver = options[0]
277+
}
278+
buildParam = sql.GetBuildByDriver(driver)
279+
s, err := sql.BuildToSaveBatch(table, models, driver, buildParam)
280+
if err != nil {
281+
return -1, err
282+
}
283+
if len(s) > 0 {
284+
return c.ExecBatch(ctx, false, s...)
285+
} else {
286+
return 0, nil
287+
}
288+
}
289+
func (c *GRPCClient) InsertWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error) {
290+
var buildParam func(int) string
291+
if len(options) > 0 {
292+
buildParam = options[0]
293+
} else {
294+
buildParam = sql.BuildDollarParam
295+
}
296+
s, values := sql.BuildToInsert(table, model, buildParam)
297+
return c.ExecWithTx(ctx, tx, commit, s, values...)
298+
}
299+
func (c *GRPCClient) UpdateWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error) {
300+
var buildParam func(int) string
301+
if len(options) > 0 {
302+
buildParam = options[0]
303+
} else {
304+
buildParam = sql.BuildDollarParam
305+
}
306+
s, values := sql.BuildToUpdate(table, model, buildParam)
307+
return c.ExecWithTx(ctx, tx, commit, s, values...)
308+
}
309+
func (c *GRPCClient) SaveWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...string) (int64, error) {
310+
driver := sql.DriverPostgres
311+
var buildParam func(int) string
312+
if len(options) > 0 {
313+
driver = options[0]
314+
}
315+
buildParam = sql.GetBuildByDriver(driver)
316+
s, values, err := sql.BuildToSave(table, model, driver, buildParam)
317+
if err != nil {
318+
return -1, err
319+
}
320+
return c.ExecWithTx(ctx, tx, commit, s, values...)
321+
}
322+
func (c *GRPCClient) InsertBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
323+
driver := sql.DriverPostgres
324+
var buildParam func(int) string
325+
if len(options) > 0 {
326+
driver = options[0]
327+
}
328+
buildParam = sql.GetBuildByDriver(driver)
329+
s, values, err := sql.BuildToInsertBatch(table, models, driver, buildParam)
330+
if err != nil {
331+
return -1, err
332+
}
333+
return c.ExecWithTx(ctx, tx, commit, s, values...)
334+
}
335+
func (c *GRPCClient) UpdateBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
336+
driver := sql.DriverPostgres
337+
var buildParam func(int) string
338+
if len(options) > 0 {
339+
driver = options[0]
340+
}
341+
buildParam = sql.GetBuildByDriver(driver)
342+
s, err := sql.BuildToUpdateBatch(table, models, buildParam, driver)
343+
if err != nil {
344+
return -1, err
345+
}
346+
if len(s) > 0 {
347+
return c.ExecBatchWithTx(ctx, tx, commit, false, s...)
348+
} else {
349+
return 0, nil
350+
}
351+
}
352+
func (c *GRPCClient) SaveBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error) {
353+
driver := sql.DriverPostgres
354+
var buildParam func(int) string
355+
if len(options) > 0 {
356+
driver = options[0]
357+
}
358+
buildParam = sql.GetBuildByDriver(driver)
359+
s, err := sql.BuildToSaveBatch(table, models, driver, buildParam)
360+
if err != nil {
361+
return -1, err
362+
}
363+
if len(s) > 0 {
364+
return c.ExecBatchWithTx(ctx, tx, commit, false, s...)
365+
} else {
366+
return 0, nil
367+
}
368+
}

sql_util.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,17 @@ type Proxy interface {
642642
ExecWithTx(ctx context.Context, tx string, commit bool, query string, values ...interface{}) (int64, error)
643643
ExecBatchWithTx(ctx context.Context, tx string, commit bool, master bool, stm ...Statement) (int64, error)
644644
QueryWithTx(ctx context.Context, tx string, commit bool, result interface{}, query string, values ...interface{}) error
645+
646+
Insert(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error)
647+
Update(ctx context.Context, table string, model interface{}, options...func(int) string) (int64, error)
648+
Save(ctx context.Context, table string, model interface{}, options...string) (int64, error)
649+
InsertBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error)
650+
UpdateBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error)
651+
SaveBatch(ctx context.Context, table string, models interface{}, options...string) (int64, error)
652+
InsertWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error)
653+
UpdateWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...func(int) string) (int64, error)
654+
SaveWithTx(ctx context.Context, tx string, commit bool, table string, model interface{}, options...string) (int64, error)
655+
InsertBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error)
656+
UpdateBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error)
657+
SaveBatchWithTx(ctx context.Context, tx string, commit bool, table string, models interface{}, options...string) (int64, error)
645658
}

0 commit comments

Comments
 (0)