Skip to content

Commit

Permalink
Merge pull request #788 from l0nax/expose-exec-for-raw
Browse files Browse the repository at this point in the history
Expose Exec(…) method for RawQuery
  • Loading branch information
vmihailenco authored Mar 4, 2023
2 parents 26ab63a + 11192c8 commit 0aad086
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
7 changes: 6 additions & 1 deletion query_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,12 @@ type customValueQuery struct {
func (q *customValueQuery) addValue(
table *schema.Table, column string, value string, args []interface{},
) {
if _, ok := table.FieldMap[column]; ok {
ok := false
if table != nil {
_, ok = table.FieldMap[column]
}

if ok {
if q.modelValues == nil {
q.modelValues = make(map[string]schema.QueryWithArgs)
}
Expand Down
40 changes: 34 additions & 6 deletions query_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bun

import (
"context"
"database/sql"

"github.com/uptrace/bun/schema"
)
Expand Down Expand Up @@ -46,19 +47,46 @@ func (q *RawQuery) Err(err error) *RawQuery {
return q
}

func (q *RawQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
return q.scanOrExec(ctx, dest, len(dest) > 0)
}

func (q *RawQuery) Scan(ctx context.Context, dest ...interface{}) error {
_, err := q.scanOrExec(ctx, dest, true)
return err
}

func (q *RawQuery) scanOrExec(
ctx context.Context, dest []interface{}, hasDest bool,
) (sql.Result, error) {
if q.err != nil {
return q.err
return nil, q.err
}

model, err := q.getModel(dest)
if err != nil {
return err
var model Model
var err error

if hasDest {
model, err = q.getModel(dest)
if err != nil {
return nil, err
}
}

query := q.db.format(q.query, q.args)
_, err = q.scan(ctx, q, query, model, true)
return err
var res sql.Result

if hasDest {
res, err = q.scan(ctx, q, query, model, hasDest)
} else {
res, err = q.exec(ctx, q, query)
}

if err != nil {
return nil, err
}

return res, nil
}

func (q *RawQuery) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error) {
Expand Down

0 comments on commit 0aad086

Please sign in to comment.