forked from wgliang/pgproxy
/
formate.go
96 lines (87 loc) · 2.39 KB
/
formate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2017 wgliang. All rights reserved.
// Use of this source code is governed by Apache
// license that can be found in the LICENSE file.
// Package proxy provides proxy service and redirects requests
// form proxy.Addr to remote.Addr.
package proxy
import (
"database/sql"
"fmt"
"os"
"strconv"
"github.com/golang/glog"
"github.com/olekukonko/tablewriter"
)
// Parse query's results and formate it,then will be print
// in command line such as:
// +---------+----------------+----------+
// | ID | IP | NAME |
// +---------+----------------+----------+
// | 1 | 180.17.95.2 | Jack |
// | 2 | 180.17.95.3 | Wong |
// | 3 | 180.17.95.4 | Lin |
// | 4 | 180.17.95.5 | Trump |
// +---------+----------------+----------+
// else error
func RowsFormater(rows *sql.Rows) {
cols, err := rows.Columns()
if err != nil {
glog.Errorln(err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(cols)
data := make([][]string, 1)
count := 0
for rows.Next() {
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
fmt.Println(err)
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
row := make([]string, 0)
for i, _ := range cols {
val := columnPointers[i].(*interface{})
row = append(row, interface2String(*val))
}
data = append(data, row)
count = count + 1
}
for _, v := range data {
table.Append(v)
}
table.Render()
if count > 0 {
fmt.Printf("(%d rows of records)\n", count)
}
}
// Parse exec's results and formate it,then will be print
// in command line such as:
// OK, [n] rows affected
// else error
func ResultFormater(res sql.Result) {
rowsAffected, err := res.RowsAffected()
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("OK, %d rows affected\n", rowsAffected)
}
}
// Convert type interface{} into string just for friendly display.
func interface2String(input interface{}) string {
switch input.(type) {
case string:
return input.(string)
case int64:
return strconv.FormatInt(input.(int64), 10)
case []byte:
return string(input.([]byte))
default:
return ""
}
}