Skip to content

Commit

Permalink
Fixed: Numerics in CSV, regression after pgx 5.2
Browse files Browse the repository at this point in the history
With upgrade to pgx 5.2, issues with numeric types were addressed. While JSON marshalling works well, numeric to string conversion is still an issue. Reintroduced old workaround to fix the issue for CSV exports for now.
  • Loading branch information
r3-gabriel committed Aug 28, 2023
1 parent f63b24b commit 4006de5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions handler/csv_download/csv_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"unicode/utf8"

"github.com/gofrs/uuid"
"github.com/jackc/pgx/v5/pgtype"
)

var (
Expand Down Expand Up @@ -201,12 +202,12 @@ func Handler(w http.ResponseWriter, r *http.Request) {

atr, exists := cache.AttributeIdMap[expr.AttributeId.Bytes]
if !exists {
handler.AbortRequest(w, handlerContext, errors.New("unknown attribute"), handler.ErrGeneral)
handler.AbortRequest(w, handlerContext, handler.ErrSchemaUnknownAttribute(expr.AttributeId.Bytes), handler.ErrGeneral)
return
}
rel, exists := cache.RelationIdMap[atr.RelationId]
if !exists {
handler.AbortRequest(w, handlerContext, errors.New("unknown relation"), handler.ErrGeneral)
handler.AbortRequest(w, handlerContext, handler.ErrSchemaUnknownRelation(atr.RelationId), handler.ErrGeneral)
return
}
columnNames[i] = rel.Name + "." + atr.Name
Expand Down Expand Up @@ -351,6 +352,8 @@ func dataToCsv(writer *csv.Writer, get types.DataGet, locUser *time.Location,
stringValues[pos] = parseIntegerValues(columnAttributeContentUse[pos], int64(v))
case int64:
stringValues[pos] = parseIntegerValues(columnAttributeContentUse[pos], v)
case pgtype.Numeric:
stringValues[pos] = tools.PgxNumericToString(v)
default:
stringValues[pos] = fmt.Sprintf("%v", value)
}
Expand Down
31 changes: 31 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tools

import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -167,6 +168,36 @@ func UuidStringToNullUuid(input string) pgtype.UUID {
}
}

func PgxNumericToString(value pgtype.Numeric) string {
s := value.Int.String()
l := len(s)
e := int(value.Exp)

// zero exponent, as in 12 (int=12, len=2, exp=0)
if e == 0 {
return s
}

// positive exponent, as in 2500 (int=25, len=2, exp=2)
if e > 0 {
return fmt.Sprintf("%s%s", s, strings.Repeat("0", e))
}

// negative exponents
// equals out length, as in 0.12 (int=12, len=2, exp=-2)
if l+e == 0 {
return fmt.Sprintf("0.%s", s)
}

// below zero, as in 0.012 (int=12, len=2, exp=-3)
if l+e < 0 {
return fmt.Sprintf("0.%s%s", strings.Repeat("0", (l+e)-((l+e)*2)), s)
}

// above zero, as in 11.1 (int=111, len=3, exp=-1)
return fmt.Sprintf("%s.%s", s[0:l+e], s[l+e:])
}

func RandStringRunes(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

Expand Down

0 comments on commit 4006de5

Please sign in to comment.