Skip to content

Fix panic when scanning generated queries #397

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

Merged
merged 1 commit into from
Apr 26, 2025
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.33.1] - 2025-04-26

### Changed

- When a query returns only one column, instead of returning a struct with one field, it will return the type of the column. This is to make it easier to use the generated code.

### Fixed

- Fix panic when scanning returned rows from generated queries.

## [v0.33.0] - 2025-04-26

### Added
Expand Down
12 changes: 12 additions & 0 deletions gen/templates/queries/query/01_query.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
{{$queryType := (lower $query.Type.String | titleCase)}}
{{$dialectType := printf "*dialect.%sQuery" $queryType}}
{{$colParams := printf "%s, %s" $queryRowName (or $query.Config.RowSliceName (printf "[]%s" $queryRowName)) }}
{{if eq (len $query.Columns) 1}}
{{$col := index $query.Columns 0}}
{{$colType := $col.Type $.Importer $.Types}}
{{$colParams = printf "%s, %s" $colType (or $query.Config.RowSliceName (printf "[]%s" $colType)) }}
{{end}}

const {{$lowerName}}SQL = `{{replace "`" "`+\"`\"+`" $query.SQL}}`

Expand Down Expand Up @@ -71,6 +76,13 @@ func {{$upperName}} ({{join ", " $args}}) orm.ModExecQuery[{{$dialectType}}] {
QueryType: bob.QueryType{{$queryType}},
},
},
{{if gt (len $query.Columns) 1 -}}
{{- $.Importer.Import "github.com/stephenafamo/scan" -}}
Scanner: scan.StructMapper[{{$queryRowName}}](),
{{- else -}}
{{- $col := index $query.Columns 0 -}}
Scanner: scan.ColumnMapper[{{$col.Type $.Importer $.Types}}]("{{$col.DBName}}"),
{{- end}}
},
}
}
Expand Down