mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve OmitEmpty feature for package gdb; mark package gmvc deprecated
This commit is contained in:
@ -61,8 +61,6 @@ type whereHolder struct {
|
||||
}
|
||||
|
||||
const (
|
||||
OptionOmitEmpty = 1
|
||||
OptionAllowEmpty = 2
|
||||
linkTypeMaster = 1
|
||||
linkTypeSlave = 2
|
||||
whereHolderWhere = 1
|
||||
@ -128,7 +126,6 @@ func (c *Core) Model(tableNameQueryOrStruct ...interface{}) *Model {
|
||||
fields: "*",
|
||||
start: -1,
|
||||
offset: -1,
|
||||
option: OptionAllowEmpty,
|
||||
filter: true,
|
||||
extraArgs: extraArgs,
|
||||
}
|
||||
|
||||
@ -386,7 +386,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
|
||||
case whereHolderWhere:
|
||||
if conditionWhere == "" {
|
||||
newWhere, newArgs := formatWhere(
|
||||
m.db, v.where, v.args, m.option&OptionOmitEmpty > 0, m.schema, m.tables,
|
||||
m.db, v.where, v.args, m.option&optionOmitEmptyWhere > 0, m.schema, m.tables,
|
||||
)
|
||||
if len(newWhere) > 0 {
|
||||
conditionWhere = newWhere
|
||||
@ -398,7 +398,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
|
||||
|
||||
case whereHolderAnd:
|
||||
newWhere, newArgs := formatWhere(
|
||||
m.db, v.where, v.args, m.option&OptionOmitEmpty > 0, m.schema, m.tables,
|
||||
m.db, v.where, v.args, m.option&optionOmitEmptyWhere > 0, m.schema, m.tables,
|
||||
)
|
||||
if len(newWhere) > 0 {
|
||||
if len(conditionWhere) == 0 {
|
||||
@ -413,7 +413,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
|
||||
|
||||
case whereHolderOr:
|
||||
newWhere, newArgs := formatWhere(
|
||||
m.db, v.where, v.args, m.option&OptionOmitEmpty > 0, m.schema, m.tables,
|
||||
m.db, v.where, v.args, m.option&optionOmitEmptyWhere > 0, m.schema, m.tables,
|
||||
)
|
||||
if len(newWhere) > 0 {
|
||||
if len(conditionWhere) == 0 {
|
||||
@ -455,7 +455,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
|
||||
// HAVING.
|
||||
if len(m.having) > 0 {
|
||||
havingStr, havingArgs := formatWhere(
|
||||
m.db, m.having[0], gconv.Interfaces(m.having[1]), m.option&OptionOmitEmpty > 0, m.schema, m.tables,
|
||||
m.db, m.having[0], gconv.Interfaces(m.having[1]), m.option&optionOmitEmptyWhere > 0, m.schema, m.tables,
|
||||
)
|
||||
if len(havingStr) > 0 {
|
||||
conditionExtra += " HAVING " + havingStr
|
||||
|
||||
@ -6,22 +6,40 @@
|
||||
|
||||
package gdb
|
||||
|
||||
const (
|
||||
optionOmitEmpty = optionOmitEmptyWhere | optionOmitEmptyData
|
||||
optionOmitEmptyWhere = 1 << iota // 8
|
||||
optionOmitEmptyData // 16
|
||||
)
|
||||
|
||||
// Option adds extra operation option for the model.
|
||||
// Deprecated, use separate operations instead.
|
||||
func (m *Model) Option(option int) *Model {
|
||||
model := m.getModel()
|
||||
model.option = model.option | option
|
||||
return model
|
||||
}
|
||||
|
||||
// OptionOmitEmpty sets OptionOmitEmpty option for the model, which automatically filers
|
||||
// the data and where attributes for empty values.
|
||||
// Deprecated, use OmitEmpty instead.
|
||||
func (m *Model) OptionOmitEmpty() *Model {
|
||||
return m.Option(OptionOmitEmpty)
|
||||
// OmitEmpty sets OmitEmpty option for the model, which automatically filers
|
||||
// the data and where parameters for `empty` values.
|
||||
func (m *Model) OmitEmpty() *Model {
|
||||
model := m.getModel()
|
||||
model.option = model.option | optionOmitEmpty
|
||||
return model
|
||||
}
|
||||
|
||||
// OmitEmpty sets OptionOmitEmpty option for the model, which automatically filers
|
||||
// the data and where attributes for empty values.
|
||||
func (m *Model) OmitEmpty() *Model {
|
||||
return m.Option(OptionOmitEmpty)
|
||||
// OmitEmptyWhere sets OmitEmptyWhere option for the model, which automatically filers
|
||||
// the Where/Having parameters for `empty` values.
|
||||
func (m *Model) OmitEmptyWhere() *Model {
|
||||
model := m.getModel()
|
||||
model.option = model.option | optionOmitEmptyWhere
|
||||
return model
|
||||
}
|
||||
|
||||
// OmitEmptyData sets OmitEmptyData option for the model, which automatically filers
|
||||
// the Data parameters for `empty` values.
|
||||
func (m *Model) OmitEmptyData() *Model {
|
||||
model := m.getModel()
|
||||
model.option = model.option | optionOmitEmptyData
|
||||
return model
|
||||
}
|
||||
|
||||
@ -110,7 +110,7 @@ func (m *Model) doMappingAndFilterForInsertOrUpdateDataMap(data Map, allowOmitEm
|
||||
return nil, err
|
||||
}
|
||||
// Remove key-value pairs of which the value is empty.
|
||||
if allowOmitEmpty && m.option&OptionOmitEmpty > 0 {
|
||||
if allowOmitEmpty && m.option&optionOmitEmptyData > 0 {
|
||||
tempMap := make(Map, len(data))
|
||||
for k, v := range data {
|
||||
if empty.IsEmpty(v) {
|
||||
|
||||
@ -1938,7 +1938,7 @@ func Test_Model_Option_Map(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
r, err := db.Model(table).Option(gdb.OptionOmitEmpty).Data(g.Map{
|
||||
r, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"id": 1,
|
||||
"passport": 0,
|
||||
"password": 0,
|
||||
@ -1958,7 +1958,7 @@ func Test_Model_Option_Map(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Model(table).Option(gdb.OptionOmitEmpty).Data(g.Map{
|
||||
_, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"id": 1,
|
||||
"passport": 0,
|
||||
"password": 0,
|
||||
@ -1994,7 +1994,7 @@ func Test_Model_Option_Map(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Model(table).Option(gdb.OptionOmitEmpty).Data(g.Map{
|
||||
_, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"id": 1,
|
||||
"passport": 0,
|
||||
"password": 0,
|
||||
@ -2031,7 +2031,7 @@ func Test_Model_Option_Map(t *testing.T) {
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
_, err = db.Model(table).Option(gdb.OptionOmitEmpty).Data(g.Map{"nickname": ""}).Where("id", 2).Update()
|
||||
_, err = db.Model(table).OmitEmptyData().Data(g.Map{"nickname": ""}).Where("id", 2).Update()
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
r, err = db.Model(table).OmitEmpty().Data(g.Map{"nickname": "", "password": "123"}).Where("id", 3).Update()
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gmvc provides basic object classes for MVC.
|
||||
// Deprecated, no longer suggested.
|
||||
package gmvc
|
||||
|
||||
import (
|
||||
@ -12,6 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// Controller is used for controller register of ghttp.Server.
|
||||
// Deprecated, no longer suggested.
|
||||
type Controller struct {
|
||||
Request *ghttp.Request
|
||||
Response *ghttp.Response
|
||||
|
||||
@ -9,6 +9,11 @@ package gmvc
|
||||
import "github.com/gogf/gf/database/gdb"
|
||||
|
||||
type (
|
||||
M = Model // M is alias for Model, just for short write purpose.
|
||||
Model = *gdb.Model // Model is alias for *gdb.Model.
|
||||
// M is alias for Model, just for short write purpose.
|
||||
// Deprecated, no longer suggested.
|
||||
M = *gdb.Model
|
||||
|
||||
// Model is alias for *gdb.Model.
|
||||
// Deprecated, no longer suggested.
|
||||
Model = *gdb.Model
|
||||
)
|
||||
|
||||
@ -19,6 +19,7 @@ import (
|
||||
// View is the view object for controller.
|
||||
// It's initialized when controller request initializes and destroyed
|
||||
// when the controller request closes.
|
||||
// Deprecated, no longer suggested.
|
||||
type View struct {
|
||||
mu sync.RWMutex
|
||||
view *gview.View
|
||||
@ -27,6 +28,7 @@ type View struct {
|
||||
}
|
||||
|
||||
// NewView creates and returns a controller view object.
|
||||
// Deprecated, no longer suggested.
|
||||
func NewView(w *ghttp.Response) *View {
|
||||
return &View{
|
||||
view: gins.View(),
|
||||
@ -76,7 +78,7 @@ func (view *View) LockFunc(f func(data gview.Params)) {
|
||||
f(view.data)
|
||||
}
|
||||
|
||||
// LockFunc locks reading for template variables by callback function <f>.
|
||||
// RLockFunc locks reading for template variables by callback function <f>.
|
||||
func (view *View) RLockFunc(f func(data gview.Params)) {
|
||||
view.mu.RLock()
|
||||
defer view.mu.RUnlock()
|
||||
|
||||
Reference in New Issue
Block a user