Class: Sequel::SQL::StringAgg

Overview

The StringAgg class represents an aggregate string concatentation.

Defined Under Namespace

Modules: DatasetMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SubscriptMethods

#sql_subscript

Methods included from PatternMatchMethods

#!~, #=~

Methods included from OrderMethods

#asc, #desc

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from AliasMethods

#as

Methods included from StringConcatenationMethods

#+

Methods included from StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from IsDistinctFrom::Methods

#is_distinct_from

Methods included from Sequel::SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from Postgres::HStoreOpMethods

#hstore

Methods included from Postgres::RangeOpMethods

#pg_range

Methods included from Postgres::ArrayOpMethods

#pg_array

Methods included from Postgres::JSONOpMethods

#pg_json, #pg_jsonb

Methods included from Postgres::InetOpMethods

#pg_inet

Methods included from Postgres::PGRowOp::ExpressionMethods

#pg_row

Methods included from NumericMethods

#+, #coerce

Methods included from ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from BooleanMethods

#~

Methods inherited from Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

#initialize(expr, separator = nil) {|_self| ... } ⇒ StringAgg

Set the expression and separator

Yields:

  • (_self)

Yield Parameters:



161
162
163
164
165
166
# File 'lib/sequel/extensions/string_agg.rb', line 161

def initialize(expr, separator=nil)
  @expr = expr
  @separator = separator
  yield self if defined?(yield)
  freeze
end

Instance Attribute Details

#exprObject (readonly)

The string expression for each row that will concatenated to the output.



152
153
154
# File 'lib/sequel/extensions/string_agg.rb', line 152

def expr
  @expr
end

#order_exprObject (readonly)

The expression that the aggregation is ordered by.



158
159
160
# File 'lib/sequel/extensions/string_agg.rb', line 158

def order_expr
  @order_expr
end

#separatorObject (readonly)

The separator between each string expression.



155
156
157
# File 'lib/sequel/extensions/string_agg.rb', line 155

def separator
  @separator
end

Instance Method Details

#distinctObject

Return a modified StringAgg that uses distinct expressions



174
175
176
177
178
179
# File 'lib/sequel/extensions/string_agg.rb', line 174

def distinct
  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

#is_distinct?Boolean

Whether the current expression uses distinct expressions

Returns:

  • (Boolean)


169
170
171
# File 'lib/sequel/extensions/string_agg.rb', line 169

def is_distinct?
  @distinct == true
end

#order(*o) ⇒ Object

Return a modified StringAgg with the given order



182
183
184
185
186
187
# File 'lib/sequel/extensions/string_agg.rb', line 182

def order(*o)
  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