-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathxsql.go
193 lines (168 loc) · 4.85 KB
/
xsql.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package xsql
import (
"bytes"
"database/sql"
"fmt"
"github.com/shomali11/util/xstrings"
"strings"
)
const (
empty = ""
space = " "
plus = "+"
minus = "-"
pipe = "|"
newLine = "\n"
intString = "int"
floatString = "float"
leftJustification = "LEFT"
rightJustification = "RIGHT"
sqlRowFooter = "(1 row)"
sqlRowsFooterFormat = "(%d rows)"
)
// Pretty returns a pretty sql string
func Pretty(rows *sql.Rows) (string, error) {
columnNames, err := rows.Columns()
if err != nil {
return empty, err
}
if len(columnNames) == 0 {
return getFooter(0), nil
}
columnTypes, err := rows.ColumnTypes()
if err != nil {
return empty, err
}
values, err := getValues(rows)
if err != nil {
return empty, err
}
columnSizes := getColumnSizes(columnNames, values)
columnJustifications := getColumnJustifications(columnTypes)
header := getHeader(columnTypes, columnSizes)
body := getBody(values, columnSizes, columnJustifications)
footer := getFooter(len(values))
var results bytes.Buffer
results.WriteString(header)
results.WriteString(body)
results.WriteString(footer)
return results.String(), nil
}
func getValues(rows *sql.Rows) ([][]string, error) {
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, err
}
values := [][]string{}
dataBytes := make([][]byte, len(columnTypes))
pointers := make([]interface{}, len(columnTypes))
for i := range dataBytes {
pointers[i] = &dataBytes[i]
}
for rows.Next() {
err = rows.Scan(pointers...)
if err != nil {
return nil, err
}
rowValues := make([]string, len(columnTypes))
for i, data := range dataBytes {
if data == nil {
rowValues[i] = empty
} else {
rowValues[i] = string(data)
}
}
values = append(values, rowValues)
}
err = rows.Err()
if err != nil {
return nil, err
}
return values, nil
}
func getColumnSizes(columnNames []string, values [][]string) []int {
columnMaxSizes := make([]int, len(columnNames))
for i, columnName := range columnNames {
columnMaxSizes[i] = len(columnName)
}
for rowIndex := 0; rowIndex < len(values); rowIndex++ {
for columnIndex := 0; columnIndex < len(values[rowIndex]); columnIndex++ {
if columnMaxSizes[columnIndex] < len(values[rowIndex][columnIndex]) {
columnMaxSizes[columnIndex] = len(values[rowIndex][columnIndex])
}
}
}
return columnMaxSizes
}
func getColumnJustifications(columnTypes []*sql.ColumnType) []string {
columnJustifications := make([]string, len(columnTypes))
for i, columnType := range columnTypes {
kindString := columnType.ScanType().Kind().String()
if strings.Contains(kindString, intString) || strings.Contains(kindString, floatString) {
columnJustifications[i] = rightJustification
} else {
columnJustifications[i] = leftJustification
}
}
return columnJustifications
}
func getHeader(columnTypes []*sql.ColumnType, columnSizes []int) string {
var header bytes.Buffer
header.WriteString(space)
header.WriteString(xstrings.Center(columnTypes[0].Name(), columnSizes[0]))
header.WriteString(space)
for i := 1; i < len(columnTypes); i++ {
header.WriteString(pipe)
header.WriteString(space)
header.WriteString(xstrings.Center(columnTypes[i].Name(), columnSizes[i]))
header.WriteString(space)
}
header.WriteString(newLine)
for i := 0; i < 2+columnSizes[0]; i++ {
header.WriteString(minus)
}
for i := 1; i < len(columnTypes); i++ {
header.WriteString(plus)
for j := 0; j < 2+columnSizes[i]; j++ {
header.WriteString(minus)
}
}
header.WriteString(newLine)
return header.String()
}
func getBody(values [][]string, columnSizes []int, columnJustifications []string) string {
var body bytes.Buffer
for rowIndex := 0; rowIndex < len(values); rowIndex++ {
body.WriteString(space)
switch columnJustifications[0] {
case leftJustification:
body.WriteString(xstrings.Left(values[rowIndex][0], columnSizes[0]))
case rightJustification:
body.WriteString(xstrings.Right(values[rowIndex][0], columnSizes[0]))
default:
body.WriteString(xstrings.Center(values[rowIndex][0], columnSizes[0]))
}
body.WriteString(space)
for columnIndex := 1; columnIndex < len(values[rowIndex]); columnIndex++ {
body.WriteString(pipe)
body.WriteString(space)
switch columnJustifications[columnIndex] {
case leftJustification:
body.WriteString(xstrings.Left(values[rowIndex][columnIndex], columnSizes[columnIndex]))
case rightJustification:
body.WriteString(xstrings.Right(values[rowIndex][columnIndex], columnSizes[columnIndex]))
default:
body.WriteString(xstrings.Center(values[rowIndex][columnIndex], columnSizes[columnIndex]))
}
body.WriteString(space)
}
body.WriteString(newLine)
}
return body.String()
}
func getFooter(rowCount int) string {
if rowCount == 1 {
return sqlRowFooter
}
return fmt.Sprintf(sqlRowsFooterFormat, rowCount)
}