clause.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package client
  2. import "encoding/json"
  3. type Clause struct {
  4. queryAndArgsPars map[string][]any
  5. }
  6. func NewClause() *Clause {
  7. return &Clause{queryAndArgsPars: make(map[string][]any)}
  8. }
  9. func (clause *Clause) Common(query string, args ...any) *Clause {
  10. clause.queryAndArgsPars[query] = args
  11. return clause
  12. }
  13. func (clause *Clause) Equal(columnName string, value any) *Clause {
  14. clause.queryAndArgsPars[columnName+" = ?"] = []any{value}
  15. return clause
  16. }
  17. func (clause *Clause) Like(columnName string, value any) *Clause {
  18. clause.queryAndArgsPars[columnName+" LIKE ?"] = []any{value}
  19. return clause
  20. }
  21. func (clause *Clause) In(columnName string, value any) *Clause {
  22. clause.queryAndArgsPars[columnName+" IN ?"] = []any{value}
  23. return clause
  24. }
  25. func (clause *Clause) NotIn(columnName string, value any) *Clause {
  26. clause.queryAndArgsPars[columnName+" NOT IN ?"] = []any{value}
  27. return clause
  28. }
  29. func (clause *Clause) Not(columnName string, value any) *Clause {
  30. clause.queryAndArgsPars[columnName+" != ?"] = []any{value}
  31. return clause
  32. }
  33. func (clause *Clause) LessThan(columnName string, value any) *Clause {
  34. clause.queryAndArgsPars[columnName+" < ?"] = []any{value}
  35. return clause
  36. }
  37. func (clause *Clause) LessThanAndEqual(columnName string, value any) *Clause {
  38. clause.queryAndArgsPars[columnName+" <= ?"] = []any{value}
  39. return clause
  40. }
  41. func (clause *Clause) GreaterThan(columnName string, value any) *Clause {
  42. clause.queryAndArgsPars[columnName+" > ?"] = []any{value}
  43. return clause
  44. }
  45. func (clause *Clause) GreaterThanAndEqual(columnName string, value any) *Clause {
  46. clause.queryAndArgsPars[columnName+" >= ?"] = []any{value}
  47. return clause
  48. }
  49. func (clause *Clause) ToJson() ([]byte, error) {
  50. return json.Marshal(clause.queryAndArgsPars)
  51. }