-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.go
157 lines (135 loc) · 3.56 KB
/
generator.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
package main
import (
"database/sql"
"fmt"
"log"
"strconv"
"strings"
"time"
helper "github.com/shouva/dailyhelper"
)
var db *sql.DB
func generate() {
start := time.Now()
connect()
createMain()
createCon()
createConfig()
copyConfig()
createGitIgnore()
// handler file harus di lengkapi setelah pembuatan seluruh model. :-)
createHandlerFile()
tables := getAllTablename()
var modelnames []string
var routes []Route
c := config.Database
items := []Item{}
for _, tablename := range tables {
modelname := helper.SnakeCaseToCamelCase(helper.Singular(tablename), true)
tablemodelstring, modelname, queries := createStruct(c.DBName, tablename)
tablemodelstring = "package models\n" + tablemodelstring
objectname := helper.SnakeCaseToCamelCase(helper.Singular(tablename), false)
objectname = helper.BeautySingularity(objectname)
url := strings.ToLower(objectname)
objectname = "_" + objectname
createModel(tablename, tablemodelstring)
createAPI(tablename, modelname, objectname, url)
modelnames = append(modelnames, modelname)
routes = append(routes, Route{Name: modelname, URL: url})
item := createItems(tablename, url, &queries)
items = append(items, *item...)
}
completer(folderhandler + "/handler.go")
createMigrations(modelnames)
createRoutes(routes)
generatePostman(c.DBName, &items)
fmt.Println(len(tables), "table * 5 endpoint API have generated!")
elapsed := time.Since(start)
log.Printf("Process take finished in %s", elapsed)
}
func createDir() {
fmt.Println("wow")
// config.Package
}
func connect() {
if config.Database.Server == "mysql" {
connectMYSQL()
} else if config.Database.Server == "mssql" {
connectMSSQL()
} else {
panic("server tidak dikenali")
}
fmt.Println("berhasil connect")
}
func connectMYSQL() {
c := config.Database
var err error
var constring string
constring = c.User + ":" + c.Password + "@tcp(" + c.Host + ":" + c.Port + ")/" + c.DBName + "?&parseTime=True"
db, err = sql.Open("mysql", constring)
if err != nil {
panic(err.Error)
}
fmt.Println("berhasil connect")
}
func connectMSSQL() {
c := config.Database
var err error
port, _ := strconv.Atoi(c.Port)
connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
c.Host, c.User, c.Password, port, c.DBName)
fmt.Println(connectionString)
db, err = sql.Open("mssql", connectionString)
if err != nil {
panic(err.Error)
}
}
const mySQL = "mysql"
const msSQL = "mssql"
func getAllTablename() []string {
tables := []string{}
var query string
if config.Database.Server == msSQL {
query = `
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES;
`
} else if config.Database.Server == mySQL {
query = "SHOW TABLES"
}
res, err := db.Query(query)
if err != nil {
fmt.Println(err)
return nil
}
var table string
for res.Next() {
res.Scan(&table)
tables = append(tables, table)
}
return tables
}
func createFunctionName(strucName, tablename string) string {
return fmt.Sprintf("\n// TableName : \nfunc (*%s) TableName() string {"+
"\nreturn \"%s\""+
"\n}", strucName, tablename)
}
func stringifyType(colType string) string {
switch colType {
case "tinyint", "int", "smallint", "mediumint":
return "uint16"
case "bigint":
return "uint64"
case "char", "enum", "varchar", "nvarchar", "longtext", "mediumtext", "text", "tinytext":
return "string"
case "date", "datetime", "time", "timestamp":
return "*time.Time"
case "decimal", "double", "numeric":
return "float64"
case "float":
return "float32"
case "binary", "blob", "longblob", "mediumblob", "varbinary":
return "[]byte"
}
return ""
}