Skip to content

Commit 9ff5a0f

Browse files
committed
Add feature: 新增'-s'选项以支持对数据库、表、字段按字典序排列。#28
1 parent 095c9dc commit 9ff5a0f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ type Config struct {
1212
Viewer string
1313
Output string
1414
Debug bool
15+
Sorted bool
1516
}

main.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"io"
66
"os"
77
"os/user"
8+
"sort"
89
"strings"
10+
"time"
911

1012
"github.com/urfave/cli"
1113
"github.com/voidint/tsdump/build"
@@ -67,11 +69,12 @@ COPYRIGHT:
6769
}
6870

6971
func main() {
72+
now := time.Now()
7073
app := cli.NewApp()
7174
app.Name = "tsdump"
7275
app.Usage = "Database table structure dump tool."
7376
app.Version = build.Version()
74-
app.Copyright = "Copyright (c) 2017-2020, voidint. All rights reserved."
77+
app.Copyright = fmt.Sprintf("Copyright (c) 2017-%d, voidint. All rights reserved.", now.Year())
7578
app.Authors = []cli.Author{
7679
cli.Author{
7780
Name: "voidint",
@@ -127,6 +130,11 @@ func main() {
127130
Usage: "write to a file, instead of STDOUT",
128131
Destination: &c.Output,
129132
},
133+
cli.BoolFlag{
134+
Name: "s, sorted",
135+
Usage: "sort table columns",
136+
Destination: &c.Sorted,
137+
},
130138
}
131139

132140
app.Before = func(ctx *cli.Context) (err error) {
@@ -155,6 +163,10 @@ func main() {
155163
return cli.NewExitError(fmt.Sprintf("[tsdump] %s", err.Error()), 1)
156164
}
157165

166+
if c.Sorted {
167+
sortedDBs(dbs)
168+
}
169+
158170
if c.Output != "" {
159171
var f *os.File
160172
if f, err = os.Create(c.Output); err != nil {
@@ -231,3 +243,28 @@ func getMetadata(repo model.IRepo, db string, tables ...string) (dbs []model.DB,
231243
}
232244
return dbs, nil
233245
}
246+
247+
func sortedDBs(dbs []model.DB) {
248+
for i := range dbs {
249+
sortedTables(dbs[i].Tables)
250+
}
251+
252+
sort.Slice(dbs, func(i, j int) bool {
253+
return dbs[i].Name < dbs[j].Name
254+
})
255+
}
256+
257+
func sortedTables(tables []model.Table) {
258+
for i := range tables {
259+
sortedColumns(tables[i].Columns)
260+
}
261+
sort.Slice(tables, func(i, j int) bool {
262+
return tables[i].Name < tables[j].Name
263+
})
264+
}
265+
266+
func sortedColumns(columns []model.Column) {
267+
sort.Slice(columns, func(i, j int) bool {
268+
return columns[i].Name < columns[j].Name
269+
})
270+
}

0 commit comments

Comments
 (0)