2014-08-11 09:01:00 +00:00
|
|
|
package main
|
|
|
|
|
2014-08-18 03:51:57 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2014-08-11 09:01:00 +00:00
|
|
|
|
2014-08-14 06:53:28 +00:00
|
|
|
func generateScaffold(sname, fields, crupath, driver, conn string) {
|
2014-08-11 09:01:00 +00:00
|
|
|
// generate model
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[INFO] Do you want to create a %v model? [yes|no]] ", sname)
|
2014-08-11 09:01:00 +00:00
|
|
|
if askForConfirmation() {
|
|
|
|
generateModel(sname, fields, crupath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate controller
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[INFO] Do you want to create a %v controller? [yes|no]] ", sname)
|
2014-08-11 09:01:00 +00:00
|
|
|
if askForConfirmation() {
|
|
|
|
generateController(sname, crupath)
|
|
|
|
}
|
|
|
|
// generate view
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[INFO] Do you want to create views for this %v resource? [yes|no]] ", sname)
|
2014-08-11 09:01:00 +00:00
|
|
|
if askForConfirmation() {
|
|
|
|
generateView(sname, crupath)
|
|
|
|
}
|
|
|
|
// generate migration
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[INFO] Do you want to create a %v migration and schema for this resource? [yes|no]] ", sname)
|
2014-08-11 09:01:00 +00:00
|
|
|
if askForConfirmation() {
|
2014-08-18 03:51:57 +00:00
|
|
|
upsql := ""
|
2014-08-18 04:21:21 +00:00
|
|
|
downsql := ""
|
2014-08-18 03:51:57 +00:00
|
|
|
if fields != "" {
|
2015-11-10 02:09:26 +00:00
|
|
|
upsql = `m.SQL("CREATE TABLE ` + sname + "(" + generateSQLFromFields(fields) + `)");`
|
|
|
|
downsql = `m.SQL("DROP TABLE ` + "`" + sname + "`" + `")`
|
2016-07-22 23:14:33 +00:00
|
|
|
if driver == "" {
|
|
|
|
downsql = strings.Replace(downsql, "`", "", -1)
|
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
}
|
2014-08-18 04:21:21 +00:00
|
|
|
generateMigration(sname, upsql, downsql, crupath)
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
|
|
|
// run migration
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[INFO] Do you want to migrate the database? [yes|no]] ")
|
2014-08-11 09:01:00 +00:00
|
|
|
if askForConfirmation() {
|
2014-08-14 06:53:28 +00:00
|
|
|
migrateUpdate(crupath, driver, conn)
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
|
|
|
ColorLog("[INFO] All done! Don't forget to add beego.Router(\"/%v\" ,&controllers.%vController{}) to routers/route.go\n", sname, strings.Title(sname))
|
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
|
|
|
|
func generateSQLFromFields(fields string) string {
|
|
|
|
sql := ""
|
|
|
|
tags := ""
|
|
|
|
fds := strings.Split(fields, ",")
|
|
|
|
for i, v := range fds {
|
|
|
|
kv := strings.SplitN(v, ":", 2)
|
|
|
|
if len(kv) != 2 {
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
2014-08-18 03:51:57 +00:00
|
|
|
return ""
|
|
|
|
}
|
2016-07-07 12:21:47 +00:00
|
|
|
typ, tag := "", ""
|
|
|
|
switch driver {
|
|
|
|
case "mysql":
|
2016-07-22 23:05:01 +00:00
|
|
|
typ, tag = getSQLTypeMysql(kv[1])
|
2016-07-07 12:21:47 +00:00
|
|
|
case "postgres":
|
2016-07-22 23:05:01 +00:00
|
|
|
typ, tag = getSQLTypePostgresql(kv[1])
|
2016-07-07 12:21:47 +00:00
|
|
|
default:
|
2016-07-22 23:05:01 +00:00
|
|
|
typ, tag = getSQLTypeMysql(kv[1])
|
2016-07-07 12:21:47 +00:00
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
if typ == "" {
|
2016-06-01 12:30:29 +00:00
|
|
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
2014-08-18 03:51:57 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
2016-07-07 12:21:47 +00:00
|
|
|
switch driver {
|
|
|
|
case "mysql":
|
|
|
|
sql = sql + "`id` int(11) NOT NULL AUTO_INCREMENT,"
|
|
|
|
tags = tags + "PRIMARY KEY (`id`),"
|
|
|
|
case "postgres":
|
|
|
|
sql = sql + "id interger serial primary key,"
|
|
|
|
default:
|
|
|
|
sql = sql + "`id` int(11) NOT NULL AUTO_INCREMENT,"
|
|
|
|
tags = tags + "PRIMARY KEY (`id`),"
|
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
}
|
2016-07-07 12:21:47 +00:00
|
|
|
|
2014-08-18 03:51:57 +00:00
|
|
|
sql = sql + "`" + snakeString(kv[0]) + "` " + typ + ","
|
|
|
|
if tag != "" {
|
|
|
|
tags = tags + fmt.Sprintf(tag, "`"+snakeString(kv[0])+"`") + ","
|
|
|
|
}
|
|
|
|
}
|
2016-07-07 12:21:47 +00:00
|
|
|
if driver == "postgres" {
|
|
|
|
sql = strings.Replace(sql, "`", "", -1)
|
|
|
|
tags = strings.Replace(tags, "`", "", -1)
|
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
sql = strings.TrimRight(sql+tags, ",")
|
|
|
|
return sql
|
|
|
|
}
|
|
|
|
|
2016-07-22 23:05:01 +00:00
|
|
|
func getSQLTypeMysql(ktype string) (tp, tag string) {
|
2014-08-18 03:51:57 +00:00
|
|
|
kv := strings.SplitN(ktype, ":", 2)
|
|
|
|
switch kv[0] {
|
|
|
|
case "string":
|
|
|
|
if len(kv) == 2 {
|
|
|
|
return "varchar(" + kv[1] + ") NOT NULL", ""
|
|
|
|
}
|
2016-07-22 23:05:01 +00:00
|
|
|
return "varchar(128) NOT NULL", ""
|
2014-08-18 03:51:57 +00:00
|
|
|
case "text":
|
|
|
|
return "longtext NOT NULL", ""
|
|
|
|
case "auto":
|
|
|
|
return "int(11) NOT NULL AUTO_INCREMENT", ""
|
|
|
|
case "pk":
|
|
|
|
return "int(11) NOT NULL", "PRIMARY KEY (%s)"
|
|
|
|
case "datetime":
|
|
|
|
return "datetime NOT NULL", ""
|
|
|
|
case "int", "int8", "int16", "int32", "int64":
|
|
|
|
fallthrough
|
|
|
|
case "uint", "uint8", "uint16", "uint32", "uint64":
|
|
|
|
return "int(11) DEFAULT NULL", ""
|
|
|
|
case "bool":
|
|
|
|
return "tinyint(1) NOT NULL", ""
|
|
|
|
case "float32", "float64":
|
|
|
|
return "float NOT NULL", ""
|
|
|
|
case "float":
|
|
|
|
return "float NOT NULL", ""
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
2016-07-07 12:21:47 +00:00
|
|
|
|
2016-07-22 23:05:01 +00:00
|
|
|
func getSQLTypePostgresql(ktype string) (tp, tag string) {
|
2016-07-07 12:21:47 +00:00
|
|
|
kv := strings.SplitN(ktype, ":", 2)
|
|
|
|
switch kv[0] {
|
|
|
|
case "string":
|
|
|
|
if len(kv) == 2 {
|
|
|
|
return "char(" + kv[1] + ") NOT NULL", ""
|
|
|
|
}
|
2016-07-22 23:05:01 +00:00
|
|
|
return "TEXT NOT NULL", ""
|
2016-07-07 12:21:47 +00:00
|
|
|
case "text":
|
|
|
|
return "TEXT NOT NULL", ""
|
|
|
|
case "auto", "pk":
|
|
|
|
return "serial primary key", ""
|
|
|
|
case "datetime":
|
|
|
|
return "TIMESTAMP WITHOUT TIME ZONE NOT NULL", ""
|
|
|
|
case "int", "int8", "int16", "int32", "int64":
|
|
|
|
fallthrough
|
|
|
|
case "uint", "uint8", "uint16", "uint32", "uint64":
|
|
|
|
return "integer DEFAULT NULL", ""
|
|
|
|
case "bool":
|
|
|
|
return "boolean NOT NULL", ""
|
|
|
|
case "float32", "float64", "float":
|
|
|
|
return "numeric NOT NULL", ""
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|