Permalink
Browse files

Make Dataset#group_and_count handle arguments other than Symbols

I think with a past change to handle implicit aliases in Symbol
arguments, it no longer worked with other objects, as it assumed
all arguments were Symbols.  That is not a valid assumption.

Introduce Dataset#unaliased_identifier private method that
handles both implicit aliases in symbols and AliasedExpression
objects, returning the unaliased argument, and have group_and_count
use it.
  • Loading branch information...
1 parent a523655 commit f439fc1e6609836dff7191d4938f21968ef5f649 @jeremyevans committed Nov 20, 2009
Showing with 40 additions and 6 deletions.
  1. +2 −0 CHANGELOG
  2. +19 −6 lib/sequel/dataset/convenience.rb
  3. +19 −0 spec/core/dataset_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Make Dataset#group_and_count handle arguments other than Symbols (jeremyevans)
+
* Add :only_if_modified option to validates_unique method in validation_helpers plugin (jeremyevans)
* Allow specifying the dataset alias via :alias option when using union/intersect/except (jeremyevans)
View
25 lib/sequel/dataset/convenience.rb
@@ -91,13 +91,9 @@ def get(column=nil, &block)
# ds.group_and_count(:first_name, :last_name).all => [{:first_name=>'a', :last_name=>'b', :count=>1}, ...]
# ds.group_and_count(:first_name___name).all => [{:name=>'a', :count=>1}, ...]
def group_and_count(*columns)
- groups = columns.map do |c|
- c_table, column, _ = split_symbol(c)
- c_table ? column.to_sym.qualify(c_table) : column.to_sym
- end
- group(*groups).select(*(columns + [COUNT_OF_ALL_AS_COUNT])).order(:count)
+ group(*columns.map{|c| unaliased_identifier(c)}).select(*(columns + [COUNT_OF_ALL_AS_COUNT])).order(:count)
end
-
+
# Inserts multiple records into the associated table. This method can be
# to efficiently insert a large amounts of records into a table. Inserts
# are automatically wrapped in a transaction.
@@ -245,5 +241,22 @@ def to_hash(key_column, value_column = nil)
m
end
end
+
+ private
+
+ # Return the unaliased part of the identifier. Handles both
+ # implicit aliases in symbols, as well as SQL::AliasedExpression
+ # objects. Other objects are returned as is.
+ def unaliased_identifier(c)
+ case c
+ when Symbol
+ c_table, column, _ = split_symbol(c)
+ c_table ? column.to_sym.qualify(c_table) : column.to_sym
+ when SQL::AliasedExpression
+ c.expression
+ else
+ c
+ end
+ end
end
end
View
19 spec/core/dataset_spec.rb
@@ -1489,6 +1489,25 @@ def fetch_rows(sql)
specify "should format column aliases in the select clause but not in the group clause" do
@ds.group_and_count(:name___n).sql.should ==
"SELECT name AS n, count(*) AS count FROM test GROUP BY name ORDER BY count"
+ @ds.group_and_count(:name__n).sql.should ==
+ "SELECT name.n, count(*) AS count FROM test GROUP BY name.n ORDER BY count"
+ end
+
+ specify "should handle identifiers" do
+ @ds.group_and_count(:name___n.identifier).sql.should ==
+ "SELECT name___n, count(*) AS count FROM test GROUP BY name___n ORDER BY count"
+ end
+
+ specify "should handle literal strings" do
+ @ds.group_and_count("name".lit).sql.should ==
+ "SELECT name, count(*) AS count FROM test GROUP BY name ORDER BY count"
+ end
+
+ specify "should handle aliased expressions" do
+ @ds.group_and_count(:name.as(:n)).sql.should ==
+ "SELECT name AS n, count(*) AS count FROM test GROUP BY name ORDER BY count"
+ @ds.group_and_count(:name.identifier.as(:n)).sql.should ==
+ "SELECT name AS n, count(*) AS count FROM test GROUP BY name ORDER BY count"
end
end

0 comments on commit f439fc1

Please sign in to comment.