Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added raw query calls #596

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions query_raw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bun

import (
"context"
"github.com/uptrace/bun/schema"
)

type RawQuery struct {
baseQuery

query string
args []interface{}
}

func (db *DB) Raw(query string, args ...interface{}) *RawQuery {
return &RawQuery{
baseQuery: baseQuery{
db: db,
conn: db.DB,
},
query: query,
args: args,
}
}

func (q *RawQuery) Scan(ctx context.Context, dest ...interface{}) error {
if q.err != nil {
return q.err
}

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

query := q.db.format(q.query, q.args)
_, err = q.scan(ctx, q, query, model, true)
return err
}

func (q *RawQuery) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error) {
query := []byte(fmter.FormatQuery(q.query, q.args))
b = append(b, query...)

return b, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be return fmter.AppendQuery(b, q.query, q.args), nil

}

func (q *RawQuery) Operation() string {
return "SELECT"
}