github.com/RevenueMonster/sqlike@v1.0.6/sqlike/actions/find.go (about)

     1  package actions
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/RevenueMonster/sqlike/sql/expr"
     7  	"github.com/RevenueMonster/sqlike/sqlike/primitive"
     8  )
     9  
    10  // SelectStatement :
    11  type SelectStatement interface {
    12  	Distinct() SelectStatement
    13  	Select(fields ...interface{}) SelectStatement
    14  	From(values ...string) SelectStatement
    15  	Where(fields ...interface{}) SelectStatement
    16  	Having(fields ...interface{}) SelectStatement
    17  	GroupBy(fields ...interface{}) SelectStatement
    18  	OrderBy(fields ...interface{}) SelectStatement
    19  	Limit(num uint) SelectStatement
    20  	Offset(num uint) SelectStatement
    21  }
    22  
    23  // FindActions :
    24  type FindActions struct {
    25  	DistinctOn  bool
    26  	Database    string
    27  	Table       string
    28  	Projections []interface{}
    29  	IndexHints  string
    30  	Conditions  primitive.Group
    31  	Havings     primitive.Group
    32  	GroupBys    []interface{}
    33  	Sorts       []interface{}
    34  	Skip        uint
    35  	Count       uint
    36  }
    37  
    38  // Select :
    39  func (act *FindActions) Select(fields ...interface{}) SelectStatement {
    40  	act.Projections = fields
    41  	return act
    42  }
    43  
    44  // Distinct :
    45  func (act *FindActions) Distinct() SelectStatement {
    46  	act.DistinctOn = true
    47  	return act
    48  }
    49  
    50  // From :
    51  func (act *FindActions) From(values ...string) SelectStatement {
    52  	length := len(values)
    53  	if length == 0 {
    54  		panic("empty table name")
    55  	}
    56  	switch length {
    57  	case 1:
    58  		act.Table = strings.TrimSpace(values[0])
    59  	case 2:
    60  		act.Database = strings.TrimSpace(values[0])
    61  		act.Table = strings.TrimSpace(values[1])
    62  	case 3:
    63  		act.Database = strings.TrimSpace(values[0])
    64  		act.Table = strings.TrimSpace(values[1])
    65  		act.IndexHints = strings.TrimSpace(values[3])
    66  	default:
    67  		panic("invalid length of arguments")
    68  	}
    69  	return act
    70  }
    71  
    72  // Where :
    73  func (act *FindActions) Where(fields ...interface{}) SelectStatement {
    74  	act.Conditions = expr.And(fields...)
    75  	return act
    76  }
    77  
    78  // Having :
    79  func (act *FindActions) Having(fields ...interface{}) SelectStatement {
    80  	act.Havings = expr.And(fields...)
    81  	return act
    82  }
    83  
    84  // OrderBy :
    85  func (act *FindActions) OrderBy(fields ...interface{}) SelectStatement {
    86  	act.Sorts = fields
    87  	return act
    88  }
    89  
    90  // GroupBy :
    91  func (act *FindActions) GroupBy(fields ...interface{}) SelectStatement {
    92  	act.GroupBys = fields
    93  	return act
    94  }
    95  
    96  // Limit :
    97  func (act *FindActions) Limit(num uint) SelectStatement {
    98  	if num > 0 {
    99  		act.Count = num
   100  	}
   101  	return act
   102  }
   103  
   104  // Offset :
   105  func (act *FindActions) Offset(num uint) SelectStatement {
   106  	if num > 0 {
   107  		act.Skip = num
   108  	}
   109  	return act
   110  }