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

Implement support for pgx's CopyFrom #1352

Merged
merged 5 commits into from
Jan 16, 2022
Merged

Conversation

Jille
Copy link
Contributor

@Jille Jille commented Dec 21, 2021

I implemented my own feature request #1350.

@Jille Jille force-pushed the copyfrom branch 3 times, most recently from 2fe607f to 720a65d Compare December 21, 2021 21:48
This allows type-safe bulk loading with great performance.

I didn't implement it for Python and Kotlin.

This change is fully backwards compatible, as it only changes the DBTX
interface when someone adds their first :copyFrom query (at which point
it's reasonable to require the CopyFrom method on the DBTX).
@Jille
Copy link
Contributor Author

Jille commented Jan 3, 2022

Any thoughts on this?

Copy link
Collaborator

@kyleconroy kyleconroy left a comment

Choose a reason for hiding this comment

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

  • Please rename :copyFrom to :copyfrom.
  • We need to return an error if you try to use :copyfrom using stdlib PostgreSQL or MySQL
  • The newly generated code should live in a different file

Let me know if you have any questions.

Comment on lines 117 to 162
{{if eq .Cmd ":copyFrom"}}
// iteratorFor{{.MethodName}} implements pgx.CopyFromSource.
type iteratorFor{{.MethodName}} struct {
rows []{{.Arg.DefineType}}
skippedFirstNextCall bool
}

func (r *iteratorFor{{.MethodName}}) Next() bool {
if len(r.rows) == 0 {
return false
}
if !r.skippedFirstNextCall {
r.skippedFirstNextCall = true
return true
}
r.rows = r.rows[1:]
return len(r.rows) > 0
}

func (r iteratorFor{{.MethodName}}) Values() ([]interface{}, error) {
return []interface{}{
{{- if .Arg.Struct }}
{{- range .Arg.Struct.Fields }}
r.rows[0].{{.Name}},
{{- end }}
{{- else }}
r.rows[0],
{{- end }}
}, nil
}

func (r iteratorFor{{.MethodName}}) Err() error {
return nil
}

{{range .Comments}}//{{.}}
{{end -}}
{{- if $.EmitMethodsWithDBArgument}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.SlicePair}}) (int64, error) {
return db.CopyFrom(ctx, {{.TableIdentifier}}, {{.Arg.ColumnNames}}, &iteratorFor{{.MethodName}}{rows: {{.Arg.Name}}})
{{- else}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.SlicePair}}) (int64, error) {
return q.db.CopyFrom(ctx, {{.TableIdentifier}}, {{.Arg.ColumnNames}}, &iteratorFor{{.MethodName}}{rows: {{.Arg.Name}}})
{{- end}}
}
{{end}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's put this code into a different file, something called {query}_copyfrom.go

@Jille
Copy link
Contributor Author

Jille commented Jan 12, 2022

Thanks for the review :)

Did you want a separate file for each query? What about settings to override the filename for those?

And what about the MyQueryParams and MyQueryRow structs? Would they still be generated in querier.go, or should they be moved too? (If they'd have to be moved too, I'd template queryCode.tmpl twice, once for :copyfrom and once for the rest, but put their output in separate files.)

@kyleconroy
Copy link
Collaborator

Did you want a separate file for each query?

Up to you! Probably easier to make it one file for each package.

What about settings to override the filename for those?

That can come later in a follow-on PR.

And what about the MyQueryParams and MyQueryRow structs?

We should keep those where they are today.

Hope that help!

@Jille
Copy link
Contributor Author

Jille commented Jan 13, 2022

Thanks!

All done. Let me know when you want me to rebase+squash.

@kyleconroy kyleconroy merged commit 7d4ff0b into sqlc-dev:main Jan 16, 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.

2 participants