-
Notifications
You must be signed in to change notification settings - Fork 61
/
sql2pb.go
56 lines (45 loc) · 1.81 KB
/
sql2pb.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
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/Mikaelemmmm/sql2pb/core"
"log"
"strings"
_ "github.com/go-sql-driver/mysql"
)
func main() {
dbType := flag.String("db", "mysql", "the database type")
host := flag.String("host", "localhost", "the database host")
port := flag.Int("port", 3306, "the database port")
user := flag.String("user", "root", "the database user")
password := flag.String("password", "", "the database password")
schema := flag.String("schema", "", "the database schema")
table := flag.String("table", "*", "the table schema,multiple tables ',' split. ")
serviceName := flag.String("service_name", *schema, "the protobuf service name , defaults to the database schema.")
packageName := flag.String("package", *schema, "the protocol buffer package. defaults to the database schema.")
goPackageName := flag.String("go_package", "", "the protocol buffer go_package. defaults to the database schema.")
ignoreTableStr := flag.String("ignore_tables", "", "a comma spaced list of tables to ignore")
ignoreColumnStr := flag.String("ignore_columns", "", "a comma spaced list of mysql columns to ignore")
fieldStyle := flag.String("field_style", "sqlPb", "gen protobuf field style, sql_pb | sqlPb")
flag.Parse()
if *schema == "" {
fmt.Println(" - please input the database schema ")
return
}
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", *user, *password, *host, *port, *schema)
db, err := sql.Open(*dbType, connStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
ignoreTables := strings.Split(*ignoreTableStr, ",")
ignoreColumns := strings.Split(*ignoreColumnStr, ",")
s, err := core.GenerateSchema(db, *table, ignoreTables, ignoreColumns, *serviceName, *goPackageName, *packageName, *fieldStyle)
if nil != err {
log.Fatal(err)
}
if nil != s {
fmt.Println(s)
}
}