forked from smallnest/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (129 loc) · 4.22 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"strings"
"github.com/droundy/goopt"
"github.com/gobuffalo/packr/v2"
"github.com/jimsmart/schema"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/si9ma/gen/dbmeta"
)
var (
sqlType = goopt.String([]string{"--sqltype"}, "mysql", "sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]")
sqlConnStr = goopt.String([]string{"-c", "--connstr"}, "nil", "database connection string")
sqlDatabase = goopt.String([]string{"-d", "--database"}, "nil", "Database to for connection")
sqlTable = goopt.String([]string{"-t", "--table"}, "", "Table to build struct from")
baseTemplates *packr.Box
)
func init() {
// Setup goopts
goopt.Description = func() string {
return "ORM and RESTful meta data viewer for SQl databases"
}
goopt.Version = "v0.9.27 (08/04/2020)"
goopt.Summary = `dbmeta [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName>
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
`
//Parse options
goopt.Parse(nil)
}
func main() {
baseTemplates = packr.New("gen", "./template")
var err error
var content []byte
content, err = baseTemplates.Find("mapping.json")
if err != nil {
fmt.Printf("Error getting default map[mapping file error: %v\n", err)
return
}
err = dbmeta.ProcessMappings("internal", content, false)
if err != nil {
fmt.Printf("Error processing default mapping file error: %v\n", err)
return
}
// Username is required
if sqlConnStr == nil || *sqlConnStr == "" || *sqlConnStr == "nil" {
fmt.Printf("sql connection string is required! Add it with --connstr=s\n\n")
fmt.Println(goopt.Usage())
return
}
if sqlDatabase == nil || *sqlDatabase == "" || *sqlDatabase == "nil" {
fmt.Printf("Database can not be null\n\n")
fmt.Println(goopt.Usage())
return
}
var db *sql.DB
db, err = sql.Open(*sqlType, *sqlConnStr)
if err != nil {
fmt.Printf("Error in open database: %v\n\n", err.Error())
return
}
defer func() { _ = db.Close() }()
err = db.Ping()
if err != nil {
fmt.Printf("Error connecting to database: %v\n\n", err.Error())
return
}
var dbTables []string
// parse or read tables
if *sqlTable != "" && *sqlTable != "all" {
dbTables = strings.Split(*sqlTable, ",")
fmt.Printf("showing meta for table(s): %s\n", *sqlTable)
} else {
fmt.Printf("showing meta for all tables\n")
dbTables, err = schema.TableNames(db)
if err != nil {
fmt.Printf("Error in fetching tables information from %s information schema from %s\n", *sqlType, *sqlConnStr)
return
}
}
for _, tableName := range dbTables {
fmt.Printf("---------------------------\n")
if strings.HasPrefix(tableName, "[") && strings.HasSuffix(tableName, "]") {
tableName = tableName[1 : len(tableName)-1]
}
fmt.Printf("[%s]\n", tableName)
tableInfo, err := dbmeta.LoadMeta(*sqlType, db, *sqlDatabase, tableName)
if err != nil {
fmt.Printf("Error getting table info for %s error: %v\n\n\n\n", tableName, err)
continue
}
fmt.Printf("\n\nDDL\n%s\n\n\n", tableInfo.DDL())
for _, col := range tableInfo.Columns() {
fmt.Printf("%s\n", col.String())
colMapping, err := dbmeta.SQLTypeToMapping(strings.ToLower(col.DatabaseTypeName()))
if err != nil { // unknown type
fmt.Printf("unable to find mapping for db type: %s\n", col.DatabaseTypeName())
continue
}
fmt.Printf(" %s\n", colMapping.String())
}
primaryCnt := dbmeta.PrimaryKeyCount(tableInfo)
fmt.Printf("primaryCnt: %d\n", primaryCnt)
fmt.Printf("\n\n")
delSQL, err := dbmeta.GenerateDeleteSQL(tableInfo)
if err == nil {
fmt.Printf("delSQL: %s\n", delSQL)
}
updateSQL, err := dbmeta.GenerateUpdateSQL(tableInfo)
if err == nil {
fmt.Printf("updateSQL: %s\n", updateSQL)
}
insertSQL, err := dbmeta.GenerateInsertSQL(tableInfo)
if err == nil {
fmt.Printf("insertSQL: %s\n", insertSQL)
}
selectOneSQL, err := dbmeta.GenerateSelectOneSQL(tableInfo)
if err == nil {
fmt.Printf("selectOneSQL: %s\n", selectOneSQL)
}
selectMultiSQL, err := dbmeta.GenerateSelectMultiSQL(tableInfo)
if err == nil {
fmt.Printf("selectMultiSQL: %s\n", selectMultiSQL)
}
}
}