From 55bf48baec1c1260d3c48e65bc2eec6a1ea5d0a5 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Sun, 5 Apr 2020 12:32:09 -0700 Subject: [PATCH 1/2] inflection: Add fix for campus --- internal/inflection/singular.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 internal/inflection/singular.go diff --git a/internal/inflection/singular.go b/internal/inflection/singular.go new file mode 100644 index 0000000000..cc501764d0 --- /dev/null +++ b/internal/inflection/singular.go @@ -0,0 +1,18 @@ +package inflection + +import ( + "strings" + + upstream "github.com/jinzhu/inflection" +) + +func Singular(name string) string { + // Manual fix for incorrect handling of "campus" + // + // https://github.com/kyleconroy/sqlc/issues/430 + // https://github.com/jinzhu/inflection/issues/13 + if strings.ToLower(name) == "campus" { + return name + } + return upstream.Singular(name) +} From e30cde15abe51565be59487b529541819da02ed6 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Sun, 5 Apr 2020 12:33:33 -0700 Subject: [PATCH 2/2] gen: Allow table names to be renamed --- Makefile | 2 +- internal/dinosql/gen.go | 5 ++- .../endtoend/testdata/inflection/go/db.go | 29 +++++++++++++++ .../endtoend/testdata/inflection/go/models.go | 9 +++++ .../testdata/inflection/go/query.sql.go | 35 +++++++++++++++++++ .../endtoend/testdata/inflection/query.sql | 4 +++ .../endtoend/testdata/inflection/sqlc.json | 9 +++++ 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 internal/endtoend/testdata/inflection/go/db.go create mode 100644 internal/endtoend/testdata/inflection/go/models.go create mode 100644 internal/endtoend/testdata/inflection/go/query.sql.go create mode 100644 internal/endtoend/testdata/inflection/query.sql create mode 100644 internal/endtoend/testdata/inflection/sqlc.json diff --git a/Makefile b/Makefile index 73beb11f80..1e4cef6a5e 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ sqlc-dev: go build -o ~/bin/sqlc-dev --tags=exp ./cmd/sqlc/ regen: - cd internal/endtoend && ./regenerate.sh + ./scripts/regenerate.sh diff --git a/internal/dinosql/gen.go b/internal/dinosql/gen.go index 168338ebe7..a09439a8fc 100644 --- a/internal/dinosql/gen.go +++ b/internal/dinosql/gen.go @@ -14,9 +14,8 @@ import ( "github.com/kyleconroy/sqlc/internal/catalog" "github.com/kyleconroy/sqlc/internal/config" + "github.com/kyleconroy/sqlc/internal/inflection" core "github.com/kyleconroy/sqlc/internal/pg" - - "github.com/jinzhu/inflection" ) var identPattern = regexp.MustCompile("[^a-zA-Z0-9_]+") @@ -598,7 +597,7 @@ func (r Result) Structs(settings config.CombinedSettings) []GoStruct { } s := GoStruct{ Table: core.FQN{Schema: name, Rel: table.Name}, - Name: inflection.Singular(StructName(tableName, settings)), + Name: StructName(inflection.Singular(tableName), settings), Comment: table.Comment, } for _, column := range table.Columns { diff --git a/internal/endtoend/testdata/inflection/go/db.go b/internal/endtoend/testdata/inflection/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/inflection/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/inflection/go/models.go b/internal/endtoend/testdata/inflection/go/models.go new file mode 100644 index 0000000000..88aa409788 --- /dev/null +++ b/internal/endtoend/testdata/inflection/go/models.go @@ -0,0 +1,9 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import () + +type Campus struct { + ID int32 +} diff --git a/internal/endtoend/testdata/inflection/go/query.sql.go b/internal/endtoend/testdata/inflection/go/query.sql.go new file mode 100644 index 0000000000..dbfaf14209 --- /dev/null +++ b/internal/endtoend/testdata/inflection/go/query.sql.go @@ -0,0 +1,35 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" +) + +const listCampus = `-- name: ListCampus :many +SELECT id FROM campus +` + +func (q *Queries) ListCampus(ctx context.Context) ([]int32, error) { + rows, err := q.db.QueryContext(ctx, listCampus) + if err != nil { + return nil, err + } + defer rows.Close() + var items []int32 + for rows.Next() { + var id int32 + if err := rows.Scan(&id); err != nil { + return nil, err + } + items = append(items, id) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/inflection/query.sql b/internal/endtoend/testdata/inflection/query.sql new file mode 100644 index 0000000000..e9fe99b2bf --- /dev/null +++ b/internal/endtoend/testdata/inflection/query.sql @@ -0,0 +1,4 @@ +CREATE TABLE campus (id serial not null); + +-- name: ListCampus :many +SELECT * FROM campus; diff --git a/internal/endtoend/testdata/inflection/sqlc.json b/internal/endtoend/testdata/inflection/sqlc.json new file mode 100644 index 0000000000..1161aac713 --- /dev/null +++ b/internal/endtoend/testdata/inflection/sqlc.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "packages": [{ + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + }] +}