From fcc359af11650ff3c523ece170f9ec134ffa8aef Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 3 Sep 2014 09:25:34 +0800 Subject: [PATCH 1/7] beego: fix the Upper for the _method value --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index 21aa4724..7c52b9fc 100644 --- a/router.go +++ b/router.go @@ -705,7 +705,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) if EnableXSRF { execController.XsrfToken() if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" || - (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) { + (r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) { execController.CheckXsrfCookie() } } From 9c07332cfcd729cba80eaa9cab4abc6dbde5e314 Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 9 Oct 2014 09:34:22 +0800 Subject: [PATCH 2/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 85fdc690..0d7414b8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## Beego [![Build Status](https://drone.io/github.com/astaxie/beego/status.png)](https://drone.io/github.com/astaxie/beego/latest) -[![GoDoc](https://godoc.org/github.com/astaxie/beego?status.svg)](https://godoc.org/github.com/astaxie/beego) +[![GoDoc](http://godoc.org/github.com/astaxie/beego?status.svg)](http://godoc.org/github.com/astaxie/beego) beego is an open-source, high-performance, modularity, full-stack web framework. From e4988b714ec3f3fc7c960a5253f0e63625137eba Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 17 Oct 2014 12:27:53 +0400 Subject: [PATCH 3/7] Add property colDefault to fieldInfo object, set its true if there is orm configuration default `orm:"default(1)"` --- orm/models_info_f.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/orm/models_info_f.go b/orm/models_info_f.go index a79ffab2..84a0c024 100644 --- a/orm/models_info_f.go +++ b/orm/models_info_f.go @@ -116,6 +116,7 @@ type fieldInfo struct { null bool index bool unique bool + colDefault bool initial StrTo size int auto_now bool @@ -280,6 +281,11 @@ checkType: fi.pk = attrs["pk"] fi.unique = attrs["unique"] + // Mark object property if there is attribute "default" in the orm configuration + if _, ok := tags["default"]; ok { + fi.colDefault = true + } + switch fieldType { case RelManyToMany, RelReverseMany, RelReverseOne: fi.null = false From 4cae7af3f93b47807cfc3b7bd17562873f31f030 Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 17 Oct 2014 12:53:59 +0400 Subject: [PATCH 4/7] Add attribute DEFAULT '' to the CREAT, ALTER constructors --- orm/cmd_utils.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 8304da6b..64f3f7c6 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -104,7 +104,11 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string { typ += " " + "NOT NULL" } - return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ) + return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s", + Q, fi.mi.table, Q, + Q, fi.column, Q, + typ, getColumnDefault(fi), + ) } // create database creation string. @@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex //if fi.initial.String() != "" { // column += " DEFAULT " + fi.initial.String() //} + + // Append attribute DEFAULT + column += getColumnDefault(fi) if fi.unique { column += " " + "UNIQUE" @@ -239,3 +246,42 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex return } + + +// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands +func getColumnDefault(fi *fieldInfo) string { + var ( + v, t, d string + ) + + t = " DEFAULT '%s' " + + // These defaults will be useful if there no config value orm:"default" and NOT NULL is on + switch fieldType { + case TypeDateField: + d = "0000-00-00" + + case TypeDateTimeField: + d = "0000-00-00 00:00:00" + + case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, + TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, + TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField: + TypeDecimalField: + d = "0" + } + + if fi.colDefault { + if !fi.initial.Exist() { + v = fmt.Strintf(t, "") + } else { + v = fmt.Strintf(t, fi.initial.String()) + } + } else { + if !fi.null { + v = fmt.Strintf(t, d) + } + } + + return v +} From b00c42b3df8e15b87039649aca091419bf01479e Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 17 Oct 2014 12:56:44 +0400 Subject: [PATCH 5/7] Fix undefind variable fieldType --- orm/cmd_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 64f3f7c6..3570949e 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -257,7 +257,7 @@ func getColumnDefault(fi *fieldInfo) string { t = " DEFAULT '%s' " // These defaults will be useful if there no config value orm:"default" and NOT NULL is on - switch fieldType { + switch fi.fieldType { case TypeDateField: d = "0000-00-00" From 04431a7a15e9905409912b6fae16fce65ff97a19 Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 17 Oct 2014 12:59:24 +0400 Subject: [PATCH 6/7] Fix function name fmt.Stprintf -> fmt.Sprintf --- orm/cmd_utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 3570949e..fbe9f1b2 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -273,13 +273,13 @@ func getColumnDefault(fi *fieldInfo) string { if fi.colDefault { if !fi.initial.Exist() { - v = fmt.Strintf(t, "") + v = fmt.Srintf(t, "") } else { - v = fmt.Strintf(t, fi.initial.String()) + v = fmt.Sprintf(t, fi.initial.String()) } } else { if !fi.null { - v = fmt.Strintf(t, d) + v = fmt.Sprintf(t, d) } } From ab28edaf258e4cf100cba5f114a4573ac80e6cb1 Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 17 Oct 2014 13:02:18 +0400 Subject: [PATCH 7/7] Fix comma in the switch, fix wronf function name --- orm/cmd_utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index fbe9f1b2..cabd957c 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -266,14 +266,14 @@ func getColumnDefault(fi *fieldInfo) string { case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, - TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField: + TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField, TypeDecimalField: d = "0" } if fi.colDefault { if !fi.initial.Exist() { - v = fmt.Srintf(t, "") + v = fmt.Sprintf(t, "") } else { v = fmt.Sprintf(t, fi.initial.String()) }