diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 35e392caa..d9a7813ec 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -265,6 +265,7 @@ type ( ) const ( + defaultModelSafe = false queryTypeNormal = 0 queryTypeCount = 1 unionTypeNormal = 0 diff --git a/database/gdb/gdb_model.go b/database/gdb/gdb_model.go index 717ab6e01..1398c3454 100644 --- a/database/gdb/gdb_model.go +++ b/database/gdb/gdb_model.go @@ -132,7 +132,7 @@ func (c *Core) Model(tableNameQueryOrStruct ...interface{}) *Model { tableStr = c.QuotePrefixTableName(tableNames[0]) } } - return &Model{ + m := &Model{ db: c.db, tablesInit: tableStr, tables: tableStr, @@ -142,6 +142,10 @@ func (c *Core) Model(tableNameQueryOrStruct ...interface{}) *Model { filter: true, extraArgs: extraArgs, } + if defaultModelSafe { + m.safe = true + } + return m } // Raw creates and returns a model based on a raw sql not a table. @@ -154,7 +158,7 @@ func (c *Core) Raw(rawSql string, args ...interface{}) *Model { return model } -// Raw creates and returns a model based on a raw sql not a table. +// Raw sets current model as a raw sql model. // Example: // db.Raw("SELECT * FROM `user` WHERE `name` = ?", "john").Scan(&result) // See Core.Raw. @@ -169,7 +173,7 @@ func (tx *TX) Raw(rawSql string, args ...interface{}) *Model { return tx.Model().Raw(rawSql, args...) } -// With creates and returns an ORM model based on meta data of given object. +// With creates and returns an ORM model based on metadata of given object. func (c *Core) With(objects ...interface{}) *Model { return c.db.Model().With(objects...) } diff --git a/database/gdb/gdb_model_condition.go b/database/gdb/gdb_model_condition.go index 9a6d4ac50..bcf73bb9c 100644 --- a/database/gdb/gdb_model_condition.go +++ b/database/gdb/gdb_model_condition.go @@ -236,16 +236,6 @@ func (m *Model) WhereOrNotNull(columns ...string) *Model { return model } -// Group sets the "GROUP BY" statement for the model. -func (m *Model) Group(groupBy ...string) *Model { - if len(groupBy) > 0 { - model := m.getModel() - model.groupBy = m.db.GetCore().QuoteString(gstr.Join(groupBy, ",")) - return model - } - return m -} - // And adds "AND" condition to the where statement. // Deprecated, use Where instead. func (m *Model) And(where interface{}, args ...interface{}) *Model { @@ -267,11 +257,17 @@ func (m *Model) Or(where interface{}, args ...interface{}) *Model { return m.WhereOr(where, args...) } -// GroupBy is alias of Model.Group. -// See Model.Group. -// Deprecated, use Group instead. -func (m *Model) GroupBy(groupBy string) *Model { - return m.Group(groupBy) +// Group sets the "GROUP BY" statement for the model. +func (m *Model) Group(groupBy ...string) *Model { + if len(groupBy) == 0 { + return m + } + model := m.getModel() + if model.groupBy != "" { + model.groupBy += "," + } + model.groupBy = model.db.GetCore().QuoteString(strings.Join(groupBy, ",")) + return model } // Order sets the "ORDER BY" statement for the model. @@ -283,7 +279,7 @@ func (m *Model) Order(orderBy ...string) *Model { if model.orderBy != "" { model.orderBy += "," } - model.orderBy = m.db.GetCore().QuoteString(strings.Join(orderBy, " ")) + model.orderBy = model.db.GetCore().QuoteString(strings.Join(orderBy, " ")) return model } @@ -292,12 +288,7 @@ func (m *Model) OrderAsc(column string) *Model { if len(column) == 0 { return m } - model := m.getModel() - if model.orderBy != "" { - model.orderBy += "," - } - model.orderBy = m.db.GetCore().QuoteWord(column) + " ASC" - return model + return m.Order(column + " ASC") } // OrderDesc sets the "ORDER BY xxx DESC" statement for the model. @@ -305,12 +296,7 @@ func (m *Model) OrderDesc(column string) *Model { if len(column) == 0 { return m } - model := m.getModel() - if model.orderBy != "" { - model.orderBy += "," - } - model.orderBy = m.db.GetCore().QuoteWord(column) + " DESC" - return model + return m.Order(column + " DESC") } // OrderRandom sets the "ORDER BY RANDOM()" statement for the model. @@ -320,13 +306,6 @@ func (m *Model) OrderRandom() *Model { return model } -// OrderBy is alias of Model.Order. -// See Model.Order. -// Deprecated, use Order instead. -func (m *Model) OrderBy(orderBy string) *Model { - return m.Order(orderBy) -} - // Limit sets the "LIMIT" statement for the model. // The parameter `limit` can be either one or two number, if passed two number is passed, // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]" diff --git a/database/gdb/gdb_z_mysql_method_test.go b/database/gdb/gdb_z_mysql_method_test.go index b571a1da3..c3e7f7aa5 100644 --- a/database/gdb/gdb_z_mysql_method_test.go +++ b/database/gdb/gdb_z_mysql_method_test.go @@ -1313,14 +1313,14 @@ func Test_Model_InnerJoin(t *testing.T) { t.Assert(n, 5) - result, err := db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").OrderBy("u1.id").Select() + result, err := db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").Order("u1.id").Select() if err != nil { t.Fatal(err) } t.Assert(len(result), 5) - result, err = db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ?", 1).OrderBy("u1.id").Select() + result, err = db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ?", 1).Order("u1.id").Select() if err != nil { t.Fatal(err) } diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index f9f95fcef..20e92bf9c 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -435,6 +435,7 @@ func Test_Model_Clone(t *testing.T) { table := createInitTable() defer dropTable(table) + db.SetDebug(true) gtest.C(t, func(t *gtest.T) { md := db.Model(table).Where("id IN(?)", g.Slice{1, 3}) count, err := md.Count() @@ -1225,7 +1226,7 @@ func Test_Model_GroupBy(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { - result, err := db.Model(table).GroupBy("id").Select() + result, err := db.Model(table).Group("id").All() t.AssertNil(err) t.Assert(len(result), TableSize) t.Assert(result[0]["nickname"].String(), "name_1") diff --git a/net/ghttp/ghttp_request_param_post.go b/net/ghttp/ghttp_request_param_post.go deleted file mode 100644 index 8ddfa17ab..000000000 --- a/net/ghttp/ghttp_request_param_post.go +++ /dev/null @@ -1,215 +0,0 @@ -// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package ghttp - -import ( - "github.com/gogf/gf/container/gvar" - "github.com/gogf/gf/util/gconv" -) - -// GetPost retrieves and returns parameter from form and body. -// It returns if does not exist in neither form nor body. -// It returns nil if is not passed. -// -// Note that if there're multiple parameters with the same name, the parameters are retrieved -// and overwrote in order of priority: form > body. -// -// Deprecated, use GetForm instead. -func (r *Request) GetPost(key string, def ...interface{}) interface{} { - r.parseForm() - if len(r.formMap) > 0 { - if v, ok := r.formMap[key]; ok { - return v - } - } - r.parseBody() - if len(r.bodyMap) > 0 { - if v, ok := r.bodyMap[key]; ok { - return v - } - } - if len(def) > 0 { - return def[0] - } - return nil -} - -// Deprecated, use GetFormVar instead. -func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var { - return gvar.New(r.GetPost(key, def...)) -} - -// Deprecated, use GetFormString instead. -func (r *Request) GetPostString(key string, def ...interface{}) string { - return r.GetPostVar(key, def...).String() -} - -// Deprecated, use GetFormBool instead. -func (r *Request) GetPostBool(key string, def ...interface{}) bool { - return r.GetPostVar(key, def...).Bool() -} - -// Deprecated, use GetFormInt instead. -func (r *Request) GetPostInt(key string, def ...interface{}) int { - return r.GetPostVar(key, def...).Int() -} - -// Deprecated, use GetFormInt32 instead. -func (r *Request) GetPostInt32(key string, def ...interface{}) int32 { - return r.GetPostVar(key, def...).Int32() -} - -// Deprecated, use GetFormInt64 instead. -func (r *Request) GetPostInt64(key string, def ...interface{}) int64 { - return r.GetPostVar(key, def...).Int64() -} - -// Deprecated, use GetFormInts instead. -func (r *Request) GetPostInts(key string, def ...interface{}) []int { - return r.GetPostVar(key, def...).Ints() -} - -// Deprecated, use GetFormUint instead. -func (r *Request) GetPostUint(key string, def ...interface{}) uint { - return r.GetPostVar(key, def...).Uint() -} - -// Deprecated, use GetFormUint32 instead. -func (r *Request) GetPostUint32(key string, def ...interface{}) uint32 { - return r.GetPostVar(key, def...).Uint32() -} - -// Deprecated, use GetFormUint64 instead. -func (r *Request) GetPostUint64(key string, def ...interface{}) uint64 { - return r.GetPostVar(key, def...).Uint64() -} - -// Deprecated, use GetFormFloat32 instead. -func (r *Request) GetPostFloat32(key string, def ...interface{}) float32 { - return r.GetPostVar(key, def...).Float32() -} - -// Deprecated, use GetFormFloat64 instead. -func (r *Request) GetPostFloat64(key string, def ...interface{}) float64 { - return r.GetPostVar(key, def...).Float64() -} - -// Deprecated, use GetFormFloats instead. -func (r *Request) GetPostFloats(key string, def ...interface{}) []float64 { - return r.GetPostVar(key, def...).Floats() -} - -// Deprecated, use GetFormArray instead. -func (r *Request) GetPostArray(key string, def ...interface{}) []string { - return r.GetPostVar(key, def...).Strings() -} - -// Deprecated, use GetFormStrings instead. -func (r *Request) GetPostStrings(key string, def ...interface{}) []string { - return r.GetPostVar(key, def...).Strings() -} - -// Deprecated, use GetFormInterfaces instead. -func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{} { - return r.GetPostVar(key, def...).Interfaces() -} - -// GetPostMap retrieves and returns all parameters in the form and body passed from client -// as map. The parameter specifies the keys retrieving from client parameters, -// the associated values are the default values if the client does not pass. -// -// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote -// in order of priority: form > body. -// -// Deprecated. -func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interface{} { - r.parseForm() - r.parseBody() - var ok, filter bool - if len(kvMap) > 0 && kvMap[0] != nil { - filter = true - } - m := make(map[string]interface{}, len(r.formMap)+len(r.bodyMap)) - for k, v := range r.bodyMap { - if filter { - if _, ok = kvMap[0][k]; !ok { - continue - } - } - m[k] = v - } - for k, v := range r.formMap { - if filter { - if _, ok = kvMap[0][k]; !ok { - continue - } - } - m[k] = v - } - // Check none exist parameters and assign it with default value. - if filter { - for k, v := range kvMap[0] { - if _, ok = m[k]; !ok { - m[k] = v - } - } - } - return m -} - -// GetPostMapStrStr retrieves and returns all parameters in the form and body passed from client -// as map[string]string. The parameter specifies the keys -// retrieving from client parameters, the associated values are the default values if the client -// does not pass. -// -// Deprecated. -func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]string { - postMap := r.GetPostMap(kvMap...) - if len(postMap) > 0 { - m := make(map[string]string, len(postMap)) - for k, v := range postMap { - m[k] = gconv.String(v) - } - return m - } - return nil -} - -// GetPostMapStrVar retrieves and returns all parameters in the form and body passed from client -// as map[string]*gvar.Var. The parameter specifies the keys -// retrieving from client parameters, the associated values are the default values if the client -// does not pass. -// -// Deprecated. -func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { - postMap := r.GetPostMap(kvMap...) - if len(postMap) > 0 { - m := make(map[string]*gvar.Var, len(postMap)) - for k, v := range postMap { - m[k] = gvar.New(v) - } - return m - } - return nil -} - -// GetPostStruct retrieves all parameters in the form and body passed from client -// and converts them to given struct object. Note that the parameter is a pointer -// to the struct object. The optional parameter is used to specify the key to -// attribute mapping. -// -// Deprecated. -func (r *Request) GetPostStruct(pointer interface{}, mapping ...map[string]string) error { - return gconv.Struct(r.GetPostMap(), pointer, mapping...) -} - -// GetPostToStruct is alias of GetQueryStruct. See GetPostStruct. -// -// Deprecated. -func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error { - return r.GetPostStruct(pointer, mapping...) -} diff --git a/net/ghttp/ghttp_request_param_query.go b/net/ghttp/ghttp_request_param_query.go index b108b4b3c..13c8624dc 100644 --- a/net/ghttp/ghttp_request_param_query.go +++ b/net/ghttp/ghttp_request_param_query.go @@ -25,7 +25,7 @@ func (r *Request) SetQuery(key string, value interface{}) { // and request body. It returns if does not exist in the query and is given, // or else it returns nil. // -// Note that if there're multiple parameters with the same name, the parameters are retrieved +// Note that if there are multiple parameters with the same name, the parameters are retrieved // and overwrote in order of priority: query > body. func (r *Request) GetQuery(key string, def ...interface{}) interface{} { r.parseQuery() @@ -116,7 +116,7 @@ func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface // as map. The parameter specifies the keys retrieving from client parameters, // the associated values are the default values if the client does not pass. // -// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote +// Note that if there are multiple parameters with the same name, the parameters are retrieved and overwrote // in order of priority: query > body. func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} { r.parseQuery() diff --git a/net/ghttp/ghttp_request_param_request.go b/net/ghttp/ghttp_request_param_request.go index 2e62a1992..8b4cc13d8 100644 --- a/net/ghttp/ghttp_request_param_request.go +++ b/net/ghttp/ghttp_request_param_request.go @@ -20,7 +20,7 @@ import ( // // GetRequest is one of the most commonly used functions for retrieving parameters. // -// Note that if there're multiple parameters with the same name, the parameters are +// Note that if there are multiple parameters with the same name, the parameters are // retrieved and overwrote in order of priority: router < query < body < form < custom. func (r *Request) GetRequest(key string, def ...interface{}) interface{} { value := r.GetParam(key) @@ -167,7 +167,7 @@ func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interfa // // GetRequestMap is one of the most commonly used functions for retrieving parameters. // -// Note that if there're multiple parameters with the same name, the parameters are retrieved +// Note that if there are multiple parameters with the same name, the parameters are retrieved // and overwrote in order of priority: router < query < body < form < custom. func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} { r.parseQuery() diff --git a/net/ghttp/ghttp_unit_request_test.go b/net/ghttp/ghttp_unit_request_test.go index 96ba9a375..cc8de827c 100644 --- a/net/ghttp/ghttp_unit_request_test.go +++ b/net/ghttp/ghttp_unit_request_test.go @@ -95,39 +95,7 @@ func Test_Params_Basic(t *testing.T) { r.Response.Write(r.GetMapStrStr()["a"]) } }) - // POST - s.BindHandler("/post", func(r *ghttp.Request) { - if r.GetPost("array") != nil { - r.Response.Write(r.GetPost("array")) - } - if r.GetPost("slice") != nil { - r.Response.Write(r.GetPost("slice")) - } - if r.GetPost("bool") != nil { - r.Response.Write(r.GetPostBool("bool")) - } - if r.GetPost("float32") != nil { - r.Response.Write(r.GetPostFloat32("float32")) - } - if r.GetPost("float64") != nil { - r.Response.Write(r.GetPostFloat64("float64")) - } - if r.GetPost("int") != nil { - r.Response.Write(r.GetPostInt("int")) - } - if r.GetPost("uint") != nil { - r.Response.Write(r.GetPostUint("uint")) - } - if r.GetPost("string") != nil { - r.Response.Write(r.GetPostString("string")) - } - if r.GetPost("map") != nil { - r.Response.Write(r.GetPostMap()["map"].(map[string]interface{})["b"]) - } - if r.GetPost("a") != nil { - r.Response.Write(r.GetPostMapStrStr()["a"]) - } - }) + // DELETE s.BindHandler("/delete", func(r *ghttp.Request) { if r.Get("array") != nil { @@ -330,21 +298,6 @@ func Test_Params_Basic(t *testing.T) { t.Assert(client.PutContent("/put", "map[a]=1&map[b]=2"), `2`) t.Assert(client.PutContent("/put", "a=1&b=2"), `1`) - // POST - t.Assert(client.PostContent("/post", "array[]=1&array[]=2"), `["1","2"]`) - t.Assert(client.PostContent("/post", "slice=1&slice=2"), `2`) - t.Assert(client.PostContent("/post", "bool=1"), `true`) - t.Assert(client.PostContent("/post", "bool=0"), `false`) - t.Assert(client.PostContent("/post", "float32=0.11"), `0.11`) - t.Assert(client.PostContent("/post", "float64=0.22"), `0.22`) - t.Assert(client.PostContent("/post", "int=-10000"), `-10000`) - t.Assert(client.PostContent("/post", "int=10000"), `10000`) - t.Assert(client.PostContent("/post", "uint=10000"), `10000`) - t.Assert(client.PostContent("/post", "uint=9"), `9`) - t.Assert(client.PostContent("/post", "string=key"), `key`) - t.Assert(client.PostContent("/post", "map[a]=1&map[b]=2"), `2`) - t.Assert(client.PostContent("/post", "a=1&b=2"), `1`) - // DELETE t.Assert(client.DeleteContent("/delete", "array[]=1&array[]=2"), `["1","2"]`) t.Assert(client.DeleteContent("/delete", "slice=1&slice=2"), `2`) @@ -458,9 +411,6 @@ func Test_Params_Priority(t *testing.T) { s.BindHandler("/query", func(r *ghttp.Request) { r.Response.Write(r.GetQuery("a")) }) - s.BindHandler("/post", func(r *ghttp.Request) { - r.Response.Write(r.GetPost("a")) - }) s.BindHandler("/form", func(r *ghttp.Request) { r.Response.Write(r.GetForm("a")) }) @@ -485,7 +435,6 @@ func Test_Params_Priority(t *testing.T) { client.SetPrefix(prefix) t.Assert(client.GetContent("/query?a=1", "a=100"), "100") - t.Assert(client.PostContent("/post?a=1", "a=100"), "100") t.Assert(client.PostContent("/form?a=1", "a=100"), "100") t.Assert(client.PutContent("/form?a=1", "a=100"), "100") t.Assert(client.GetContent("/request?a=1", "a=100"), "100")