http urls monitor.

comparison.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package db
  22. import (
  23. "reflect"
  24. "time"
  25. )
  26. // Comparison defines methods for representing comparison operators in a
  27. // portable way across databases.
  28. type Comparison interface {
  29. Operator() ComparisonOperator
  30. Value() interface{}
  31. }
  32. // ComparisonOperator is a type we use to label comparison operators.
  33. type ComparisonOperator uint8
  34. // Comparison operators
  35. const (
  36. ComparisonOperatorNone ComparisonOperator = iota
  37. ComparisonOperatorEqual
  38. ComparisonOperatorNotEqual
  39. ComparisonOperatorLessThan
  40. ComparisonOperatorGreaterThan
  41. ComparisonOperatorLessThanOrEqualTo
  42. ComparisonOperatorGreaterThanOrEqualTo
  43. ComparisonOperatorBetween
  44. ComparisonOperatorNotBetween
  45. ComparisonOperatorIn
  46. ComparisonOperatorNotIn
  47. ComparisonOperatorIs
  48. ComparisonOperatorIsNot
  49. ComparisonOperatorLike
  50. ComparisonOperatorNotLike
  51. ComparisonOperatorRegExp
  52. ComparisonOperatorNotRegExp
  53. ComparisonOperatorAfter
  54. ComparisonOperatorBefore
  55. ComparisonOperatorOnOrAfter
  56. ComparisonOperatorOnOrBefore
  57. )
  58. type dbComparisonOperator struct {
  59. t ComparisonOperator
  60. op string
  61. v interface{}
  62. }
  63. func (c *dbComparisonOperator) CustomOperator() string {
  64. return c.op
  65. }
  66. func (c *dbComparisonOperator) Operator() ComparisonOperator {
  67. return c.t
  68. }
  69. func (c *dbComparisonOperator) Value() interface{} {
  70. return c.v
  71. }
  72. // Gte indicates whether the reference is greater than or equal to the given
  73. // argument.
  74. func Gte(v interface{}) Comparison {
  75. return &dbComparisonOperator{
  76. t: ComparisonOperatorGreaterThanOrEqualTo,
  77. v: v,
  78. }
  79. }
  80. // Lte indicates whether the reference is less than or equal to the given
  81. // argument.
  82. func Lte(v interface{}) Comparison {
  83. return &dbComparisonOperator{
  84. t: ComparisonOperatorLessThanOrEqualTo,
  85. v: v,
  86. }
  87. }
  88. // Eq indicates whether the constraint is equal to the given argument.
  89. func Eq(v interface{}) Comparison {
  90. return &dbComparisonOperator{
  91. t: ComparisonOperatorEqual,
  92. v: v,
  93. }
  94. }
  95. // NotEq indicates whether the constraint is not equal to the given argument.
  96. func NotEq(v interface{}) Comparison {
  97. return &dbComparisonOperator{
  98. t: ComparisonOperatorNotEqual,
  99. v: v,
  100. }
  101. }
  102. // Gt indicates whether the constraint is greater than the given argument.
  103. func Gt(v interface{}) Comparison {
  104. return &dbComparisonOperator{
  105. t: ComparisonOperatorGreaterThan,
  106. v: v,
  107. }
  108. }
  109. // Lt indicates whether the constraint is less than the given argument.
  110. func Lt(v interface{}) Comparison {
  111. return &dbComparisonOperator{
  112. t: ComparisonOperatorLessThan,
  113. v: v,
  114. }
  115. }
  116. // In indicates whether the argument is part of the reference.
  117. func In(v interface{}) Comparison {
  118. return &dbComparisonOperator{
  119. t: ComparisonOperatorIn,
  120. v: toInterfaceArray(v),
  121. }
  122. }
  123. // NotIn indicates whether the argument is not part of the reference.
  124. func NotIn(v interface{}) Comparison {
  125. return &dbComparisonOperator{
  126. t: ComparisonOperatorNotIn,
  127. v: toInterfaceArray(v),
  128. }
  129. }
  130. // After indicates whether the reference is after the given time.
  131. func After(t time.Time) Comparison {
  132. return &dbComparisonOperator{
  133. t: ComparisonOperatorGreaterThan,
  134. v: t,
  135. }
  136. }
  137. // Before indicates whether the reference is before the given time.
  138. func Before(t time.Time) Comparison {
  139. return &dbComparisonOperator{
  140. t: ComparisonOperatorLessThan,
  141. v: t,
  142. }
  143. }
  144. // OnOrAfter indicater whether the reference is after or equal to the given
  145. // time value.
  146. func OnOrAfter(t time.Time) Comparison {
  147. return &dbComparisonOperator{
  148. t: ComparisonOperatorGreaterThanOrEqualTo,
  149. v: t,
  150. }
  151. }
  152. // OnOrBefore indicates whether the reference is before or equal to the given
  153. // time value.
  154. func OnOrBefore(t time.Time) Comparison {
  155. return &dbComparisonOperator{
  156. t: ComparisonOperatorLessThanOrEqualTo,
  157. v: t,
  158. }
  159. }
  160. // Between indicates whether the reference is contained between the two given
  161. // values.
  162. func Between(a interface{}, b interface{}) Comparison {
  163. return &dbComparisonOperator{
  164. t: ComparisonOperatorBetween,
  165. v: []interface{}{a, b},
  166. }
  167. }
  168. // NotBetween indicates whether the reference is not contained between the two
  169. // given values.
  170. func NotBetween(a interface{}, b interface{}) Comparison {
  171. return &dbComparisonOperator{
  172. t: ComparisonOperatorNotBetween,
  173. v: []interface{}{a, b},
  174. }
  175. }
  176. // Is indicates whether the reference is nil, true or false.
  177. func Is(v interface{}) Comparison {
  178. return &dbComparisonOperator{
  179. t: ComparisonOperatorIs,
  180. v: v,
  181. }
  182. }
  183. // IsNot indicates whether the reference is not nil, true nor false.
  184. func IsNot(v interface{}) Comparison {
  185. return &dbComparisonOperator{
  186. t: ComparisonOperatorIsNot,
  187. v: v,
  188. }
  189. }
  190. // IsNull indicates whether the reference is a NULL value.
  191. func IsNull() Comparison {
  192. return Is(nil)
  193. }
  194. // IsNotNull indicates whether the reference is a NULL value.
  195. func IsNotNull() Comparison {
  196. return IsNot(nil)
  197. }
  198. /*
  199. // IsDistinctFrom indicates whether the reference is different from
  200. // the given value, including NULL values.
  201. func IsDistinctFrom(v interface{}) Comparison {
  202. return &dbComparisonOperator{
  203. t: ComparisonOperatorIsDistinctFrom,
  204. v: v,
  205. }
  206. }
  207. // IsNotDistinctFrom indicates whether the reference is not different from the
  208. // given value, including NULL values.
  209. func IsNotDistinctFrom(v interface{}) Comparison {
  210. return &dbComparisonOperator{
  211. t: ComparisonOperatorIsNotDistinctFrom,
  212. v: v,
  213. }
  214. }
  215. */
  216. // Like indicates whether the reference matches the wildcard value.
  217. func Like(v string) Comparison {
  218. return &dbComparisonOperator{
  219. t: ComparisonOperatorLike,
  220. v: v,
  221. }
  222. }
  223. // NotLike indicates whether the reference does not match the wildcard value.
  224. func NotLike(v string) Comparison {
  225. return &dbComparisonOperator{
  226. t: ComparisonOperatorNotLike,
  227. v: v,
  228. }
  229. }
  230. /*
  231. // ILike indicates whether the reference matches the wildcard value (case
  232. // insensitive).
  233. func ILike(v string) Comparison {
  234. return &dbComparisonOperator{
  235. t: ComparisonOperatorILike,
  236. v: v,
  237. }
  238. }
  239. // NotILike indicates whether the reference does not match the wildcard value
  240. // (case insensitive).
  241. func NotILike(v string) Comparison {
  242. return &dbComparisonOperator{
  243. t: ComparisonOperatorNotILike,
  244. v: v,
  245. }
  246. }
  247. */
  248. // RegExp indicates whether the reference matches the regexp pattern.
  249. func RegExp(v string) Comparison {
  250. return &dbComparisonOperator{
  251. t: ComparisonOperatorRegExp,
  252. v: v,
  253. }
  254. }
  255. // NotRegExp indicates whether the reference does not match the regexp pattern.
  256. func NotRegExp(v string) Comparison {
  257. return &dbComparisonOperator{
  258. t: ComparisonOperatorNotRegExp,
  259. v: v,
  260. }
  261. }
  262. // Op represents a custom comparison operator against the reference.
  263. func Op(customOperator string, v interface{}) Comparison {
  264. return &dbComparisonOperator{
  265. op: customOperator,
  266. t: ComparisonOperatorNone,
  267. v: v,
  268. }
  269. }
  270. func toInterfaceArray(v interface{}) []interface{} {
  271. rv := reflect.ValueOf(v)
  272. switch rv.Type().Kind() {
  273. case reflect.Ptr:
  274. return toInterfaceArray(rv.Elem().Interface())
  275. case reflect.Slice:
  276. elems := rv.Len()
  277. args := make([]interface{}, elems)
  278. for i := 0; i < elems; i++ {
  279. args[i] = rv.Index(i).Interface()
  280. }
  281. return args
  282. }
  283. return []interface{}{v}
  284. }
  285. var _ Comparison = &dbComparisonOperator{}