1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 06:40:54 +00:00

Merge pull request #2 from kyle-wang/master

Update develop
This commit is contained in:
kyle 2016-01-07 11:43:23 +08:00
commit f0cd17dbb1
3 changed files with 39 additions and 25 deletions

View File

@ -362,12 +362,12 @@ func getTableObjects(tableNames []string, db *sql.DB, dbTransformer DbTransforme
// and fill in Table struct // and fill in Table struct
func (*MysqlDB) GetConstraints(db *sql.DB, table *Table, blackList map[string]bool) { func (*MysqlDB) GetConstraints(db *sql.DB, table *Table, blackList map[string]bool) {
rows, err := db.Query( rows, err := db.Query(
`SELECT `SELECT
c.constraint_type, u.column_name, u.referenced_table_schema, u.referenced_table_name, referenced_column_name, u.ordinal_position c.constraint_type, u.column_name, u.referenced_table_schema, u.referenced_table_name, referenced_column_name, u.ordinal_position
FROM FROM
information_schema.table_constraints c information_schema.table_constraints c
INNER JOIN INNER JOIN
information_schema.key_column_usage u ON c.constraint_name = u.constraint_name information_schema.key_column_usage u ON c.constraint_name = u.constraint_name
WHERE WHERE
c.table_schema = database() AND c.table_name = ? AND u.table_schema = database() AND u.table_name = ?`, c.table_schema = database() AND c.table_name = ? AND u.table_schema = database() AND u.table_name = ?`,
table.Name, table.Name) // u.position_in_unique_constraint, table.Name, table.Name) // u.position_in_unique_constraint,
@ -412,9 +412,9 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin
// retrieve columns // retrieve columns
colDefRows, _ := db.Query( colDefRows, _ := db.Query(
`SELECT `SELECT
column_name, data_type, column_type, is_nullable, column_default, extra column_name, data_type, column_type, is_nullable, column_default, extra
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
table_schema = database() AND table_name = ?`, table_schema = database() AND table_name = ?`,
table.Name) table.Name)
@ -537,7 +537,7 @@ func (*PostgresDB) GetTableNames(db *sql.DB) (tables []string) {
// GetConstraints for PostgreSQL // GetConstraints for PostgreSQL
func (*PostgresDB) GetConstraints(db *sql.DB, table *Table, blackList map[string]bool) { func (*PostgresDB) GetConstraints(db *sql.DB, table *Table, blackList map[string]bool) {
rows, err := db.Query( rows, err := db.Query(
`SELECT `SELECT
c.constraint_type, c.constraint_type,
u.column_name, u.column_name,
cu.table_catalog AS referenced_table_catalog, cu.table_catalog AS referenced_table_catalog,
@ -545,13 +545,13 @@ func (*PostgresDB) GetConstraints(db *sql.DB, table *Table, blackList map[string
cu.column_name AS referenced_column_name, cu.column_name AS referenced_column_name,
u.ordinal_position u.ordinal_position
FROM FROM
information_schema.table_constraints c information_schema.table_constraints c
INNER JOIN INNER JOIN
information_schema.key_column_usage u ON c.constraint_name = u.constraint_name information_schema.key_column_usage u ON c.constraint_name = u.constraint_name
INNER JOIN INNER JOIN
information_schema.constraint_column_usage cu ON cu.constraint_name = c.constraint_name information_schema.constraint_column_usage cu ON cu.constraint_name = c.constraint_name
WHERE WHERE
c.table_catalog = current_database() AND c.table_schema = 'public' AND c.table_name = $1 c.table_catalog = current_database() AND c.table_schema = 'public' AND c.table_name = $1
AND u.table_catalog = current_database() AND u.table_schema = 'public' AND u.table_name = $2`, AND u.table_catalog = current_database() AND u.table_schema = 'public' AND u.table_name = $2`,
table.Name, table.Name) // u.position_in_unique_constraint, table.Name, table.Name) // u.position_in_unique_constraint,
if err != nil { if err != nil {
@ -606,7 +606,7 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
column_default, column_default,
'' AS extra '' AS extra
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
table_catalog = current_database() AND table_schema = 'public' AND table_name = $1`, table_catalog = current_database() AND table_schema = 'public' AND table_name = $1`,
table.Name) table.Name)
@ -985,6 +985,12 @@ func getPackagePath(curpath string) (packpath string) {
ColorLog("[ERRO] Can't generate application code outside of GOPATH '%s'\n", gopath) ColorLog("[ERRO] Can't generate application code outside of GOPATH '%s'\n", gopath)
os.Exit(2) os.Exit(2)
} }
if curpath == appsrcpath {
ColorLog("[ERRO] Can't generate application code outside of application PATH \n")
os.Exit(2)
}
packpath = strings.Join(strings.Split(curpath[len(appsrcpath)+1:], string(filepath.Separator)), "/") packpath = strings.Join(strings.Split(curpath[len(appsrcpath)+1:], string(filepath.Separator)), "/")
return return
} }
@ -1167,14 +1173,18 @@ func (c *{{ctrlName}}Controller) URLMapping() {
// @Title Post // @Title Post
// @Description create {{ctrlName}} // @Description create {{ctrlName}}
// @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content"
// @Success 200 {int} models.{{ctrlName}}.Id // @Success 201 {int} models.{{ctrlName}}
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (c *{{ctrlName}}Controller) Post() { func (c *{{ctrlName}}Controller) Post() {
var v models.{{ctrlName}} var v models.{{ctrlName}}
json.Unmarshal(c.Ctx.Input.RequestBody, &v) if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if id, err := models.Add{{ctrlName}}(&v); err == nil { if _, err := models.Add{{ctrlName}}(&v); err == nil {
c.Data["json"] = map[string]int64{"id": id} c.Ctx.Output.SetStatus(201)
c.Data["json"] = v
} else {
c.Data["json"] = err.Error()
}
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
@ -1208,7 +1218,7 @@ func (c *{{ctrlName}}Controller) GetOne() {
// @Param limit query string false "Limit the size of result set. Must be an integer" // @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer" // @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.{{ctrlName}} // @Success 200 {object} models.{{ctrlName}}
// @Failure 403 // @Failure 403
// @router / [get] // @router / [get]
func (c *{{ctrlName}}Controller) GetAll() { func (c *{{ctrlName}}Controller) GetAll() {
var fields []string var fields []string
@ -1272,9 +1282,12 @@ func (c *{{ctrlName}}Controller) Put() {
idStr := c.Ctx.Input.Params[":id"] idStr := c.Ctx.Input.Params[":id"]
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v := models.{{ctrlName}}{Id: id} v := models.{{ctrlName}}{Id: id}
json.Unmarshal(c.Ctx.Input.RequestBody, &v) if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.Update{{ctrlName}}ById(&v); err == nil { if err := models.Update{{ctrlName}}ById(&v); err == nil {
c.Data["json"] = "OK" c.Data["json"] = "OK"
} else {
c.Data["json"] = err.Error()
}
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }

View File

@ -88,7 +88,7 @@ func (c *{{controllerName}}Controller) URLMapping() {
// @Title Post // @Title Post
// @Description create {{controllerName}} // @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
// @Success 200 {int} models.{{controllerName}}.Id // @Success 201 {object} models.{{controllerName}}
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (c *{{controllerName}}Controller) Post() { func (c *{{controllerName}}Controller) Post() {
@ -128,7 +128,7 @@ func (c *{{controllerName}}Controller) GetAll() {
// @Failure 403 :id is not int // @Failure 403 :id is not int
// @router /:id [put] // @router /:id [put]
func (c *{{controllerName}}Controller) Put() { func (c *{{controllerName}}Controller) Put() {
} }
// @Title Delete // @Title Delete
@ -138,7 +138,7 @@ func (c *{{controllerName}}Controller) Put() {
// @Failure 403 id is empty // @Failure 403 id is empty
// @router /:id [delete] // @router /:id [delete]
func (c *{{controllerName}}Controller) Delete() { func (c *{{controllerName}}Controller) Delete() {
} }
` `
@ -170,14 +170,15 @@ func (c *{{controllerName}}Controller) URLMapping() {
// @Title Post // @Title Post
// @Description create {{controllerName}} // @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
// @Success 200 {int} models.{{controllerName}}.Id // @Success 201 {int} models.{{controllerName}}
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (c *{{controllerName}}Controller) Post() { func (c *{{controllerName}}Controller) Post() {
var v models.{{controllerName}} var v models.{{controllerName}}
json.Unmarshal(c.Ctx.Input.RequestBody, &v) json.Unmarshal(c.Ctx.Input.RequestBody, &v)
if id, err := models.Add{{controllerName}}(&v); err == nil { if _, err := models.Add{{controllerName}}(&v); err == nil {
c.Data["json"] = map[string]int64{"id": id} c.Ctx.Output.SetStatus(201)
c.Data["json"] = v
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
@ -211,7 +212,7 @@ func (c *{{controllerName}}Controller) GetOne() {
// @Param limit query string false "Limit the size of result set. Must be an integer" // @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer" // @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.{{controllerName}} // @Success 200 {object} models.{{controllerName}}
// @Failure 403 // @Failure 403
// @router / [get] // @router / [get]
func (c *{{controllerName}}Controller) GetAll() { func (c *{{controllerName}}Controller) GetAll() {
var fields []string var fields []string

View File

@ -455,7 +455,7 @@ func packDirectory(excludePrefix []string, excludeSuffix []string,
func isBeegoProject(thePath string) bool { func isBeegoProject(thePath string) bool {
fh, _ := os.Open(thePath) fh, _ := os.Open(thePath)
fis, _ := fh.Readdir(-1) fis, _ := fh.Readdir(-1)
regex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?"github.com/astaxie/beego".*?\).*func main()`) regex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?github.com/astaxie/beego".*?\).*func main()`)
for _, fi := range fis { for _, fi := range fis {
if fi.IsDir() == false && strings.HasSuffix(fi.Name(), ".go") { if fi.IsDir() == false && strings.HasSuffix(fi.Name(), ".go") {
data, err := ioutil.ReadFile(path.Join(thePath, fi.Name())) data, err := ioutil.ReadFile(path.Join(thePath, fi.Name()))