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

Conversation

josearomeroj
Copy link
Contributor

Added raw query support: #593

@vmihailenco
Copy link
Member

Hey,

The suggested API looks like this:

err := db.Raw(query, args...).Scan(ctx, &strct)
err := db.Raw(query, args...).Scan(ctx, &slice)
err := db.Raw(query, args...).Scan(ctx, &var1, &var2)

The implementation should look like this:

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

type RawQuery struct {
    db *DB
    query string
    args ...interface{}
}

func (q *RawQuery) Scan(ctx context.Context, dest ...interface{}) error {
    // extract and reuse Scan implementation from SelectQuery.Scan
    // to format the query, use query := db.format(q.query, q.args)
    return scanModel(...)
}

Does that make sense?

@josearomeroj
Copy link
Contributor Author

Did you mean something like that?

@vmihailenco
Copy link
Member

Did you mean something like that?

Yes, exactly 👍 Could you add a simple test like db.Raw("SELECT ?", 123).Scan(ctx, &num)?

query_raw.go Outdated
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

@josearomeroj
Copy link
Contributor Author

Let me know if you see the need to change something else

@vmihailenco
Copy link
Member

Very good - thanks 👍

@vmihailenco vmihailenco merged commit 127644d into uptrace:master Jul 1, 2022
@choyri choyri mentioned this pull request Aug 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants