@@ -3,6 +3,7 @@ package golang
33import (
44 "bufio"
55 "bytes"
6+ "errors"
67 "fmt"
78 "go/format"
89 "strings"
@@ -11,6 +12,7 @@ import (
1112 "github.com/kyleconroy/sqlc/internal/codegen"
1213 "github.com/kyleconroy/sqlc/internal/compiler"
1314 "github.com/kyleconroy/sqlc/internal/config"
15+ "github.com/kyleconroy/sqlc/internal/metadata"
1416)
1517
1618type Generateable interface {
@@ -37,6 +39,7 @@ type tmplCtx struct {
3739 EmitInterface bool
3840 EmitEmptySlices bool
3941 EmitMethodsWithDBArgument bool
42+ UsesCopyFrom bool
4043}
4144
4245func (t * tmplCtx ) OutputQuery (sourceName string ) bool {
@@ -87,6 +90,7 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
8790 EmitPreparedQueries : golang .EmitPreparedQueries ,
8891 EmitEmptySlices : golang .EmitEmptySlices ,
8992 EmitMethodsWithDBArgument : golang .EmitMethodsWithDBArgument ,
93+ UsesCopyFrom : usesCopyFrom (queries ),
9094 SQLPackage : SQLPackageFromString (golang .SQLPackage ),
9195 Q : "`" ,
9296 Package : golang .Package ,
@@ -95,6 +99,10 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
9599 Structs : structs ,
96100 }
97101
102+ if tctx .UsesCopyFrom && tctx .SQLPackage != SQLPackagePGX {
103+ return nil , errors .New (":copyfrom is only supported by pgx" )
104+ }
105+
98106 output := map [string ]string {}
99107
100108 execute := func (name , templateName string ) error {
@@ -135,6 +143,8 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
135143 if golang .OutputQuerierFileName != "" {
136144 querierFileName = golang .OutputQuerierFileName
137145 }
146+ copyfromFileName := "copyfrom.go"
147+ // TODO(Jille): Make this configurable.
138148
139149 if err := execute (dbFileName , "dbFile" ); err != nil {
140150 return nil , err
@@ -147,6 +157,11 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
147157 return nil , err
148158 }
149159 }
160+ if tctx .UsesCopyFrom {
161+ if err := execute (copyfromFileName , "copyfromFile" ); err != nil {
162+ return nil , err
163+ }
164+ }
150165
151166 files := map [string ]struct {}{}
152167 for _ , gq := range queries {
@@ -160,3 +175,12 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
160175 }
161176 return output , nil
162177}
178+
179+ func usesCopyFrom (queries []Query ) bool {
180+ for _ , q := range queries {
181+ if q .Cmd == metadata .CmdCopyFrom {
182+ return true
183+ }
184+ }
185+ return false
186+ }
0 commit comments