@@ -102,7 +102,7 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
102102 case copyfromFileName :
103103 return mergeImports (i .copyfromImports ())
104104 case batchFileName :
105- return mergeImports (i .batchImports (filename ))
105+ return mergeImports (i .batchImports ())
106106 default :
107107 return mergeImports (i .queryImports (filename ))
108108 }
@@ -114,11 +114,14 @@ func (i *importer) dbImports() fileImports {
114114 {Path : "context" },
115115 }
116116
117- sqlpkg := SQLPackageFromString (i .Settings .Go .SqlPackage )
117+ sqlpkg := parseDriver (i .Settings .Go .SqlPackage )
118118 switch sqlpkg {
119- case SQLPackagePGX :
119+ case SQLDriverPGXV4 :
120120 pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgconn" })
121121 pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v4" })
122+ case SQLDriverPGXV5 :
123+ pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v5/pgconn" })
124+ pkg = append (pkg , ImportSpec {Path : "github.com/jackc/pgx/v5" })
122125 default :
123126 std = append (std , ImportSpec {Path : "database/sql" })
124127 if i .Settings .Go .EmitPreparedQueries {
@@ -136,22 +139,8 @@ var stdlibTypes = map[string]string{
136139 "time.Time" : "time" ,
137140 "net.IP" : "net" ,
138141 "net.HardwareAddr" : "net" ,
139- }
140-
141- var pgtypeTypes = map [string ]struct {}{
142- "pgtype.CIDR" : {},
143- "pgtype.Daterange" : {},
144- "pgtype.Inet" : {},
145- "pgtype.Int4range" : {},
146- "pgtype.Int8range" : {},
147- "pgtype.JSON" : {},
148- "pgtype.JSONB" : {},
149- "pgtype.Hstore" : {},
150- "pgtype.Macaddr" : {},
151- "pgtype.Numeric" : {},
152- "pgtype.Numrange" : {},
153- "pgtype.Tsrange" : {},
154- "pgtype.Tstzrange" : {},
142+ "netip.Addr" : "net/netip" ,
143+ "netip.Prefix" : "net/netip" ,
155144}
156145
157146var pqtypeTypes = map [string ]struct {}{
@@ -169,12 +158,14 @@ func buildImports(settings *plugin.Settings, queries []Query, uses func(string)
169158 std ["database/sql" ] = struct {}{}
170159 }
171160
172- sqlpkg := SQLPackageFromString (settings .Go .SqlPackage )
161+ sqlpkg := parseDriver (settings .Go .SqlPackage )
173162 for _ , q := range queries {
174163 if q .Cmd == metadata .CmdExecResult {
175164 switch sqlpkg {
176- case SQLPackagePGX :
165+ case SQLDriverPGXV4 :
177166 pkg [ImportSpec {Path : "github.com/jackc/pgconn" }] = struct {}{}
167+ case SQLDriverPGXV5 :
168+ pkg [ImportSpec {Path : "github.com/jackc/pgx/v5/pgconn" }] = struct {}{}
178169 default :
179170 std ["database/sql" ] = struct {}{}
180171 }
@@ -187,15 +178,18 @@ func buildImports(settings *plugin.Settings, queries []Query, uses func(string)
187178 }
188179 }
189180
190- for typeName , _ := range pgtypeTypes {
191- if uses (typeName ) {
181+ if uses ("pgtype." ) {
182+ if sqlpkg == SQLDriverPGXV5 {
183+ pkg [ImportSpec {Path : "github.com/jackc/pgx/v5/pgtype" }] = struct {}{}
184+ } else {
192185 pkg [ImportSpec {Path : "github.com/jackc/pgtype" }] = struct {}{}
193186 }
194187 }
195188
196189 for typeName , _ := range pqtypeTypes {
197190 if uses (typeName ) {
198191 pkg [ImportSpec {Path : "github.com/tabbed/pqtype" }] = struct {}{}
192+ break
199193 }
200194 }
201195
@@ -373,8 +367,8 @@ func (i *importer) queryImports(filename string) fileImports {
373367 std ["context" ] = struct {}{}
374368 }
375369
376- sqlpkg := SQLPackageFromString (i .Settings .Go .SqlPackage )
377- if sliceScan () && sqlpkg != SQLPackagePGX {
370+ sqlpkg := parseDriver (i .Settings .Go .SqlPackage )
371+ if sliceScan () && ! sqlpkg . IsPGX () {
378372 pkg [ImportSpec {Path : "github.com/lib/pq" }] = struct {}{}
379373 }
380374
@@ -409,7 +403,7 @@ func (i *importer) copyfromImports() fileImports {
409403 return sortedImports (std , pkg )
410404}
411405
412- func (i * importer ) batchImports (filename string ) fileImports {
406+ func (i * importer ) batchImports () fileImports {
413407 batchQueries := make ([]Query , 0 , len (i .Queries ))
414408 for _ , q := range i .Queries {
415409 if usesBatch ([]Query {q }) {
@@ -452,7 +446,13 @@ func (i *importer) batchImports(filename string) fileImports {
452446
453447 std ["context" ] = struct {}{}
454448 std ["errors" ] = struct {}{}
455- pkg [ImportSpec {Path : "github.com/jackc/pgx/v4" }] = struct {}{}
449+ sqlpkg := parseDriver (i .Settings .Go .SqlPackage )
450+ switch sqlpkg {
451+ case SQLDriverPGXV4 :
452+ pkg [ImportSpec {Path : "github.com/jackc/pgx/v4" }] = struct {}{}
453+ case SQLDriverPGXV5 :
454+ pkg [ImportSpec {Path : "github.com/jackc/pgx/v5" }] = struct {}{}
455+ }
456456
457457 return sortedImports (std , pkg )
458458}
0 commit comments