2016-01-05 03:19:16 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-11-15 01:10:50 +03:00
|
|
|
"log"
|
2018-11-28 01:51:51 +03:00
|
|
|
"math"
|
2016-01-07 20:27:16 +03:00
|
|
|
"strconv"
|
2016-06-28 06:38:48 +03:00
|
|
|
"time"
|
2017-09-14 07:27:54 +03:00
|
|
|
|
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
2016-01-05 03:19:16 +03:00
|
|
|
)
|
|
|
|
|
2022-12-02 01:51:12 +03:00
|
|
|
const (
|
|
|
|
ObjTypeTable = "table"
|
|
|
|
ObjTypeView = "view"
|
|
|
|
ObjTypeMaterializedView = "materialized_view"
|
|
|
|
ObjTypeSequence = "sequence"
|
|
|
|
)
|
2016-01-05 03:19:16 +03:00
|
|
|
|
2022-12-02 01:51:12 +03:00
|
|
|
type (
|
|
|
|
Row []interface{}
|
2016-01-08 23:16:53 +03:00
|
|
|
|
2022-12-02 01:51:12 +03:00
|
|
|
Pagination struct {
|
|
|
|
Rows int64 `json:"rows_count"`
|
|
|
|
Page int64 `json:"page"`
|
|
|
|
Pages int64 `json:"pages_count"`
|
|
|
|
PerPage int64 `json:"per_page"`
|
|
|
|
}
|
2016-01-05 03:19:16 +03:00
|
|
|
|
2022-12-02 01:51:12 +03:00
|
|
|
Result struct {
|
|
|
|
Pagination *Pagination `json:"pagination,omitempty"`
|
|
|
|
Columns []string `json:"columns"`
|
|
|
|
Rows []Row `json:"rows"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Objects struct {
|
|
|
|
Tables []string `json:"table"`
|
|
|
|
Views []string `json:"view"`
|
|
|
|
MaterializedViews []string `json:"materialized_view"`
|
|
|
|
Sequences []string `json:"sequence"`
|
|
|
|
}
|
|
|
|
)
|
2016-01-13 06:33:44 +03:00
|
|
|
|
2016-01-07 20:27:16 +03:00
|
|
|
// Due to big int number limitations in javascript, numbers should be encoded
|
|
|
|
// as strings so they could be properly loaded on the frontend.
|
2021-12-29 20:03:50 +03:00
|
|
|
func (res *Result) PostProcess() {
|
2016-01-07 20:27:16 +03:00
|
|
|
for i, row := range res.Rows {
|
|
|
|
for j, col := range row {
|
|
|
|
if col == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-29 20:03:50 +03:00
|
|
|
switch val := col.(type) {
|
|
|
|
case int64:
|
2016-01-08 05:10:53 +03:00
|
|
|
if val < -9007199254740991 || val > 9007199254740991 {
|
|
|
|
res.Rows[i][j] = strconv.FormatInt(col.(int64), 10)
|
|
|
|
}
|
2021-12-29 20:03:50 +03:00
|
|
|
case float64:
|
2018-11-28 01:51:51 +03:00
|
|
|
// json.Marshal panics when dealing with NaN/Inf values
|
|
|
|
// issue: https://github.com/golang/go/issues/25721
|
|
|
|
if math.IsNaN(val) {
|
|
|
|
res.Rows[i][j] = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-01-08 05:10:53 +03:00
|
|
|
if val < -999999999999999 || val > 999999999999999 {
|
|
|
|
res.Rows[i][j] = strconv.FormatFloat(val, 'e', -1, 64)
|
|
|
|
}
|
2021-12-29 20:03:50 +03:00
|
|
|
case string:
|
|
|
|
if hasBinary(val, 8) && BinaryCodec != CodecNone {
|
|
|
|
res.Rows[i][j] = encodeBinaryData([]byte(val), BinaryCodec)
|
|
|
|
}
|
2016-01-07 20:27:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 03:19:16 +03:00
|
|
|
func (res *Result) Format() []map[string]interface{} {
|
|
|
|
var items []map[string]interface{}
|
|
|
|
|
|
|
|
for _, row := range res.Rows {
|
|
|
|
item := make(map[string]interface{})
|
|
|
|
|
|
|
|
for i, c := range res.Columns {
|
|
|
|
item[c] = row[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func (res *Result) CSV() []byte {
|
|
|
|
buff := &bytes.Buffer{}
|
|
|
|
writer := csv.NewWriter(buff)
|
|
|
|
|
2022-11-15 01:10:50 +03:00
|
|
|
if err := writer.Write(res.Columns); err != nil {
|
|
|
|
log.Printf("result csv write error: %v\n", err)
|
|
|
|
}
|
2016-01-05 03:19:16 +03:00
|
|
|
|
|
|
|
for _, row := range res.Rows {
|
|
|
|
record := make([]string, len(res.Columns))
|
|
|
|
|
|
|
|
for i, item := range row {
|
|
|
|
if item != nil {
|
2016-06-28 06:38:48 +03:00
|
|
|
switch v := item.(type) {
|
|
|
|
case time.Time:
|
|
|
|
record[i] = v.Format("2006-01-02 15:04:05")
|
|
|
|
default:
|
|
|
|
record[i] = fmt.Sprintf("%v", item)
|
|
|
|
}
|
2016-01-05 03:19:16 +03:00
|
|
|
} else {
|
|
|
|
record[i] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := writer.Write(record)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (res *Result) JSON() []byte {
|
2017-09-14 07:27:54 +03:00
|
|
|
var data []byte
|
|
|
|
|
2019-11-02 21:00:23 +03:00
|
|
|
if command.Opts.DisablePrettyJSON {
|
2017-09-14 07:27:54 +03:00
|
|
|
data, _ = json.Marshal(res.Format())
|
2017-09-17 05:57:04 +03:00
|
|
|
} else {
|
|
|
|
data, _ = json.MarshalIndent(res.Format(), "", " ")
|
2017-09-14 07:27:54 +03:00
|
|
|
}
|
2017-09-17 05:57:04 +03:00
|
|
|
|
2016-01-05 03:19:16 +03:00
|
|
|
return data
|
|
|
|
}
|
2016-01-13 06:33:44 +03:00
|
|
|
|
|
|
|
func ObjectsFromResult(res *Result) map[string]*Objects {
|
|
|
|
objects := map[string]*Objects{}
|
|
|
|
|
|
|
|
for _, row := range res.Rows {
|
|
|
|
schema := row[0].(string)
|
|
|
|
name := row[1].(string)
|
2022-12-02 01:51:12 +03:00
|
|
|
objectType := row[2].(string)
|
2016-01-13 06:33:44 +03:00
|
|
|
|
|
|
|
if objects[schema] == nil {
|
|
|
|
objects[schema] = &Objects{
|
2016-01-15 06:30:19 +03:00
|
|
|
Tables: []string{},
|
|
|
|
Views: []string{},
|
|
|
|
MaterializedViews: []string{},
|
|
|
|
Sequences: []string{},
|
2016-01-13 06:33:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 01:51:12 +03:00
|
|
|
switch objectType {
|
|
|
|
case ObjTypeTable:
|
2016-01-13 06:33:44 +03:00
|
|
|
objects[schema].Tables = append(objects[schema].Tables, name)
|
2022-12-02 01:51:12 +03:00
|
|
|
case ObjTypeView:
|
2016-01-13 06:33:44 +03:00
|
|
|
objects[schema].Views = append(objects[schema].Views, name)
|
2022-12-02 01:51:12 +03:00
|
|
|
case ObjTypeMaterializedView:
|
2016-01-15 06:30:19 +03:00
|
|
|
objects[schema].MaterializedViews = append(objects[schema].MaterializedViews, name)
|
2022-12-02 01:51:12 +03:00
|
|
|
case ObjTypeSequence:
|
2016-01-13 06:33:44 +03:00
|
|
|
objects[schema].Sequences = append(objects[schema].Sequences, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objects
|
|
|
|
}
|