Class: SDBTools::Selection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sdbtools/selection.rb

Constant Summary collapse

MAX_BATCH_LIMIT =
250
DEFAULT_RESULT_LIMIT =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdb, domain, options = {}) ⇒ Selection

Returns a new instance of Selection.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sdbtools/selection.rb', line 37

def initialize(sdb, domain, options={})
  @sdb             = sdb
  @domain          = domain.to_s
  @attributes      = options.fetch(:attributes) { :all }
  @conditions      = options[:conditions].to_s
  @order           = options.fetch(:order) { :ascending }
  @order_by        = options.fetch(:order_by) { :none }
  @order_by        = @order_by.to_s unless @order_by == :none
  self.limit       = options.fetch(:limit) { :none }
  self.batch_limit = options.fetch(:batch_limit) { DEFAULT_RESULT_LIMIT }.to_i
  @offset          = options.fetch(:offset) { 0 }.to_i
  @logger          = options.fetch(:logger){::Logger.new($stderr)}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/sdbtools/selection.rb', line 12

def attributes
  @attributes
end

#batch_limitObject

Returns the value of attribute batch_limit.



15
16
17
# File 'lib/sdbtools/selection.rb', line 15

def batch_limit
  @batch_limit
end

#conditionsObject

Returns the value of attribute conditions.



13
14
15
# File 'lib/sdbtools/selection.rb', line 13

def conditions
  @conditions
end

#domainObject

Returns the value of attribute domain.



11
12
13
# File 'lib/sdbtools/selection.rb', line 11

def domain
  @domain
end

#limitObject

Returns the value of attribute limit.



14
15
16
# File 'lib/sdbtools/selection.rb', line 14

def limit
  @limit
end

#loggerObject

Returns the value of attribute logger.



20
21
22
# File 'lib/sdbtools/selection.rb', line 20

def logger
  @logger
end

#offsetObject

Returns the value of attribute offset.



16
17
18
# File 'lib/sdbtools/selection.rb', line 16

def offset
  @offset
end

#orderObject

Returns the value of attribute order.



18
19
20
# File 'lib/sdbtools/selection.rb', line 18

def order
  @order
end

#order_byObject

Returns the value of attribute order_by.



17
18
19
# File 'lib/sdbtools/selection.rb', line 17

def order_by
  @order_by
end

#sdbObject

Returns the value of attribute sdb.



19
20
21
# File 'lib/sdbtools/selection.rb', line 19

def sdb
  @sdb
end

#starting_tokenObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sdbtools/selection.rb', line 118

def starting_token
  @starting_token ||=
    case offset
    when 0 then nil
    else
      op    = offset_count_operation
      count = 0
      op.each do |results, operation|
      count += results[:items].first["Domain"]["Count"].first.to_i
      if count == offset || results[:next_token].nil?
        return results[:next_token]
      end
    end
      raise "Failed to find offset #{offset}"
    end
end

Class Method Details

.quote_name(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/sdbtools/selection.rb', line 25

def self.quote_name(name)
  if name.to_s =~ /^[A-Z$_][A-Z0-9$_]*$/i
    name.to_s
  else
    "`" + name.to_s.gsub("`", "``") + "`"
  end
end

.quote_value(value) ⇒ Object



33
34
35
# File 'lib/sdbtools/selection.rb', line 33

def self.quote_value(value)
  '"' + value.to_s.gsub(/"/, '""') + '"'
end

Instance Method Details

#countObject Also known as: size, length



67
68
69
70
71
72
73
# File 'lib/sdbtools/selection.rb', line 67

def count
  Transaction.open(count_expression) do |t|
    @count ||= count_operation.inject(0){|count, (results, operation)| 
      count += results[:items].first["Domain"]["Count"].first.to_i
    }
  end
end

#count_expressionObject



59
60
61
# File 'lib/sdbtools/selection.rb', line 59

def count_expression
  "SELECT count(*) FROM #{quote_name(domain)}#{match_expression}#{sort_instructions}#{limit_clause_for_count}"
end

#count_operationObject



106
107
108
# File 'lib/sdbtools/selection.rb', line 106

def count_operation
  Operation.new(sdb, :select, count_expression, :starting_token => starting_token)
end

#eachObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sdbtools/selection.rb', line 78

def each
  return if limit == 0
  num_items = 0
  Transaction.open(to_s) do
    select_operation(limit, num_items).each do |results, operation|
      results[:items].each do |item|
        yield(item.keys.first, item.values.first)
        num_items += 1
        return if limit != :none && num_items >= limit
      end
      operation.args[0] = to_s(limit, num_items)
    end
  end
rescue Aws::AwsError => error
  if error.message =~ /InvalidQueryExpression/
    raise error, error.message.to_s + " (#{to_s(limit, num_items)})", error.backtrace
  else
    raise
  end
end

#offset_count_expressionObject



63
64
65
# File 'lib/sdbtools/selection.rb', line 63

def offset_count_expression
  "SELECT count(*) FROM #{quote_name(domain)}#{match_expression}#{sort_instructions} LIMIT #{offset}"
end

#offset_count_operationObject



110
111
112
# File 'lib/sdbtools/selection.rb', line 110

def offset_count_operation
  Operation.new(sdb, :select, offset_count_expression)
end

#resultsObject



99
100
101
102
103
104
# File 'lib/sdbtools/selection.rb', line 99

def results
  @results ||= inject(Arrayfields.new){|results, (name, value)|
    results[name] = value
    results
  }
end

#select_operation(query_limit = limit, offset = 0) ⇒ Object



114
115
116
# File 'lib/sdbtools/selection.rb', line 114

def select_operation(query_limit=limit, offset=0)
  Operation.new(sdb, :select, to_s(query_limit, offset), :starting_token => starting_token)
end

#to_s(query_limit = limit, offset = 0) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sdbtools/selection.rb', line 51

def to_s(query_limit=limit, offset=0)
  "SELECT #{output_list}"       \
  " FROM #{quote_name(domain)}" \
  "#{match_expression}"         \
  "#{sort_instructions}"        \
  "#{limit_clause(query_limit,offset)}"
end