From cd15f3eafd980922b1c9e59fc9f52fef72c35ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Thu, 3 Mar 2022 14:20:55 +0100 Subject: [PATCH] Fix field name casing error when building PgSQL reports --- store/rdbms/compose_record_datasource.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/store/rdbms/compose_record_datasource.go b/store/rdbms/compose_record_datasource.go index 5bf68c01a..722a7c4f7 100644 --- a/store/rdbms/compose_record_datasource.go +++ b/store/rdbms/compose_record_datasource.go @@ -561,20 +561,33 @@ func (b *recordDatasource) calculatePaging(out []*report.Frame, sorting filter.S return out } -func (b *recordDatasource) cast(row sqlx.ColScanner, out *report.Frame) error { - var err error +func (b *recordDatasource) mapRow(row sqlx.ColScanner) (out map[string]interface{}, err error) { aux := make(map[string]interface{}) if err = sqlx.MapScan(row, aux); err != nil { - return err + return } + // PgSQL treats idents as lowercase which would cause not lower-case field names + // to not work. + // Since fields can not be same with different capitalization, converting to lowercase + // does the trick. + out = make(map[string]interface{}) + for k, v := range aux { + out[strings.ToLower(k)] = v + } + + return +} + +func (b *recordDatasource) cast(row sqlx.ColScanner, out *report.Frame) (err error) { + aux, err := b.mapRow(row) r := make(report.FrameRow, len(out.Columns)) k := "" for i, c := range out.Columns { k = "" + c.Name // cols are wrapped so we need to handle those properly - v, ok := aux[wrapCol(k)] + v, ok := aux[strings.ToLower(wrapCol(k))] if !ok { continue }