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.
 
 
 

112 lines
3.0 KiB

  1. package dynamodb
  2. import (
  3. "errors"
  4. "fmt"
  5. simplejson "github.com/bitly/go-simplejson"
  6. )
  7. func (t *Table) Query(attributeComparisons []AttributeComparison) ([]map[string]*Attribute, error) {
  8. q := NewQuery(t)
  9. q.AddKeyConditions(attributeComparisons)
  10. return runQuery(q, t)
  11. }
  12. func (t *Table) QueryOnIndex(attributeComparisons []AttributeComparison, indexName string) ([]map[string]*Attribute, error) {
  13. q := NewQuery(t)
  14. q.AddKeyConditions(attributeComparisons)
  15. q.AddIndex(indexName)
  16. return runQuery(q, t)
  17. }
  18. func (t *Table) QueryOnIndexDescending(attributeComparisons []AttributeComparison, indexName string) ([]map[string]*Attribute, error) {
  19. q := NewQuery(t)
  20. q.AddKeyConditions(attributeComparisons)
  21. q.AddIndex(indexName)
  22. q.ScanIndexDescending()
  23. return runQuery(q, t)
  24. }
  25. func (t *Table) LimitedQuery(attributeComparisons []AttributeComparison, limit int64) ([]map[string]*Attribute, error) {
  26. q := NewQuery(t)
  27. q.AddKeyConditions(attributeComparisons)
  28. q.AddLimit(limit)
  29. return runQuery(q, t)
  30. }
  31. func (t *Table) LimitedQueryOnIndex(attributeComparisons []AttributeComparison, indexName string, limit int64) ([]map[string]*Attribute, error) {
  32. q := NewQuery(t)
  33. q.AddKeyConditions(attributeComparisons)
  34. q.AddIndex(indexName)
  35. q.AddLimit(limit)
  36. return runQuery(q, t)
  37. }
  38. func (t *Table) LimitedQueryDescending(attributeComparisons []AttributeComparison, limit int64) ([]map[string]*Attribute, error) {
  39. q := NewQuery(t)
  40. q.AddKeyConditions(attributeComparisons)
  41. q.AddLimit(limit)
  42. q.ScanIndexDescending()
  43. return runQuery(q, t)
  44. }
  45. func (t *Table) LimitedQueryOnIndexDescending(attributeComparisons []AttributeComparison, indexName string, limit int64) ([]map[string]*Attribute, error) {
  46. q := NewQuery(t)
  47. q.AddKeyConditions(attributeComparisons)
  48. q.AddIndex(indexName)
  49. q.AddLimit(limit)
  50. q.ScanIndexDescending()
  51. return runQuery(q, t)
  52. }
  53. func (t *Table) CountQuery(attributeComparisons []AttributeComparison) (int64, error) {
  54. q := NewQuery(t)
  55. q.AddKeyConditions(attributeComparisons)
  56. q.AddSelect("COUNT")
  57. jsonResponse, err := t.Server.queryServer("DynamoDB_20120810.Query", q)
  58. if err != nil {
  59. return 0, err
  60. }
  61. json, err := simplejson.NewJson(jsonResponse)
  62. if err != nil {
  63. return 0, err
  64. }
  65. itemCount, err := json.Get("Count").Int64()
  66. if err != nil {
  67. return 0, err
  68. }
  69. return itemCount, nil
  70. }
  71. func runQuery(q *Query, t *Table) ([]map[string]*Attribute, error) {
  72. jsonResponse, err := t.Server.queryServer("DynamoDB_20120810.Query", q)
  73. if err != nil {
  74. return nil, err
  75. }
  76. json, err := simplejson.NewJson(jsonResponse)
  77. if err != nil {
  78. return nil, err
  79. }
  80. itemCount, err := json.Get("Count").Int()
  81. if err != nil {
  82. message := fmt.Sprintf("Unexpected response %s", jsonResponse)
  83. return nil, errors.New(message)
  84. }
  85. results := make([]map[string]*Attribute, itemCount)
  86. for i, _ := range results {
  87. item, err := json.Get("Items").GetIndex(i).Map()
  88. if err != nil {
  89. message := fmt.Sprintf("Unexpected response %s", jsonResponse)
  90. return nil, errors.New(message)
  91. }
  92. results[i] = parseAttributes(item)
  93. }
  94. return results, nil
  95. }