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

#ilike, #like

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:


147
148
149
150
151
152
# File 'lib/sequel/extensions/string_agg.rb', line 147

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

Instance Attribute Details

#exprObject (readonly)

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


138
139
140
# File 'lib/sequel/extensions/string_agg.rb', line 138

def expr
  @expr
end

#order_exprObject (readonly)

The expression that the aggregation is ordered by.


144
145
146
# File 'lib/sequel/extensions/string_agg.rb', line 144

def order_expr
  @order_expr
end

#separatorObject (readonly)

The separator between each string expression.


141
142
143
# File 'lib/sequel/extensions/string_agg.rb', line 141

def separator
  @separator
end

Instance Method Details

#distinctObject

Return a modified StringAgg that uses distinct expressions


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

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)

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

def is_distinct?
  @distinct == true
end

#order(*o) ⇒ Object

Return a modified StringAgg with the given order


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

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