diff --git a/internal/endtoend/testdata/inflection/mysql/go/models.go b/internal/endtoend/testdata/inflection/mysql/go/models.go index a78dbd714d..505b2fae7b 100644 --- a/internal/endtoend/testdata/inflection/mysql/go/models.go +++ b/internal/endtoend/testdata/inflection/mysql/go/models.go @@ -6,6 +6,10 @@ package querytest import () +type Calorie struct { + ID string +} + type Campus struct { ID string } diff --git a/internal/endtoend/testdata/inflection/mysql/go/query.sql.go b/internal/endtoend/testdata/inflection/mysql/go/query.sql.go index 1a61097b0b..2cb530297e 100644 --- a/internal/endtoend/testdata/inflection/mysql/go/query.sql.go +++ b/internal/endtoend/testdata/inflection/mysql/go/query.sql.go @@ -9,6 +9,33 @@ import ( "context" ) +const listCalories = `-- name: ListCalories :many +SELECT id FROM calories +` + +func (q *Queries) ListCalories(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, listCalories) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var id string + 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 +} + const listCampuses = `-- name: ListCampuses :many SELECT id FROM campus ` diff --git a/internal/endtoend/testdata/inflection/mysql/query.sql b/internal/endtoend/testdata/inflection/mysql/query.sql index 23f1cb26d9..24ca8bdadd 100644 --- a/internal/endtoend/testdata/inflection/mysql/query.sql +++ b/internal/endtoend/testdata/inflection/mysql/query.sql @@ -1,6 +1,7 @@ CREATE TABLE campus (id text not null); CREATE TABLE students (id text not null); CREATE TABLE product_meta (id text not null); +CREATE TABLE calories (id text not null); /* name: ListCampuses :many */ SELECT * FROM campus; @@ -10,3 +11,6 @@ SELECT * FROM students; /* name: ListMetadata :many */ SELECT * FROM product_meta; + +/* name: ListCalories :many */ +SELECT * FROM calories; diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/models.go b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/models.go index a78dbd714d..505b2fae7b 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/models.go +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/models.go @@ -6,6 +6,10 @@ package querytest import () +type Calorie struct { + ID string +} + type Campus struct { ID string } diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/query.sql.go b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/query.sql.go index ec5eebb1e4..9c3f166bba 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/query.sql.go +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/go/query.sql.go @@ -9,6 +9,30 @@ import ( "context" ) +const listCalories = `-- name: ListCalories :many +SELECT id FROM calories +` + +func (q *Queries) ListCalories(ctx context.Context) ([]string, error) { + rows, err := q.db.Query(ctx, listCalories) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var id string + if err := rows.Scan(&id); err != nil { + return nil, err + } + items = append(items, id) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listCampuses = `-- name: ListCampuses :many SELECT id FROM campus ` diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/query.sql b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/query.sql index 0971c2d229..f1958fa869 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v4/query.sql +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v4/query.sql @@ -1,6 +1,7 @@ CREATE TABLE campus (id text not null); CREATE TABLE students (id text not null); CREATE TABLE product_meta (id text not null); +CREATE TABLE calories (id text not null); -- name: ListCampuses :many SELECT * FROM campus; @@ -10,3 +11,6 @@ SELECT * FROM students; -- name: ListMetadata :many SELECT * FROM product_meta; + +-- name: ListCalories :many +SELECT * FROM calories; diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/models.go b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/models.go index a78dbd714d..505b2fae7b 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/models.go +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/models.go @@ -6,6 +6,10 @@ package querytest import () +type Calorie struct { + ID string +} + type Campus struct { ID string } diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/query.sql.go b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/query.sql.go index ec5eebb1e4..9c3f166bba 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/query.sql.go +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/go/query.sql.go @@ -9,6 +9,30 @@ import ( "context" ) +const listCalories = `-- name: ListCalories :many +SELECT id FROM calories +` + +func (q *Queries) ListCalories(ctx context.Context) ([]string, error) { + rows, err := q.db.Query(ctx, listCalories) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var id string + if err := rows.Scan(&id); err != nil { + return nil, err + } + items = append(items, id) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listCampuses = `-- name: ListCampuses :many SELECT id FROM campus ` diff --git a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/query.sql b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/query.sql index 0971c2d229..f1958fa869 100644 --- a/internal/endtoend/testdata/inflection/postgresql/pgx/v5/query.sql +++ b/internal/endtoend/testdata/inflection/postgresql/pgx/v5/query.sql @@ -1,6 +1,7 @@ CREATE TABLE campus (id text not null); CREATE TABLE students (id text not null); CREATE TABLE product_meta (id text not null); +CREATE TABLE calories (id text not null); -- name: ListCampuses :many SELECT * FROM campus; @@ -10,3 +11,6 @@ SELECT * FROM students; -- name: ListMetadata :many SELECT * FROM product_meta; + +-- name: ListCalories :many +SELECT * FROM calories; diff --git a/internal/endtoend/testdata/inflection/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/inflection/postgresql/stdlib/go/models.go index a78dbd714d..505b2fae7b 100644 --- a/internal/endtoend/testdata/inflection/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/inflection/postgresql/stdlib/go/models.go @@ -6,6 +6,10 @@ package querytest import () +type Calorie struct { + ID string +} + type Campus struct { ID string } diff --git a/internal/endtoend/testdata/inflection/postgresql/stdlib/go/query.sql.go b/internal/endtoend/testdata/inflection/postgresql/stdlib/go/query.sql.go index 1a61097b0b..2cb530297e 100644 --- a/internal/endtoend/testdata/inflection/postgresql/stdlib/go/query.sql.go +++ b/internal/endtoend/testdata/inflection/postgresql/stdlib/go/query.sql.go @@ -9,6 +9,33 @@ import ( "context" ) +const listCalories = `-- name: ListCalories :many +SELECT id FROM calories +` + +func (q *Queries) ListCalories(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, listCalories) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var id string + 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 +} + const listCampuses = `-- name: ListCampuses :many SELECT id FROM campus ` diff --git a/internal/endtoend/testdata/inflection/postgresql/stdlib/query.sql b/internal/endtoend/testdata/inflection/postgresql/stdlib/query.sql index 0971c2d229..f1958fa869 100644 --- a/internal/endtoend/testdata/inflection/postgresql/stdlib/query.sql +++ b/internal/endtoend/testdata/inflection/postgresql/stdlib/query.sql @@ -1,6 +1,7 @@ CREATE TABLE campus (id text not null); CREATE TABLE students (id text not null); CREATE TABLE product_meta (id text not null); +CREATE TABLE calories (id text not null); -- name: ListCampuses :many SELECT * FROM campus; @@ -10,3 +11,6 @@ SELECT * FROM students; -- name: ListMetadata :many SELECT * FROM product_meta; + +-- name: ListCalories :many +SELECT * FROM calories; diff --git a/internal/endtoend/testdata/inflection/sqlite/go/models.go b/internal/endtoend/testdata/inflection/sqlite/go/models.go index a78dbd714d..505b2fae7b 100644 --- a/internal/endtoend/testdata/inflection/sqlite/go/models.go +++ b/internal/endtoend/testdata/inflection/sqlite/go/models.go @@ -6,6 +6,10 @@ package querytest import () +type Calorie struct { + ID string +} + type Campus struct { ID string } diff --git a/internal/endtoend/testdata/inflection/sqlite/go/query.sql.go b/internal/endtoend/testdata/inflection/sqlite/go/query.sql.go index 1a61097b0b..2cb530297e 100644 --- a/internal/endtoend/testdata/inflection/sqlite/go/query.sql.go +++ b/internal/endtoend/testdata/inflection/sqlite/go/query.sql.go @@ -9,6 +9,33 @@ import ( "context" ) +const listCalories = `-- name: ListCalories :many +SELECT id FROM calories +` + +func (q *Queries) ListCalories(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, listCalories) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var id string + 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 +} + const listCampuses = `-- name: ListCampuses :many SELECT id FROM campus ` diff --git a/internal/endtoend/testdata/inflection/sqlite/query.sql b/internal/endtoend/testdata/inflection/sqlite/query.sql index 23f1cb26d9..24ca8bdadd 100644 --- a/internal/endtoend/testdata/inflection/sqlite/query.sql +++ b/internal/endtoend/testdata/inflection/sqlite/query.sql @@ -1,6 +1,7 @@ CREATE TABLE campus (id text not null); CREATE TABLE students (id text not null); CREATE TABLE product_meta (id text not null); +CREATE TABLE calories (id text not null); /* name: ListCampuses :many */ SELECT * FROM campus; @@ -10,3 +11,6 @@ SELECT * FROM students; /* name: ListMetadata :many */ SELECT * FROM product_meta; + +/* name: ListCalories :many */ +SELECT * FROM calories; diff --git a/internal/inflection/singular.go b/internal/inflection/singular.go index 518b5edb67..6718bf7ef1 100644 --- a/internal/inflection/singular.go +++ b/internal/inflection/singular.go @@ -32,5 +32,12 @@ func Singular(s SingularParams) string { if strings.ToLower(s.Name) == "meta" { return s.Name } + // Manual fix for incorrect handling of "calories" + // + // https://github.com/kyleconroy/sqlc/issues/2017 + // https://github.com/jinzhu/inflection/issues/23 + if strings.ToLower(s.Name) == "calories" { + return "calorie" + } return upstream.Singular(s.Name) }