Module Sequel::SQL::StringAgg::DatasetMethods
In: lib/sequel/extensions/string_agg.rb

These methods are added to datasets using the string_agg extension, for the purposes of correctly literalizing StringAgg expressions for the appropriate database type.

Methods

Public Instance methods

Append the SQL fragment for the StringAgg expression to the SQL query.

[Source]

     # File lib/sequel/extensions/string_agg.rb, line 87
 87:         def string_agg_sql_append(sql, sa)
 88:           if defined?(super)
 89:             return super
 90:           end
 91: 
 92:           expr = sa.expr
 93:           separator = sa.separator || ","
 94:           order = sa.order_expr
 95:           distinct = sa.is_distinct?
 96: 
 97:           case db_type = db.database_type
 98:           when :postgres, :sqlanywhere
 99:             f = Function.new(db_type == :postgres ? :string_agg : :list, expr, separator)
100:             if order
101:               f = f.order(*order)
102:             end
103:             if distinct
104:               f = f.distinct
105:             end
106:             literal_append(sql, f)
107:           when :mysql, :hsqldb, :cubrid, :h2
108:             sql << "GROUP_CONCAT("
109:             if distinct
110:               sql << "DISTINCT "
111:             end
112:             literal_append(sql, expr)
113:             if order
114:               sql << " ORDER BY "
115:               expression_list_append(sql, order)
116:             end
117:             sql << " SEPARATOR "
118:             literal_append(sql, separator)
119:             sql << ")"
120:           when :oracle, :db2
121:             if distinct
122:               raise Error, "string_agg with distinct is not implemented on #{db.database_type}"
123:             end
124:             literal_append(sql, Function.new(:listagg, expr, separator))
125:             if order
126:               sql << " WITHIN GROUP (ORDER BY "
127:               expression_list_append(sql, order)
128:               sql << ")"
129:             else
130:               sql << " WITHIN GROUP (ORDER BY 1)"
131:             end
132:           else
133:             raise Error, "string_agg is not implemented on #{db.database_type}"
134:           end
135:         end

[Validate]