github.com/erda-project/erda-infra@v1.0.9/providers/mysql/v2/option.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v2
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"gorm.io/gorm"
    22  )
    23  
    24  var (
    25  	// DESC means select by DESC
    26  	DESC Order = "DESC"
    27  	// ASC means select by ASC
    28  	ASC Order = "ASC"
    29  )
    30  
    31  // Option is the function processes *gorm.DB
    32  type Option func(db *gorm.DB) *gorm.DB
    33  
    34  // Column is an interface for shortcut of WhereColumn, OrderColumn, SetColumn
    35  type Column interface {
    36  	WhereColumn
    37  	OrderColumn
    38  	SetColumn
    39  }
    40  
    41  // WhereColumn contains options for conditions
    42  type WhereColumn interface {
    43  	Is(value interface{}) Option
    44  	In(values []interface{}) Option
    45  	InMap(values map[interface{}]struct{}) Option
    46  	Like(value interface{}) Option
    47  	GreaterThan(value interface{}) Option
    48  	EqGreaterThan(value interface{}) Option
    49  	LessThan(value interface{}) Option
    50  	EqLessThan(value interface{}) Option
    51  }
    52  
    53  // OrderColumn contains order by options
    54  type OrderColumn interface {
    55  	DESC() Option
    56  	ASC() Option
    57  }
    58  
    59  // SetColumn is used in update statements
    60  type SetColumn interface {
    61  	Set(value interface{}) Option
    62  }
    63  
    64  // Where is the option for conditions
    65  func Where(format string, args ...interface{}) Option {
    66  	return func(db *gorm.DB) *gorm.DB {
    67  		return db.Where(format, args...)
    68  	}
    69  }
    70  
    71  // Wheres is the option for conditions.
    72  // the m can be a map or a struct.
    73  func Wheres(m interface{}) Option {
    74  	return func(db *gorm.DB) *gorm.DB {
    75  		return db.Where(m)
    76  	}
    77  }
    78  
    79  // Col returns a Column interface
    80  func Col(col string) Column {
    81  	return column{col: col}
    82  }
    83  
    84  type column struct {
    85  	col string
    86  }
    87  
    88  func (c column) Is(value interface{}) Option {
    89  	if value == nil {
    90  		return func(db *gorm.DB) *gorm.DB {
    91  			return db.Where(c.col + " IS NULL")
    92  		}
    93  	}
    94  	return func(db *gorm.DB) *gorm.DB {
    95  		return db.Where(c.col+" = ?", value)
    96  	}
    97  }
    98  
    99  func (c column) In(values []interface{}) Option {
   100  	return func(db *gorm.DB) *gorm.DB {
   101  		return db.Where(c.col+" IN ?", values)
   102  	}
   103  }
   104  
   105  func (c column) InMap(values map[interface{}]struct{}) Option {
   106  	var list []interface{}
   107  	for key := range values {
   108  		list = append(list, key)
   109  	}
   110  	return c.In(list)
   111  }
   112  
   113  func (c column) Like(value interface{}) Option {
   114  	return func(db *gorm.DB) *gorm.DB {
   115  		return db.Where(c.col+" LIKE ?", value)
   116  	}
   117  }
   118  
   119  func (c column) GreaterThan(value interface{}) Option {
   120  	return func(db *gorm.DB) *gorm.DB {
   121  		return db.Where(c.col+" > ?", value)
   122  	}
   123  }
   124  
   125  func (c column) EqGreaterThan(value interface{}) Option {
   126  	return func(db *gorm.DB) *gorm.DB {
   127  		return db.Where(c.col+" >= ?", value)
   128  	}
   129  }
   130  
   131  func (c column) LessThan(value interface{}) Option {
   132  	return func(db *gorm.DB) *gorm.DB {
   133  		return db.Where(c.col+" < ?", value)
   134  	}
   135  }
   136  
   137  func (c column) EqLessThan(value interface{}) Option {
   138  	return func(db *gorm.DB) *gorm.DB {
   139  		return db.Where(c.col+" <= ?", value)
   140  	}
   141  }
   142  
   143  func (c column) DESC() Option {
   144  	return func(db *gorm.DB) *gorm.DB {
   145  		return db.Order(c.col + " DESC")
   146  	}
   147  }
   148  
   149  func (c column) ASC() Option {
   150  	return func(db *gorm.DB) *gorm.DB {
   151  		return db.Order(c.col + " ASC")
   152  	}
   153  }
   154  
   155  func (c column) Set(value interface{}) Option {
   156  	return func(db *gorm.DB) *gorm.DB {
   157  		return db.Update(c.col, value)
   158  	}
   159  }
   160  
   161  // WhereValue contains the option presents where the value in
   162  type WhereValue interface {
   163  	In(cols ...string) Option
   164  }
   165  
   166  // Value .
   167  func Value(value interface{}) WhereValue {
   168  	return whereValue{value: value}
   169  }
   170  
   171  type whereValue struct {
   172  	value interface{}
   173  }
   174  
   175  func (w whereValue) In(cols ...string) Option {
   176  	return func(db *gorm.DB) *gorm.DB {
   177  		return db.Where(fmt.Sprintf("? IN (%s)", strings.Join(cols, ",")), w.value)
   178  	}
   179  }
   180  
   181  // Paging returns an Option for selecting by paging
   182  func Paging(size, no int) Option {
   183  	if size < 0 {
   184  		size = 0
   185  	}
   186  	if no < 1 {
   187  		no = 1
   188  	}
   189  	return func(db *gorm.DB) *gorm.DB {
   190  		return db.Limit(size).Offset((no - 1) * size)
   191  	}
   192  }
   193  
   194  // Order .
   195  type Order string
   196  
   197  // OrderBy returns an Option for selecting order by some column
   198  func OrderBy(col string, order Order) Option {
   199  	if !strings.EqualFold(string(order), string(DESC)) &&
   200  		!strings.EqualFold(string(order), string(ASC)) {
   201  		order = "DESC"
   202  	}
   203  	return func(db *gorm.DB) *gorm.DB {
   204  		return db.Order(col + " " + strings.ToUpper(string(order)))
   205  	}
   206  }