5
5
"sort"
6
6
"strings"
7
7
8
- "github.com/sqlc-dev/plugin-sdk-go/metadata"
9
8
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
9
+ "github.com/sqlc-dev/plugin-sdk-go/metadata"
10
10
)
11
11
12
12
type fileImports struct {
@@ -75,6 +75,11 @@ func (i *importer) usesType(typ string) bool {
75
75
return false
76
76
}
77
77
78
+ func (i * importer ) HasImports (filename string ) bool {
79
+ imports := i .Imports (filename )
80
+ return len (imports [0 ]) != 0 || len (imports [1 ]) != 0
81
+ }
82
+
78
83
func (i * importer ) Imports (filename string ) [][]ImportSpec {
79
84
dbFileName := "db.go"
80
85
if i .Options .OutputDbFileName != "" {
@@ -121,10 +126,10 @@ func (i *importer) dbImports() fileImports {
121
126
122
127
sqlpkg := parseDriver (i .Options .SqlPackage )
123
128
switch sqlpkg {
124
- case SQLDriverPGXV4 :
129
+ case opts . SQLDriverPGXV4 :
125
130
pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgconn" })
126
131
pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v4" })
127
- case SQLDriverPGXV5 :
132
+ case opts . SQLDriverPGXV5 :
128
133
pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v5/pgconn" })
129
134
pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v5" })
130
135
default :
@@ -167,9 +172,9 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
167
172
for _ , q := range queries {
168
173
if q .Cmd == metadata .CmdExecResult {
169
174
switch sqlpkg {
170
- case SQLDriverPGXV4 :
175
+ case opts . SQLDriverPGXV4 :
171
176
pkg [ImportSpec {Path : "github.com/jackc/pgconn" }] = struct {}{}
172
- case SQLDriverPGXV5 :
177
+ case opts . SQLDriverPGXV5 :
173
178
pkg [ImportSpec {Path : "github.com/jackc/pgx/v5/pgconn" }] = struct {}{}
174
179
default :
175
180
std ["database/sql" ] = struct {}{}
@@ -184,7 +189,7 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
184
189
}
185
190
186
191
if uses ("pgtype." ) {
187
- if sqlpkg == SQLDriverPGXV5 {
192
+ if sqlpkg == opts . SQLDriverPGXV5 {
188
193
pkg [ImportSpec {Path : "github.com/jackc/pgx/v5/pgtype" }] = struct {}{}
189
194
} else {
190
195
pkg [ImportSpec {Path : "github.com/jackc/pgtype" }] = struct {}{}
@@ -424,7 +429,7 @@ func (i *importer) copyfromImports() fileImports {
424
429
})
425
430
426
431
std ["context" ] = struct {}{}
427
- if i .Options .SqlDriver == SQLDriverGoSQLDriverMySQL {
432
+ if i .Options .SqlDriver == opts . SQLDriverGoSQLDriverMySQL {
428
433
std ["io" ] = struct {}{}
429
434
std ["fmt" ] = struct {}{}
430
435
std ["sync/atomic" ] = struct {}{}
@@ -476,9 +481,9 @@ func (i *importer) batchImports() fileImports {
476
481
std ["errors" ] = struct {}{}
477
482
sqlpkg := parseDriver (i .Options .SqlPackage )
478
483
switch sqlpkg {
479
- case SQLDriverPGXV4 :
484
+ case opts . SQLDriverPGXV4 :
480
485
pkg [ImportSpec {Path : "github.com/jackc/pgx/v4" }] = struct {}{}
481
- case SQLDriverPGXV5 :
486
+ case opts . SQLDriverPGXV5 :
482
487
pkg [ImportSpec {Path : "github.com/jackc/pgx/v5" }] = struct {}{}
483
488
}
484
489
0 commit comments