Skip to content

Instantly share code, notes, and snippets.

@bpo
Created September 8, 2011 10:37
Show Gist options
  • Select an option

  • Save bpo/1203111 to your computer and use it in GitHub Desktop.

Select an option

Save bpo/1203111 to your computer and use it in GitHub Desktop.
sorted simpledb select support (ruby 1.8)
module Fog
module Parsers
module AWS
module SimpleDB
class Select < Fog::Parsers::AWS::SimpleDB::Basic
class ItemEnumeration
include Enumerable
attr_writer :response
def initialize
@contents = []
end
def <<(obj)
@contents << obj
end
def each
@contents.each do |item_name|
yield(@response['Items'][item_name])
end
end
end
def reset
@item_name = @attribute_name = nil
@response = { 'Items' => {}, 'Enumeration' => ItemEnumeration.new }
@response['Enumeration'].response = @response
end
def end_element(name)
case name
when 'BoxUsage'
response[name] = value.to_f
when 'Item'
@item_name = @attribute_name = nil
when 'Name'
if @item_name.nil?
@item_name = value
response['Enumeration'] << @item_name
response['Items'][@item_name] = {}
else
@attribute_name = value
response['Items'][@item_name][@attribute_name] ||= []
end
when 'NextToken', 'RequestId'
response[name] = value
when 'Value'
response['Items'][@item_name][@attribute_name] << sdb_decode(value)
end
end
end
end
end
end
end
@bpo
Copy link
Author

bpo commented Sep 8, 2011

query = "select * from documents where created_at is not null order by created_at"
items = sdb.select( query ).body["Enumeration"]
# sorted.
items.each { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment