You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

186 lines
4.4 KiB

  1. package dynamodb
  2. import (
  3. "strconv"
  4. )
  5. const (
  6. TYPE_STRING = "S"
  7. TYPE_NUMBER = "N"
  8. TYPE_BINARY = "B"
  9. TYPE_STRING_SET = "SS"
  10. TYPE_NUMBER_SET = "NS"
  11. TYPE_BINARY_SET = "BS"
  12. COMPARISON_EQUAL = "EQ"
  13. COMPARISON_NOT_EQUAL = "NE"
  14. COMPARISON_LESS_THAN_OR_EQUAL = "LE"
  15. COMPARISON_LESS_THAN = "LT"
  16. COMPARISON_GREATER_THAN_OR_EQUAL = "GE"
  17. COMPARISON_GREATER_THAN = "GT"
  18. COMPARISON_ATTRIBUTE_EXISTS = "NOT_NULL"
  19. COMPARISON_ATTRIBUTE_DOES_NOT_EXIST = "NULL"
  20. COMPARISON_CONTAINS = "CONTAINS"
  21. COMPARISON_DOES_NOT_CONTAIN = "NOT_CONTAINS"
  22. COMPARISON_BEGINS_WITH = "BEGINS_WITH"
  23. COMPARISON_IN = "IN"
  24. COMPARISON_BETWEEN = "BETWEEN"
  25. )
  26. type Key struct {
  27. HashKey string
  28. RangeKey string
  29. }
  30. type PrimaryKey struct {
  31. KeyAttribute *Attribute
  32. RangeAttribute *Attribute
  33. }
  34. type Attribute struct {
  35. Type string
  36. Name string
  37. Value string
  38. SetValues []string
  39. Exists string // exists on dynamodb? Values: "true", "false", or ""
  40. }
  41. type AttributeComparison struct {
  42. AttributeName string
  43. ComparisonOperator string
  44. AttributeValueList []Attribute // contains attributes with only types and names (value ignored)
  45. }
  46. func NewEqualInt64AttributeComparison(attributeName string, equalToValue int64) *AttributeComparison {
  47. numeric := NewNumericAttribute(attributeName, strconv.FormatInt(equalToValue, 10))
  48. return &AttributeComparison{attributeName,
  49. COMPARISON_EQUAL,
  50. []Attribute{*numeric},
  51. }
  52. }
  53. func NewEqualStringAttributeComparison(attributeName string, equalToValue string) *AttributeComparison {
  54. str := NewStringAttribute(attributeName, equalToValue)
  55. return &AttributeComparison{attributeName,
  56. COMPARISON_EQUAL,
  57. []Attribute{*str},
  58. }
  59. }
  60. func NewStringAttributeComparison(attributeName string, comparisonOperator string, value string) *AttributeComparison {
  61. valueToCompare := NewStringAttribute(attributeName, value)
  62. return &AttributeComparison{attributeName,
  63. comparisonOperator,
  64. []Attribute{*valueToCompare},
  65. }
  66. }
  67. func NewNumericAttributeComparison(attributeName string, comparisonOperator string, value int64) *AttributeComparison {
  68. valueToCompare := NewNumericAttribute(attributeName, strconv.FormatInt(value, 10))
  69. return &AttributeComparison{attributeName,
  70. comparisonOperator,
  71. []Attribute{*valueToCompare},
  72. }
  73. }
  74. func NewBinaryAttributeComparison(attributeName string, comparisonOperator string, value bool) *AttributeComparison {
  75. valueToCompare := NewBinaryAttribute(attributeName, strconv.FormatBool(value))
  76. return &AttributeComparison{attributeName,
  77. comparisonOperator,
  78. []Attribute{*valueToCompare},
  79. }
  80. }
  81. func NewStringAttribute(name string, value string) *Attribute {
  82. return &Attribute{
  83. Type: TYPE_STRING,
  84. Name: name,
  85. Value: value,
  86. }
  87. }
  88. func NewNumericAttribute(name string, value string) *Attribute {
  89. return &Attribute{
  90. Type: TYPE_NUMBER,
  91. Name: name,
  92. Value: value,
  93. }
  94. }
  95. func NewBinaryAttribute(name string, value string) *Attribute {
  96. return &Attribute{
  97. Type: TYPE_BINARY,
  98. Name: name,
  99. Value: value,
  100. }
  101. }
  102. func NewStringSetAttribute(name string, values []string) *Attribute {
  103. return &Attribute{
  104. Type: TYPE_STRING_SET,
  105. Name: name,
  106. SetValues: values,
  107. }
  108. }
  109. func NewNumericSetAttribute(name string, values []string) *Attribute {
  110. return &Attribute{
  111. Type: TYPE_NUMBER_SET,
  112. Name: name,
  113. SetValues: values,
  114. }
  115. }
  116. func NewBinarySetAttribute(name string, values []string) *Attribute {
  117. return &Attribute{
  118. Type: TYPE_BINARY_SET,
  119. Name: name,
  120. SetValues: values,
  121. }
  122. }
  123. func (a *Attribute) SetType() bool {
  124. switch a.Type {
  125. case TYPE_BINARY_SET, TYPE_NUMBER_SET, TYPE_STRING_SET:
  126. return true
  127. }
  128. return false
  129. }
  130. func (a *Attribute) SetExists(exists bool) *Attribute {
  131. if exists {
  132. a.Exists = "true"
  133. } else {
  134. a.Exists = "false"
  135. }
  136. return a
  137. }
  138. func (k *PrimaryKey) HasRange() bool {
  139. return k.RangeAttribute != nil
  140. }
  141. // Useful when you may have many goroutines using a primary key, so they don't fuxor up your values.
  142. func (k *PrimaryKey) Clone(h string, r string) []Attribute {
  143. pk := &Attribute{
  144. Type: k.KeyAttribute.Type,
  145. Name: k.KeyAttribute.Name,
  146. Value: h,
  147. }
  148. result := []Attribute{*pk}
  149. if k.HasRange() {
  150. rk := &Attribute{
  151. Type: k.RangeAttribute.Type,
  152. Name: k.RangeAttribute.Name,
  153. Value: r,
  154. }
  155. result = append(result, *rk)
  156. }
  157. return result
  158. }