MSP-5013 add full variable support
This commit is contained in:
1 parent
d6a8f77610
commit
ecf2d06cfa
20 files changed
+786
-156
No files matched your search
+45
-15
@@ -42,7 +42,7 @@ func (d Adapter) Select(ctx context.Context, ptr any, query string, args ...any)
|
||||
consistencyFromContext(ctx).appendParams(params)
|
||||
err := d.cli.Post("/db/query").
|
||||
QueryParams(params).
|
||||
JsonRequestBody(requests(request(query, args...))).
|
||||
JsonRequestBody(Requests(Request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
@@ -69,7 +69,7 @@ func (d Adapter) SelectRow(ctx context.Context, ptr any, query string, args ...a
|
||||
consistencyFromContext(ctx).appendParams(params)
|
||||
httpResp, err := d.cli.Post("/db/request").
|
||||
QueryParams(params).
|
||||
JsonRequestBody(requests(request(query, args...))).
|
||||
JsonRequestBody(Requests(Request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
Do(ctx)
|
||||
@@ -98,21 +98,19 @@ func (d Adapter) SelectRow(ctx context.Context, ptr any, query string, args ...a
|
||||
}
|
||||
|
||||
func (d Adapter) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
result := &Result{}
|
||||
resp := &Response{
|
||||
Results: []*Result{result},
|
||||
}
|
||||
err := d.cli.Post("/db/execute").
|
||||
QueryParams(map[string]any{
|
||||
"timings": true,
|
||||
}).JsonRequestBody(requests(request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
results, err := d.exec(ctx, false, Requests(Request(query, args...)))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "call rqlite")
|
||||
return nil, err
|
||||
}
|
||||
err = catchError(resp, result)
|
||||
|
||||
if len(results) == 0 {
|
||||
return nil, errors.Errorf("unexpected empty results")
|
||||
}
|
||||
result, ok := results[0].(*Result)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected result type: %T", results[0])
|
||||
}
|
||||
err = catchError(nil, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,6 +122,38 @@ func (d Adapter) ExecNamed(ctx context.Context, query string, arg any) (sql.Resu
|
||||
return d.Exec(ctx, query, arg)
|
||||
}
|
||||
|
||||
func (d Adapter) ExecTransaction(ctx context.Context, requests [][]any) ([]sql.Result, error) {
|
||||
return d.exec(ctx, true, requests)
|
||||
}
|
||||
|
||||
func (d Adapter) exec(ctx context.Context, inTx bool, requests [][]any) ([]sql.Result, error) {
|
||||
resp := &Response{
|
||||
Results: []*Result{},
|
||||
}
|
||||
err := d.cli.Post("/db/execute").
|
||||
QueryParams(map[string]any{
|
||||
"timings": true,
|
||||
"transaction": inTx,
|
||||
}).JsonRequestBody(requests).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "call rqlite")
|
||||
}
|
||||
err = catchError(resp, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := make([]sql.Result, 0, len(requests))
|
||||
for _, result := range resp.Results {
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func catchError(resp *Response, result *Result) error {
|
||||
if resp != nil && resp.Error != "" {
|
||||
return errors.Errorf("rqlite api: %s", resp.Error)
|
||||
|
||||
@@ -10,4 +10,5 @@ type DB interface {
|
||||
SelectRow(ctx context.Context, ptr any, query string, args ...any) error
|
||||
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
|
||||
ExecTransaction(ctx context.Context, requests [][]any) ([]sql.Result, error)
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package db
|
||||
|
||||
func request(query string, args ...any) []any {
|
||||
func Request(query string, args ...any) []any {
|
||||
return append([]any{query}, args...)
|
||||
}
|
||||
|
||||
func requests(requests ...[]any) [][]any {
|
||||
func Requests(requests ...[]any) [][]any {
|
||||
return requests
|
||||
}
|
||||
Reference in new issue
Block a user