github.com/wangyougui/gf/v2@v2.6.5/database/gdb/gdb_model_option.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gdb
     8  
     9  const (
    10  	optionOmitNil             = optionOmitNilWhere | optionOmitNilData
    11  	optionOmitEmpty           = optionOmitEmptyWhere | optionOmitEmptyData
    12  	optionOmitNilDataInternal = optionOmitNilData | optionOmitNilDataList // this option is used internally only for ForDao feature.
    13  	optionOmitEmptyWhere      = 1 << iota                                 // 8
    14  	optionOmitEmptyData                                                   // 16
    15  	optionOmitNilWhere                                                    // 32
    16  	optionOmitNilData                                                     // 64
    17  	optionOmitNilDataList                                                 // 128
    18  )
    19  
    20  // OmitEmpty sets optionOmitEmpty option for the model, which automatically filers
    21  // the data and where parameters for `empty` values.
    22  func (m *Model) OmitEmpty() *Model {
    23  	model := m.getModel()
    24  	model.option = model.option | optionOmitEmpty
    25  	return model
    26  }
    27  
    28  // OmitEmptyWhere sets optionOmitEmptyWhere option for the model, which automatically filers
    29  // the Where/Having parameters for `empty` values.
    30  //
    31  // Eg:
    32  //
    33  //	Where("id", []int{}).All()             -> SELECT xxx FROM xxx WHERE 0=1
    34  //	Where("name", "").All()                -> SELECT xxx FROM xxx WHERE `name`=''
    35  //	OmitEmpty().Where("id", []int{}).All() -> SELECT xxx FROM xxx
    36  //	OmitEmpty().("name", "").All()         -> SELECT xxx FROM xxx.
    37  func (m *Model) OmitEmptyWhere() *Model {
    38  	model := m.getModel()
    39  	model.option = model.option | optionOmitEmptyWhere
    40  	return model
    41  }
    42  
    43  // OmitEmptyData sets optionOmitEmptyData option for the model, which automatically filers
    44  // the Data parameters for `empty` values.
    45  func (m *Model) OmitEmptyData() *Model {
    46  	model := m.getModel()
    47  	model.option = model.option | optionOmitEmptyData
    48  	return model
    49  }
    50  
    51  // OmitNil sets optionOmitNil option for the model, which automatically filers
    52  // the data and where parameters for `nil` values.
    53  func (m *Model) OmitNil() *Model {
    54  	model := m.getModel()
    55  	model.option = model.option | optionOmitNil
    56  	return model
    57  }
    58  
    59  // OmitNilWhere sets optionOmitNilWhere option for the model, which automatically filers
    60  // the Where/Having parameters for `nil` values.
    61  func (m *Model) OmitNilWhere() *Model {
    62  	model := m.getModel()
    63  	model.option = model.option | optionOmitNilWhere
    64  	return model
    65  }
    66  
    67  // OmitNilData sets optionOmitNilData option for the model, which automatically filers
    68  // the Data parameters for `nil` values.
    69  func (m *Model) OmitNilData() *Model {
    70  	model := m.getModel()
    71  	model.option = model.option | optionOmitNilData
    72  	return model
    73  }