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

fix(opts): Validate SQL package and driver options #3241

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
46 changes: 7 additions & 39 deletions internal/codegen/golang/driver.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
package golang

type SQLDriver string
import "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"

const (
SQLPackagePGXV4 string = "pgx/v4"
SQLPackagePGXV5 string = "pgx/v5"
SQLPackageStandard string = "database/sql"
)

const (
SQLDriverPGXV4 SQLDriver = "github.com/jackc/pgx/v4"
SQLDriverPGXV5 = "github.com/jackc/pgx/v5"
SQLDriverLibPQ = "github.com/lib/pq"
SQLDriverGoSQLDriverMySQL = "github.com/go-sql-driver/mysql"
)

func parseDriver(sqlPackage string) SQLDriver {
func parseDriver(sqlPackage string) opts.SQLDriver {
switch sqlPackage {
case SQLPackagePGXV4:
return SQLDriverPGXV4
case SQLPackagePGXV5:
return SQLDriverPGXV5
default:
return SQLDriverLibPQ
}
}

func (d SQLDriver) IsPGX() bool {
return d == SQLDriverPGXV4 || d == SQLDriverPGXV5
}

func (d SQLDriver) IsGoSQLDriverMySQL() bool {
return d == SQLDriverGoSQLDriverMySQL
}

func (d SQLDriver) Package() string {
switch d {
case SQLDriverPGXV4:
return SQLPackagePGXV4
case SQLDriverPGXV5:
return SQLPackagePGXV5
case opts.SQLPackagePGXV4:
return opts.SQLDriverPGXV4
case opts.SQLPackagePGXV5:
return opts.SQLDriverPGXV5
default:
return SQLPackageStandard
return opts.SQLDriverLibPQ
}
}
8 changes: 4 additions & 4 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
type tmplCtx struct {
Q string
Package string
SQLDriver SQLDriver
SQLDriver opts.SQLDriver
Enums []Enum
Structs []Struct
GoQueries []Query
Expand Down Expand Up @@ -189,15 +189,15 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
OmitSqlcVersion: options.OmitSqlcVersion,
}

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

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

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

sqlpkg := parseDriver(i.Options.SqlPackage)
switch sqlpkg {
case SQLDriverPGXV4:
case opts.SQLDriverPGXV4:
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgconn"})
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgx/v4"})
case SQLDriverPGXV5:
case opts.SQLDriverPGXV5:
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgx/v5/pgconn"})
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgx/v5"})
default:
Expand Down Expand Up @@ -172,9 +172,9 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
for _, q := range queries {
if q.Cmd == metadata.CmdExecResult {
switch sqlpkg {
case SQLDriverPGXV4:
case opts.SQLDriverPGXV4:
pkg[ImportSpec{Path: "github.com/jackc/pgconn"}] = struct{}{}
case SQLDriverPGXV5:
case opts.SQLDriverPGXV5:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v5/pgconn"}] = struct{}{}
default:
std["database/sql"] = struct{}{}
Expand All @@ -189,7 +189,7 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
}

if uses("pgtype.") {
if sqlpkg == SQLDriverPGXV5 {
if sqlpkg == opts.SQLDriverPGXV5 {
pkg[ImportSpec{Path: "github.com/jackc/pgx/v5/pgtype"}] = struct{}{}
} else {
pkg[ImportSpec{Path: "github.com/jackc/pgtype"}] = struct{}{}
Expand Down Expand Up @@ -429,7 +429,7 @@ func (i *importer) copyfromImports() fileImports {
})

std["context"] = struct{}{}
if i.Options.SqlDriver == SQLDriverGoSQLDriverMySQL {
if i.Options.SqlDriver == opts.SQLDriverGoSQLDriverMySQL {
std["io"] = struct{}{}
std["fmt"] = struct{}{}
std["sync/atomic"] = struct{}{}
Expand Down Expand Up @@ -481,9 +481,9 @@ func (i *importer) batchImports() fileImports {
std["errors"] = struct{}{}
sqlpkg := parseDriver(i.Options.SqlPackage)
switch sqlpkg {
case SQLDriverPGXV4:
case opts.SQLDriverPGXV4:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v4"}] = struct{}{}
case SQLDriverPGXV5:
case opts.SQLDriverPGXV5:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v5"}] = struct{}{}
}

Expand Down
64 changes: 64 additions & 0 deletions internal/codegen/golang/opts/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package opts

import "fmt"

type SQLDriver string

const (
SQLPackagePGXV4 string = "pgx/v4"
SQLPackagePGXV5 string = "pgx/v5"
SQLPackageStandard string = "database/sql"
)

var validPackages = map[string]struct{}{
string(SQLPackagePGXV4): {},
string(SQLPackagePGXV5): {},
string(SQLPackageStandard): {},
}

func validatePackage(sqlPackage string) error {
if _, found := validPackages[sqlPackage]; !found {
return fmt.Errorf("unknown SQL package: %s", sqlPackage)
}
return nil
}

const (
SQLDriverPGXV4 SQLDriver = "github.com/jackc/pgx/v4"
SQLDriverPGXV5 = "github.com/jackc/pgx/v5"
SQLDriverLibPQ = "github.com/lib/pq"
SQLDriverGoSQLDriverMySQL = "github.com/go-sql-driver/mysql"
)

var validDrivers = map[string]struct{}{
string(SQLDriverPGXV4): {},
string(SQLDriverPGXV5): {},
string(SQLDriverLibPQ): {},
string(SQLDriverGoSQLDriverMySQL): {},
}

func validateDriver(sqlDriver string) error {
if _, found := validDrivers[sqlDriver]; !found {
return fmt.Errorf("unknown SQL driver: %s", sqlDriver)
}
return nil
}

func (d SQLDriver) IsPGX() bool {
return d == SQLDriverPGXV4 || d == SQLDriverPGXV5
}

func (d SQLDriver) IsGoSQLDriverMySQL() bool {
return d == SQLDriverGoSQLDriverMySQL
}

func (d SQLDriver) Package() string {
switch d {
case SQLDriverPGXV4:
return SQLPackagePGXV4
case SQLDriverPGXV5:
return SQLPackagePGXV5
default:
return SQLPackageStandard
}
}
12 changes: 12 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
}
}

if options.SqlPackage != "" {
if err := validatePackage(options.SqlPackage); err != nil {
return nil, fmt.Errorf("invalid options: %s", err)
}
}

if options.SqlDriver != "" {
if err := validateDriver(options.SqlDriver); err != nil {
return nil, fmt.Errorf("invalid options: %s", err)
}
}

if options.QueryParameterLimit == nil {
options.QueryParameterLimit = new(int32)
*options.QueryParameterLimit = 1
Expand Down
Loading
Loading