-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_show.go
209 lines (184 loc) · 4.76 KB
/
db_show.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package lsm3
import (
"fmt"
"reflect"
"strings"
)
func (c *Contains) ShowTables() {
fmt.Println("+-----------------+")
fmt.Println("| Tables |")
fmt.Println("+-----------------+")
for table := range c.ts {
fmt.Printf("| %-15s |\n", table)
}
fmt.Println("+-----------------+")
}
func (c *Contains) ShowColumns(table string) {
v, ok := c.table[table]
if !ok {
fmt.Printf("Table: %s 不存在!\n", table)
return
}
fmt.Printf("+-----------------+-----------------+\n")
fmt.Printf("| Column Name | Data Type |\n")
fmt.Printf("+-----------------+-----------------+\n")
for _, column := range v.TableColumns {
fmt.Printf("| %-15s | %-15s |\n", column.Name, column.DataType)
}
fmt.Printf("+-----------------+-----------------+\n")
}
func (c *Contains) Show() {
c.show()
}
func (c *Contains) show() {
for k, v := range c.table {
fmt.Println(k, v)
}
fmt.Println("========================")
err := c.db.Fold(func(key, value []byte) bool {
fmt.Printf("(k:%s-v:%s)\n", key, value)
return true
})
fmt.Println(err)
}
func printTable(values []interface{}) {
if len(values) == 0 {
return
}
firstValue := reflect.ValueOf(values[0]).Elem()
t := firstValue.Type()
// 获取每列的最大宽度
widths := make([]int, firstValue.NumField())
for i := 0; i < firstValue.NumField(); i++ {
fieldName := t.Field(i).Name
widths[i] = len(fieldName)
}
for _, value := range values {
v := reflect.ValueOf(value).Elem()
for i := 0; i < v.NumField(); i++ {
fieldValue := fmt.Sprintf("%v", v.Field(i).Interface())
if len(fieldValue) > widths[i] {
widths[i] = len(fieldValue)
}
}
}
// 打印表头的分隔符
printSeparator(widths)
// 打印表头
fmt.Print("|")
for i := 0; i < firstValue.NumField(); i++ {
fmt.Printf(" %-*s |", widths[i], t.Field(i).Name)
}
fmt.Println()
// 打印表头下面的分隔符
printSeparator(widths)
// 打印每一行数据
for _, value := range values {
v := reflect.ValueOf(value).Elem()
fmt.Print("|")
for i := 0; i < v.NumField(); i++ {
fieldValue := fmt.Sprintf("%v", v.Field(i).Interface())
fmt.Printf(" %-*s |", widths[i], fieldValue)
}
fmt.Println()
printSeparator(widths)
}
}
func printSeparator(widths []int) {
fmt.Print("+")
for _, width := range widths {
fmt.Print(strings.Repeat("-", width+2)) // +2 是为了两边的空格
fmt.Print("+")
}
fmt.Println()
}
func printStruct(s interface{}) {
v := reflect.ValueOf(s).Elem()
t := v.Type()
// 打印表头
fmt.Println("+----------------+----------------+")
fmt.Printf("| %-14s | %-14s |\n", "Field", "Value")
fmt.Println("+----------------+----------------+")
// 打印每个字段的名称和值
for i := 0; i < v.NumField(); i++ {
fieldName := t.Field(i).Name
fieldValue := v.Field(i).Interface()
fmt.Printf("| %-14s | %-14v |\n", fieldName, fieldValue)
}
fmt.Println("+----------------+----------------+")
}
func printTableWithColumns(values []interface{}, columns []string) {
if len(values) == 0 || len(columns) == 0 {
return
}
firstValue := reflect.ValueOf(values[0]).Elem()
t := firstValue.Type()
// 仅选择指定列
selectedFields := make([]int, 0, len(columns))
widths := make([]int, len(columns))
notFields := make(map[int]string)
for i, col := range columns {
flag := false
for j := 0; j < t.NumField(); j++ {
if t.Field(j).Name == strings.Title(col) {
flag = true
selectedFields = append(selectedFields, j)
widths[i] = len(col)
break
}
}
if !flag {
notFields[i] = col
}
}
for _, value := range values {
v := reflect.ValueOf(value).Elem()
for i, fieldIdx := range selectedFields {
fieldValue := fmt.Sprintf("%v", v.Field(fieldIdx).Interface())
if len(fieldValue) > widths[i] {
widths[i] = len(fieldValue)
}
}
}
for k := 0; k < len(widths); k++ {
if _, ok := notFields[k]; ok {
widths = append(widths[:k], widths[k+1:]...)
}
}
if len(notFields) != 0 {
fmt.Printf("不存在的参数:[")
for _, v := range notFields {
fmt.Printf(" %s ", v)
}
fmt.Printf("]\n")
}
if len(notFields) == len(columns) {
fmt.Printf("select语句错误,column数据没有找到,%v\n", columns)
return
}
// fmt.Println(widths, len(widths), notFields)
// 打印表头的分隔符
printSeparator(widths)
// 打印表头
fmt.Print("|")
for i, col := range columns {
if _, ok := notFields[i]; ok {
continue
}
fmt.Printf(" %-*s |", widths[i], col)
}
fmt.Println()
// 打印表头下面的分隔符
printSeparator(widths)
// 打印每一行数据
for _, value := range values {
v := reflect.ValueOf(value).Elem()
fmt.Print("|")
for i, fieldIdx := range selectedFields {
fieldValue := fmt.Sprintf("%v", v.Field(fieldIdx).Interface())
fmt.Printf(" %-*s |", widths[i], fieldValue)
}
fmt.Println()
printSeparator(widths)
}
}