1
0
mirror of https://github.com/beego/bee.git synced 2024-11-29 11:41:28 +00:00
bee/g_scaffold.go

155 lines
4.1 KiB
Go
Raw Normal View History

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
func generateScaffold(sname, fields, currpath, driver, conn string) {
ColorLog("[INFO] Do you want to create a '%v' model? [Yes|No] ", sname)
// Generate the model
2014-08-11 09:01:00 +00:00
if askForConfirmation() {
generateModel(sname, fields, currpath)
2014-08-11 09:01:00 +00:00
}
// Generate the controller
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, currpath)
2014-08-11 09:01:00 +00:00
}
// Generate the views
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, currpath)
2014-08-11 09:01:00 +00:00
}
// Generate a migration
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 != "" {
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
}
generateMigration(sname, upsql, downsql, currpath)
2014-08-11 09:01:00 +00:00
}
// Run the migration
ColorLog("[INFO] Do you want to migrate the database? [Yes|No] ")
2014-08-11 09:01:00 +00:00
if askForConfirmation() {
migrateUpdate(currpath, 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 {
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
2014-08-18 03:51:57 +00:00
return ""
}
typ, tag := "", ""
switch driver {
case "mysql":
2016-07-22 23:05:01 +00:00
typ, tag = getSQLTypeMysql(kv[1])
case "postgres":
2016-07-22 23:05:01 +00:00
typ, tag = getSQLTypePostgresql(kv[1])
default:
2016-07-22 23:05:01 +00:00
typ, tag = getSQLTypeMysql(kv[1])
}
2014-08-18 03:51:57 +00:00
if typ == "" {
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" {
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
}
2014-08-18 03:51:57 +00:00
sql = sql + "`" + snakeString(kv[0]) + "` " + typ + ","
if tag != "" {
tags = tags + fmt.Sprintf(tag, "`"+snakeString(kv[0])+"`") + ","
}
}
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-22 23:05:01 +00:00
func getSQLTypePostgresql(ktype string) (tp, tag string) {
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", ""
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 "", ""
}