http urls monitor.

cond.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "fmt"
  24. "sort"
  25. )
  26. // Cond is a map that defines conditions for a query and satisfies the
  27. // Constraints and Compound interfaces.
  28. //
  29. // Each entry of the map represents a condition (a column-value relation bound
  30. // by a comparison operator). The comparison operator is optional and can be
  31. // specified after the column name, if no comparison operator is provided the
  32. // equality is used.
  33. //
  34. // Examples:
  35. //
  36. // // Where age equals 18.
  37. // db.Cond{"age": 18}
  38. // // // Where age is greater than or equal to 18.
  39. // db.Cond{"age >=": 18}
  40. //
  41. // // Where id is in a list of ids.
  42. // db.Cond{"id IN": []{1, 2, 3}}
  43. //
  44. // // Where age is lower than 18 (you could use this syntax when using
  45. // // mongodb).
  46. // db.Cond{"age $lt": 18}
  47. //
  48. // // Where age > 32 and age < 35
  49. // db.Cond{"age >": 32, "age <": 35}
  50. type Cond map[interface{}]interface{}
  51. // Constraints returns each one of the Cond map records as a constraint.
  52. func (c Cond) Constraints() []Constraint {
  53. z := make([]Constraint, 0, len(c))
  54. for _, k := range c.Keys() {
  55. z = append(z, NewConstraint(k, c[k]))
  56. }
  57. return z
  58. }
  59. // Keys returns the keys of this map sorted by name.
  60. func (c Cond) Keys() []interface{} {
  61. keys := make(condKeys, 0, len(c))
  62. for k := range c {
  63. keys = append(keys, k)
  64. }
  65. if len(c) > 1 {
  66. sort.Sort(keys)
  67. }
  68. return keys
  69. }
  70. // Sentences return each one of the map records as a compound.
  71. func (c Cond) Sentences() []Compound {
  72. z := make([]Compound, 0, len(c))
  73. for _, k := range c.Keys() {
  74. z = append(z, Cond{k: c[k]})
  75. }
  76. return z
  77. }
  78. // Operator returns the default compound operator.
  79. func (c Cond) Operator() CompoundOperator {
  80. return OperatorNone
  81. }
  82. // Empty returns false if there are no conditions.
  83. func (c Cond) Empty() bool {
  84. for range c {
  85. return false
  86. }
  87. return true
  88. }
  89. type condKeys []interface{}
  90. func (ck condKeys) Len() int {
  91. return len(ck)
  92. }
  93. func (ck condKeys) Less(i, j int) bool {
  94. return fmt.Sprintf("%v", ck[i]) < fmt.Sprintf("%v", ck[j])
  95. }
  96. func (ck condKeys) Swap(i, j int) {
  97. ck[i], ck[j] = ck[j], ck[i]
  98. }