Skip to content

Instantly share code, notes, and snippets.

@edward
Created October 23, 2008 14:35
Show Gist options
  • Save edward/19039 to your computer and use it in GitHub Desktop.
Save edward/19039 to your computer and use it in GitHub Desktop.
--- simplerdb-0.2/lib/simplerdb/db.rb 2008-03-15 17:56:16.000000000 +0000
+++ simplerdb-0.2/lib/simplerdb/db.rb 2008-10-20 12:03:37.000000000 +0100
@@ -128,15 +128,22 @@
attribute_params.each { |attr| item.put_attribute(attr.to_attr, attr.replace) }
end
- def delete_attributes(domain_name, item_name, attributes)
+ def delete_attributes(domain_name, item_name, attributes = [])
domain = @domains[domain_name]
item = domain.items[item_name]
- attributes.each { |attr| item.delete_attribute(attr) }
+ if attributes.empty?
+ attributes = get_attributes(domain_name, item_name)
+ end
+ attributes.each { |attr|
+ item.delete_attribute(attr)
+ }
end
- def query(domain, query, max = 100, token = 0)
- @query_executor.do_query(query, @domains[domain], max, token)
+ def query(domain, query = "", max = 100, token = 0)
+ result = @query_executor.do_query(query, @domains[domain], max, token)
+ result
end
+
end
end
--- simplerdb-0.2/lib/simplerdb/query_language.rb 2008-03-15 04:26:01.000000000 +0000
+++ simplerdb-0.2/lib/simplerdb/query_language.rb 2008-10-20 12:05:15.000000000 +0100
@@ -14,17 +14,23 @@
# Execute the query
def do_query(query, domain, max = 100, token = 0)
- parse_result = @parser.parse(@lexer.lex(query))
token = 0 if token.nil?
- case parse_result
- when Dhaka::TokenizerErrorResult
- raise tokenize_error_message(parse_result.unexpected_char_index, query)
- when Dhaka::ParseErrorResult
- raise parse_error_message(parse_result.unexpected_token, query)
- end
+ if query.nil? || query.empty?
+ items = QueryEvaluator.new(domain).all_items
+ else
+ parse_result = @parser.parse(@lexer.lex(query))
+ case parse_result
+ when Dhaka::TokenizerErrorResult
+ raise tokenize_error_message(parse_result.unexpected_char_index, query)
+ when Dhaka::ParseErrorResult
+ raise parse_error_message(parse_result.unexpected_token, query)
+ end
- items = QueryEvaluator.new(domain).evaluate(parse_result)
+ items = QueryEvaluator.new(domain).evaluate(parse_result)
+ items
+ end
+
results = []
count = 0
items.each do |item|
@@ -32,7 +38,6 @@
results << item if count >= token
count += 1
end
-
if (count == items.size)
return results,nil
else
@@ -236,7 +241,9 @@
def all_items
unless @all_items
- @all_items = @domain.items.collect { |k,v| k }.to_set
+ # all_items requires result sets as object sets,
+ # rather than keys in string
+ @all_items = @domain.items.collect { |k,v| v }.to_set
end
return @all_items
--- simplerdb-0.2/test/functional_test.rb 2008-03-15 23:20:49.000000000 +0000
+++ simplerdb-0.2/test/functional_test.rb 2008-10-20 12:03:58.000000000 +0100
@@ -68,6 +68,10 @@
results = @sdb.query("bands", "['albums' starts-with 'OK' or 'albums' = 'The Soft Bulletin']")
assert results[:items].sort == ["Radiohead", "The Flaming Lips"]
+
+ # Query with no attribute argument should return all items
+ results = @sdb.query("bands")
+ assert(results, ["Radiohead", "Wilco", "The Flaming Lips"])
# Modify and re-read attributes
@sdb.put_attributes("bands", "The Flaming Lips", {:albums => "Yoshimi Battles the Pink Robots"})
--- simplerdb-0.2/test/simplerdb_test.rb 2008-03-15 17:58:16.000000000 +0000
+++ simplerdb-0.2/test/simplerdb_test.rb 2008-10-20 10:33:57.000000000 +0100
@@ -77,4 +77,13 @@
assert @db.get_attributes("test", "item1").size == 2
end
+ def test_delete_all_attributes_of_an_item_if_attribute_name_is_not_given
+ @db.create_domain("test")
+
+ attrs = [AttributeParam.new("a1", "v1"), AttributeParam.new("a1", "v2"), AttributeParam.new("a2", "v1")]
+ @db.put_attributes("test", "item1", attrs)
+ @db.delete_attributes("test", "item1")
+ assert @db.get_attributes("test", "item1").size == 0
+ end
+
end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment