Skip to content

Commit 06869e7

Browse files
committed
Generate schema field names with same case as the Go struct field to prevent keyword collisions
1 parent bdb24e2 commit 06869e7

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

example/pgx/repository/mappings_project_gen.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pgx/repository/mappings_todo_gen.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pgx/repository/repository_project.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ func projectBuildFindQuery() builder.SelectBuilder {
2121
return SelectJson(projectJson()).
2222
From(project).
2323
LeftJoin(
24-
Select(fn.Count(todo.projectID)).As("count").
25-
Select(todo.projectID).
24+
Select(fn.Count(todo.ProjectID)).As("count").
25+
Select(todo.ProjectID).
2626
From(todo).
27-
GroupBy(todo.projectID),
28-
).As("todo_counts").On(project.id.Eq(N("todo_counts.project_id")))
27+
GroupBy(todo.ProjectID),
28+
).As("todo_counts").On(project.ID.Eq(N("todo_counts.project_id")))
2929
}
3030

3131
// FindProjectByID finds a single project by id
3232
func FindProjectByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (result model.Project, err error) {
3333
q := projectBuildFindQuery().
34-
Where(project.id.Eq(Arg(id)))
34+
Where(project.ID.Eq(Arg(id)))
3535

3636
row, err := qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx)
3737
if err != nil {
@@ -43,7 +43,7 @@ func FindProjectByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID
4343
// FindAllProjects finds all projects sorted by title
4444
func FindAllProjects(ctx context.Context, executor qrbpgx.Executor) (result []model.Project, err error) {
4545
q := projectBuildFindQuery().
46-
OrderBy(project.title)
46+
OrderBy(project.Title)
4747

4848
rows, err := qrbpgx.Build(q).WithExecutor(executor).Query(ctx)
4949
if err != nil {

example/pgx/repository/repository_todo.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func todoBuildFindQuery() builder.SelectBuilder {
2020
return qrb.
2121
SelectJson(todoJson()).
2222
From(todo).
23-
LeftJoin(project).On(todo.projectID.Eq(project.id))
23+
LeftJoin(project).On(todo.ProjectID.Eq(project.ID))
2424
}
2525

2626
// FindTodoByID finds a single todo by id
2727
func FindTodoByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (result model.Todo, err error) {
2828
q := todoBuildFindQuery().
29-
Where(todo.id.Eq(qrb.Arg(id)))
29+
Where(todo.ID.Eq(qrb.Arg(id)))
3030

3131
row, err := qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx)
3232
if err != nil {
@@ -38,7 +38,12 @@ func FindTodoByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (
3838
// FindAllTodos finds all todos sorted by title
3939
func FindAllTodos(ctx context.Context, executor qrbpgx.Executor, filter model.TodosFilter) (result []model.Todo, err error) {
4040
q := todoBuildFindQuery().
41-
OrderBy(todo.completedAt).Desc().NullsFirst()
41+
OrderBy(todo.CompletedAt).Desc().NullsFirst().
42+
SelectBuilder
43+
44+
if filter.ProjectID != nil {
45+
q = q.Where(todo.ProjectID.Eq(qrb.Arg(filter.ProjectID)))
46+
}
4247

4348
rows, err := qrbpgx.Build(q).WithExecutor(executor).Query(ctx)
4449
if err != nil {
@@ -63,7 +68,7 @@ func UpdateTodo(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID, cha
6368
q := qrb.
6469
Update(todo).
6570
SetMap(changeSet.toMap()).
66-
Where(todo.id.Eq(qrb.Arg(id)))
71+
Where(todo.ID.Eq(qrb.Arg(id)))
6772

6873
res, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)
6974
if err != nil {

internal/fixtures/repository/mappings_mytype_gen.go

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/generate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Generate(m *StructMapping, goPackage string, goFile string, w io.Writer) (o
3434

3535
for _, fm := range m.FieldMappings {
3636
if fm.WriteColDef != nil {
37-
fieldName := firstToUpper(fm.Name)
37+
fieldName := fm.Name
3838

3939
var prepareStmt *Statement
4040
if fm.WriteColDef.ToJSON {
@@ -124,22 +124,22 @@ func generateSchemaVar(f *File, m *StructMapping) {
124124
if fm.ReadColDef == nil {
125125
continue
126126
}
127-
g.Id(firstToLower(fm.Name)).Qual("github.com/networkteam/qrb/builder", "IdentExp")
127+
g.Id(fm.Name).Qual("github.com/networkteam/qrb/builder", "IdentExp")
128128
}
129129
}).Values(DictFunc(func(d Dict) {
130130
if m.TableName != "" {
131131
d[Id("Identer")] = Qual("github.com/networkteam/qrb", "N").Call(Lit(m.TableName))
132132
}
133133
for _, fm := range m.FieldMappings {
134134
if fm.ReadColDef != nil {
135-
d[Id(firstToLower(fm.Name))] = Qual("github.com/networkteam/qrb", "N").Call(Lit(fm.ReadColDef.Col))
135+
d[Id(fm.Name)] = Qual("github.com/networkteam/qrb", "N").Call(Lit(fm.ReadColDef.Col))
136136
}
137137
}
138138
})).Line()
139139
}
140140

141141
func readColVarName(m *StructMapping, fm FieldMapping) string {
142-
return firstToLower(m.MappingTypeName) + "." + firstToLower(fm.Name)
142+
return firstToLower(m.MappingTypeName) + "." + fm.Name
143143
}
144144

145145
func generateDefaultSelectJsonObject(f *File, m *StructMapping) {

0 commit comments

Comments
 (0)