lib/sequel/extensions/string_agg.rb in sequel-4.49.0 vs lib/sequel/extensions/string_agg.rb in sequel-5.0.0

- old
+ new

@@ -101,12 +101,11 @@ end if distinct f = f.distinct end literal_append(sql, f) - # SEQUEL5: Remove cubrid - when :mysql, :hsqldb, :cubrid, :h2 + when :mysql, :hsqldb, :h2 sql << "GROUP_CONCAT(" if distinct sql << "DISTINCT " end literal_append(sql, expr) @@ -146,28 +145,32 @@ # Set the expression and separator def initialize(expr, separator=nil) @expr = expr @separator = separator + yield self if block_given? + freeze end # Whether the current expression uses distinct expressions def is_distinct? @distinct == true end # Return a modified StringAgg that uses distinct expressions def distinct - sa = dup - sa.instance_variable_set(:@distinct, true) - sa + self.class.new(@expr, @separator) do |sa| + sa.instance_variable_set(:@order_expr, @order_expr) if @order_expr + sa.instance_variable_set(:@distinct, true) + end end # Return a modified StringAgg with the given order def order(*o) - sa = dup - sa.instance_variable_set(:@order_expr, o.empty? ? nil : o) - sa + self.class.new(@expr, @separator) do |sa| + sa.instance_variable_set(:@distinct, @distinct) if @distinct + sa.instance_variable_set(:@order_expr, o.empty? ? nil : o.freeze) + end end to_s_method :string_agg_sql end end