Skip to content

fix parseDriver function to return a correct Driver when using MySQL #2692

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion internal/codegen/golang/driver.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package golang

import "github.com/sqlc-dev/sqlc/internal/config"

type SQLDriver string

const (
Expand All @@ -15,12 +17,24 @@ const (
SQLDriverGoSQLDriverMySQL = "github.com/go-sql-driver/mysql"
)

func parseDriver(sqlPackage string) SQLDriver {
func parseDriver(sqlPackage string, engine string) SQLDriver {
switch sqlPackage {
case SQLPackagePGXV4:
return SQLDriverPGXV4
case SQLPackagePGXV5:
return SQLDriverPGXV5
default:
driver := driverFromEngine(engine)
return driver
}
}

func driverFromEngine(engine string) SQLDriver {
switch engine {
case string(config.EnginePostgreSQL):
return SQLDriverLibPQ
case string(config.EngineMySQL):
return SQLDriverGoSQLDriverMySQL
default:
return SQLDriverLibPQ
}
Expand Down
8 changes: 3 additions & 5 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
Enums: enums,
Structs: structs,
}

golang := req.Settings.Go
tctx := tmplCtx{
EmitInterface: golang.EmitInterface,
Expand All @@ -137,23 +136,22 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
EmitAllEnumValues: golang.EmitAllEnumValues,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(golang.SqlPackage),
SQLDriver: parseDriver(golang.SqlPackage, req.Settings.Engine),
Q: "`",
Package: golang.Package,
Enums: enums,
Structs: structs,
SqlcVersion: req.SqlcVersion,
}

if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && golang.SqlDriver != SQLDriverGoSQLDriverMySQL {
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && tctx.SQLDriver != SQLDriverGoSQLDriverMySQL {
return nil, errors.New(":copyfrom is only supported by pgx and github.com/go-sql-driver/mysql")
}

if tctx.UsesCopyFrom && golang.SqlDriver == SQLDriverGoSQLDriverMySQL {
if tctx.UsesCopyFrom && tctx.SQLDriver == SQLDriverGoSQLDriverMySQL {
if err := checkNoTimesForMySQLCopyFrom(queries); err != nil {
return nil, err
}
tctx.SQLDriver = SQLDriverGoSQLDriverMySQL
}

if tctx.UsesBatch && !tctx.SQLDriver.IsPGX() {
Expand Down
8 changes: 4 additions & 4 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (i *importer) dbImports() fileImports {
{Path: "context"},
}

sqlpkg := parseDriver(i.Settings.Go.SqlPackage)
sqlpkg := parseDriver(i.Settings.Go.SqlPackage, i.Settings.Engine)
switch sqlpkg {
case SQLDriverPGXV4:
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgconn"})
Expand Down Expand Up @@ -160,7 +160,7 @@ func buildImports(settings *plugin.Settings, queries []Query, uses func(string)
std["database/sql"] = struct{}{}
}

sqlpkg := parseDriver(settings.Go.SqlPackage)
sqlpkg := parseDriver(settings.Go.SqlPackage, settings.Engine)
for _, q := range queries {
if q.Cmd == metadata.CmdExecResult {
switch sqlpkg {
Expand Down Expand Up @@ -374,7 +374,7 @@ func (i *importer) queryImports(filename string) fileImports {
std["context"] = struct{}{}
}

sqlpkg := parseDriver(i.Settings.Go.SqlPackage)
sqlpkg := parseDriver(i.Settings.Go.SqlPackage, i.Settings.Engine)
if sqlcSliceScan() {
std["strings"] = struct{}{}
}
Expand Down Expand Up @@ -459,7 +459,7 @@ func (i *importer) batchImports() fileImports {

std["context"] = struct{}{}
std["errors"] = struct{}{}
sqlpkg := parseDriver(i.Settings.Go.SqlPackage)
sqlpkg := parseDriver(i.Settings.Go.SqlPackage, i.Settings.Engine)
switch sqlpkg {
case SQLDriverPGXV4:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v4"}] = struct{}{}
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func parseIdentifierString(name string) (*plugin.Identifier, error) {
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
driver := parseDriver(req.Settings.Go.SqlPackage)
driver := parseDriver(req.Settings.Go.SqlPackage, req.Settings.Engine)
emitPointersForNull := driver.IsPGX() && req.Settings.Go.EmitPointersForNullTypes

switch columnType {
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
Comments: query.Comments,
Table: query.InsertIntoTable,
}
sqlpkg := parseDriver(req.Settings.Go.SqlPackage)
sqlpkg := parseDriver(req.Settings.Go.SqlPackage, req.Settings.Engine)

qpl := int(*req.Settings.Go.QueryParameterLimit)

Expand Down