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