mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 11:52:12 +03:00
Encode bigints as strings
This commit is contained in:
parent
581a2f164b
commit
fbb8ffbfc6
@ -207,6 +207,8 @@ func HandleQuery(query string, c *gin.Context) {
|
|||||||
c.Writer.Header().Set("Content-disposition", "attachment;filename="+filename)
|
c.Writer.Header().Set("Content-disposition", "attachment;filename="+filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.PrepareBigints()
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
case "csv":
|
case "csv":
|
||||||
c.Data(200, "text/csv", result.CSV())
|
c.Data(200, "text/csv", result.CSV())
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Row []interface{}
|
type Row []interface{}
|
||||||
@ -14,6 +16,22 @@ type Result struct {
|
|||||||
Rows []Row `json:"rows"`
|
Rows []Row `json:"rows"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Due to big int number limitations in javascript, numbers should be encoded
|
||||||
|
// as strings so they could be properly loaded on the frontend.
|
||||||
|
func (res *Result) PrepareBigints() {
|
||||||
|
for i, row := range res.Rows {
|
||||||
|
for j, col := range row {
|
||||||
|
if col == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if reflect.TypeOf(col).Kind() == reflect.Int64 {
|
||||||
|
res.Rows[i][j] = strconv.FormatInt(col.(int64), 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (res *Result) Format() []map[string]interface{} {
|
func (res *Result) Format() []map[string]interface{} {
|
||||||
var items []map[string]interface{}
|
var items []map[string]interface{}
|
||||||
|
|
||||||
|
@ -7,6 +7,23 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_PrepareBigints(t *testing.T) {
|
||||||
|
result := Result{
|
||||||
|
Columns: []string{"value"},
|
||||||
|
Rows: []Row{
|
||||||
|
Row{int(1234)},
|
||||||
|
Row{int64(9223372036854775807)},
|
||||||
|
Row{int64(-9223372036854775808)},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result.PrepareBigints()
|
||||||
|
|
||||||
|
assert.Equal(t, 1234, result.Rows[0][0])
|
||||||
|
assert.Equal(t, "9223372036854775807", result.Rows[1][0])
|
||||||
|
assert.Equal(t, "-9223372036854775808", result.Rows[2][0])
|
||||||
|
}
|
||||||
|
|
||||||
func Test_CSV(t *testing.T) {
|
func Test_CSV(t *testing.T) {
|
||||||
result := Result{
|
result := Result{
|
||||||
Columns: []string{"id", "name", "email"},
|
Columns: []string{"id", "name", "email"},
|
||||||
|
Loading…
Reference in New Issue
Block a user