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

Scan into embedded structs #160

Closed
1 of 3 tasks
JeremyCraven opened this issue Sep 17, 2019 · 6 comments
Closed
1 of 3 tasks

Scan into embedded structs #160

JeremyCraven opened this issue Sep 17, 2019 · 6 comments
Assignees

Comments

@JeremyCraven
Copy link

Is your feature request related to a problem? Please describe.
Being able to use the built in goqu functionality like ScanStructContext and ScanStructsContext is great and makes for concise, readable code. However, I have not yet found a way to scan into a struct that has embedded structs within it.

Describe the solution you'd like
A simple example of something we would like to be able to do with goqu below.

Let's say there are two tables (table1 and table2) and we write a query to join these tables and select all columns from both the tables. I want to scan into a struct that looks like this rows struct that contains structs that define all the columns for table1 and table 2.

var rows []struct {
  Table1 table1Model `db:"table1"`
  Table2 table2Model `db:"table2"`
}

var table1Model struct {
  col1 datatype `db:"col1"`
  col2 datatype `db:"col2"`
}

var table2Model struct {
  col1 datatype `db:"col1"`
  col2 datatype `db:"col2"`
}

The query would look roughly like this.

goqu.From("table1").
  Prepared(true).
  InnerJoin(
    goqu.T("table2"),
    goqu.On(goqu.T("table1).Col("col1")Eq(goqu.T("table2").Col("col2"))),
  ).
  Select(
    goqu.L("table1"."col1" as "table1.col1"),
    goqu.L("table1"."col2" as "table1.col2"),
    goqu.L("table2"."col1" as "table2.col1"),
    goqu.L("table2"."col2" as "table2.col2"),
  ).
  ScanStructsContext(ctx, &rows)

Describe alternatives you've considered
Right now I can accomplish this by using the ToSQL() method on my query and then executing the query with sqlx because the sqlx StructScan method supports scanning into nested structs.

Dialect

  • postgres
  • mysql
  • sqlite3

Additional context
This is for sure a nice to have in goqu as it is not blocking functionality at the moment, but could help clean up a lot of code by using only goqu to accomplish these queries.

@doug-martin
Copy link
Owner

Thank you for the feature request! If you want to open a PR adding the functionality go for it!

If you don't open a PR I'll take a look at how sqlx handles this.

doug-martin added a commit that referenced this issue Sep 30, 2019
* [ADDED] Ability to scan into struct fields from multiple tables #160
@doug-martin doug-martin mentioned this issue Sep 30, 2019
@doug-martin
Copy link
Owner

@JeremyCraven just added this in #171 let me know what you think and I'll merge.

doug-martin added a commit that referenced this issue Sep 30, 2019
* [ADDED] Ability to scan into struct fields from multiple tables #160
@doug-martin
Copy link
Owner

Released as v9.4.0

@aliksend
Copy link

aliksend commented Jun 3, 2020

As I see it doesn't support nullable embedded structs, that can be receiver using OUTER JOIN. Is there any workaround to get it in one request?

@JeremyCraven
Copy link
Author

@JeremyCraven just added this in #171 let me know what you think and I'll merge.

The notification must have slipped through awhile back, but this is definitely being leveraged. Thank you.

@aliksend
Copy link

aliksend commented Jun 9, 2020

this is definitely being leveraged

So do you plan to implement it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants