Class: SDBTools::Selection
- Inherits:
-
Object
- Object
- SDBTools::Selection
- Includes:
- Enumerable
- Defined in:
- lib/sdbtools/selection.rb
Constant Summary collapse
- MAX_BATCH_LIMIT =
250
- DEFAULT_RESULT_LIMIT =
100
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#batch_limit ⇒ Object
Returns the value of attribute batch_limit.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#order ⇒ Object
Returns the value of attribute order.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#sdb ⇒ Object
Returns the value of attribute sdb.
- #starting_token ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #count ⇒ Object (also: #size, #length)
- #count_expression ⇒ Object
- #count_operation ⇒ Object
- #each ⇒ Object
-
#initialize(sdb, domain, options = {}) ⇒ Selection
constructor
A new instance of Selection.
- #offset_count_expression ⇒ Object
- #offset_count_operation ⇒ Object
- #results ⇒ Object
- #select_operation(query_limit = limit, offset = 0) ⇒ Object
- #to_s(query_limit = limit, offset = 0) ⇒ Object
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, ={}) @sdb = sdb @domain = domain.to_s @attributes = .fetch(:attributes) { :all } @conditions = [:conditions].to_s @order = .fetch(:order) { :ascending } @order_by = .fetch(:order_by) { :none } @order_by = @order_by.to_s unless @order_by == :none self.limit = .fetch(:limit) { :none } self.batch_limit = .fetch(:batch_limit) { DEFAULT_RESULT_LIMIT }.to_i @offset = .fetch(:offset) { 0 }.to_i @logger = .fetch(:logger){::Logger.new($stderr)} end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/sdbtools/selection.rb', line 12 def attributes @attributes end |
#batch_limit ⇒ Object
Returns the value of attribute batch_limit.
15 16 17 |
# File 'lib/sdbtools/selection.rb', line 15 def batch_limit @batch_limit end |
#conditions ⇒ Object
Returns the value of attribute conditions.
13 14 15 |
# File 'lib/sdbtools/selection.rb', line 13 def conditions @conditions end |
#domain ⇒ Object
Returns the value of attribute domain.
11 12 13 |
# File 'lib/sdbtools/selection.rb', line 11 def domain @domain end |
#limit ⇒ Object
Returns the value of attribute limit.
14 15 16 |
# File 'lib/sdbtools/selection.rb', line 14 def limit @limit end |
#logger ⇒ Object
Returns the value of attribute logger.
20 21 22 |
# File 'lib/sdbtools/selection.rb', line 20 def logger @logger end |
#offset ⇒ Object
Returns the value of attribute offset.
16 17 18 |
# File 'lib/sdbtools/selection.rb', line 16 def offset @offset end |
#order ⇒ Object
Returns the value of attribute order.
18 19 20 |
# File 'lib/sdbtools/selection.rb', line 18 def order @order end |
#order_by ⇒ Object
Returns the value of attribute order_by.
17 18 19 |
# File 'lib/sdbtools/selection.rb', line 17 def order_by @order_by end |
#sdb ⇒ Object
Returns the value of attribute sdb.
19 20 21 |
# File 'lib/sdbtools/selection.rb', line 19 def sdb @sdb end |
#starting_token ⇒ Object
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
#count ⇒ Object 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_expression ⇒ Object
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_operation ⇒ Object
106 107 108 |
# File 'lib/sdbtools/selection.rb', line 106 def count_operation Operation.new(sdb, :select, count_expression, :starting_token => starting_token) end |
#each ⇒ Object
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. =~ /InvalidQueryExpression/ raise error, error..to_s + " (#{to_s(limit, num_items)})", error.backtrace else raise end end |
#offset_count_expression ⇒ Object
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_operation ⇒ Object
110 111 112 |
# File 'lib/sdbtools/selection.rb', line 110 def offset_count_operation Operation.new(sdb, :select, offset_count_expression) end |
#results ⇒ Object
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 |