1// Copyright 2018 Huan Du. All rights reserved. 2// Licensed under the MIT license that can be found in the LICENSE file. 3 4package sqlbuilder 5 6import ( 7 "fmt" 8 "strings" 9) 10 11// Cond provides several helper methods to build conditions. 12type Cond struct { 13 Args *Args 14} 15 16// Equal represents "field = value". 17func (c *Cond) Equal(field string, value interface{}) string { 18 return fmt.Sprintf("%s = %s", Escape(field), c.Args.Add(value)) 19} 20 21// E is an alias of Equal. 22func (c *Cond) E(field string, value interface{}) string { 23 return c.Equal(field, value) 24} 25 26// NotEqual represents "field != value". 27func (c *Cond) NotEqual(field string, value interface{}) string { 28 return fmt.Sprintf("%s <> %s", Escape(field), c.Args.Add(value)) 29} 30 31// NE is an alias of NotEqual. 32func (c *Cond) NE(field string, value interface{}) string { 33 return c.NotEqual(field, value) 34} 35 36// GreaterThan represents "field > value". 37func (c *Cond) GreaterThan(field string, value interface{}) string { 38 return fmt.Sprintf("%s > %s", Escape(field), c.Args.Add(value)) 39} 40 41// G is an alias of GreaterThan. 42func (c *Cond) G(field string, value interface{}) string { 43 return c.GreaterThan(field, value) 44} 45 46// GreaterEqualThan represents "field >= value". 47func (c *Cond) GreaterEqualThan(field string, value interface{}) string { 48 return fmt.Sprintf("%s >= %s", Escape(field), c.Args.Add(value)) 49} 50 51// GE is an alias of GreaterEqualThan. 52func (c *Cond) GE(field string, value interface{}) string { 53 return c.GreaterEqualThan(field, value) 54} 55 56// LessThan represents "field < value". 57func (c *Cond) LessThan(field string, value interface{}) string { 58 return fmt.Sprintf("%s < %s", Escape(field), c.Args.Add(value)) 59} 60 61// L is an alias of LessThan. 62func (c *Cond) L(field string, value interface{}) string { 63 return c.LessThan(field, value) 64} 65 66// LessEqualThan represents "field <= value". 67func (c *Cond) LessEqualThan(field string, value interface{}) string { 68 return fmt.Sprintf("%s <= %s", Escape(field), c.Args.Add(value)) 69} 70 71// LE is an alias of LessEqualThan. 72func (c *Cond) LE(field string, value interface{}) string { 73 return c.LessEqualThan(field, value) 74} 75 76// In represents "field IN (value...)". 77func (c *Cond) In(field string, value ...interface{}) string { 78 vs := make([]string, 0, len(value)) 79 80 for _, v := range value { 81 vs = append(vs, c.Args.Add(v)) 82 } 83 84 return fmt.Sprintf("%s IN (%s)", Escape(field), strings.Join(vs, ", ")) 85} 86 87// NotIn represents "field NOT IN (value...)". 88func (c *Cond) NotIn(field string, value ...interface{}) string { 89 vs := make([]string, 0, len(value)) 90 91 for _, v := range value { 92 vs = append(vs, c.Args.Add(v)) 93 } 94 95 return fmt.Sprintf("%s NOT IN (%s)", Escape(field), strings.Join(vs, ", ")) 96} 97 98// Like represents "field LIKE value". 99func (c *Cond) Like(field string, value interface{}) string { 100 return fmt.Sprintf("%s LIKE %s", Escape(field), c.Args.Add(value)) 101} 102 103// NotLike represents "field NOT LIKE value". 104func (c *Cond) NotLike(field string, value interface{}) string { 105 return fmt.Sprintf("%s NOT LIKE %s", Escape(field), c.Args.Add(value)) 106} 107 108// IsNull represents "field IS NULL". 109func (c *Cond) IsNull(field string) string { 110 return fmt.Sprintf("%s IS NULL", Escape(field)) 111} 112 113// IsNotNull represents "field IS NOT NULL". 114func (c *Cond) IsNotNull(field string) string { 115 return fmt.Sprintf("%s IS NOT NULL", Escape(field)) 116} 117 118// Between represents "field BETWEEN lower AND upper". 119func (c *Cond) Between(field string, lower, upper interface{}) string { 120 return fmt.Sprintf("%s BETWEEN %s AND %s", Escape(field), c.Args.Add(lower), c.Args.Add(upper)) 121} 122 123// NotBetween represents "field NOT BETWEEN lower AND upper". 124func (c *Cond) NotBetween(field string, lower, upper interface{}) string { 125 return fmt.Sprintf("%s NOT BETWEEN %s AND %s", Escape(field), c.Args.Add(lower), c.Args.Add(upper)) 126} 127 128// Or represents OR logic like "expr1 OR expr2 OR expr3". 129func (c *Cond) Or(orExpr ...string) string { 130 return fmt.Sprintf("(%s)", strings.Join(orExpr, " OR ")) 131} 132 133// And represents AND logic like "expr1 AND expr2 AND expr3". 134func (c *Cond) And(andExpr ...string) string { 135 return fmt.Sprintf("(%s)", strings.Join(andExpr, " AND ")) 136} 137 138// Var returns a placeholder for value. 139func (c *Cond) Var(value interface{}) string { 140 return c.Args.Add(value) 141} 142